blob: 8eb77470a3d6b959986197ae1305f4c59fe541e1 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaar41009372013-07-01 22:03:04 +020016static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
17
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020018#if PY_VERSION_HEX < 0x02050000
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010019typedef int Py_ssize_t; // Python 2.4 and earlier don't have this type.
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020020#endif
21
Bram Moolenaard518f952020-01-01 15:04:17 +010022// Use values that are known to work, others may make Vim crash.
23#define ENC_OPT (enc_utf8 ? "utf-8" : enc_dbcs ? "euc-jp" : (char *)p_enc)
Bram Moolenaard620aa92013-05-17 16:40:06 +020024#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020025
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020026static const char *vim_special_path = "_vim_path_";
27
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020028#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020029#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020030#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010031#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
32#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
33#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020034
35#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020039#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020040 N_("empty keys are not allowed"))
41#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
42#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
43#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
44#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
45#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
46#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020047#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020049#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020050 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020051 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020053#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
54#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020055#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020056
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020058typedef void (*runner)(const char *, void *
59#ifdef PY_CAN_RECURSE
60 , PyGILState_STATE *
61#endif
62 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064static int ConvertFromPyObject(PyObject *, typval_T *);
65static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020066static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020067static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020068static PyObject *WindowNew(win_T *, tabpage_T *);
69static PyObject *BufferNew (buf_T *);
70static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020071
72static PyInt RangeStart;
73static PyInt RangeEnd;
74
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020075static PyObject *globals;
76
Bram Moolenaarf4258302013-06-02 18:20:17 +020077static PyObject *py_chdir;
78static PyObject *py_fchdir;
79static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020080static PyObject *vim_module;
81static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020082
Bram Moolenaar79a494d2018-07-22 04:30:21 +020083#if PY_VERSION_HEX >= 0x030700f0
84static PyObject *py_find_spec;
85#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020086static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020087#endif
Bram Moolenaarb999ba22019-02-14 13:28:45 +010088static PyObject *py_find_module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +020089
90static PyObject *VimError;
91
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020092/*
93 * obtain a lock on the Vim data structures
94 */
95 static void
96Python_Lock_Vim(void)
97{
98}
99
100/*
101 * release a lock on the Vim data structures
102 */
103 static void
104Python_Release_Vim(void)
105{
106}
107
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200108/*
109 * The "todecref" argument holds a pointer to PyObject * that must be
110 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
111 * was needed to generate returned value is object.
112 *
113 * Use Py_XDECREF to decrement reference count.
114 */
115 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200116StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200117{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200118 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200119
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121 {
122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
124 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 return NULL;
126
127 *todecref = NULL;
128 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200129 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200130 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200131 PyObject *bytes;
132
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 return NULL;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
137 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200138 {
139 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200140 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200142
143 *todecref = bytes;
144 }
145 else
146 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200147#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200148 PyErr_FORMAT(PyExc_TypeError,
149 N_("expected str() or unicode() instance, but got %s"),
150 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected bytes() or str() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200156 return NULL;
157 }
158
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200159 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160}
161
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200162#define NUMBER_LONG 1
163#define NUMBER_INT 2
164#define NUMBER_NATURAL 4
165#define NUMBER_UNSIGNED 8
166
167 static int
168NumberToLong(PyObject *obj, long *result, int flags)
169{
170#if PY_MAJOR_VERSION < 3
171 if (PyInt_Check(obj))
172 {
173 *result = PyInt_AsLong(obj);
174 if (PyErr_Occurred())
175 return -1;
176 }
177 else
178#endif
179 if (PyLong_Check(obj))
180 {
181 *result = PyLong_AsLong(obj);
182 if (PyErr_Occurred())
183 return -1;
184 }
185 else if (PyNumber_Check(obj))
186 {
187 PyObject *num;
188
189 if (!(num = PyNumber_Long(obj)))
190 return -1;
191
192 *result = PyLong_AsLong(num);
193
194 Py_DECREF(num);
195
196 if (PyErr_Occurred())
197 return -1;
198 }
199 else
200 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200201#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200202 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200203 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200204 "coercing to long(), but got %s"),
205 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200206#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200208 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200209 "but got %s"),
210 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200211#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200212 return -1;
213 }
214
215 if (flags & NUMBER_INT)
216 {
217 if (*result > INT_MAX)
218 {
219 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200220 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200221 return -1;
222 }
223 else if (*result < INT_MIN)
224 {
225 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200226 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200227 return -1;
228 }
229 }
230
231 if (flags & NUMBER_NATURAL)
232 {
233 if (*result <= 0)
234 {
235 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100236 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200237 return -1;
238 }
239 }
240 else if (flags & NUMBER_UNSIGNED)
241 {
242 if (*result < 0)
243 {
244 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200245 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200246 return -1;
247 }
248 }
249
250 return 0;
251}
252
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200253 static int
254add_string(PyObject *list, char *s)
255{
256 PyObject *string;
257
258 if (!(string = PyString_FromString(s)))
259 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200260
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200261 if (PyList_Append(list, string))
262 {
263 Py_DECREF(string);
264 return -1;
265 }
266
267 Py_DECREF(string);
268 return 0;
269}
270
271 static PyObject *
272ObjectDir(PyObject *self, char **attributes)
273{
274 PyMethodDef *method;
275 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200276 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200277
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200278 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200279 return NULL;
280
281 if (self)
282 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200283 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200284 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200285 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200286 return NULL;
287 }
288
289 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200290 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200291 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200292 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200293 return NULL;
294 }
295
296#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200297 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200298 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200299 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200300 return NULL;
301 }
302#endif
303
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200304 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200305}
306
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100307// Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100309// Function to write a line, points to either msg() or emsg().
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200310typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311
312static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313
314typedef struct
315{
316 PyObject_HEAD
317 long softspace;
318 long error;
319} OutputObject;
320
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200321static char *OutputAttrs[] = {
322 "softspace",
323 NULL
324};
325
326 static PyObject *
327OutputDir(PyObject *self)
328{
329 return ObjectDir(self, OutputAttrs);
330}
331
Bram Moolenaar77045652012-09-21 13:46:06 +0200332 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200333OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200334{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200335 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200337 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200338 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200339 return -1;
340 }
341
342 if (strcmp(name, "softspace") == 0)
343 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200344 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200345 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200346 return 0;
347 }
348
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200349 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
351}
352
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100353// Buffer IO, we write one whole line at a time.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200354static garray_T io_ga = {0, 0, 1, 80, NULL};
355static writefn old_fn = NULL;
356
357 static void
358PythonIO_Flush(void)
359{
360 if (old_fn != NULL && io_ga.ga_len > 0)
361 {
362 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
363 old_fn((char_u *)io_ga.ga_data);
364 }
365 io_ga.ga_len = 0;
366}
367
368 static void
369writer(writefn fn, char_u *str, PyInt n)
370{
371 char_u *ptr;
372
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100373 // Flush when switching output function.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200374 if (fn != old_fn)
375 PythonIO_Flush();
376 old_fn = fn;
377
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200378 // Write each NL separated line. Text after the last NL is kept for
379 // writing later.
380 // For normal messages: Do not output when "got_int" was set. This avoids
381 // a loop gone crazy flooding the terminal with messages. Also for when
382 // "q" is pressed at the more-prompt.
383 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
384 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200399 // Put the remaining text into io_ga for later printing.
400 if (n > 0 && (fn == (writefn)emsg || !got_int)
401 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200420 if (error)
421 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100422 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423 Python_Release_Vim();
424 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200425 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200426
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200427 return 0;
428}
429
430 static PyObject *
431OutputWrite(OutputObject *self, PyObject *string)
432{
433 if (write_output(self, string))
434 return NULL;
435
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200436 Py_INCREF(Py_None);
437 return Py_None;
438}
439
440 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200441OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 PyObject *iterator;
444 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200449 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200451 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200452 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200453 Py_DECREF(iterator);
454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 return NULL;
456 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200458 }
459
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200460 Py_DECREF(iterator);
461
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100462 // Iterator may have finished due to an exception
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200463 if (PyErr_Occurred())
464 return NULL;
465
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200466 Py_INCREF(Py_None);
467 return Py_None;
468}
469
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100471AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100472{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100473 // do nothing
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100474 Py_INCREF(Py_None);
475 return Py_None;
476}
477
Bram Moolenaard4247472015-11-02 13:28:59 +0100478 static PyObject *
479AlwaysFalse(PyObject *self UNUSED)
480{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100481 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100482 PyObject *ret = Py_False;
483 Py_INCREF(ret);
484 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100485}
486
487 static PyObject *
488AlwaysTrue(PyObject *self UNUSED)
489{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100490 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100491 PyObject *ret = Py_True;
492 Py_INCREF(ret);
493 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100494}
495
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200496/***************/
497
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200498static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100499 // name, function, calling, doc
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200500 {"write", (PyCFunction)OutputWrite, METH_O, ""},
501 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100502 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
503 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
504 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
505 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
506 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
507 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200508 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200509 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200510 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200511};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200512
513static OutputObject Output =
514{
515 PyObject_HEAD_INIT(&OutputType)
516 0,
517 0
518};
519
520static OutputObject Error =
521{
522 PyObject_HEAD_INIT(&OutputType)
523 0,
524 1
525};
526
527 static int
528PythonIO_Init_io(void)
529{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200530 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
531 return -1;
532 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
533 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200534
535 if (PyErr_Occurred())
536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100537 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200538 return -1;
539 }
540
541 return 0;
542}
543
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200544#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200545static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
546
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200547typedef struct
548{
549 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200550 char *fullname;
551 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200552} LoaderObject;
553static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200554
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200555 static void
556LoaderDestructor(LoaderObject *self)
557{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200558 vim_free(self->fullname);
559 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200560 DESTRUCTOR_FINISH(self);
561}
562
563 static PyObject *
564LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
565{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200566 char *fullname = self->fullname;
567 PyObject *result = self->result;
568 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200569
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200570 if (!fullname)
571 {
572 module = result ? result : Py_None;
573 Py_INCREF(module);
574 return module;
575 }
576
577 module = call_load_module(fullname, (int)STRLEN(fullname), result);
578
579 self->fullname = NULL;
580 self->result = module;
581
582 vim_free(fullname);
583 Py_DECREF(result);
584
585 if (!module)
586 {
587 if (PyErr_Occurred())
588 return NULL;
589
590 Py_INCREF(Py_None);
591 return Py_None;
592 }
593
594 Py_INCREF(module);
595 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200596}
597
598static struct PyMethodDef LoaderMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100599 // name, function, calling, doc
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200600 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
601 { NULL, NULL, 0, NULL}
602};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200603#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200604
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100605/*
606 * Check to see whether a Vim error has been reported, or a keyboard
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200607 * interrupt has been detected.
608 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 static void
610VimTryStart(void)
611{
612 ++trylevel;
613}
614
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200615 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200616VimTryEnd(void)
617{
618 --trylevel;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100619 // Without this it stops processing all subsequent Vim script commands and
620 // generates strange error messages if I e.g. try calling Test() in a cycle
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200621 did_emsg = FALSE;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100622 // Keyboard interrupt should be preferred over anything else
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200623 if (got_int)
624 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100625 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100626 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100627 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200628 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200629 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200630 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100631 else if (msg_list != NULL && *msg_list != NULL)
632 {
633 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100634 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100635
636 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
637
638 if (msg == NULL)
639 {
640 PyErr_NoMemory();
641 return -1;
642 }
643
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100644 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100645
646 free_global_msglist();
647
648 if (should_free)
649 vim_free(msg);
650
651 return -1;
652 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200653 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200654 return (PyErr_Occurred() ? -1 : 0);
Bram Moolenaar86181df2020-05-11 23:14:04 +0200655 // Python exception is preferred over Vim one; unlikely to occur though
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200656 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200657 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100658 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200659 return -1;
660 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100661 // Finally transform Vim script exception to python one
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200662 else
663 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200664 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200666 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200667 }
668}
669
670 static int
671VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200672{
673 if (got_int)
674 {
675 PyErr_SetNone(PyExc_KeyboardInterrupt);
676 return 1;
677 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200678 return 0;
679}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200680
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100681// Vim module - Implementation
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200682
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200684VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200685{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200686 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200687 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200688 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689
Bram Moolenaar389a1792013-06-23 13:00:44 +0200690 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 return NULL;
692
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 Py_BEGIN_ALLOW_THREADS
694 Python_Lock_Vim();
695
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200696 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200697 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200698 update_screen(VALID);
699
700 Python_Release_Vim();
701 Py_END_ALLOW_THREADS
702
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200703 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200704 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200705 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200706 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200709 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711}
712
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713/*
714 * Function to translate a typval_T into a PyObject; this will recursively
715 * translate lists/dictionaries into their Python equivalents.
716 *
717 * The depth parameter is to avoid infinite recursion, set it to 1 when
718 * you call VimToPython.
719 */
720 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200721VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200722{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200723 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200724 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200725 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100727 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 if (depth > 100)
729 {
730 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200731 ret = Py_None;
732 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200733 }
734
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100735 // Check if we run into a recursive loop. The item must be in lookup_dict
736 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
738 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
739 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200740 sprintf(ptrBuf, "%p",
741 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
742 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200743
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200744 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 Py_INCREF(ret);
747 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200748 }
749 }
750
751 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200752 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200753 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754 else if (our_tv->v_type == VAR_NUMBER)
755 {
756 char buf[NUMBUFLEN];
757
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100758 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200759 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200760 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100762#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 else if (our_tv->v_type == VAR_FLOAT)
764 {
765 char buf[NUMBUFLEN];
766
767 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200768 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100770#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 else if (our_tv->v_type == VAR_LIST)
772 {
773 list_T *list = our_tv->vval.v_list;
774 listitem_T *curr;
775
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 if (list == NULL)
777 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200779 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 return NULL;
781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200783 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 return NULL;
786 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200788 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200789 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200790 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200791 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200792 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200794 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200796 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200797 {
798 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200799 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 return NULL;
801 }
802 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803 }
804 }
805 else if (our_tv->v_type == VAR_DICT)
806 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200807
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100808 hashtab_T *ht;
809 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200810 hashitem_T *hi;
811 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100812
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200813 if (our_tv->vval.v_dict == NULL)
814 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100815 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200816
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200817 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200818 return NULL;
819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200820 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200821 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200822 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200823 return NULL;
824 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200825
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100826 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200827 for (hi = ht->ht_array; todo > 0; ++hi)
828 {
829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200830 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200831 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200833 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200834 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200835 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200837 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200838 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200840 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200841 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200842 Py_DECREF(newObj);
843 return NULL;
844 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200845 }
846 }
847 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100848 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100849 {
850 if (our_tv->vval.v_number == VVAL_FALSE)
851 {
852 ret = Py_False;
853 Py_INCREF(ret);
854 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100855 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100856 {
857 ret = Py_True;
858 Py_INCREF(ret);
859 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100860 return ret;
861 }
862 else if (our_tv->v_type == VAR_SPECIAL)
863 {
864 Py_INCREF(Py_None);
865 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100866 return ret;
867 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100868 else if (our_tv->v_type == VAR_BLOB)
869 ret = PyBytes_FromStringAndSize(
870 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
871 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200872 else
873 {
874 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200875 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200876 }
877
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200878 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200879}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200880
881 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200882VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200883{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200884 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200886 PyObject *string;
887 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200888 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200889 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200890
Bram Moolenaar389a1792013-06-23 13:00:44 +0200891 if (!PyArg_ParseTuple(args, "O", &string))
892 return NULL;
893
894 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200895 return NULL;
896
897 Py_BEGIN_ALLOW_THREADS
898 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200899 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200900 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200901 Python_Release_Vim();
902 Py_END_ALLOW_THREADS
903
Bram Moolenaar389a1792013-06-23 13:00:44 +0200904 Py_XDECREF(todecref);
905
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200906 if (VimTryEnd())
907 return NULL;
908
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200909 if (our_tv == NULL)
910 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200911 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200912 return NULL;
913 }
914
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100915 // Convert the Vim type into a Python type. Create a dictionary that's
916 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200917 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200918 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200919 else
920 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200921 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200922 Py_DECREF(lookup_dict);
923 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200924
925
926 Py_BEGIN_ALLOW_THREADS
927 Python_Lock_Vim();
928 free_tv(our_tv);
929 Python_Release_Vim();
930 Py_END_ALLOW_THREADS
931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200932 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200933}
934
Bram Moolenaardb913952012-06-29 12:54:53 +0200935static PyObject *ConvertToPyObject(typval_T *);
936
937 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200938VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200939{
Bram Moolenaardb913952012-06-29 12:54:53 +0200940 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200941 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200942 char_u *expr;
943 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200944
Bram Moolenaar389a1792013-06-23 13:00:44 +0200945 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200946 return NULL;
947
948 Py_BEGIN_ALLOW_THREADS
949 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200950 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200951 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200952 Python_Release_Vim();
953 Py_END_ALLOW_THREADS
954
Bram Moolenaar389a1792013-06-23 13:00:44 +0200955 Py_XDECREF(todecref);
956
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200957 if (VimTryEnd())
958 return NULL;
959
Bram Moolenaardb913952012-06-29 12:54:53 +0200960 if (our_tv == NULL)
961 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200962 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200963 return NULL;
964 }
965
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200966 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200967 Py_BEGIN_ALLOW_THREADS
968 Python_Lock_Vim();
969 free_tv(our_tv);
970 Python_Release_Vim();
971 Py_END_ALLOW_THREADS
972
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200973 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200974}
975
976 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200977VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200978{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200979 char_u *str;
980 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200981 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200982
Bram Moolenaar389a1792013-06-23 13:00:44 +0200983 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200984 return NULL;
985
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200986 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200987
988 Py_XDECREF(todecref);
989
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200990 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200991}
992
Bram Moolenaarf4258302013-06-02 18:20:17 +0200993 static PyObject *
994_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
995{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200996 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200997 PyObject *newwd;
998 PyObject *todecref;
999 char_u *new_dir;
1000
Bram Moolenaard4209d22013-06-05 20:34:15 +02001001 if (_chdir == NULL)
1002 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001003 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001004 return NULL;
1005
1006 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1007 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001008 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001009 return NULL;
1010 }
1011
1012 if (!(new_dir = StringToChars(newwd, &todecref)))
1013 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001014 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001015 Py_DECREF(newwd);
1016 return NULL;
1017 }
1018
1019 VimTryStart();
1020
1021 if (vim_chdir(new_dir))
1022 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001023 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001024 Py_DECREF(newwd);
1025 Py_XDECREF(todecref);
1026
1027 if (VimTryEnd())
1028 return NULL;
1029
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001030 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001031 return NULL;
1032 }
1033
1034 Py_DECREF(newwd);
1035 Py_XDECREF(todecref);
1036
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001037 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001038
1039 if (VimTryEnd())
1040 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001041 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001042 return NULL;
1043 }
1044
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001045 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001046}
1047
1048 static PyObject *
1049VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1050{
1051 return _VimChdir(py_chdir, args, kwargs);
1052}
1053
1054 static PyObject *
1055VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1056{
1057 return _VimChdir(py_fchdir, args, kwargs);
1058}
1059
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001060typedef struct {
1061 PyObject *callable;
1062 PyObject *result;
1063} map_rtp_data;
1064
1065 static void
1066map_rtp_callback(char_u *path, void *_data)
1067{
1068 void **data = (void **) _data;
1069 PyObject *pathObject;
1070 map_rtp_data *mr_data = *((map_rtp_data **) data);
1071
Bram Moolenaar41009372013-07-01 22:03:04 +02001072 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001073 {
1074 *data = NULL;
1075 return;
1076 }
1077
1078 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1079 pathObject, NULL);
1080
1081 Py_DECREF(pathObject);
1082
1083 if (!mr_data->result || mr_data->result != Py_None)
1084 *data = NULL;
1085 else
1086 {
1087 Py_DECREF(mr_data->result);
1088 mr_data->result = NULL;
1089 }
1090}
1091
1092 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001093VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001094{
1095 map_rtp_data data;
1096
Bram Moolenaar389a1792013-06-23 13:00:44 +02001097 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001098 data.result = NULL;
1099
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001100 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001101
1102 if (data.result == NULL)
1103 {
1104 if (PyErr_Occurred())
1105 return NULL;
1106 else
1107 {
1108 Py_INCREF(Py_None);
1109 return Py_None;
1110 }
1111 }
1112 return data.result;
1113}
1114
1115/*
1116 * _vim_runtimepath_ special path implementation.
1117 */
1118
1119 static void
1120map_finder_callback(char_u *path, void *_data)
1121{
1122 void **data = (void **) _data;
1123 PyObject *list = *((PyObject **) data);
1124 PyObject *pathObject1, *pathObject2;
1125 char *pathbuf;
1126 size_t pathlen;
1127
1128 pathlen = STRLEN(path);
1129
1130#if PY_MAJOR_VERSION < 3
1131# define PY_MAIN_DIR_STRING "python2"
1132#else
1133# define PY_MAIN_DIR_STRING "python3"
1134#endif
1135#define PY_ALTERNATE_DIR_STRING "pythonx"
1136
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001137#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001138 if (!(pathbuf = PyMem_New(char,
1139 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1140 {
1141 PyErr_NoMemory();
1142 *data = NULL;
1143 return;
1144 }
1145
1146 mch_memmove(pathbuf, path, pathlen + 1);
1147 add_pathsep((char_u *) pathbuf);
1148
1149 pathlen = STRLEN(pathbuf);
1150 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1151 PYTHONX_STRING_LENGTH + 1);
1152
1153 if (!(pathObject1 = PyString_FromString(pathbuf)))
1154 {
1155 *data = NULL;
1156 PyMem_Free(pathbuf);
1157 return;
1158 }
1159
1160 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1161 PYTHONX_STRING_LENGTH + 1);
1162
1163 if (!(pathObject2 = PyString_FromString(pathbuf)))
1164 {
1165 Py_DECREF(pathObject1);
1166 PyMem_Free(pathbuf);
1167 *data = NULL;
1168 return;
1169 }
1170
1171 PyMem_Free(pathbuf);
1172
1173 if (PyList_Append(list, pathObject1)
1174 || PyList_Append(list, pathObject2))
1175 *data = NULL;
1176
1177 Py_DECREF(pathObject1);
1178 Py_DECREF(pathObject2);
1179}
1180
1181 static PyObject *
1182Vim_GetPaths(PyObject *self UNUSED)
1183{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001184 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001185
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001186 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001187 return NULL;
1188
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001189 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001190
1191 if (PyErr_Occurred())
1192 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001193 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001194 return NULL;
1195 }
1196
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001197 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001198}
1199
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001200#if PY_VERSION_HEX >= 0x030700f0
1201 static PyObject *
1202FinderFindSpec(PyObject *self, PyObject *args)
1203{
1204 char *fullname;
1205 PyObject *paths;
1206 PyObject *target = Py_None;
1207 PyObject *spec;
1208
1209 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1210 return NULL;
1211
1212 if (!(paths = Vim_GetPaths(self)))
1213 return NULL;
1214
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001215 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001216
1217 Py_DECREF(paths);
1218
1219 if (!spec)
1220 {
1221 if (PyErr_Occurred())
1222 return NULL;
1223
1224 Py_INCREF(Py_None);
1225 return Py_None;
1226 }
1227
1228 return spec;
1229}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001230
1231 static PyObject *
1232FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1233{
1234 // Apparently returning None works.
1235 Py_INCREF(Py_None);
1236 return Py_None;
1237}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001238#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001239 static PyObject *
1240call_load_module(char *name, int len, PyObject *find_module_result)
1241{
1242 PyObject *fd, *pathname, *description;
1243
Bram Moolenaarc476e522013-06-23 13:46:40 +02001244 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001245 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001246 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001247 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001248 Py_TYPE_NAME(find_module_result));
1249 return NULL;
1250 }
1251 if (PyTuple_GET_SIZE(find_module_result) != 3)
1252 {
1253 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001254 N_("expected 3-tuple as imp.find_module() result, but got "
1255 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001256 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001257 return NULL;
1258 }
1259
1260 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1261 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1262 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1263 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001264 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001265 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001266 return NULL;
1267 }
1268
1269 return PyObject_CallFunction(py_load_module,
1270 "s#OOO", name, len, fd, pathname, description);
1271}
1272
1273 static PyObject *
1274find_module(char *fullname, char *tail, PyObject *new_path)
1275{
1276 PyObject *find_module_result;
1277 PyObject *module;
1278 char *dot;
1279
Bram Moolenaar41009372013-07-01 22:03:04 +02001280 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001281 {
1282 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001283 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001284 * first component
1285 */
1286 PyObject *newest_path;
1287 int partlen = (int) (dot - 1 - tail);
1288
1289 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1290 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001291 {
1292 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1293 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001294 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001295 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001296
1297 if (!(module = call_load_module(
1298 fullname,
1299 ((int) (tail - fullname)) + partlen,
1300 find_module_result)))
1301 {
1302 Py_DECREF(find_module_result);
1303 return NULL;
1304 }
1305
1306 Py_DECREF(find_module_result);
1307
1308 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1309 {
1310 Py_DECREF(module);
1311 return NULL;
1312 }
1313
1314 Py_DECREF(module);
1315
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001316 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001317
1318 Py_DECREF(newest_path);
1319
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001320 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001321 }
1322 else
1323 {
1324 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1325 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001326 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001327 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1328 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001329 return NULL;
1330 }
1331
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001332 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001333 }
1334}
1335
1336 static PyObject *
1337FinderFindModule(PyObject *self, PyObject *args)
1338{
1339 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001340 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001341 PyObject *new_path;
1342 LoaderObject *loader;
1343
1344 if (!PyArg_ParseTuple(args, "s", &fullname))
1345 return NULL;
1346
1347 if (!(new_path = Vim_GetPaths(self)))
1348 return NULL;
1349
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001350 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001351
1352 Py_DECREF(new_path);
1353
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001354 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001355 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001356 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001357 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001358
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001359 Py_INCREF(Py_None);
1360 return Py_None;
1361 }
1362
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001363 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001364 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001365 Py_DECREF(result);
1366 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001367 return NULL;
1368 }
1369
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001370 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1371 {
1372 vim_free(fullname);
1373 Py_DECREF(result);
1374 return NULL;
1375 }
1376
1377 loader->fullname = fullname;
1378 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001379
1380 return (PyObject *) loader;
1381}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001382#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001383
1384 static PyObject *
1385VimPathHook(PyObject *self UNUSED, PyObject *args)
1386{
1387 char *path;
1388
1389 if (PyArg_ParseTuple(args, "s", &path)
1390 && STRCMP(path, vim_special_path) == 0)
1391 {
1392 Py_INCREF(vim_module);
1393 return vim_module;
1394 }
1395
1396 PyErr_Clear();
1397 PyErr_SetNone(PyExc_ImportError);
1398 return NULL;
1399}
1400
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001401/*
1402 * Vim module - Definitions
1403 */
1404
1405static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001406 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001407 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001408 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001409 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001410 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001411 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1412 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001413 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001414#if PY_VERSION_HEX >= 0x030700f0
1415 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001416#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001417 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001418 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1419 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1420 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001421};
1422
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001423/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001424 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001425 */
1426
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001427static PyTypeObject IterType;
1428
1429typedef PyObject *(*nextfun)(void **);
1430typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001431typedef int (*traversefun)(void *, visitproc, void *);
1432typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001433
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001434// Main purpose of this object is removing the need for do python
1435// initialization (i.e. PyType_Ready and setting type attributes) for a big
1436// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001437typedef struct
1438{
1439 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001440 void *cur;
1441 nextfun next;
1442 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001443 traversefun traverse;
1444 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001445} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001446
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001447 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001448IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1449 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001450{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001451 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001452
Bram Moolenaar774267b2013-05-21 20:51:59 +02001453 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001454 self->cur = start;
1455 self->next = next;
1456 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001457 self->traverse = traverse;
1458 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001459
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001460 return (PyObject *)(self);
1461}
1462
1463 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001464IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001465{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001466 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001467 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001468 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001469}
1470
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001471 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001472IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001473{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001474 if (self->traverse != NULL)
1475 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001476 else
1477 return 0;
1478}
1479
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001480// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001481#ifdef clear
1482# undef clear
1483#endif
1484
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001485 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001486IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001487{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001488 if (self->clear != NULL)
1489 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001490 else
1491 return 0;
1492}
1493
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001494 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001495IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001496{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001497 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001498}
1499
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001500 static PyObject *
1501IterIter(PyObject *self)
1502{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001503 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001504 return self;
1505}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001506
Bram Moolenaardb913952012-06-29 12:54:53 +02001507typedef struct pylinkedlist_S {
1508 struct pylinkedlist_S *pll_next;
1509 struct pylinkedlist_S *pll_prev;
1510 PyObject *pll_obj;
1511} pylinkedlist_T;
1512
1513static pylinkedlist_T *lastdict = NULL;
1514static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001515static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001516
1517 static void
1518pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1519{
1520 if (ref->pll_prev == NULL)
1521 {
1522 if (ref->pll_next == NULL)
1523 {
1524 *last = NULL;
1525 return;
1526 }
1527 }
1528 else
1529 ref->pll_prev->pll_next = ref->pll_next;
1530
1531 if (ref->pll_next == NULL)
1532 *last = ref->pll_prev;
1533 else
1534 ref->pll_next->pll_prev = ref->pll_prev;
1535}
1536
1537 static void
1538pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1539{
1540 if (*last == NULL)
1541 ref->pll_prev = NULL;
1542 else
1543 {
1544 (*last)->pll_next = ref;
1545 ref->pll_prev = *last;
1546 }
1547 ref->pll_next = NULL;
1548 ref->pll_obj = self;
1549 *last = ref;
1550}
1551
1552static PyTypeObject DictionaryType;
1553
1554typedef struct
1555{
1556 PyObject_HEAD
1557 dict_T *dict;
1558 pylinkedlist_T ref;
1559} DictionaryObject;
1560
Bram Moolenaara9922d62013-05-30 13:01:18 +02001561static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1562
1563#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1564
Bram Moolenaardb913952012-06-29 12:54:53 +02001565 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001566DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001567{
1568 DictionaryObject *self;
1569
Bram Moolenaara9922d62013-05-30 13:01:18 +02001570 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001571 if (self == NULL)
1572 return NULL;
1573 self->dict = dict;
1574 ++dict->dv_refcount;
1575
1576 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1577
1578 return (PyObject *)(self);
1579}
1580
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001582py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001583{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001584 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001585
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001586 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001587 {
1588 PyErr_NoMemory();
1589 return NULL;
1590 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001591 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594}
1595
1596 static PyObject *
1597DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1598{
1599 DictionaryObject *self;
1600 dict_T *dict;
1601
1602 if (!(dict = py_dict_alloc()))
1603 return NULL;
1604
1605 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1606
1607 --dict->dv_refcount;
1608
1609 if (kwargs || PyTuple_Size(args))
1610 {
1611 PyObject *tmp;
1612 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1613 {
1614 Py_DECREF(self);
1615 return NULL;
1616 }
1617
1618 Py_DECREF(tmp);
1619 }
1620
1621 return (PyObject *)(self);
1622}
1623
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001624 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001625DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001626{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001627 pyll_remove(&self->ref, &lastdict);
1628 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001629
1630 DESTRUCTOR_FINISH(self);
1631}
1632
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001633static char *DictionaryAttrs[] = {
1634 "locked", "scope",
1635 NULL
1636};
1637
1638 static PyObject *
1639DictionaryDir(PyObject *self)
1640{
1641 return ObjectDir(self, DictionaryAttrs);
1642}
1643
Bram Moolenaardb913952012-06-29 12:54:53 +02001644 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001645DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001646{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001647 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001648 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001649 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001650 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001651 return -1;
1652 }
1653
1654 if (strcmp(name, "locked") == 0)
1655 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001656 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001657 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001658 PyErr_SET_STRING(PyExc_TypeError,
1659 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001660 return -1;
1661 }
1662 else
1663 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001665 if (istrue == -1)
1666 return -1;
1667 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001668 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001669 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001670 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001671 }
1672 return 0;
1673 }
1674 else
1675 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001676 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001677 return -1;
1678 }
1679}
1680
1681 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001682DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001683{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001684 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001685}
1686
Bram Moolenaara9922d62013-05-30 13:01:18 +02001687#define DICT_FLAG_HAS_DEFAULT 0x01
1688#define DICT_FLAG_POP 0x02
1689#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001690#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001691#define DICT_FLAG_RETURN_PAIR 0x10
1692
Bram Moolenaardb913952012-06-29 12:54:53 +02001693 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001694_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001695{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001696 PyObject *keyObject;
1697 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001698 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001699 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001700 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701 dict_T *dict = self->dict;
1702 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001703 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001704
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705 if (flags & DICT_FLAG_HAS_DEFAULT)
1706 {
1707 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1708 return NULL;
1709 }
1710 else
1711 keyObject = args;
1712
1713 if (flags & DICT_FLAG_RETURN_BOOL)
1714 defObject = Py_False;
1715
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001716 if (!(key = StringToChars(keyObject, &todecref)))
1717 return NULL;
1718
1719 if (*key == NUL)
1720 {
1721 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001722 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001723 return NULL;
1724 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001725
Bram Moolenaara9922d62013-05-30 13:01:18 +02001726 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001727
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001728 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001729
Bram Moolenaara9922d62013-05-30 13:01:18 +02001730 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001731 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001732 if (defObject)
1733 {
1734 Py_INCREF(defObject);
1735 return defObject;
1736 }
1737 else
1738 {
1739 PyErr_SetObject(PyExc_KeyError, keyObject);
1740 return NULL;
1741 }
1742 }
1743 else if (flags & DICT_FLAG_RETURN_BOOL)
1744 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001745 ret = Py_True;
1746 Py_INCREF(ret);
1747 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001748 }
1749
1750 di = dict_lookup(hi);
1751
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001752 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001753 return NULL;
1754
1755 if (flags & DICT_FLAG_POP)
1756 {
1757 if (dict->dv_lock)
1758 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001759 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001760 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001761 return NULL;
1762 }
1763
1764 hash_remove(&dict->dv_hashtab, hi);
1765 dictitem_free(di);
1766 }
1767
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001768 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001769}
1770
1771 static PyObject *
1772DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1773{
1774 return _DictionaryItem(self, keyObject, 0);
1775}
1776
1777 static int
1778DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1779{
1780 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001781 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001782
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001783 if (rObj == NULL)
1784 return -1;
1785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001786 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001787
Bram Moolenaardee2e312013-06-23 16:35:47 +02001788 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001790 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001791}
1792
1793typedef struct
1794{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001795 int dii_changed;
1796 hashtab_T *dii_ht;
1797 hashitem_T *dii_hi;
1798 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001799} dictiterinfo_T;
1800
1801 static PyObject *
1802DictionaryIterNext(dictiterinfo_T **dii)
1803{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001804 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001805
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001806 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001807 return NULL;
1808
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001809 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001810 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001811 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001812 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001813 return NULL;
1814 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001815
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001816 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
1817 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001818
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001819 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001820
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001821 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001822 return NULL;
1823
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001824 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001825}
1826
1827 static PyObject *
1828DictionaryIter(DictionaryObject *self)
1829{
1830 dictiterinfo_T *dii;
1831 hashtab_T *ht;
1832
1833 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1834 {
1835 PyErr_NoMemory();
1836 return NULL;
1837 }
1838
1839 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001840 dii->dii_changed = ht->ht_changed;
1841 dii->dii_ht = ht;
1842 dii->dii_hi = ht->ht_array;
1843 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001844
1845 return IterNew(dii,
1846 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1847 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001848}
1849
1850 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001851DictionaryAssItem(
1852 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001853{
1854 char_u *key;
1855 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001856 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001857 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001858 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001859
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001860 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001861 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001862 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001863 return -1;
1864 }
1865
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001866 if (!(key = StringToChars(keyObject, &todecref)))
1867 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001868
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001869 if (*key == NUL)
1870 {
1871 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001872 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001873 return -1;
1874 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001875
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001876 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001877
1878 if (valObject == NULL)
1879 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001880 hashitem_T *hi;
1881
Bram Moolenaardb913952012-06-29 12:54:53 +02001882 if (di == NULL)
1883 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001884 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001885 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001886 return -1;
1887 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001888 hi = hash_find(&dict->dv_hashtab, di->di_key);
1889 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001890 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001891 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001892 return 0;
1893 }
1894
1895 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001896 {
1897 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001898 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001899 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001900
1901 if (di == NULL)
1902 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001903 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001904 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001905 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001906 PyErr_NoMemory();
1907 return -1;
1908 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001909 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001910
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001911 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001912 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001913 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001914 RAISE_KEY_ADD_FAIL(key);
1915 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001916 return -1;
1917 }
1918 }
1919 else
1920 clear_tv(&di->di_tv);
1921
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001922 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001923
1924 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001925 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001926 return 0;
1927}
1928
Bram Moolenaara9922d62013-05-30 13:01:18 +02001929typedef PyObject *(*hi_to_py)(hashitem_T *);
1930
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001932DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001933{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001934 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001935 long_u todo = dict->dv_hashtab.ht_used;
1936 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001937 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001938 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001941 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1943 {
1944 if (!HASHITEM_EMPTY(hi))
1945 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001946 if (!(newObj = hiconvert(hi)))
1947 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001948 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 return NULL;
1950 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001951 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001952 --todo;
1953 ++i;
1954 }
1955 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001956 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001957}
1958
Bram Moolenaara9922d62013-05-30 13:01:18 +02001959 static PyObject *
1960dict_key(hashitem_T *hi)
1961{
1962 return PyBytes_FromString((char *)(hi->hi_key));
1963}
1964
1965 static PyObject *
1966DictionaryListKeys(DictionaryObject *self)
1967{
1968 return DictionaryListObjects(self, dict_key);
1969}
1970
1971 static PyObject *
1972dict_val(hashitem_T *hi)
1973{
1974 dictitem_T *di;
1975
1976 di = dict_lookup(hi);
1977 return ConvertToPyObject(&di->di_tv);
1978}
1979
1980 static PyObject *
1981DictionaryListValues(DictionaryObject *self)
1982{
1983 return DictionaryListObjects(self, dict_val);
1984}
1985
1986 static PyObject *
1987dict_item(hashitem_T *hi)
1988{
1989 PyObject *keyObject;
1990 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001991 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001992
1993 if (!(keyObject = dict_key(hi)))
1994 return NULL;
1995
1996 if (!(valObject = dict_val(hi)))
1997 {
1998 Py_DECREF(keyObject);
1999 return NULL;
2000 }
2001
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002002 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002003
2004 Py_DECREF(keyObject);
2005 Py_DECREF(valObject);
2006
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002007 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002008}
2009
2010 static PyObject *
2011DictionaryListItems(DictionaryObject *self)
2012{
2013 return DictionaryListObjects(self, dict_item);
2014}
2015
2016 static PyObject *
2017DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2018{
2019 dict_T *dict = self->dict;
2020
2021 if (dict->dv_lock)
2022 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002023 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002024 return NULL;
2025 }
2026
2027 if (kwargs)
2028 {
2029 typval_T tv;
2030
2031 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2032 return NULL;
2033
2034 VimTryStart();
2035 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2036 clear_tv(&tv);
2037 if (VimTryEnd())
2038 return NULL;
2039 }
2040 else
2041 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002042 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002043
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002044 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045 return NULL;
2046
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002047 if (obj == NULL)
2048 {
2049 Py_INCREF(Py_None);
2050 return Py_None;
2051 }
2052
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002053 if (PyObject_HasAttrString(obj, "keys"))
2054 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 else
2056 {
2057 PyObject *iterator;
2058 PyObject *item;
2059
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002060 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002061 return NULL;
2062
2063 while ((item = PyIter_Next(iterator)))
2064 {
2065 PyObject *fast;
2066 PyObject *keyObject;
2067 PyObject *valObject;
2068 PyObject *todecref;
2069 char_u *key;
2070 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002071 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002072
2073 if (!(fast = PySequence_Fast(item, "")))
2074 {
2075 Py_DECREF(iterator);
2076 Py_DECREF(item);
2077 return NULL;
2078 }
2079
2080 Py_DECREF(item);
2081
2082 if (PySequence_Fast_GET_SIZE(fast) != 2)
2083 {
2084 Py_DECREF(iterator);
2085 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002086 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002087 N_("expected sequence element of size 2, "
2088 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002089 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002090 return NULL;
2091 }
2092
2093 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2094
2095 if (!(key = StringToChars(keyObject, &todecref)))
2096 {
2097 Py_DECREF(iterator);
2098 Py_DECREF(fast);
2099 return NULL;
2100 }
2101
2102 di = dictitem_alloc(key);
2103
2104 Py_XDECREF(todecref);
2105
2106 if (di == NULL)
2107 {
2108 Py_DECREF(fast);
2109 Py_DECREF(iterator);
2110 PyErr_NoMemory();
2111 return NULL;
2112 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002113 di->di_tv.v_type = VAR_UNKNOWN;
2114
2115 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2116
2117 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2118 {
2119 Py_DECREF(iterator);
2120 Py_DECREF(fast);
2121 dictitem_free(di);
2122 return NULL;
2123 }
2124
2125 Py_DECREF(fast);
2126
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002127 hi = hash_find(&dict->dv_hashtab, di->di_key);
2128 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002129 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002130 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002131 Py_DECREF(iterator);
2132 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002133 return NULL;
2134 }
2135 }
2136
2137 Py_DECREF(iterator);
2138
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002139 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002140 if (PyErr_Occurred())
2141 return NULL;
2142 }
2143 }
2144 Py_INCREF(Py_None);
2145 return Py_None;
2146}
2147
2148 static PyObject *
2149DictionaryGet(DictionaryObject *self, PyObject *args)
2150{
2151 return _DictionaryItem(self, args,
2152 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2153}
2154
2155 static PyObject *
2156DictionaryPop(DictionaryObject *self, PyObject *args)
2157{
2158 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2159}
2160
2161 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002162DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002163{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002164 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002165 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002166 PyObject *valObject;
2167 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002168
Bram Moolenaarde71b562013-06-02 17:41:54 +02002169 if (self->dict->dv_hashtab.ht_used == 0)
2170 {
2171 PyErr_SetNone(PyExc_KeyError);
2172 return NULL;
2173 }
2174
2175 hi = self->dict->dv_hashtab.ht_array;
2176 while (HASHITEM_EMPTY(hi))
2177 ++hi;
2178
2179 di = dict_lookup(hi);
2180
2181 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002182 return NULL;
2183
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002184 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002185 {
2186 Py_DECREF(valObject);
2187 return NULL;
2188 }
2189
2190 hash_remove(&self->dict->dv_hashtab, hi);
2191 dictitem_free(di);
2192
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002193 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002194}
2195
2196 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002197DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002198{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002199 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2200}
2201
2202static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002203 0, // sq_length
2204 0, // sq_concat
2205 0, // sq_repeat
2206 0, // sq_item
2207 0, // sq_slice
2208 0, // sq_ass_item
2209 0, // sq_ass_slice
2210 (objobjproc) DictionaryContains, // sq_contains
2211 0, // sq_inplace_concat
2212 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002213};
2214
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002215static PyMappingMethods DictionaryAsMapping = {
2216 (lenfunc) DictionaryLength,
2217 (binaryfunc) DictionaryItem,
2218 (objobjargproc) DictionaryAssItem,
2219};
2220
Bram Moolenaardb913952012-06-29 12:54:53 +02002221static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002222 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002223 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2224 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2225 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2226 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2227 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002228 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002229 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002230 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2231 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002232};
2233
2234static PyTypeObject ListType;
2235
2236typedef struct
2237{
2238 PyObject_HEAD
2239 list_T *list;
2240 pylinkedlist_T ref;
2241} ListObject;
2242
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002243#define NEW_LIST(list) ListNew(&ListType, list)
2244
Bram Moolenaardb913952012-06-29 12:54:53 +02002245 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002246ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002247{
2248 ListObject *self;
2249
Bram Moolenaarab589462020-07-06 21:03:06 +02002250 if (list == NULL)
2251 return NULL;
2252
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002253 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002254 if (self == NULL)
2255 return NULL;
2256 self->list = list;
2257 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002258 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002259
2260 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2261
2262 return (PyObject *)(self);
2263}
2264
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002265 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002266py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002267{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002268 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002269
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002270 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002271 {
2272 PyErr_NoMemory();
2273 return NULL;
2274 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002275 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002277 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002278}
2279
2280 static int
2281list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2282{
2283 PyObject *iterator;
2284 PyObject *item;
2285 listitem_T *li;
2286
2287 if (!(iterator = PyObject_GetIter(obj)))
2288 return -1;
2289
2290 while ((item = PyIter_Next(iterator)))
2291 {
2292 if (!(li = listitem_alloc()))
2293 {
2294 PyErr_NoMemory();
2295 Py_DECREF(item);
2296 Py_DECREF(iterator);
2297 return -1;
2298 }
2299 li->li_tv.v_lock = 0;
2300 li->li_tv.v_type = VAR_UNKNOWN;
2301
2302 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2303 {
2304 Py_DECREF(item);
2305 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002307 return -1;
2308 }
2309
2310 Py_DECREF(item);
2311
2312 list_append(l, li);
2313 }
2314
2315 Py_DECREF(iterator);
2316
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002317 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002318 if (PyErr_Occurred())
2319 return -1;
2320
2321 return 0;
2322}
2323
2324 static PyObject *
2325ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2326{
2327 list_T *list;
2328 PyObject *obj = NULL;
2329
2330 if (kwargs)
2331 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002332 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002333 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002334 return NULL;
2335 }
2336
2337 if (!PyArg_ParseTuple(args, "|O", &obj))
2338 return NULL;
2339
2340 if (!(list = py_list_alloc()))
2341 return NULL;
2342
2343 if (obj)
2344 {
2345 PyObject *lookup_dict;
2346
2347 if (!(lookup_dict = PyDict_New()))
2348 {
2349 list_unref(list);
2350 return NULL;
2351 }
2352
2353 if (list_py_concat(list, obj, lookup_dict) == -1)
2354 {
2355 Py_DECREF(lookup_dict);
2356 list_unref(list);
2357 return NULL;
2358 }
2359
2360 Py_DECREF(lookup_dict);
2361 }
2362
2363 return ListNew(subtype, list);
2364}
2365
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002366 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002367ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002368{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002369 pyll_remove(&self->ref, &lastlist);
2370 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002371
2372 DESTRUCTOR_FINISH(self);
2373}
2374
Bram Moolenaardb913952012-06-29 12:54:53 +02002375 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002377{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002378 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002379}
2380
2381 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002382ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002383{
2384 listitem_T *li;
2385
Bram Moolenaard6e39182013-05-21 18:30:34 +02002386 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002387 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002388 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002389 return NULL;
2390 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002391 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002392 if (li == NULL)
2393 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002394 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002395 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002396 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002397 return NULL;
2398 }
2399 return ConvertToPyObject(&li->li_tv);
2400}
2401
Bram Moolenaardb913952012-06-29 12:54:53 +02002402 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002403ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2404 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002405{
2406 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002407 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002408
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002409 if (step == 0)
2410 {
2411 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2412 return NULL;
2413 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002414
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002415 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 if (list == NULL)
2417 return NULL;
2418
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002419 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002420 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002421 PyObject *item;
2422
2423 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002424 if (item == NULL)
2425 {
2426 Py_DECREF(list);
2427 return NULL;
2428 }
2429
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002430 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002431 }
2432
2433 return list;
2434}
2435
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002436 static PyObject *
2437ListItem(ListObject *self, PyObject* idx)
2438{
2439#if PY_MAJOR_VERSION < 3
2440 if (PyInt_Check(idx))
2441 {
2442 long _idx = PyInt_AsLong(idx);
2443 return ListIndex(self, _idx);
2444 }
2445 else
2446#endif
2447 if (PyLong_Check(idx))
2448 {
2449 long _idx = PyLong_AsLong(idx);
2450 return ListIndex(self, _idx);
2451 }
2452 else if (PySlice_Check(idx))
2453 {
2454 Py_ssize_t start, stop, step, slicelen;
2455
Bram Moolenaar922a4662014-03-30 16:11:43 +02002456 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002457 &start, &stop, &step, &slicelen) < 0)
2458 return NULL;
2459 return ListSlice(self, start, step, slicelen);
2460 }
2461 else
2462 {
2463 RAISE_INVALID_INDEX_TYPE(idx);
2464 return NULL;
2465 }
2466}
2467
2468 static void
2469list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2470 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2471{
2472 while (numreplaced--)
2473 {
2474 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2475 listitem_remove(l, lis[slicelen + numreplaced]);
2476 }
2477 while (numadded--)
2478 {
2479 listitem_T *next;
2480
2481 next = lastaddedli->li_prev;
2482 listitem_remove(l, lastaddedli);
2483 lastaddedli = next;
2484 }
2485}
2486
2487 static int
2488ListAssSlice(ListObject *self, Py_ssize_t first,
2489 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2490{
2491 PyObject *iterator;
2492 PyObject *item;
2493 listitem_T *li;
2494 listitem_T *lastaddedli = NULL;
2495 listitem_T *next;
2496 typval_T v;
2497 list_T *l = self->list;
2498 PyInt i;
2499 PyInt j;
2500 PyInt numreplaced = 0;
2501 PyInt numadded = 0;
2502 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002503 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002504
2505 size = ListLength(self);
2506
2507 if (l->lv_lock)
2508 {
2509 RAISE_LOCKED_LIST;
2510 return -1;
2511 }
2512
2513 if (step == 0)
2514 {
2515 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2516 return -1;
2517 }
2518
2519 if (step != 1 && slicelen == 0)
2520 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002521 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002522 int ret = 0;
2523
2524 if (obj == NULL)
2525 return 0;
2526
2527 if (!(iterator = PyObject_GetIter(obj)))
2528 return -1;
2529
2530 if ((item = PyIter_Next(iterator)))
2531 {
2532 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002533 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002534 "to extended slice"), 0);
2535 Py_DECREF(item);
2536 ret = -1;
2537 }
2538 Py_DECREF(iterator);
2539 return ret;
2540 }
2541
2542 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002543 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002544 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2545 {
2546 PyErr_NoMemory();
2547 return -1;
2548 }
2549
2550 if (first == size)
2551 li = NULL;
2552 else
2553 {
2554 li = list_find(l, (long) first);
2555 if (li == NULL)
2556 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002557 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002558 (int)first);
2559 if (obj != NULL)
2560 PyMem_Free(lis);
2561 return -1;
2562 }
2563 i = slicelen;
2564 while (i-- && li != NULL)
2565 {
2566 j = step;
2567 next = li;
2568 if (step > 0)
2569 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2570 else
2571 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2572
2573 if (obj == NULL)
2574 listitem_remove(l, li);
2575 else
2576 lis[slicelen - i - 1] = li;
2577
2578 li = next;
2579 }
2580 if (li == NULL && i != -1)
2581 {
2582 PyErr_SET_VIM(N_("internal error: not enough list items"));
2583 if (obj != NULL)
2584 PyMem_Free(lis);
2585 return -1;
2586 }
2587 }
2588
2589 if (obj == NULL)
2590 return 0;
2591
2592 if (!(iterator = PyObject_GetIter(obj)))
2593 {
2594 PyMem_Free(lis);
2595 return -1;
2596 }
2597
2598 i = 0;
2599 while ((item = PyIter_Next(iterator)))
2600 {
2601 if (ConvertFromPyObject(item, &v) == -1)
2602 {
2603 Py_DECREF(iterator);
2604 Py_DECREF(item);
2605 PyMem_Free(lis);
2606 return -1;
2607 }
2608 Py_DECREF(item);
2609 if (list_insert_tv(l, &v, numreplaced < slicelen
2610 ? lis[numreplaced]
2611 : li) == FAIL)
2612 {
2613 clear_tv(&v);
2614 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2615 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2616 PyMem_Free(lis);
2617 return -1;
2618 }
2619 if (numreplaced < slicelen)
2620 {
2621 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002622 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002623 numreplaced++;
2624 }
2625 else
2626 {
2627 if (li)
2628 lastaddedli = li->li_prev;
2629 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002630 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002631 numadded++;
2632 }
2633 clear_tv(&v);
2634 if (step != 1 && i >= slicelen)
2635 {
2636 Py_DECREF(iterator);
2637 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002638 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002639 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002640 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2641 PyMem_Free(lis);
2642 return -1;
2643 }
2644 ++i;
2645 }
2646 Py_DECREF(iterator);
2647
2648 if (step != 1 && i != slicelen)
2649 {
2650 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002651 N_("attempt to assign sequence of size %d to extended slice "
2652 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002653 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2654 PyMem_Free(lis);
2655 return -1;
2656 }
2657
2658 if (PyErr_Occurred())
2659 {
2660 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2661 PyMem_Free(lis);
2662 return -1;
2663 }
2664
2665 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002667 if (step == 1)
2668 for (i = numreplaced; i < slicelen; i++)
2669 listitem_remove(l, lis[i]);
2670
2671 PyMem_Free(lis);
2672
2673 return 0;
2674}
2675
2676 static int
2677ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2678{
2679 typval_T tv;
2680 list_T *l = self->list;
2681 listitem_T *li;
2682 Py_ssize_t length = ListLength(self);
2683
2684 if (l->lv_lock)
2685 {
2686 RAISE_LOCKED_LIST;
2687 return -1;
2688 }
2689 if (index > length || (index == length && obj == NULL))
2690 {
2691 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2692 return -1;
2693 }
2694
2695 if (obj == NULL)
2696 {
2697 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002698 if (li == NULL)
2699 {
2700 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2701 "list item %d"), (int) index);
2702 return -1;
2703 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002704 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002705 clear_tv(&li->li_tv);
2706 vim_free(li);
2707 return 0;
2708 }
2709
2710 if (ConvertFromPyObject(obj, &tv) == -1)
2711 return -1;
2712
2713 if (index == length)
2714 {
2715 if (list_append_tv(l, &tv) == FAIL)
2716 {
2717 clear_tv(&tv);
2718 PyErr_SET_VIM(N_("failed to add item to list"));
2719 return -1;
2720 }
2721 }
2722 else
2723 {
2724 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002725 if (li == NULL)
2726 {
2727 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2728 "list item %d"), (int) index);
2729 return -1;
2730 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002731 clear_tv(&li->li_tv);
2732 copy_tv(&tv, &li->li_tv);
2733 clear_tv(&tv);
2734 }
2735 return 0;
2736}
2737
2738 static Py_ssize_t
2739ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2740{
2741#if PY_MAJOR_VERSION < 3
2742 if (PyInt_Check(idx))
2743 {
2744 long _idx = PyInt_AsLong(idx);
2745 return ListAssIndex(self, _idx, obj);
2746 }
2747 else
2748#endif
2749 if (PyLong_Check(idx))
2750 {
2751 long _idx = PyLong_AsLong(idx);
2752 return ListAssIndex(self, _idx, obj);
2753 }
2754 else if (PySlice_Check(idx))
2755 {
2756 Py_ssize_t start, stop, step, slicelen;
2757
Bram Moolenaar922a4662014-03-30 16:11:43 +02002758 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002759 &start, &stop, &step, &slicelen) < 0)
2760 return -1;
2761 return ListAssSlice(self, start, step, slicelen,
2762 obj);
2763 }
2764 else
2765 {
2766 RAISE_INVALID_INDEX_TYPE(idx);
2767 return -1;
2768 }
2769}
2770
2771 static PyObject *
2772ListConcatInPlace(ListObject *self, PyObject *obj)
2773{
2774 list_T *l = self->list;
2775 PyObject *lookup_dict;
2776
2777 if (l->lv_lock)
2778 {
2779 RAISE_LOCKED_LIST;
2780 return NULL;
2781 }
2782
2783 if (!(lookup_dict = PyDict_New()))
2784 return NULL;
2785
2786 if (list_py_concat(l, obj, lookup_dict) == -1)
2787 {
2788 Py_DECREF(lookup_dict);
2789 return NULL;
2790 }
2791 Py_DECREF(lookup_dict);
2792
2793 Py_INCREF(self);
2794 return (PyObject *)(self);
2795}
2796
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002797typedef struct
2798{
2799 listwatch_T lw;
2800 list_T *list;
2801} listiterinfo_T;
2802
2803 static void
2804ListIterDestruct(listiterinfo_T *lii)
2805{
2806 list_rem_watch(lii->list, &lii->lw);
2807 PyMem_Free(lii);
2808}
2809
2810 static PyObject *
2811ListIterNext(listiterinfo_T **lii)
2812{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002813 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002814
2815 if (!((*lii)->lw.lw_item))
2816 return NULL;
2817
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002818 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002819 return NULL;
2820
2821 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2822
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002823 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002824}
2825
2826 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002827ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002828{
2829 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002830 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002831
2832 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2833 {
2834 PyErr_NoMemory();
2835 return NULL;
2836 }
2837
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002838 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002839 list_add_watch(l, &lii->lw);
2840 lii->lw.lw_item = l->lv_first;
2841 lii->list = l;
2842
2843 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002844 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2845 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002846}
2847
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002848static char *ListAttrs[] = {
2849 "locked",
2850 NULL
2851};
2852
2853 static PyObject *
2854ListDir(PyObject *self)
2855{
2856 return ObjectDir(self, ListAttrs);
2857}
2858
Bram Moolenaar66b79852012-09-21 14:00:35 +02002859 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002860ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002861{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002862 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002863 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002864 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002865 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002866 return -1;
2867 }
2868
2869 if (strcmp(name, "locked") == 0)
2870 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002871 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002873 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002874 return -1;
2875 }
2876 else
2877 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002878 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002879 if (istrue == -1)
2880 return -1;
2881 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002882 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002883 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002884 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002885 }
2886 return 0;
2887 }
2888 else
2889 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002890 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002891 return -1;
2892 }
2893}
2894
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002895static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002896 (lenfunc) ListLength, // sq_length, len(x)
2897 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2898 0, // RangeRepeat, sq_repeat, x*n
2899 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2900 0, // was_sq_slice, x[i:j]
2901 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2902 0, // was_sq_ass_slice, x[i:j]=v
2903 0, // sq_contains
2904 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2905 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002906};
2907
2908static PyMappingMethods ListAsMapping = {
2909 /* mp_length */ (lenfunc) ListLength,
2910 /* mp_subscript */ (binaryfunc) ListItem,
2911 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2912};
2913
Bram Moolenaardb913952012-06-29 12:54:53 +02002914static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002915 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2916 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2917 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002918};
2919
2920typedef struct
2921{
2922 PyObject_HEAD
2923 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002924 int argc;
2925 typval_T *argv;
2926 dict_T *self;
2927 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002928 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002929} FunctionObject;
2930
2931static PyTypeObject FunctionType;
2932
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002933#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2934 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002935
Bram Moolenaardb913952012-06-29 12:54:53 +02002936 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002937FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002938 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002939{
2940 FunctionObject *self;
2941
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002942 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002943 if (self == NULL)
2944 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002945
2946 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002947 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002948 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002949 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002950 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002951 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002952 return NULL;
2953 }
2954 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002955 }
2956 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002957 {
2958 char_u *p;
2959
2960 if ((p = get_expanded_name(name,
2961 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002962 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002963 PyErr_FORMAT(PyExc_ValueError,
2964 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002965 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002966 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002967
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002968 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2969 {
2970 char_u *np;
2971 size_t len = STRLEN(p) + 1;
2972
Bram Moolenaar51e14382019-05-25 20:21:28 +02002973 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002974 {
2975 vim_free(p);
2976 return NULL;
2977 }
2978 mch_memmove(np, "<SNR>", 5);
2979 mch_memmove(np + 5, p + 3, len - 3);
2980 vim_free(p);
2981 self->name = np;
2982 }
2983 else
2984 self->name = p;
2985 }
2986
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002987 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002988 self->argc = argc;
2989 self->argv = argv;
2990 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002991 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002992
2993 if (self->argv || self->self)
2994 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2995
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002996 return (PyObject *)(self);
2997}
2998
2999 static PyObject *
3000FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3001{
3002 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003003 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003004 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003005 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003006 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003007 typval_T selfdicttv;
3008 typval_T argstv;
3009 list_T *argslist = NULL;
3010 dict_T *selfdict = NULL;
3011 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003012 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003013 typval_T *argv = NULL;
3014 typval_T *curtv;
3015 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003016
Bram Moolenaar8110a092016-04-14 15:56:09 +02003017 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003018 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003019 selfdictObject = PyDict_GetItemString(kwargs, "self");
3020 if (selfdictObject != NULL)
3021 {
3022 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3023 return NULL;
3024 selfdict = selfdicttv.vval.v_dict;
3025 }
3026 argsObject = PyDict_GetItemString(kwargs, "args");
3027 if (argsObject != NULL)
3028 {
3029 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3030 {
3031 dict_unref(selfdict);
3032 return NULL;
3033 }
3034 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003035 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003036
3037 argc = argslist->lv_len;
3038 if (argc != 0)
3039 {
3040 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003041 if (argv == NULL)
3042 {
3043 PyErr_NoMemory();
3044 dict_unref(selfdict);
3045 list_unref(argslist);
3046 return NULL;
3047 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003048 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003049 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003050 copy_tv(&li->li_tv, curtv++);
3051 }
3052 list_unref(argslist);
3053 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003054 if (selfdict != NULL)
3055 {
3056 auto_rebind = FALSE;
3057 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3058 if (autoRebindObject != NULL)
3059 {
3060 auto_rebind = PyObject_IsTrue(autoRebindObject);
3061 if (auto_rebind == -1)
3062 {
3063 dict_unref(selfdict);
3064 list_unref(argslist);
3065 return NULL;
3066 }
3067 }
3068 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003069 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003070
Bram Moolenaar389a1792013-06-23 13:00:44 +02003071 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003072 {
3073 dict_unref(selfdict);
3074 while (argc--)
3075 clear_tv(&argv[argc]);
3076 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003077 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003078 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003079
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003080 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003081
Bram Moolenaar389a1792013-06-23 13:00:44 +02003082 PyMem_Free(name);
3083
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003084 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003085}
3086
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003087 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003088FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003089{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003090 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003091 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003092 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003093 for (i = 0; i < self->argc; ++i)
3094 clear_tv(&self->argv[i]);
3095 PyMem_Free(self->argv);
3096 dict_unref(self->self);
3097 if (self->argv || self->self)
3098 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003099
3100 DESTRUCTOR_FINISH(self);
3101}
3102
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003103static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003104 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003105 NULL
3106};
3107
3108 static PyObject *
3109FunctionDir(PyObject *self)
3110{
3111 return ObjectDir(self, FunctionAttrs);
3112}
3113
Bram Moolenaardb913952012-06-29 12:54:53 +02003114 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003115FunctionAttr(FunctionObject *self, char *name)
3116{
3117 list_T *list;
3118 int i;
3119 if (strcmp(name, "name") == 0)
3120 return PyString_FromString((char *)(self->name));
3121 else if (strcmp(name, "args") == 0)
3122 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003123 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003124 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003125
Bram Moolenaar8110a092016-04-14 15:56:09 +02003126 for (i = 0; i < self->argc; ++i)
3127 list_append_tv(list, &self->argv[i]);
3128 return NEW_LIST(list);
3129 }
3130 else if (strcmp(name, "self") == 0)
3131 return self->self == NULL
3132 ? AlwaysNone(NULL)
3133 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003134 else if (strcmp(name, "auto_rebind") == 0)
3135 return self->auto_rebind
3136 ? AlwaysTrue(NULL)
3137 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003138 else if (strcmp(name, "__members__") == 0)
3139 return ObjectDir(NULL, FunctionAttrs);
3140 return NULL;
3141}
3142
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003143/*
3144 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003145 *
3146 * "exported" should be set to true when it is needed to construct a partial
3147 * that may be stored in a variable (i.e. may be freed by Vim).
3148 */
3149 static void
3150set_partial(FunctionObject *self, partial_T *pt, int exported)
3151{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003152 int i;
3153
3154 pt->pt_name = self->name;
3155 if (self->argv)
3156 {
3157 pt->pt_argc = self->argc;
3158 if (exported)
3159 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003160 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003161 for (i = 0; i < pt->pt_argc; ++i)
3162 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3163 }
3164 else
3165 pt->pt_argv = self->argv;
3166 }
3167 else
3168 {
3169 pt->pt_argc = 0;
3170 pt->pt_argv = NULL;
3171 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003172 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003173 pt->pt_dict = self->self;
3174 if (exported && self->self)
3175 ++pt->pt_dict->dv_refcount;
3176 if (exported)
3177 pt->pt_name = vim_strsave(pt->pt_name);
3178 pt->pt_refcount = 1;
3179}
3180
3181 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003182FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003183{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003184 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003185 typval_T args;
3186 typval_T selfdicttv;
3187 typval_T rettv;
3188 dict_T *selfdict = NULL;
3189 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003190 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003191 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003192 partial_T pt;
3193 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003194
Bram Moolenaar8110a092016-04-14 15:56:09 +02003195 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003196 return NULL;
3197
3198 if (kwargs != NULL)
3199 {
3200 selfdictObject = PyDict_GetItemString(kwargs, "self");
3201 if (selfdictObject != NULL)
3202 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003203 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003204 {
3205 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003206 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003207 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003208 selfdict = selfdicttv.vval.v_dict;
3209 }
3210 }
3211
Bram Moolenaar8110a092016-04-14 15:56:09 +02003212 if (self->argv || self->self)
3213 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003214 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003215 set_partial(self, &pt, FALSE);
3216 pt_ptr = &pt;
3217 }
3218
Bram Moolenaar71700b82013-05-15 17:49:05 +02003219 Py_BEGIN_ALLOW_THREADS
3220 Python_Lock_Vim();
3221
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003222 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003223 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003224
3225 Python_Release_Vim();
3226 Py_END_ALLOW_THREADS
3227
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003228 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003229 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003230 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003231 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003232 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003233 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003234 }
3235 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003236 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003237
Bram Moolenaardb913952012-06-29 12:54:53 +02003238 clear_tv(&args);
3239 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003240 if (selfdict != NULL)
3241 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003242
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003244}
3245
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003246 static PyObject *
3247FunctionRepr(FunctionObject *self)
3248{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003249 PyObject *ret;
3250 garray_T repr_ga;
3251 int i;
3252 char_u *tofree = NULL;
3253 typval_T tv;
3254 char_u numbuf[NUMBUFLEN];
3255
3256 ga_init2(&repr_ga, (int)sizeof(char), 70);
3257 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3258 if (self->name)
3259 ga_concat(&repr_ga, self->name);
3260 else
3261 ga_concat(&repr_ga, (char_u *)"<NULL>");
3262 ga_append(&repr_ga, '\'');
3263 if (self->argv)
3264 {
3265 ga_concat(&repr_ga, (char_u *)", args=[");
3266 ++emsg_silent;
3267 for (i = 0; i < self->argc; i++)
3268 {
3269 if (i != 0)
3270 ga_concat(&repr_ga, (char_u *)", ");
3271 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3272 get_copyID()));
3273 vim_free(tofree);
3274 }
3275 --emsg_silent;
3276 ga_append(&repr_ga, ']');
3277 }
3278 if (self->self)
3279 {
3280 ga_concat(&repr_ga, (char_u *)", self=");
3281 tv.v_type = VAR_DICT;
3282 tv.vval.v_dict = self->self;
3283 ++emsg_silent;
3284 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3285 --emsg_silent;
3286 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003287 if (self->auto_rebind)
3288 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003289 }
3290 ga_append(&repr_ga, '>');
3291 ret = PyString_FromString((char *)repr_ga.ga_data);
3292 ga_clear(&repr_ga);
3293 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003294}
3295
Bram Moolenaardb913952012-06-29 12:54:53 +02003296static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003297 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3298 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003299};
3300
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003301/*
3302 * Options object
3303 */
3304
3305static PyTypeObject OptionsType;
3306
3307typedef int (*checkfun)(void *);
3308
3309typedef struct
3310{
3311 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003312 int opt_type;
3313 void *from;
3314 checkfun Check;
3315 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003316} OptionsObject;
3317
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003318 static int
3319dummy_check(void *arg UNUSED)
3320{
3321 return 0;
3322}
3323
3324 static PyObject *
3325OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3326{
3327 OptionsObject *self;
3328
Bram Moolenaar774267b2013-05-21 20:51:59 +02003329 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003330 if (self == NULL)
3331 return NULL;
3332
3333 self->opt_type = opt_type;
3334 self->from = from;
3335 self->Check = Check;
3336 self->fromObj = fromObj;
3337 if (fromObj)
3338 Py_INCREF(fromObj);
3339
3340 return (PyObject *)(self);
3341}
3342
3343 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003345{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003346 PyObject_GC_UnTrack((void *)(self));
3347 Py_XDECREF(self->fromObj);
3348 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003349}
3350
3351 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003352OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003353{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003354 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003355 return 0;
3356}
3357
3358 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003359OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003360{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003361 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003362 return 0;
3363}
3364
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003365 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003367{
3368 char_u *key;
3369 int flags;
3370 long numval;
3371 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003372 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003373
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003374 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003375 return NULL;
3376
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003377 if (!(key = StringToChars(keyObject, &todecref)))
3378 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003379
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003380 if (*key == NUL)
3381 {
3382 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003383 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003384 return NULL;
3385 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386
3387 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003388 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003389
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003390 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003391
3392 if (flags == 0)
3393 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003394 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003395 return NULL;
3396 }
3397
3398 if (flags & SOPT_UNSET)
3399 {
3400 Py_INCREF(Py_None);
3401 return Py_None;
3402 }
3403 else if (flags & SOPT_BOOL)
3404 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003405 PyObject *ret;
3406 ret = numval ? Py_True : Py_False;
3407 Py_INCREF(ret);
3408 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003409 }
3410 else if (flags & SOPT_NUM)
3411 return PyInt_FromLong(numval);
3412 else if (flags & SOPT_STRING)
3413 {
3414 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003415 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003416 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003417 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003418 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003419 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003420 else
3421 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003422 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003423 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003424 return NULL;
3425 }
3426 }
3427 else
3428 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003429 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003430 return NULL;
3431 }
3432}
3433
3434 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003435OptionsContains(OptionsObject *self, PyObject *keyObject)
3436{
3437 char_u *key;
3438 PyObject *todecref;
3439
3440 if (!(key = StringToChars(keyObject, &todecref)))
3441 return -1;
3442
3443 if (*key == NUL)
3444 {
3445 Py_XDECREF(todecref);
3446 return 0;
3447 }
3448
3449 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3450 {
3451 Py_XDECREF(todecref);
3452 return 1;
3453 }
3454 else
3455 {
3456 Py_XDECREF(todecref);
3457 return 0;
3458 }
3459}
3460
3461typedef struct
3462{
3463 void *lastoption;
3464 int opt_type;
3465} optiterinfo_T;
3466
3467 static PyObject *
3468OptionsIterNext(optiterinfo_T **oii)
3469{
3470 char_u *name;
3471
3472 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3473 return PyString_FromString((char *)name);
3474
3475 return NULL;
3476}
3477
3478 static PyObject *
3479OptionsIter(OptionsObject *self)
3480{
3481 optiterinfo_T *oii;
3482
3483 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3484 {
3485 PyErr_NoMemory();
3486 return NULL;
3487 }
3488
3489 oii->opt_type = self->opt_type;
3490 oii->lastoption = NULL;
3491
3492 return IterNew(oii,
3493 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3494 NULL, NULL);
3495}
3496
3497 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003498set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003499{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003500 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003501
3502 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3503 {
3504 if (VimTryEnd())
3505 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003506 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003507 return FAIL;
3508 }
3509 return OK;
3510}
3511
3512 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003513set_option_value_for(
3514 char_u *key,
3515 int numval,
3516 char_u *stringval,
3517 int opt_flags,
3518 int opt_type,
3519 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003520{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003521 win_T *save_curwin = NULL;
3522 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003523 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003524 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003525
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003526 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003527 switch (opt_type)
3528 {
3529 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003530 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003531 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003532 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003533 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003534 if (VimTryEnd())
3535 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003536 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003537 return -1;
3538 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003539 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3540 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003541 break;
3542 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003543 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003544 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003545 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003546 break;
3547 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003548 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003549 break;
3550 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003551 if (set_ret == FAIL)
3552 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003553 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003554}
3555
3556 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003557OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003558{
3559 char_u *key;
3560 int flags;
3561 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003562 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003563 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003564
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003565 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003566 return -1;
3567
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003568 if (!(key = StringToChars(keyObject, &todecref)))
3569 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003570
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003571 if (*key == NUL)
3572 {
3573 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003574 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003575 return -1;
3576 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577
3578 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003579 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003580
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003581 if (flags == 0)
3582 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003583 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003584 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003585 return -1;
3586 }
3587
3588 if (valObject == NULL)
3589 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003590 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003591 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003592 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003593 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003594 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003595 return -1;
3596 }
3597 else if (!(flags & SOPT_GLOBAL))
3598 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003599 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003600 N_("unable to unset option %s "
3601 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003602 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003603 return -1;
3604 }
3605 else
3606 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003607 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003608 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003609 return 0;
3610 }
3611 }
3612
Bram Moolenaard6e39182013-05-21 18:30:34 +02003613 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614
3615 if (flags & SOPT_BOOL)
3616 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003617 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003618
Bram Moolenaarb983f752013-05-15 16:11:50 +02003619 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003620 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003621 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003622 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003623 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003624 }
3625 else if (flags & SOPT_NUM)
3626 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003627 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003628
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003629 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003630 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003631 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003632 return -1;
3633 }
3634
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003635 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003636 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003637 }
3638 else
3639 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003640 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003641 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003642
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003643 if ((val = StringToChars(valObject, &todecref2)))
3644 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003645 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003646 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003647 Py_XDECREF(todecref2);
3648 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003649 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003650 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003651 }
3652
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003653 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003654
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003655 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003656}
3657
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003658static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003659 0, // sq_length
3660 0, // sq_concat
3661 0, // sq_repeat
3662 0, // sq_item
3663 0, // sq_slice
3664 0, // sq_ass_item
3665 0, // sq_ass_slice
3666 (objobjproc) OptionsContains, // sq_contains
3667 0, // sq_inplace_concat
3668 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003669};
3670
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003671static PyMappingMethods OptionsAsMapping = {
3672 (lenfunc) NULL,
3673 (binaryfunc) OptionsItem,
3674 (objobjargproc) OptionsAssItem,
3675};
3676
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003677// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003678
3679typedef struct
3680{
3681 PyObject_HEAD
3682 tabpage_T *tab;
3683} TabPageObject;
3684
3685static PyObject *WinListNew(TabPageObject *tabObject);
3686
3687static PyTypeObject TabPageType;
3688
3689 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003690CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003691{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003692 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003693 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003694 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003695 return -1;
3696 }
3697
3698 return 0;
3699}
3700
3701 static PyObject *
3702TabPageNew(tabpage_T *tab)
3703{
3704 TabPageObject *self;
3705
3706 if (TAB_PYTHON_REF(tab))
3707 {
3708 self = TAB_PYTHON_REF(tab);
3709 Py_INCREF(self);
3710 }
3711 else
3712 {
3713 self = PyObject_NEW(TabPageObject, &TabPageType);
3714 if (self == NULL)
3715 return NULL;
3716 self->tab = tab;
3717 TAB_PYTHON_REF(tab) = self;
3718 }
3719
3720 return (PyObject *)(self);
3721}
3722
3723 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003724TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003725{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003726 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3727 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003728
3729 DESTRUCTOR_FINISH(self);
3730}
3731
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003732static char *TabPageAttrs[] = {
3733 "windows", "number", "vars", "window", "valid",
3734 NULL
3735};
3736
3737 static PyObject *
3738TabPageDir(PyObject *self)
3739{
3740 return ObjectDir(self, TabPageAttrs);
3741}
3742
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003743 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003744TabPageAttrValid(TabPageObject *self, char *name)
3745{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003746 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003747
3748 if (strcmp(name, "valid") != 0)
3749 return NULL;
3750
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003751 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3752 Py_INCREF(ret);
3753 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003754}
3755
3756 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003757TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003758{
3759 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003760 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003761 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003762 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003763 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003764 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003765 else if (strcmp(name, "window") == 0)
3766 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003767 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003768 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003769 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003770 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003771 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003772 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003773 else if (strcmp(name, "__members__") == 0)
3774 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003775 return NULL;
3776}
3777
3778 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003779TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003780{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003781 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003782 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783 else
3784 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003785 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003786
3787 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003788 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3789 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003790 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003791 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003792 }
3793}
3794
3795static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003796 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003797 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3798 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003799};
3800
3801/*
3802 * Window list object
3803 */
3804
3805static PyTypeObject TabListType;
3806static PySequenceMethods TabListAsSeq;
3807
3808typedef struct
3809{
3810 PyObject_HEAD
3811} TabListObject;
3812
3813 static PyInt
3814TabListLength(PyObject *self UNUSED)
3815{
3816 tabpage_T *tp = first_tabpage;
3817 PyInt n = 0;
3818
3819 while (tp != NULL)
3820 {
3821 ++n;
3822 tp = tp->tp_next;
3823 }
3824
3825 return n;
3826}
3827
3828 static PyObject *
3829TabListItem(PyObject *self UNUSED, PyInt n)
3830{
3831 tabpage_T *tp;
3832
3833 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3834 if (n == 0)
3835 return TabPageNew(tp);
3836
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003837 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003838 return NULL;
3839}
3840
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003841/*
3842 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003843 */
3844
3845typedef struct
3846{
3847 PyObject_HEAD
3848 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003849 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003850} WindowObject;
3851
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003852static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003853
3854 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003855CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003856{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003857 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003858 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003859 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003860 return -1;
3861 }
3862
3863 return 0;
3864}
3865
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003866 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003867WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003868{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003869 /*
3870 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003871 * If we add a "w_python*_ref" field to the win_T structure,
3872 * then we can get at it in win_free() in vim. We then
3873 * need to create only ONE Python object per window - if
3874 * we try to create a second, just INCREF the existing one
3875 * and return it. The (single) Python object referring to
3876 * the window is stored in "w_python*_ref".
3877 * On a win_free() we set the Python object's win_T* field
3878 * to an invalid value. We trap all uses of a window
3879 * object, and reject them if the win_T* field is invalid.
3880 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003881 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003882 * w_python_ref and w_python3_ref fields respectively.
3883 */
3884
3885 WindowObject *self;
3886
3887 if (WIN_PYTHON_REF(win))
3888 {
3889 self = WIN_PYTHON_REF(win);
3890 Py_INCREF(self);
3891 }
3892 else
3893 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003894 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003895 if (self == NULL)
3896 return NULL;
3897 self->win = win;
3898 WIN_PYTHON_REF(win) = self;
3899 }
3900
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003901 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3902
Bram Moolenaar971db462013-05-12 18:44:48 +02003903 return (PyObject *)(self);
3904}
3905
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003906 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003907WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003908{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003909 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003910 if (self->win && self->win != INVALID_WINDOW_VALUE)
3911 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003912 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003913 PyObject_GC_Del((void *)(self));
3914}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003915
Bram Moolenaar774267b2013-05-21 20:51:59 +02003916 static int
3917WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3918{
3919 Py_VISIT(((PyObject *)(self->tabObject)));
3920 return 0;
3921}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003922
Bram Moolenaar774267b2013-05-21 20:51:59 +02003923 static int
3924WindowClear(WindowObject *self)
3925{
3926 Py_CLEAR(self->tabObject);
3927 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003928}
3929
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003930 static win_T *
3931get_firstwin(TabPageObject *tabObject)
3932{
3933 if (tabObject)
3934 {
3935 if (CheckTabPage(tabObject))
3936 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003937 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003938 else if (tabObject->tab == curtab)
3939 return firstwin;
3940 else
3941 return tabObject->tab->tp_firstwin;
3942 }
3943 else
3944 return firstwin;
3945}
Bram Moolenaare950f992018-06-10 13:55:55 +02003946
3947// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003948static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003949 "buffer",
3950 "cursor",
3951 "height",
3952 "row",
3953 "width",
3954 "col",
3955 "vars",
3956 "options",
3957 "number",
3958 "tabpage",
3959 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003960 NULL
3961};
3962
3963 static PyObject *
3964WindowDir(PyObject *self)
3965{
3966 return ObjectDir(self, WindowAttrs);
3967}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003968
Bram Moolenaar971db462013-05-12 18:44:48 +02003969 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003970WindowAttrValid(WindowObject *self, char *name)
3971{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003972 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003973
3974 if (strcmp(name, "valid") != 0)
3975 return NULL;
3976
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003977 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3978 Py_INCREF(ret);
3979 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003980}
3981
3982 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003983WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003984{
3985 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003986 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003987 else if (strcmp(name, "cursor") == 0)
3988 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003989 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003990
3991 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3992 }
3993 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003994 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003995 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003997 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003998 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003999 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004000 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004001 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004002 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004003 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004004 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4005 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004006 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004007 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004008 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004009 return NULL;
4010 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004011 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004012 }
4013 else if (strcmp(name, "tabpage") == 0)
4014 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 Py_INCREF(self->tabObject);
4016 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004017 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004018 else if (strcmp(name, "__members__") == 0)
4019 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004020 else
4021 return NULL;
4022}
4023
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004024 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004025WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004027 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004028 return -1;
4029
4030 if (strcmp(name, "buffer") == 0)
4031 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004032 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004033 return -1;
4034 }
4035 else if (strcmp(name, "cursor") == 0)
4036 {
4037 long lnum;
4038 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004040 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041 return -1;
4042
Bram Moolenaard6e39182013-05-21 18:30:34 +02004043 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004045 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046 return -1;
4047 }
4048
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004049 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004050 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 return -1;
4052
Bram Moolenaard6e39182013-05-21 18:30:34 +02004053 self->win->w_cursor.lnum = lnum;
4054 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004055 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004056 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004057 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004058 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004060 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061 return 0;
4062 }
4063 else if (strcmp(name, "height") == 0)
4064 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004065 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066 win_T *savewin;
4067
Bram Moolenaardee2e312013-06-23 16:35:47 +02004068 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 return -1;
4070
4071#ifdef FEAT_GUI
4072 need_mouse_correct = TRUE;
4073#endif
4074 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004075 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004076
4077 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004078 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004080 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 return -1;
4082
4083 return 0;
4084 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 else if (strcmp(name, "width") == 0)
4086 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004087 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 win_T *savewin;
4089
Bram Moolenaardee2e312013-06-23 16:35:47 +02004090 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 return -1;
4092
4093#ifdef FEAT_GUI
4094 need_mouse_correct = TRUE;
4095#endif
4096 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004097 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004098
4099 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004100 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004102 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103 return -1;
4104
4105 return 0;
4106 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 else
4108 {
4109 PyErr_SetString(PyExc_AttributeError, name);
4110 return -1;
4111 }
4112}
4113
4114 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004115WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004117 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004118 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 else
4120 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004121 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122
Bram Moolenaar6d216452013-05-12 19:00:41 +02004123 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004124 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125 (self));
4126 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004127 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004128 }
4129}
4130
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004131static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004132 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004133 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4134 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004135};
4136
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004138 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004140
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004141static PyTypeObject WinListType;
4142static PySequenceMethods WinListAsSeq;
4143
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004144typedef struct
4145{
4146 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004147 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004148} WinListObject;
4149
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004150 static PyObject *
4151WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004152{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004153 WinListObject *self;
4154
4155 self = PyObject_NEW(WinListObject, &WinListType);
4156 self->tabObject = tabObject;
4157 Py_INCREF(tabObject);
4158
4159 return (PyObject *)(self);
4160}
4161
4162 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004163WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004164{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004165 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004166
4167 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004168 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004169 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004170 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004171
4172 DESTRUCTOR_FINISH(self);
4173}
4174
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004175 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004176WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004177{
4178 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004179 PyInt n = 0;
4180
Bram Moolenaard6e39182013-05-21 18:30:34 +02004181 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004182 return -1;
4183
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004184 while (w != NULL)
4185 {
4186 ++n;
4187 w = W_NEXT(w);
4188 }
4189
4190 return n;
4191}
4192
4193 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004194WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004195{
4196 win_T *w;
4197
Bram Moolenaard6e39182013-05-21 18:30:34 +02004198 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004199 return NULL;
4200
4201 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004202 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004203 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004204
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004205 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004206 return NULL;
4207}
4208
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004209/*
4210 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004211 *
4212 * The result is in allocated memory. All internal nulls are replaced by
4213 * newline characters. It is an error for the string to contain newline
4214 * characters.
4215 *
4216 * On errors, the Python exception data is set, and NULL is returned.
4217 */
4218 static char *
4219StringToLine(PyObject *obj)
4220{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004221 char *str;
4222 char *save;
4223 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004224 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004225 PyInt i;
4226 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004227
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004228 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004229 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004230 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4231 || str == NULL)
4232 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004233 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004234 else if (PyUnicode_Check(obj))
4235 {
4236 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4237 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004238
Bram Moolenaardaa27022013-06-24 22:33:30 +02004239 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004240 || str == NULL)
4241 {
4242 Py_DECREF(bytes);
4243 return NULL;
4244 }
4245 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004246 else
4247 {
4248#if PY_MAJOR_VERSION < 3
4249 PyErr_FORMAT(PyExc_TypeError,
4250 N_("expected str() or unicode() instance, but got %s"),
4251 Py_TYPE_NAME(obj));
4252#else
4253 PyErr_FORMAT(PyExc_TypeError,
4254 N_("expected bytes() or str() instance, but got %s"),
4255 Py_TYPE_NAME(obj));
4256#endif
4257 return NULL;
4258 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004259
4260 /*
4261 * Error checking: String must not contain newlines, as we
4262 * are replacing a single line, and we must replace it with
4263 * a single line.
4264 * A trailing newline is removed, so that append(f.readlines()) works.
4265 */
4266 p = memchr(str, '\n', len);
4267 if (p != NULL)
4268 {
4269 if (p == str + len - 1)
4270 --len;
4271 else
4272 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004273 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004274 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004275 return NULL;
4276 }
4277 }
4278
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004279 /*
4280 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004281 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004282 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004283 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004284 if (save == NULL)
4285 {
4286 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004287 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004288 return NULL;
4289 }
4290
4291 for (i = 0; i < len; ++i)
4292 {
4293 if (str[i] == '\0')
4294 save[i] = '\n';
4295 else
4296 save[i] = str[i];
4297 }
4298
4299 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004300 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004301
4302 return save;
4303}
4304
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004305/*
4306 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307 * in Vim format (1-based). The line is returned as a Python
4308 * string object.
4309 */
4310 static PyObject *
4311GetBufferLine(buf_T *buf, PyInt n)
4312{
4313 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4314}
4315
4316
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004317/*
4318 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004319 * are in Vim format (1-based). The range is from lo up to, but not
4320 * including, hi. The list is returned as a Python list of string objects.
4321 */
4322 static PyObject *
4323GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4324{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004325 PyInt i;
4326 PyInt n = hi - lo;
4327 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004328
4329 if (list == NULL)
4330 return NULL;
4331
4332 for (i = 0; i < n; ++i)
4333 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004334 linenr_T lnum = (linenr_T)(lo + i);
4335 char *text;
4336 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004337
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004338 if (lnum > buf->b_ml.ml_line_count)
4339 text = "";
4340 else
4341 text = (char *)ml_get_buf(buf, lnum, FALSE);
4342 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004343 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004344 {
4345 Py_DECREF(list);
4346 return NULL;
4347 }
4348
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004349 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004350 }
4351
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004352 // The ownership of the Python list is passed to the caller (ie,
4353 // the caller should Py_DECREF() the object when it is finished
4354 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004355
4356 return list;
4357}
4358
4359/*
4360 * Check if deleting lines made the cursor position invalid.
4361 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4362 * deleted).
4363 */
4364 static void
4365py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4366{
4367 if (curwin->w_cursor.lnum >= lo)
4368 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004369 // Adjust the cursor position if it's in/after the changed
4370 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004371 if (curwin->w_cursor.lnum >= hi)
4372 {
4373 curwin->w_cursor.lnum += extra;
4374 check_cursor_col();
4375 }
4376 else if (extra < 0)
4377 {
4378 curwin->w_cursor.lnum = lo;
4379 check_cursor();
4380 }
4381 else
4382 check_cursor_col();
4383 changed_cline_bef_curs();
4384 }
4385 invalidate_botline();
4386}
4387
Bram Moolenaar19e60942011-06-19 00:27:51 +02004388/*
4389 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 * in Vim format (1-based). The replacement line is given as
4391 * a Python string object. The object is checked for validity
4392 * and correct format. Errors are returned as a value of FAIL.
4393 * The return value is OK on success.
4394 * If OK is returned and len_change is not NULL, *len_change
4395 * is set to the change in the buffer length.
4396 */
4397 static int
4398SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4399{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004400 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004401 win_T *save_curwin = NULL;
4402 tabpage_T *save_curtab = NULL;
4403
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004404 // First of all, we check the type of the supplied Python object.
4405 // There are three cases:
4406 // 1. NULL, or None - this is a deletion.
4407 // 2. A string - this is a replacement.
4408 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004409 if (line == Py_None || line == NULL)
4410 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004411 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004412 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004413
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004414 VimTryStart();
4415
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004417 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004418 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004419 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420 else
4421 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004422 if (buf == curbuf && (save_curwin != NULL
4423 || save_curbuf.br_buf == NULL))
4424 // Using an existing window for the buffer, adjust the cursor
4425 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004427 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004428 // Only adjust marks if we managed to switch to a window that
4429 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004430 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431 }
4432
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004433 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004435 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004436 return FAIL;
4437
4438 if (len_change)
4439 *len_change = -1;
4440
4441 return OK;
4442 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004443 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004444 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004445 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446
4447 if (save == NULL)
4448 return FAIL;
4449
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004450 VimTryStart();
4451
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004452 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004454 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004455
4456 if (u_savesub((linenr_T)n) == FAIL)
4457 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004458 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004459 vim_free(save);
4460 }
4461 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4462 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004463 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004464 vim_free(save);
4465 }
4466 else
4467 changed_bytes((linenr_T)n, 0);
4468
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004469 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004470
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004471 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004472 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004473 check_cursor_col();
4474
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004475 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004476 return FAIL;
4477
4478 if (len_change)
4479 *len_change = 0;
4480
4481 return OK;
4482 }
4483 else
4484 {
4485 PyErr_BadArgument();
4486 return FAIL;
4487 }
4488}
4489
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004490/*
4491 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004492 * Vim format (1-based). The range is from lo up to, but not including, hi.
4493 * The replacement lines are given as a Python list of string objects. The
4494 * list is checked for validity and correct format. Errors are returned as a
4495 * value of FAIL. The return value is OK on success.
4496 * If OK is returned and len_change is not NULL, *len_change
4497 * is set to the change in the buffer length.
4498 */
4499 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004500SetBufferLineList(
4501 buf_T *buf,
4502 PyInt lo,
4503 PyInt hi,
4504 PyObject *list,
4505 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004506{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004507 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004508 win_T *save_curwin = NULL;
4509 tabpage_T *save_curtab = NULL;
4510
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004511 // First of all, we check the type of the supplied Python object.
4512 // There are three cases:
4513 // 1. NULL, or None - this is a deletion.
4514 // 2. A list - this is a replacement.
4515 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004516 if (list == Py_None || list == NULL)
4517 {
4518 PyInt i;
4519 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004520
4521 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004522 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004523 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004524
4525 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004526 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004527 else
4528 {
4529 for (i = 0; i < n; ++i)
4530 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004531 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004532 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004533 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004534 break;
4535 }
4536 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004537 if (buf == curbuf && (save_curwin != NULL
4538 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004539 // Using an existing window for the buffer, adjust the cursor
4540 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004542 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004543 // Only adjust marks if we managed to switch to a window that
4544 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004545 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004546 }
4547
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004548 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004549
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004550 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551 return FAIL;
4552
4553 if (len_change)
4554 *len_change = -n;
4555
4556 return OK;
4557 }
4558 else if (PyList_Check(list))
4559 {
4560 PyInt i;
4561 PyInt new_len = PyList_Size(list);
4562 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004563 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004564 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004565
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004566 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004567 array = NULL;
4568 else
4569 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004570 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004571 if (array == NULL)
4572 {
4573 PyErr_NoMemory();
4574 return FAIL;
4575 }
4576 }
4577
4578 for (i = 0; i < new_len; ++i)
4579 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004580 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004581
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004582 if (!(line = PyList_GetItem(list, i)) ||
4583 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004584 {
4585 while (i)
4586 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004587 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004588 return FAIL;
4589 }
4590 }
4591
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004592 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004593 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004594
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004595 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004596 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597
4598 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004599 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004600
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004601 // If the size of the range is reducing (ie, new_len < old_len) we
4602 // need to delete some old_len. We do this at the start, by
4603 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004604 if (!PyErr_Occurred())
4605 {
4606 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004607 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004608 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004609 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004610 break;
4611 }
4612 extra -= i;
4613 }
4614
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004615 // For as long as possible, replace the existing old_len with the
4616 // new old_len. This is a more efficient operation, as it requires
4617 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004618 if (!PyErr_Occurred())
4619 {
4620 for (i = 0; i < old_len && i < new_len; ++i)
4621 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4622 == FAIL)
4623 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004624 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004625 break;
4626 }
4627 }
4628 else
4629 i = 0;
4630
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004631 // Now we may need to insert the remaining new old_len. If we do, we
4632 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004633 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004634 if (!PyErr_Occurred())
4635 {
4636 while (i < new_len)
4637 {
4638 if (ml_append((linenr_T)(lo + i - 1),
4639 (char_u *)array[i], 0, FALSE) == FAIL)
4640 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004641 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004642 break;
4643 }
4644 vim_free(array[i]);
4645 ++i;
4646 ++extra;
4647 }
4648 }
4649
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004650 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004651 while (i < new_len)
4652 {
4653 vim_free(array[i]);
4654 ++i;
4655 }
4656
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004657 // Free the array of old_len. All of its contents have now
4658 // been dealt with (either freed, or the responsibility passed
4659 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004660 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004661
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004662 // Adjust marks. Invalidate any which lie in the
4663 // changed range, and move any in the remainder of the buffer.
4664 // Only adjust marks if we managed to switch to a window that holds
4665 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004666 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004667 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004668 (long)MAXLNUM, (long)extra);
4669 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4670
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004671 if (buf == curbuf && (save_curwin != NULL
4672 || save_curbuf.br_buf == NULL))
4673 // Using an existing window for the buffer, adjust the cursor
4674 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004675 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4676
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004677 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004678 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004679
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004680 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004681 return FAIL;
4682
4683 if (len_change)
4684 *len_change = new_len - old_len;
4685
4686 return OK;
4687 }
4688 else
4689 {
4690 PyErr_BadArgument();
4691 return FAIL;
4692 }
4693}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004695/*
4696 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697 * The line number is in Vim format (1-based). The lines to be inserted are
4698 * given as a Python list of string objects or as a single string. The lines
4699 * to be added are checked for validity and correct format. Errors are
4700 * returned as a value of FAIL. The return value is OK on success.
4701 * If OK is returned and len_change is not NULL, *len_change
4702 * is set to the change in the buffer length.
4703 */
4704 static int
4705InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4706{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004707 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004708 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004709 tabpage_T *save_curtab = NULL;
4710
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004711 // First of all, we check the type of the supplied Python object.
4712 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004713 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004714 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004715 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004716
4717 if (str == NULL)
4718 return FAIL;
4719
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004720 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004721 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004722 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004724 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004725 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004726 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004727 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004728 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004729 // Only adjust marks if we managed to switch to a window that
4730 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731 appended_lines_mark((linenr_T)n, 1L);
4732
4733 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004734 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735 update_screen(VALID);
4736
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004737 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738 return FAIL;
4739
4740 if (len_change)
4741 *len_change = 1;
4742
4743 return OK;
4744 }
4745 else if (PyList_Check(lines))
4746 {
4747 PyInt i;
4748 PyInt size = PyList_Size(lines);
4749 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004750
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004751 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752 if (array == NULL)
4753 {
4754 PyErr_NoMemory();
4755 return FAIL;
4756 }
4757
4758 for (i = 0; i < size; ++i)
4759 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004760 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004761
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004762 if (!(line = PyList_GetItem(lines, i)) ||
4763 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004764 {
4765 while (i)
4766 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004767 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004768 return FAIL;
4769 }
4770 }
4771
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004772 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004773 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004774 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004775
4776 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004777 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004778 else
4779 {
4780 for (i = 0; i < size; ++i)
4781 {
4782 if (ml_append((linenr_T)(n + i),
4783 (char_u *)array[i], 0, FALSE) == FAIL)
4784 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004785 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004787 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004788 while (i < size)
4789 vim_free(array[i++]);
4790
4791 break;
4792 }
4793 vim_free(array[i]);
4794 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004795 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004796 // Only adjust marks if we managed to switch to a window that
4797 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004798 appended_lines_mark((linenr_T)n, (long)i);
4799 }
4800
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004801 // Free the array of lines. All of its contents have now
4802 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004803 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004804 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004805
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004806 update_screen(VALID);
4807
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004808 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004809 return FAIL;
4810
4811 if (len_change)
4812 *len_change = size;
4813
4814 return OK;
4815 }
4816 else
4817 {
4818 PyErr_BadArgument();
4819 return FAIL;
4820 }
4821}
4822
4823/*
4824 * Common routines for buffers and line ranges
4825 * -------------------------------------------
4826 */
4827
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004828typedef struct
4829{
4830 PyObject_HEAD
4831 buf_T *buf;
4832} BufferObject;
4833
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004834 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004835CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004836{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004837 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004838 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004839 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004840 return -1;
4841 }
4842
4843 return 0;
4844}
4845
4846 static PyObject *
4847RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4848{
4849 if (CheckBuffer(self))
4850 return NULL;
4851
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004852 if (end == -1)
4853 end = self->buf->b_ml.ml_line_count;
4854
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004855 if (n < 0)
4856 n += end - start + 1;
4857
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004858 if (n < 0 || n > end - start)
4859 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004860 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004861 return NULL;
4862 }
4863
4864 return GetBufferLine(self->buf, n+start);
4865}
4866
4867 static PyObject *
4868RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4869{
4870 PyInt size;
4871
4872 if (CheckBuffer(self))
4873 return NULL;
4874
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004875 if (end == -1)
4876 end = self->buf->b_ml.ml_line_count;
4877
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004878 size = end - start + 1;
4879
4880 if (lo < 0)
4881 lo = 0;
4882 else if (lo > size)
4883 lo = size;
4884 if (hi < 0)
4885 hi = 0;
4886 if (hi < lo)
4887 hi = lo;
4888 else if (hi > size)
4889 hi = size;
4890
4891 return GetBufferLineList(self->buf, lo+start, hi+start);
4892}
4893
4894 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004895RBAsItem(
4896 BufferObject *self,
4897 PyInt n,
4898 PyObject *valObject,
4899 PyInt start,
4900 PyInt end,
4901 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004902{
4903 PyInt len_change;
4904
4905 if (CheckBuffer(self))
4906 return -1;
4907
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004908 if (end == -1)
4909 end = self->buf->b_ml.ml_line_count;
4910
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004911 if (n < 0)
4912 n += end - start + 1;
4913
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004914 if (n < 0 || n > end - start)
4915 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004916 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004917 return -1;
4918 }
4919
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004920 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004921 return -1;
4922
4923 if (new_end)
4924 *new_end = end + len_change;
4925
4926 return 0;
4927}
4928
Bram Moolenaar19e60942011-06-19 00:27:51 +02004929 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004930RBAsSlice(
4931 BufferObject *self,
4932 PyInt lo,
4933 PyInt hi,
4934 PyObject *valObject,
4935 PyInt start,
4936 PyInt end,
4937 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004938{
4939 PyInt size;
4940 PyInt len_change;
4941
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004942 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004943 if (CheckBuffer(self))
4944 return -1;
4945
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004946 if (end == -1)
4947 end = self->buf->b_ml.ml_line_count;
4948
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004949 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004950 size = end - start + 1;
4951
4952 if (lo < 0)
4953 lo = 0;
4954 else if (lo > size)
4955 lo = size;
4956 if (hi < 0)
4957 hi = 0;
4958 if (hi < lo)
4959 hi = lo;
4960 else if (hi > size)
4961 hi = size;
4962
4963 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004964 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004965 return -1;
4966
4967 if (new_end)
4968 *new_end = end + len_change;
4969
4970 return 0;
4971}
4972
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004973
4974 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004975RBAppend(
4976 BufferObject *self,
4977 PyObject *args,
4978 PyInt start,
4979 PyInt end,
4980 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004981{
4982 PyObject *lines;
4983 PyInt len_change;
4984 PyInt max;
4985 PyInt n;
4986
4987 if (CheckBuffer(self))
4988 return NULL;
4989
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004990 if (end == -1)
4991 end = self->buf->b_ml.ml_line_count;
4992
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004993 max = n = end - start + 1;
4994
4995 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4996 return NULL;
4997
4998 if (n < 0 || n > max)
4999 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005000 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005001 return NULL;
5002 }
5003
5004 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5005 return NULL;
5006
5007 if (new_end)
5008 *new_end = end + len_change;
5009
5010 Py_INCREF(Py_None);
5011 return Py_None;
5012}
5013
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005014// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005015
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005016static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005017static PySequenceMethods RangeAsSeq;
5018static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005019
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005020typedef struct
5021{
5022 PyObject_HEAD
5023 BufferObject *buf;
5024 PyInt start;
5025 PyInt end;
5026} RangeObject;
5027
5028 static PyObject *
5029RangeNew(buf_T *buf, PyInt start, PyInt end)
5030{
5031 BufferObject *bufr;
5032 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005033 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005034 if (self == NULL)
5035 return NULL;
5036
5037 bufr = (BufferObject *)BufferNew(buf);
5038 if (bufr == NULL)
5039 {
5040 Py_DECREF(self);
5041 return NULL;
5042 }
5043 Py_INCREF(bufr);
5044
5045 self->buf = bufr;
5046 self->start = start;
5047 self->end = end;
5048
5049 return (PyObject *)(self);
5050}
5051
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005052 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005053RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005054{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005055 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005056 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005057 PyObject_GC_Del((void *)(self));
5058}
5059
5060 static int
5061RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5062{
5063 Py_VISIT(((PyObject *)(self->buf)));
5064 return 0;
5065}
5066
5067 static int
5068RangeClear(RangeObject *self)
5069{
5070 Py_CLEAR(self->buf);
5071 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005072}
5073
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005074 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005075RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005076{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005077 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005078 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005079 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005080
Bram Moolenaard6e39182013-05-21 18:30:34 +02005081 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005082}
5083
5084 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005085RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005087 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088}
5089
5090 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005091RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005092{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094}
5095
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005096static char *RangeAttrs[] = {
5097 "start", "end",
5098 NULL
5099};
5100
5101 static PyObject *
5102RangeDir(PyObject *self)
5103{
5104 return ObjectDir(self, RangeAttrs);
5105}
5106
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005108RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005109{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005110 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005111}
5112
5113 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005114RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005115{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005116 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005117 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5118 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119 else
5120 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122
5123 if (name == NULL)
5124 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005125
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005126 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005127 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005128 }
5129}
5130
5131static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005132 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005133 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005134 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5135 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136};
5137
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005138static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005139static PySequenceMethods BufferAsSeq;
5140static PyMappingMethods BufferAsMapping;
5141
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005142 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005143BufferNew(buf_T *buf)
5144{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005145 /*
5146 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005147 * If we add a "b_python*_ref" field to the buf_T structure,
5148 * then we can get at it in buf_freeall() in vim. We then
5149 * need to create only ONE Python object per buffer - if
5150 * we try to create a second, just INCREF the existing one
5151 * and return it. The (single) Python object referring to
5152 * the buffer is stored in "b_python*_ref".
5153 * Question: what to do on a buf_freeall(). We'll probably
5154 * have to either delete the Python object (DECREF it to
5155 * zero - a bad idea, as it leaves dangling refs!) or
5156 * set the buf_T * value to an invalid value (-1?), which
5157 * means we need checks in all access functions... Bah.
5158 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005159 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005160 * b_python_ref and b_python3_ref fields respectively.
5161 */
5162
5163 BufferObject *self;
5164
5165 if (BUF_PYTHON_REF(buf) != NULL)
5166 {
5167 self = BUF_PYTHON_REF(buf);
5168 Py_INCREF(self);
5169 }
5170 else
5171 {
5172 self = PyObject_NEW(BufferObject, &BufferType);
5173 if (self == NULL)
5174 return NULL;
5175 self->buf = buf;
5176 BUF_PYTHON_REF(buf) = self;
5177 }
5178
5179 return (PyObject *)(self);
5180}
5181
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005182 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005183BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005184{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005185 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5186 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005187
5188 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005189}
5190
Bram Moolenaar971db462013-05-12 18:44:48 +02005191 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005192BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005193{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005194 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005196 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005197
Bram Moolenaard6e39182013-05-21 18:30:34 +02005198 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005199}
5200
5201 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005202BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005203{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005204 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005205}
5206
5207 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005208BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005209{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005210 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005211}
5212
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005213static char *BufferAttrs[] = {
5214 "name", "number", "vars", "options", "valid",
5215 NULL
5216};
5217
5218 static PyObject *
5219BufferDir(PyObject *self)
5220{
5221 return ObjectDir(self, BufferAttrs);
5222}
5223
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005224 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005225BufferAttrValid(BufferObject *self, char *name)
5226{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005227 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005228
5229 if (strcmp(name, "valid") != 0)
5230 return NULL;
5231
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005232 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5233 Py_INCREF(ret);
5234 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005235}
5236
5237 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005238BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005239{
5240 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005241 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005242 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005243 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005244 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005245 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005246 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005247 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005248 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5249 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005250 else if (strcmp(name, "__members__") == 0)
5251 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005252 else
5253 return NULL;
5254}
5255
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005256 static int
5257BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5258{
5259 if (CheckBuffer(self))
5260 return -1;
5261
5262 if (strcmp(name, "name") == 0)
5263 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005264 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005265 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005266 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005267 PyObject *todecref;
5268
5269 if (!(val = StringToChars(valObject, &todecref)))
5270 return -1;
5271
5272 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005273 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005274 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005275 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005276 aucmd_restbuf(&aco);
5277 Py_XDECREF(todecref);
5278 if (VimTryEnd())
5279 return -1;
5280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005281 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005282 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005283 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005284 return -1;
5285 }
5286 return 0;
5287 }
5288 else
5289 {
5290 PyErr_SetString(PyExc_AttributeError, name);
5291 return -1;
5292 }
5293}
5294
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005295 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005296BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005297{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005298 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005299}
5300
5301 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005302BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005303{
5304 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005305 char_u *pmark;
5306 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005307 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005308 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005309
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311 return NULL;
5312
Bram Moolenaar389a1792013-06-23 13:00:44 +02005313 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005314 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315
Bram Moolenaar389a1792013-06-23 13:00:44 +02005316 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005317 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005318 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005319 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005320 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005321 return NULL;
5322 }
5323
5324 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005325
5326 Py_XDECREF(todecref);
5327
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005328 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005329 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005330 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005331 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005332 if (VimTryEnd())
5333 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005334
5335 if (posp == NULL)
5336 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005337 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005338 return NULL;
5339 }
5340
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005341 if (posp->lnum <= 0)
5342 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005343 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005344 Py_INCREF(Py_None);
5345 return Py_None;
5346 }
5347
5348 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5349}
5350
5351 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005352BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005353{
5354 PyInt start;
5355 PyInt end;
5356
Bram Moolenaard6e39182013-05-21 18:30:34 +02005357 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005358 return NULL;
5359
5360 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5361 return NULL;
5362
Bram Moolenaard6e39182013-05-21 18:30:34 +02005363 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005364}
5365
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005366 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005367BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005368{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005369 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005370 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005371 else
5372 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005373 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005374
5375 if (name == NULL)
5376 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005377
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005378 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005379 }
5380}
5381
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005382static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005383 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005384 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005385 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005386 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005387 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5388 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005389};
5390
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005391/*
5392 * Buffer list object - Implementation
5393 */
5394
5395static PyTypeObject BufMapType;
5396
5397typedef struct
5398{
5399 PyObject_HEAD
5400} BufMapObject;
5401
5402 static PyInt
5403BufMapLength(PyObject *self UNUSED)
5404{
5405 buf_T *b = firstbuf;
5406 PyInt n = 0;
5407
5408 while (b)
5409 {
5410 ++n;
5411 b = b->b_next;
5412 }
5413
5414 return n;
5415}
5416
5417 static PyObject *
5418BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5419{
5420 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005421 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005422
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005423 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005424 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005425
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005426 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005427
5428 if (b)
5429 return BufferNew(b);
5430 else
5431 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005432 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005433 return NULL;
5434 }
5435}
5436
5437 static void
5438BufMapIterDestruct(PyObject *buffer)
5439{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005440 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005441 if (buffer)
5442 {
5443 Py_DECREF(buffer);
5444 }
5445}
5446
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005447 static int
5448BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5449{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005450 if (buffer)
5451 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005452 return 0;
5453}
5454
5455 static int
5456BufMapIterClear(PyObject **buffer)
5457{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005458 if (*buffer)
5459 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005460 return 0;
5461}
5462
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005463 static PyObject *
5464BufMapIterNext(PyObject **buffer)
5465{
5466 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005467 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005468
5469 if (!*buffer)
5470 return NULL;
5471
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005472 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005473
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005475 {
5476 *buffer = NULL;
5477 return NULL;
5478 }
5479
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005480 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005481 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005482 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005483 return NULL;
5484 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005485 // Do not increment reference: we no longer hold it (decref), but whoever
5486 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005487 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005488}
5489
5490 static PyObject *
5491BufMapIter(PyObject *self UNUSED)
5492{
5493 PyObject *buffer;
5494
5495 buffer = BufferNew(firstbuf);
5496 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005497 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5498 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005499}
5500
5501static PyMappingMethods BufMapAsMapping = {
5502 (lenfunc) BufMapLength,
5503 (binaryfunc) BufMapItem,
5504 (objobjargproc) 0,
5505};
5506
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005507// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005508
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005509static char *CurrentAttrs[] = {
5510 "buffer", "window", "line", "range", "tabpage",
5511 NULL
5512};
5513
5514 static PyObject *
5515CurrentDir(PyObject *self)
5516{
5517 return ObjectDir(self, CurrentAttrs);
5518}
5519
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005520 static PyObject *
5521CurrentGetattr(PyObject *self UNUSED, char *name)
5522{
5523 if (strcmp(name, "buffer") == 0)
5524 return (PyObject *)BufferNew(curbuf);
5525 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005526 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005527 else if (strcmp(name, "tabpage") == 0)
5528 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005529 else if (strcmp(name, "line") == 0)
5530 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5531 else if (strcmp(name, "range") == 0)
5532 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005533 else if (strcmp(name, "__members__") == 0)
5534 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005535 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005536#if PY_MAJOR_VERSION < 3
5537 return Py_FindMethod(WindowMethods, self, name);
5538#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005539 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005540#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005541}
5542
5543 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005544CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005545{
5546 if (strcmp(name, "line") == 0)
5547 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005548 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5549 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005550 return -1;
5551
5552 return 0;
5553 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005554 else if (strcmp(name, "buffer") == 0)
5555 {
5556 int count;
5557
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005558 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005559 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005560 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005561 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005562 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005563 return -1;
5564 }
5565
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005566 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005567 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005568 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005569
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005570 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005571 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5572 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005573 if (VimTryEnd())
5574 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005575 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005576 return -1;
5577 }
5578
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005579 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005580 }
5581 else if (strcmp(name, "window") == 0)
5582 {
5583 int count;
5584
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005585 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005586 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005587 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005588 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005589 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005590 return -1;
5591 }
5592
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005593 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005594 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005595 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005596
5597 if (!count)
5598 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005599 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005600 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005601 return -1;
5602 }
5603
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005604 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 win_goto(((WindowObject *)(valObject))->win);
5606 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005607 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005608 if (VimTryEnd())
5609 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005610 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005611 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005612 return -1;
5613 }
5614
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005615 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005616 }
5617 else if (strcmp(name, "tabpage") == 0)
5618 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005619 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005621 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005622 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005623 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005624 return -1;
5625 }
5626
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005627 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005628 return -1;
5629
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005630 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005631 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5632 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005633 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005634 if (VimTryEnd())
5635 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005636 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005637 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005638 return -1;
5639 }
5640
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005641 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005642 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005643 else
5644 {
5645 PyErr_SetString(PyExc_AttributeError, name);
5646 return -1;
5647 }
5648}
5649
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005650static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005651 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005652 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5653 { NULL, NULL, 0, NULL}
5654};
5655
Bram Moolenaardb913952012-06-29 12:54:53 +02005656 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005657init_range_cmd(exarg_T *eap)
5658{
5659 RangeStart = eap->line1;
5660 RangeEnd = eap->line2;
5661}
5662
5663 static void
5664init_range_eval(typval_T *rettv UNUSED)
5665{
5666 RangeStart = (PyInt) curwin->w_cursor.lnum;
5667 RangeEnd = RangeStart;
5668}
5669
5670 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005671run_cmd(const char *cmd, void *arg UNUSED
5672#ifdef PY_CAN_RECURSE
5673 , PyGILState_STATE *pygilstate UNUSED
5674#endif
5675 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005676{
Bram Moolenaar41009372013-07-01 22:03:04 +02005677 PyObject *run_ret;
5678 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5679 if (run_ret != NULL)
5680 {
5681 Py_DECREF(run_ret);
5682 }
5683 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005685 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005686 PyErr_Clear();
5687 }
5688 else
5689 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005690}
5691
5692static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5693static int code_hdr_len = 30;
5694
5695 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005696run_do(const char *cmd, void *arg UNUSED
5697#ifdef PY_CAN_RECURSE
5698 , PyGILState_STATE *pygilstate
5699#endif
5700 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005701{
5702 PyInt lnum;
5703 size_t len;
5704 char *code;
5705 int status;
5706 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005707 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005708 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005709
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005710 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005712 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005713 return;
5714 }
5715
5716 len = code_hdr_len + STRLEN(cmd);
5717 code = PyMem_New(char, len + 1);
5718 memcpy(code, code_hdr, code_hdr_len);
5719 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005720 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5721 status = -1;
5722 if (run_ret != NULL)
5723 {
5724 status = 0;
5725 Py_DECREF(run_ret);
5726 }
5727 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5728 {
5729 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005730 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005731 PyErr_Clear();
5732 return;
5733 }
5734 else
5735 PyErr_PrintEx(1);
5736
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005737 PyMem_Free(code);
5738
5739 if (status)
5740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005741 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005742 return;
5743 }
5744
5745 status = 0;
5746 pymain = PyImport_AddModule("__main__");
5747 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005748#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005749 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005750#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005751
5752 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5753 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005754 PyObject *line;
5755 PyObject *linenr;
5756 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005757
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005758#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005759 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005760#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005761 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005762 if (lnum > curbuf->b_ml.ml_line_count
5763 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764 goto err;
5765 if (!(linenr = PyInt_FromLong((long) lnum)))
5766 {
5767 Py_DECREF(line);
5768 goto err;
5769 }
5770 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5771 Py_DECREF(line);
5772 Py_DECREF(linenr);
5773 if (!ret)
5774 goto err;
5775
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005776 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005777 if (curbuf != was_curbuf)
5778 {
5779 Py_XDECREF(ret);
5780 goto err;
5781 }
5782
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005783 if (ret != Py_None)
5784 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005785 {
5786 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005787 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005788 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005789
5790 Py_XDECREF(ret);
5791 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005792#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005793 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005794#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005795 }
5796 goto out;
5797err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005798#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005799 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005800#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005801 PyErr_PrintEx(0);
5802 PythonIO_Flush();
5803 status = 1;
5804out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005805#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806 if (!status)
5807 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005808#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005809 Py_DECREF(pyfunc);
5810 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5811 if (status)
5812 return;
5813 check_cursor();
5814 update_curbuf(NOT_VALID);
5815}
5816
5817 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005818run_eval(const char *cmd, typval_T *rettv
5819#ifdef PY_CAN_RECURSE
5820 , PyGILState_STATE *pygilstate UNUSED
5821#endif
5822 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005823{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005824 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005825
Bram Moolenaar41009372013-07-01 22:03:04 +02005826 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005827 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005828 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005829 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005831 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005832 PyErr_Clear();
5833 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005834 else
5835 {
5836 if (PyErr_Occurred() && !msg_silent)
5837 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005838 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005839 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005840 }
5841 else
5842 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005843 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar86181df2020-05-11 23:14:04 +02005844 emsg(_("E859: Failed to convert returned python object to a Vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005845 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005846 }
5847 PyErr_Clear();
5848}
5849
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005850 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005851set_ref_in_py(const int copyID)
5852{
5853 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005854 list_T *ll;
5855 int i;
5856 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005857 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005858
5859 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005860 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005861 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005862 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5863 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005864 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005865
5866 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005867 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005868 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005869 {
5870 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005871 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005872 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005873 }
5874
Bram Moolenaar8110a092016-04-14 15:56:09 +02005875 if (lastfunc != NULL)
5876 {
5877 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5878 {
5879 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005880 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005881 if (func->argc)
5882 for (i = 0; !abort && i < func->argc; ++i)
5883 abort = abort
5884 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5885 }
5886 }
5887
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005888 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005889}
5890
5891 static int
5892set_string_copy(char_u *str, typval_T *tv)
5893{
5894 tv->vval.v_string = vim_strsave(str);
5895 if (tv->vval.v_string == NULL)
5896 {
5897 PyErr_NoMemory();
5898 return -1;
5899 }
5900 return 0;
5901}
5902
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005903 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005904pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005905{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005906 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005907 char_u *key;
5908 dictitem_T *di;
5909 PyObject *keyObject;
5910 PyObject *valObject;
5911 Py_ssize_t iter = 0;
5912
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005913 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005914 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005915
5916 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005917 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918
5919 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5920 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005921 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922
Bram Moolenaara03e6312013-05-29 22:49:26 +02005923 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005924 {
5925 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005927 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005928
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005929 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005930 {
5931 dict_unref(dict);
5932 return -1;
5933 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005934
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005935 if (*key == NUL)
5936 {
5937 dict_unref(dict);
5938 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005939 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005940 return -1;
5941 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005942
5943 di = dictitem_alloc(key);
5944
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005945 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946
5947 if (di == NULL)
5948 {
5949 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005950 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951 return -1;
5952 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005954 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005955 {
5956 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005957 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005958 return -1;
5959 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005960
5961 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005962 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005963 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005964 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005966 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005967 return -1;
5968 }
5969 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005970
5971 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005972 return 0;
5973}
5974
5975 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005976pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005978 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005979 char_u *key;
5980 dictitem_T *di;
5981 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005982 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983 PyObject *keyObject;
5984 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005986 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005987 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005988
5989 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005990 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005991
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005992 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005993 {
5994 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005995 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005996 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005997
5998 if (!(iterator = PyObject_GetIter(list)))
5999 {
6000 dict_unref(dict);
6001 Py_DECREF(list);
6002 return -1;
6003 }
6004 Py_DECREF(list);
6005
6006 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006007 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006008 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006009
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006010 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006011 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006012 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006013 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006014 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006015 return -1;
6016 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006017
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006018 if (*key == NUL)
6019 {
6020 Py_DECREF(keyObject);
6021 Py_DECREF(iterator);
6022 Py_XDECREF(todecref);
6023 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006024 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006025 return -1;
6026 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006027
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006028 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006029 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006030 Py_DECREF(keyObject);
6031 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006032 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006033 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006034 return -1;
6035 }
6036
6037 di = dictitem_alloc(key);
6038
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006039 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006040 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006041
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042 if (di == NULL)
6043 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006044 Py_DECREF(iterator);
6045 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006046 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006047 PyErr_NoMemory();
6048 return -1;
6049 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006050
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006051 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006052 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006053 Py_DECREF(iterator);
6054 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006055 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006056 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006057 return -1;
6058 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006059
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006060 Py_DECREF(valObject);
6061
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006062 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006063 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006064 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006065 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 dictitem_free(di);
6067 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 return -1;
6069 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006070 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006071 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006072 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006073 return 0;
6074}
6075
6076 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006077pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006078{
6079 list_T *l;
6080
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006081 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006082 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006083
6084 tv->v_type = VAR_LIST;
6085 tv->vval.v_list = l;
6086
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006087 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006088 {
6089 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006090 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006091 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006092
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006093 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006094 return 0;
6095}
6096
Bram Moolenaardb913952012-06-29 12:54:53 +02006097typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6098
6099 static int
6100convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006101 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006102{
6103 PyObject *capsule;
6104 char hexBuf[sizeof(void *) * 2 + 3];
6105
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006106 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006107
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006108#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006109 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006110#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006111 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006112#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006113 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006114 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006115#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006116 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006117#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006118 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006119#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006120 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6121 {
6122 Py_DECREF(capsule);
6123 tv->v_type = VAR_UNKNOWN;
6124 return -1;
6125 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006126
6127 Py_DECREF(capsule);
6128
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006129 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006130 {
6131 tv->v_type = VAR_UNKNOWN;
6132 return -1;
6133 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006134 // As we are not using copy_tv which increments reference count we must
6135 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006136 if (tv->v_type == VAR_DICT)
6137 ++tv->vval.v_dict->dv_refcount;
6138 else if (tv->v_type == VAR_LIST)
6139 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006140 }
6141 else
6142 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006143 typval_T *v;
6144
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006145#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006146 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006147#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006148 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006149#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006150 copy_tv(v, tv);
6151 }
6152 return 0;
6153}
6154
6155 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006156ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6157{
6158 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006159 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006160
6161 if (!(lookup_dict = PyDict_New()))
6162 return -1;
6163
6164 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6165 {
6166 tv->v_type = VAR_DICT;
6167 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6168 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006169 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006170 }
6171 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006172 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006173 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006174 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006175 else
6176 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006177 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006178 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006179 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006180 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006181 }
6182 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006183 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006184}
6185
6186 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006187ConvertFromPySequence(PyObject *obj, typval_T *tv)
6188{
6189 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006190 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006191
6192 if (!(lookup_dict = PyDict_New()))
6193 return -1;
6194
6195 if (PyType_IsSubtype(obj->ob_type, &ListType))
6196 {
6197 tv->v_type = VAR_LIST;
6198 tv->vval.v_list = (((ListObject *)(obj))->list);
6199 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006200 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006201 }
6202 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006203 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006204 else
6205 {
6206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006207 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006208 Py_TYPE_NAME(obj));
6209 ret = -1;
6210 }
6211 Py_DECREF(lookup_dict);
6212 return ret;
6213}
6214
6215 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006216ConvertFromPyObject(PyObject *obj, typval_T *tv)
6217{
6218 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006219 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006220
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006221 if (!(lookup_dict = PyDict_New()))
6222 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006223 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006224 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006225 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006226}
6227
6228 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006229_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006230{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006231 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006232 {
6233 tv->v_type = VAR_DICT;
6234 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6235 ++tv->vval.v_dict->dv_refcount;
6236 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006237 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 {
6239 tv->v_type = VAR_LIST;
6240 tv->vval.v_list = (((ListObject *)(obj))->list);
6241 ++tv->vval.v_list->lv_refcount;
6242 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006243 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006244 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006245 FunctionObject *func = (FunctionObject *) obj;
6246 if (func->self != NULL || func->argv != NULL)
6247 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006248 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6249
Bram Moolenaar8110a092016-04-14 15:56:09 +02006250 set_partial(func, pt, TRUE);
6251 tv->vval.v_partial = pt;
6252 tv->v_type = VAR_PARTIAL;
6253 }
6254 else
6255 {
6256 if (set_string_copy(func->name, tv) == -1)
6257 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006258
Bram Moolenaar8110a092016-04-14 15:56:09 +02006259 tv->v_type = VAR_FUNC;
6260 }
6261 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006262 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006263 else if (PyBytes_Check(obj))
6264 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006265 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006266
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006267 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006268 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006269 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006270 return -1;
6271
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006272 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006273 return -1;
6274
6275 tv->v_type = VAR_STRING;
6276 }
6277 else if (PyUnicode_Check(obj))
6278 {
6279 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006280 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006281
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006282 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 if (bytes == NULL)
6284 return -1;
6285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006286 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006287 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006288 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 return -1;
6290
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006291 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 {
6293 Py_XDECREF(bytes);
6294 return -1;
6295 }
6296 Py_XDECREF(bytes);
6297
6298 tv->v_type = VAR_STRING;
6299 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006300#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006301 else if (PyInt_Check(obj))
6302 {
6303 tv->v_type = VAR_NUMBER;
6304 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006305 if (PyErr_Occurred())
6306 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006307 }
6308#endif
6309 else if (PyLong_Check(obj))
6310 {
6311 tv->v_type = VAR_NUMBER;
6312 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006313 if (PyErr_Occurred())
6314 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006315 }
6316 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006317 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006318#ifdef FEAT_FLOAT
6319 else if (PyFloat_Check(obj))
6320 {
6321 tv->v_type = VAR_FLOAT;
6322 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6323 }
6324#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006325 else if (PyObject_HasAttrString(obj, "keys"))
6326 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006327 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006328 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006329 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006330 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006331 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006332 else if (PyNumber_Check(obj))
6333 {
6334 PyObject *num;
6335
6336 if (!(num = PyNumber_Long(obj)))
6337 return -1;
6338
6339 tv->v_type = VAR_NUMBER;
6340 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6341
6342 Py_DECREF(num);
6343 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006344 else if (obj == Py_None)
6345 {
6346 tv->v_type = VAR_SPECIAL;
6347 tv->vval.v_number = VVAL_NONE;
6348 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006349 else
6350 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006351 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006352 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006353 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006354 return -1;
6355 }
6356 return 0;
6357}
6358
6359 static PyObject *
6360ConvertToPyObject(typval_T *tv)
6361{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006362 typval_T *argv;
6363 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006364 if (tv == NULL)
6365 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006366 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006367 return NULL;
6368 }
6369 switch (tv->v_type)
6370 {
6371 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006372 return PyBytes_FromString(tv->vval.v_string == NULL
6373 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006374 case VAR_NUMBER:
6375 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006376 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006377#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006378 return PyFloat_FromDouble((double) tv->vval.v_float);
6379#endif
6380 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006381 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006382 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006383 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006384 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006385 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006386 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006387 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006388 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006389 if (tv->vval.v_partial->pt_argc)
6390 {
6391 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6392 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6393 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6394 }
6395 else
6396 argv = NULL;
6397 if (tv->vval.v_partial->pt_dict != NULL)
6398 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006399 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006400 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006401 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006402 tv->vval.v_partial->pt_dict,
6403 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006404 case VAR_BLOB:
6405 return PyBytes_FromStringAndSize(
6406 (char*) tv->vval.v_blob->bv_ga.ga_data,
6407 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006408 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006409 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006411 case VAR_CHANNEL:
6412 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006413 Py_INCREF(Py_None);
6414 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006415 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006416 case VAR_SPECIAL:
6417 switch (tv->vval.v_number)
6418 {
6419 case VVAL_FALSE: return AlwaysFalse(NULL);
6420 case VVAL_TRUE: return AlwaysTrue(NULL);
6421 case VVAL_NONE:
6422 case VVAL_NULL: return AlwaysNone(NULL);
6423 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006424 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006425 return NULL;
6426 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006427 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006428}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006429
6430typedef struct
6431{
6432 PyObject_HEAD
6433} CurrentObject;
6434static PyTypeObject CurrentType;
6435
6436 static void
6437init_structs(void)
6438{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006439 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006440 OutputType.tp_name = "vim.message";
6441 OutputType.tp_basicsize = sizeof(OutputObject);
6442 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6443 OutputType.tp_doc = "vim message object";
6444 OutputType.tp_methods = OutputMethods;
6445#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006446 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6447 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448 OutputType.tp_alloc = call_PyType_GenericAlloc;
6449 OutputType.tp_new = call_PyType_GenericNew;
6450 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006451 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006452#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006453 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6454 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006455 // Disabled, because this causes a crash in test86
6456 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006457#endif
6458
Bram Moolenaara80faa82020-04-12 19:37:17 +02006459 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006460 IterType.tp_name = "vim.iter";
6461 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006462 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006463 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006464 IterType.tp_iter = (getiterfunc)IterIter;
6465 IterType.tp_iternext = (iternextfunc)IterNext;
6466 IterType.tp_dealloc = (destructor)IterDestructor;
6467 IterType.tp_traverse = (traverseproc)IterTraverse;
6468 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006469
Bram Moolenaara80faa82020-04-12 19:37:17 +02006470 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006471 BufferType.tp_name = "vim.buffer";
6472 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006473 BufferType.tp_dealloc = (destructor)BufferDestructor;
6474 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006475 BufferType.tp_as_sequence = &BufferAsSeq;
6476 BufferType.tp_as_mapping = &BufferAsMapping;
6477 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6478 BufferType.tp_doc = "vim buffer object";
6479 BufferType.tp_methods = BufferMethods;
6480#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006481 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006482 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483 BufferType.tp_alloc = call_PyType_GenericAlloc;
6484 BufferType.tp_new = call_PyType_GenericNew;
6485 BufferType.tp_free = call_PyObject_Free;
6486#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006487 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006488 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006489#endif
6490
Bram Moolenaara80faa82020-04-12 19:37:17 +02006491 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006492 WindowType.tp_name = "vim.window";
6493 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006494 WindowType.tp_dealloc = (destructor)WindowDestructor;
6495 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006496 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006497 WindowType.tp_doc = "vim Window object";
6498 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6500 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006501#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6503 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504 WindowType.tp_alloc = call_PyType_GenericAlloc;
6505 WindowType.tp_new = call_PyType_GenericNew;
6506 WindowType.tp_free = call_PyObject_Free;
6507#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006508 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6509 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006510#endif
6511
Bram Moolenaara80faa82020-04-12 19:37:17 +02006512 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006513 TabPageType.tp_name = "vim.tabpage";
6514 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6516 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006517 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6518 TabPageType.tp_doc = "vim tab page object";
6519 TabPageType.tp_methods = TabPageMethods;
6520#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006522 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6523 TabPageType.tp_new = call_PyType_GenericNew;
6524 TabPageType.tp_free = call_PyObject_Free;
6525#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006526 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006527#endif
6528
Bram Moolenaara80faa82020-04-12 19:37:17 +02006529 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006530 BufMapType.tp_name = "vim.bufferlist";
6531 BufMapType.tp_basicsize = sizeof(BufMapObject);
6532 BufMapType.tp_as_mapping = &BufMapAsMapping;
6533 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006534 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006535 BufferType.tp_doc = "vim buffer list";
6536
Bram Moolenaara80faa82020-04-12 19:37:17 +02006537 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006538 WinListType.tp_name = "vim.windowlist";
6539 WinListType.tp_basicsize = sizeof(WinListType);
6540 WinListType.tp_as_sequence = &WinListAsSeq;
6541 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6542 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006543 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006544
Bram Moolenaara80faa82020-04-12 19:37:17 +02006545 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006546 TabListType.tp_name = "vim.tabpagelist";
6547 TabListType.tp_basicsize = sizeof(TabListType);
6548 TabListType.tp_as_sequence = &TabListAsSeq;
6549 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6550 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551
Bram Moolenaara80faa82020-04-12 19:37:17 +02006552 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006553 RangeType.tp_name = "vim.range";
6554 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006555 RangeType.tp_dealloc = (destructor)RangeDestructor;
6556 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006557 RangeType.tp_as_sequence = &RangeAsSeq;
6558 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006559 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560 RangeType.tp_doc = "vim Range object";
6561 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006562 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6563 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006564#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006565 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006566 RangeType.tp_alloc = call_PyType_GenericAlloc;
6567 RangeType.tp_new = call_PyType_GenericNew;
6568 RangeType.tp_free = call_PyObject_Free;
6569#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006570 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006571#endif
6572
Bram Moolenaara80faa82020-04-12 19:37:17 +02006573 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006574 CurrentType.tp_name = "vim.currentdata";
6575 CurrentType.tp_basicsize = sizeof(CurrentObject);
6576 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6577 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006578 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006580 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6581 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006583 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6584 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006585#endif
6586
Bram Moolenaara80faa82020-04-12 19:37:17 +02006587 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588 DictionaryType.tp_name = "vim.dictionary";
6589 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006590 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006591 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006593 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006594 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006595 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006596 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6597 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6598 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006599#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006600 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6601 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006602#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6604 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006605#endif
6606
Bram Moolenaara80faa82020-04-12 19:37:17 +02006607 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006609 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006610 ListType.tp_basicsize = sizeof(ListObject);
6611 ListType.tp_as_sequence = &ListAsSeq;
6612 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006613 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006614 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006615 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006616 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006617 ListType.tp_new = (newfunc)ListConstructor;
6618 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006620 ListType.tp_getattro = (getattrofunc)ListGetattro;
6621 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006622#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006623 ListType.tp_getattr = (getattrfunc)ListGetattr;
6624 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006625#endif
6626
Bram Moolenaara80faa82020-04-12 19:37:17 +02006627 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006628 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006629 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006630 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6631 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006632 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006633 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006634 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006635 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006636 FunctionType.tp_new = (newfunc)FunctionConstructor;
6637 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006638#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006639 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006640#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006641 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006642#endif
6643
Bram Moolenaara80faa82020-04-12 19:37:17 +02006644 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006645 OptionsType.tp_name = "vim.options";
6646 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006647 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006648 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006649 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006650 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006651 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006652 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6653 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6654 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006655
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006656#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006657 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006658 LoaderType.tp_name = "vim.Loader";
6659 LoaderType.tp_basicsize = sizeof(LoaderObject);
6660 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6661 LoaderType.tp_doc = "vim message object";
6662 LoaderType.tp_methods = LoaderMethods;
6663 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006664#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006665
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006666#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006667 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006668 vimmodule.m_name = "vim";
6669 vimmodule.m_doc = "Vim Python interface\n";
6670 vimmodule.m_size = -1;
6671 vimmodule.m_methods = VimMethods;
6672#endif
6673}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006674
6675#define PYTYPE_READY(type) \
6676 if (PyType_Ready(&type)) \
6677 return -1;
6678
6679 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006680init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006681{
6682 PYTYPE_READY(IterType);
6683 PYTYPE_READY(BufferType);
6684 PYTYPE_READY(RangeType);
6685 PYTYPE_READY(WindowType);
6686 PYTYPE_READY(TabPageType);
6687 PYTYPE_READY(BufMapType);
6688 PYTYPE_READY(WinListType);
6689 PYTYPE_READY(TabListType);
6690 PYTYPE_READY(CurrentType);
6691 PYTYPE_READY(DictionaryType);
6692 PYTYPE_READY(ListType);
6693 PYTYPE_READY(FunctionType);
6694 PYTYPE_READY(OptionsType);
6695 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006696#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006697 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006698#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006699 return 0;
6700}
6701
6702 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006703init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006704{
6705 PyObject *path;
6706 PyObject *path_hook;
6707 PyObject *path_hooks;
6708
6709 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6710 return -1;
6711
6712 if (!(path_hooks = PySys_GetObject("path_hooks")))
6713 {
6714 PyErr_Clear();
6715 path_hooks = PyList_New(1);
6716 PyList_SET_ITEM(path_hooks, 0, path_hook);
6717 if (PySys_SetObject("path_hooks", path_hooks))
6718 {
6719 Py_DECREF(path_hooks);
6720 return -1;
6721 }
6722 Py_DECREF(path_hooks);
6723 }
6724 else if (PyList_Check(path_hooks))
6725 {
6726 if (PyList_Append(path_hooks, path_hook))
6727 {
6728 Py_DECREF(path_hook);
6729 return -1;
6730 }
6731 Py_DECREF(path_hook);
6732 }
6733 else
6734 {
6735 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006736 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006737 "You should now do the following:\n"
6738 "- append vim.path_hook to sys.path_hooks\n"
6739 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006740 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006741 Py_DECREF(path_hook);
6742 return 0;
6743 }
6744
6745 if (!(path = PySys_GetObject("path")))
6746 {
6747 PyErr_Clear();
6748 path = PyList_New(1);
6749 Py_INCREF(vim_special_path_object);
6750 PyList_SET_ITEM(path, 0, vim_special_path_object);
6751 if (PySys_SetObject("path", path))
6752 {
6753 Py_DECREF(path);
6754 return -1;
6755 }
6756 Py_DECREF(path);
6757 }
6758 else if (PyList_Check(path))
6759 {
6760 if (PyList_Append(path, vim_special_path_object))
6761 return -1;
6762 }
6763 else
6764 {
6765 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006766 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006767 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006768 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006769 }
6770
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006771 return 0;
6772}
6773
6774static BufMapObject TheBufferMap =
6775{
6776 PyObject_HEAD_INIT(&BufMapType)
6777};
6778
6779static WinListObject TheWindowList =
6780{
6781 PyObject_HEAD_INIT(&WinListType)
6782 NULL
6783};
6784
6785static CurrentObject TheCurrent =
6786{
6787 PyObject_HEAD_INIT(&CurrentType)
6788};
6789
6790static TabListObject TheTabPageList =
6791{
6792 PyObject_HEAD_INIT(&TabListType)
6793};
6794
6795static struct numeric_constant {
6796 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006797 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006798} numeric_constants[] = {
6799 {"VAR_LOCKED", VAR_LOCKED},
6800 {"VAR_FIXED", VAR_FIXED},
6801 {"VAR_SCOPE", VAR_SCOPE},
6802 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6803};
6804
6805static struct object_constant {
6806 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006807 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006808} object_constants[] = {
6809 {"buffers", (PyObject *)(void *)&TheBufferMap},
6810 {"windows", (PyObject *)(void *)&TheWindowList},
6811 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6812 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006813
6814 {"Buffer", (PyObject *)&BufferType},
6815 {"Range", (PyObject *)&RangeType},
6816 {"Window", (PyObject *)&WindowType},
6817 {"TabPage", (PyObject *)&TabPageType},
6818 {"Dictionary", (PyObject *)&DictionaryType},
6819 {"List", (PyObject *)&ListType},
6820 {"Function", (PyObject *)&FunctionType},
6821 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006822#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006823 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006824#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006825};
6826
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006827#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006828 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006829 return -1;
6830
6831#define ADD_CHECKED_OBJECT(m, name, obj) \
6832 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006833 PyObject *valObject = obj; \
6834 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006835 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006836 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006837 }
6838
6839 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006840populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006841{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006842 int i;
6843 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006844 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006845 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006846#if PY_VERSION_HEX >= 0x030700f0
6847 PyObject *dict;
6848 PyObject *cls;
6849#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850
6851 for (i = 0; i < (int)(sizeof(numeric_constants)
6852 / sizeof(struct numeric_constant));
6853 ++i)
6854 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006855 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006856
6857 for (i = 0; i < (int)(sizeof(object_constants)
6858 / sizeof(struct object_constant));
6859 ++i)
6860 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006861 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006862
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006863 valObject = object_constants[i].valObject;
6864 Py_INCREF(valObject);
6865 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006866 }
6867
6868 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6869 return -1;
6870 ADD_OBJECT(m, "error", VimError);
6871
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006872 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6873 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006874 ADD_CHECKED_OBJECT(m, "options",
6875 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006876
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006877 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006878 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006879 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006880
Bram Moolenaar22081f42016-06-01 20:38:34 +02006881#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006882 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006883 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006884#else
6885 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6886 return -1;
6887#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888 ADD_OBJECT(m, "_getcwd", py_getcwd)
6889
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006890 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891 return -1;
6892 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006893 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006895 if (PyObject_SetAttrString(other_module, "chdir", attr))
6896 {
6897 Py_DECREF(attr);
6898 return -1;
6899 }
6900 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006901
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006902 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006903 {
6904 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006905 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006906 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006907 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6908 {
6909 Py_DECREF(attr);
6910 return -1;
6911 }
6912 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006913 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006914 else
6915 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006916
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006917 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6918 return -1;
6919
6920 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6921
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006922#if PY_VERSION_HEX >= 0x030700f0
6923 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6924 return -1;
6925
6926 dict = PyModule_GetDict(imp);
6927
6928 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6929 {
6930 Py_DECREF(imp);
6931 return -1;
6932 }
6933
6934 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6935 {
6936 Py_DECREF(imp);
6937 return -1;
6938 }
6939
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006940 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6941 {
6942 // find_module() is deprecated, this may stop working in some later
6943 // version.
6944 ADD_OBJECT(m, "_find_module", py_find_module);
6945 }
6946
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006947 Py_DECREF(imp);
6948
6949 ADD_OBJECT(m, "_find_spec", py_find_spec);
6950#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006951 if (!(imp = PyImport_ImportModule("imp")))
6952 return -1;
6953
6954 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6955 {
6956 Py_DECREF(imp);
6957 return -1;
6958 }
6959
6960 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6961 {
6962 Py_DECREF(py_find_module);
6963 Py_DECREF(imp);
6964 return -1;
6965 }
6966
6967 Py_DECREF(imp);
6968
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006969 ADD_OBJECT(m, "_find_module", py_find_module);
6970 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006971#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006972
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006973 return 0;
6974}