blob: 36dedaf2d547c8cedc482c2d7c6adc387ad7d8ed [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 Moolenaarefc0d942020-10-11 18:05:02 +0200310typedef int (*writefn)(char *);
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 {
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200362 ((char *)io_ga.ga_data)[io_ga.ga_len] = NUL;
363 old_fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200364 }
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;
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200393 fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200394 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 Moolenaar423a85a2020-08-29 12:57:16 +02001445 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001446} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001447
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001448 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001449IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001450 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001451{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001452 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001453
Bram Moolenaar774267b2013-05-21 20:51:59 +02001454 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001455 self->cur = start;
1456 self->next = next;
1457 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001458 self->traverse = traverse;
1459 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001460 self->iter_object = iter_object;
1461
1462 if (iter_object)
1463 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001464
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001465 return (PyObject *)(self);
1466}
1467
1468 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001469IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001470{
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001471 if (self->iter_object)
1472 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001473 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001474 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001475 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001476}
1477
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001478 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001479IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001480{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001481 if (self->traverse != NULL)
1482 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001483 else
1484 return 0;
1485}
1486
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001487// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001488#ifdef clear
1489# undef clear
1490#endif
1491
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001492 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001493IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001494{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001495 if (self->clear != NULL)
1496 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001497 else
1498 return 0;
1499}
1500
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001501 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001503{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001504 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001505}
1506
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001507 static PyObject *
1508IterIter(PyObject *self)
1509{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001510 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001511 return self;
1512}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001513
Bram Moolenaardb913952012-06-29 12:54:53 +02001514typedef struct pylinkedlist_S {
1515 struct pylinkedlist_S *pll_next;
1516 struct pylinkedlist_S *pll_prev;
1517 PyObject *pll_obj;
1518} pylinkedlist_T;
1519
1520static pylinkedlist_T *lastdict = NULL;
1521static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001522static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001523
1524 static void
1525pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1526{
1527 if (ref->pll_prev == NULL)
1528 {
1529 if (ref->pll_next == NULL)
1530 {
1531 *last = NULL;
1532 return;
1533 }
1534 }
1535 else
1536 ref->pll_prev->pll_next = ref->pll_next;
1537
1538 if (ref->pll_next == NULL)
1539 *last = ref->pll_prev;
1540 else
1541 ref->pll_next->pll_prev = ref->pll_prev;
1542}
1543
1544 static void
1545pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1546{
1547 if (*last == NULL)
1548 ref->pll_prev = NULL;
1549 else
1550 {
1551 (*last)->pll_next = ref;
1552 ref->pll_prev = *last;
1553 }
1554 ref->pll_next = NULL;
1555 ref->pll_obj = self;
1556 *last = ref;
1557}
1558
1559static PyTypeObject DictionaryType;
1560
1561typedef struct
1562{
1563 PyObject_HEAD
1564 dict_T *dict;
1565 pylinkedlist_T ref;
1566} DictionaryObject;
1567
Bram Moolenaara9922d62013-05-30 13:01:18 +02001568static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1569
1570#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1571
Bram Moolenaardb913952012-06-29 12:54:53 +02001572 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001573DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001574{
1575 DictionaryObject *self;
1576
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001578 if (self == NULL)
1579 return NULL;
1580 self->dict = dict;
1581 ++dict->dv_refcount;
1582
1583 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1584
1585 return (PyObject *)(self);
1586}
1587
Bram Moolenaara9922d62013-05-30 13:01:18 +02001588 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001589py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001591 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594 {
1595 PyErr_NoMemory();
1596 return NULL;
1597 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001598 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001599
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001600 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601}
1602
1603 static PyObject *
1604DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1605{
1606 DictionaryObject *self;
1607 dict_T *dict;
1608
1609 if (!(dict = py_dict_alloc()))
1610 return NULL;
1611
1612 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1613
1614 --dict->dv_refcount;
1615
1616 if (kwargs || PyTuple_Size(args))
1617 {
1618 PyObject *tmp;
1619 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1620 {
1621 Py_DECREF(self);
1622 return NULL;
1623 }
1624
1625 Py_DECREF(tmp);
1626 }
1627
1628 return (PyObject *)(self);
1629}
1630
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001631 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001632DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001633{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001634 pyll_remove(&self->ref, &lastdict);
1635 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001636
1637 DESTRUCTOR_FINISH(self);
1638}
1639
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001640static char *DictionaryAttrs[] = {
1641 "locked", "scope",
1642 NULL
1643};
1644
1645 static PyObject *
1646DictionaryDir(PyObject *self)
1647{
1648 return ObjectDir(self, DictionaryAttrs);
1649}
1650
Bram Moolenaardb913952012-06-29 12:54:53 +02001651 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001652DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001653{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001654 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001655 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001656 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001657 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001658 return -1;
1659 }
1660
1661 if (strcmp(name, "locked") == 0)
1662 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001663 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001664 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001665 PyErr_SET_STRING(PyExc_TypeError,
1666 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001667 return -1;
1668 }
1669 else
1670 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001671 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001672 if (istrue == -1)
1673 return -1;
1674 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001675 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001676 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001677 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001678 }
1679 return 0;
1680 }
1681 else
1682 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001683 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001684 return -1;
1685 }
1686}
1687
1688 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001689DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001690{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001691 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001692}
1693
Bram Moolenaara9922d62013-05-30 13:01:18 +02001694#define DICT_FLAG_HAS_DEFAULT 0x01
1695#define DICT_FLAG_POP 0x02
1696#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001697#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001698#define DICT_FLAG_RETURN_PAIR 0x10
1699
Bram Moolenaardb913952012-06-29 12:54:53 +02001700 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001702{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001703 PyObject *keyObject;
1704 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001705 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001706 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001707 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001708 dict_T *dict = self->dict;
1709 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001710 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001711
Bram Moolenaara9922d62013-05-30 13:01:18 +02001712 if (flags & DICT_FLAG_HAS_DEFAULT)
1713 {
1714 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1715 return NULL;
1716 }
1717 else
1718 keyObject = args;
1719
1720 if (flags & DICT_FLAG_RETURN_BOOL)
1721 defObject = Py_False;
1722
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001723 if (!(key = StringToChars(keyObject, &todecref)))
1724 return NULL;
1725
1726 if (*key == NUL)
1727 {
1728 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001729 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001730 return NULL;
1731 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001732
Bram Moolenaara9922d62013-05-30 13:01:18 +02001733 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001734
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001735 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001736
Bram Moolenaara9922d62013-05-30 13:01:18 +02001737 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001738 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001739 if (defObject)
1740 {
1741 Py_INCREF(defObject);
1742 return defObject;
1743 }
1744 else
1745 {
1746 PyErr_SetObject(PyExc_KeyError, keyObject);
1747 return NULL;
1748 }
1749 }
1750 else if (flags & DICT_FLAG_RETURN_BOOL)
1751 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001752 ret = Py_True;
1753 Py_INCREF(ret);
1754 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001755 }
1756
1757 di = dict_lookup(hi);
1758
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001759 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001760 return NULL;
1761
1762 if (flags & DICT_FLAG_POP)
1763 {
1764 if (dict->dv_lock)
1765 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001766 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001767 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001768 return NULL;
1769 }
1770
1771 hash_remove(&dict->dv_hashtab, hi);
1772 dictitem_free(di);
1773 }
1774
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001775 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001776}
1777
1778 static PyObject *
1779DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1780{
1781 return _DictionaryItem(self, keyObject, 0);
1782}
1783
1784 static int
1785DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1786{
1787 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001788 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001790 if (rObj == NULL)
1791 return -1;
1792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001793 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001794
Bram Moolenaardee2e312013-06-23 16:35:47 +02001795 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001797 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798}
1799
1800typedef struct
1801{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001802 int dii_changed;
1803 hashtab_T *dii_ht;
1804 hashitem_T *dii_hi;
1805 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001806} dictiterinfo_T;
1807
1808 static PyObject *
1809DictionaryIterNext(dictiterinfo_T **dii)
1810{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001811 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001812
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001813 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001814 return NULL;
1815
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001816 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001817 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001818 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001819 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001820 return NULL;
1821 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001822
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001823 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
1824 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001825
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001826 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001827
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001828 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 return NULL;
1830
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001831 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001832}
1833
1834 static PyObject *
1835DictionaryIter(DictionaryObject *self)
1836{
1837 dictiterinfo_T *dii;
1838 hashtab_T *ht;
1839
1840 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1841 {
1842 PyErr_NoMemory();
1843 return NULL;
1844 }
1845
1846 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001847 dii->dii_changed = ht->ht_changed;
1848 dii->dii_ht = ht;
1849 dii->dii_hi = ht->ht_array;
1850 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001851
1852 return IterNew(dii,
1853 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001854 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001855}
1856
1857 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001858DictionaryAssItem(
1859 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001860{
1861 char_u *key;
1862 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001863 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001864 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001865 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001867 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001868 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001869 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 return -1;
1871 }
1872
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001873 if (!(key = StringToChars(keyObject, &todecref)))
1874 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001875
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001876 if (*key == NUL)
1877 {
1878 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001879 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001880 return -1;
1881 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001882
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001883 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001884
1885 if (valObject == NULL)
1886 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001887 hashitem_T *hi;
1888
Bram Moolenaardb913952012-06-29 12:54:53 +02001889 if (di == NULL)
1890 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001891 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001892 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 return -1;
1894 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001895 hi = hash_find(&dict->dv_hashtab, di->di_key);
1896 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001897 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001898 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 return 0;
1900 }
1901
1902 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001903 {
1904 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001905 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001906 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001907
1908 if (di == NULL)
1909 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001910 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001912 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001913 PyErr_NoMemory();
1914 return -1;
1915 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001916 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001917
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001918 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001919 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001920 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001921 RAISE_KEY_ADD_FAIL(key);
1922 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001923 return -1;
1924 }
1925 }
1926 else
1927 clear_tv(&di->di_tv);
1928
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001929 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001930
1931 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001932 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001933 return 0;
1934}
1935
Bram Moolenaara9922d62013-05-30 13:01:18 +02001936typedef PyObject *(*hi_to_py)(hashitem_T *);
1937
Bram Moolenaardb913952012-06-29 12:54:53 +02001938 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001940{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001941 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 long_u todo = dict->dv_hashtab.ht_used;
1943 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001944 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001945 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001946 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001947
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001948 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001949 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1950 {
1951 if (!HASHITEM_EMPTY(hi))
1952 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001953 if (!(newObj = hiconvert(hi)))
1954 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001955 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001956 return NULL;
1957 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001958 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001959 --todo;
1960 ++i;
1961 }
1962 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001963 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001964}
1965
Bram Moolenaara9922d62013-05-30 13:01:18 +02001966 static PyObject *
1967dict_key(hashitem_T *hi)
1968{
1969 return PyBytes_FromString((char *)(hi->hi_key));
1970}
1971
1972 static PyObject *
1973DictionaryListKeys(DictionaryObject *self)
1974{
1975 return DictionaryListObjects(self, dict_key);
1976}
1977
1978 static PyObject *
1979dict_val(hashitem_T *hi)
1980{
1981 dictitem_T *di;
1982
1983 di = dict_lookup(hi);
1984 return ConvertToPyObject(&di->di_tv);
1985}
1986
1987 static PyObject *
1988DictionaryListValues(DictionaryObject *self)
1989{
1990 return DictionaryListObjects(self, dict_val);
1991}
1992
1993 static PyObject *
1994dict_item(hashitem_T *hi)
1995{
1996 PyObject *keyObject;
1997 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001998 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001999
2000 if (!(keyObject = dict_key(hi)))
2001 return NULL;
2002
2003 if (!(valObject = dict_val(hi)))
2004 {
2005 Py_DECREF(keyObject);
2006 return NULL;
2007 }
2008
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002009 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010
2011 Py_DECREF(keyObject);
2012 Py_DECREF(valObject);
2013
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002014 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002015}
2016
2017 static PyObject *
2018DictionaryListItems(DictionaryObject *self)
2019{
2020 return DictionaryListObjects(self, dict_item);
2021}
2022
2023 static PyObject *
2024DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2025{
2026 dict_T *dict = self->dict;
2027
2028 if (dict->dv_lock)
2029 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002030 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002031 return NULL;
2032 }
2033
2034 if (kwargs)
2035 {
2036 typval_T tv;
2037
2038 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2039 return NULL;
2040
2041 VimTryStart();
2042 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2043 clear_tv(&tv);
2044 if (VimTryEnd())
2045 return NULL;
2046 }
2047 else
2048 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002049 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002050
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002051 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002052 return NULL;
2053
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002054 if (obj == NULL)
2055 {
2056 Py_INCREF(Py_None);
2057 return Py_None;
2058 }
2059
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002060 if (PyObject_HasAttrString(obj, "keys"))
2061 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002062 else
2063 {
2064 PyObject *iterator;
2065 PyObject *item;
2066
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002067 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002068 return NULL;
2069
2070 while ((item = PyIter_Next(iterator)))
2071 {
2072 PyObject *fast;
2073 PyObject *keyObject;
2074 PyObject *valObject;
2075 PyObject *todecref;
2076 char_u *key;
2077 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002078 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002079
2080 if (!(fast = PySequence_Fast(item, "")))
2081 {
2082 Py_DECREF(iterator);
2083 Py_DECREF(item);
2084 return NULL;
2085 }
2086
2087 Py_DECREF(item);
2088
2089 if (PySequence_Fast_GET_SIZE(fast) != 2)
2090 {
2091 Py_DECREF(iterator);
2092 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002093 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002094 N_("expected sequence element of size 2, "
2095 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002096 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002097 return NULL;
2098 }
2099
2100 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2101
2102 if (!(key = StringToChars(keyObject, &todecref)))
2103 {
2104 Py_DECREF(iterator);
2105 Py_DECREF(fast);
2106 return NULL;
2107 }
2108
2109 di = dictitem_alloc(key);
2110
2111 Py_XDECREF(todecref);
2112
2113 if (di == NULL)
2114 {
2115 Py_DECREF(fast);
2116 Py_DECREF(iterator);
2117 PyErr_NoMemory();
2118 return NULL;
2119 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002120 di->di_tv.v_type = VAR_UNKNOWN;
2121
2122 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2123
2124 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2125 {
2126 Py_DECREF(iterator);
2127 Py_DECREF(fast);
2128 dictitem_free(di);
2129 return NULL;
2130 }
2131
2132 Py_DECREF(fast);
2133
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002134 hi = hash_find(&dict->dv_hashtab, di->di_key);
2135 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002136 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002137 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002138 Py_DECREF(iterator);
2139 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002140 return NULL;
2141 }
2142 }
2143
2144 Py_DECREF(iterator);
2145
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002146 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002147 if (PyErr_Occurred())
2148 return NULL;
2149 }
2150 }
2151 Py_INCREF(Py_None);
2152 return Py_None;
2153}
2154
2155 static PyObject *
2156DictionaryGet(DictionaryObject *self, PyObject *args)
2157{
2158 return _DictionaryItem(self, args,
2159 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2160}
2161
2162 static PyObject *
2163DictionaryPop(DictionaryObject *self, PyObject *args)
2164{
2165 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2166}
2167
2168 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002169DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002170{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002171 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002172 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002173 PyObject *valObject;
2174 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002175
Bram Moolenaarde71b562013-06-02 17:41:54 +02002176 if (self->dict->dv_hashtab.ht_used == 0)
2177 {
2178 PyErr_SetNone(PyExc_KeyError);
2179 return NULL;
2180 }
2181
2182 hi = self->dict->dv_hashtab.ht_array;
2183 while (HASHITEM_EMPTY(hi))
2184 ++hi;
2185
2186 di = dict_lookup(hi);
2187
2188 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002189 return NULL;
2190
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002191 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002192 {
2193 Py_DECREF(valObject);
2194 return NULL;
2195 }
2196
2197 hash_remove(&self->dict->dv_hashtab, hi);
2198 dictitem_free(di);
2199
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002200 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002201}
2202
2203 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002204DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002205{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002206 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2207}
2208
2209static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002210 0, // sq_length
2211 0, // sq_concat
2212 0, // sq_repeat
2213 0, // sq_item
2214 0, // sq_slice
2215 0, // sq_ass_item
2216 0, // sq_ass_slice
2217 (objobjproc) DictionaryContains, // sq_contains
2218 0, // sq_inplace_concat
2219 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002220};
2221
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002222static PyMappingMethods DictionaryAsMapping = {
2223 (lenfunc) DictionaryLength,
2224 (binaryfunc) DictionaryItem,
2225 (objobjargproc) DictionaryAssItem,
2226};
2227
Bram Moolenaardb913952012-06-29 12:54:53 +02002228static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002229 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002230 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2231 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2232 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2233 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2234 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002235 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002236 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002237 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2238 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002239};
2240
2241static PyTypeObject ListType;
2242
2243typedef struct
2244{
2245 PyObject_HEAD
2246 list_T *list;
2247 pylinkedlist_T ref;
2248} ListObject;
2249
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002250#define NEW_LIST(list) ListNew(&ListType, list)
2251
Bram Moolenaardb913952012-06-29 12:54:53 +02002252 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002253ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002254{
2255 ListObject *self;
2256
Bram Moolenaarab589462020-07-06 21:03:06 +02002257 if (list == NULL)
2258 return NULL;
2259
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002260 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002261 if (self == NULL)
2262 return NULL;
2263 self->list = list;
2264 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002265 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002266
2267 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2268
2269 return (PyObject *)(self);
2270}
2271
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002272 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002273py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002274{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002275 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002277 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002278 {
2279 PyErr_NoMemory();
2280 return NULL;
2281 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002282 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002283
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002284 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002285}
2286
2287 static int
2288list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2289{
2290 PyObject *iterator;
2291 PyObject *item;
2292 listitem_T *li;
2293
2294 if (!(iterator = PyObject_GetIter(obj)))
2295 return -1;
2296
2297 while ((item = PyIter_Next(iterator)))
2298 {
2299 if (!(li = listitem_alloc()))
2300 {
2301 PyErr_NoMemory();
2302 Py_DECREF(item);
2303 Py_DECREF(iterator);
2304 return -1;
2305 }
2306 li->li_tv.v_lock = 0;
2307 li->li_tv.v_type = VAR_UNKNOWN;
2308
2309 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2310 {
2311 Py_DECREF(item);
2312 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002314 return -1;
2315 }
2316
2317 Py_DECREF(item);
2318
2319 list_append(l, li);
2320 }
2321
2322 Py_DECREF(iterator);
2323
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002324 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002325 if (PyErr_Occurred())
2326 return -1;
2327
2328 return 0;
2329}
2330
2331 static PyObject *
2332ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2333{
2334 list_T *list;
2335 PyObject *obj = NULL;
2336
2337 if (kwargs)
2338 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002339 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002340 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002341 return NULL;
2342 }
2343
2344 if (!PyArg_ParseTuple(args, "|O", &obj))
2345 return NULL;
2346
2347 if (!(list = py_list_alloc()))
2348 return NULL;
2349
2350 if (obj)
2351 {
2352 PyObject *lookup_dict;
2353
2354 if (!(lookup_dict = PyDict_New()))
2355 {
2356 list_unref(list);
2357 return NULL;
2358 }
2359
2360 if (list_py_concat(list, obj, lookup_dict) == -1)
2361 {
2362 Py_DECREF(lookup_dict);
2363 list_unref(list);
2364 return NULL;
2365 }
2366
2367 Py_DECREF(lookup_dict);
2368 }
2369
2370 return ListNew(subtype, list);
2371}
2372
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002373 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002374ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002375{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376 pyll_remove(&self->ref, &lastlist);
2377 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002378
2379 DESTRUCTOR_FINISH(self);
2380}
2381
Bram Moolenaardb913952012-06-29 12:54:53 +02002382 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002383ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002384{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002385 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002386}
2387
2388 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002389ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002390{
2391 listitem_T *li;
2392
Bram Moolenaard6e39182013-05-21 18:30:34 +02002393 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002394 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002395 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002396 return NULL;
2397 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002398 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002399 if (li == NULL)
2400 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002401 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002402 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002403 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002404 return NULL;
2405 }
2406 return ConvertToPyObject(&li->li_tv);
2407}
2408
Bram Moolenaardb913952012-06-29 12:54:53 +02002409 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002410ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2411 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002412{
2413 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002414 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002415
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002416 if (step == 0)
2417 {
2418 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2419 return NULL;
2420 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002421
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002422 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002423 if (list == NULL)
2424 return NULL;
2425
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002426 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002427 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002428 PyObject *item;
2429
2430 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002431 if (item == NULL)
2432 {
2433 Py_DECREF(list);
2434 return NULL;
2435 }
2436
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002437 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002438 }
2439
2440 return list;
2441}
2442
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002443 static PyObject *
2444ListItem(ListObject *self, PyObject* idx)
2445{
2446#if PY_MAJOR_VERSION < 3
2447 if (PyInt_Check(idx))
2448 {
2449 long _idx = PyInt_AsLong(idx);
2450 return ListIndex(self, _idx);
2451 }
2452 else
2453#endif
2454 if (PyLong_Check(idx))
2455 {
2456 long _idx = PyLong_AsLong(idx);
2457 return ListIndex(self, _idx);
2458 }
2459 else if (PySlice_Check(idx))
2460 {
2461 Py_ssize_t start, stop, step, slicelen;
2462
Bram Moolenaar922a4662014-03-30 16:11:43 +02002463 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002464 &start, &stop, &step, &slicelen) < 0)
2465 return NULL;
2466 return ListSlice(self, start, step, slicelen);
2467 }
2468 else
2469 {
2470 RAISE_INVALID_INDEX_TYPE(idx);
2471 return NULL;
2472 }
2473}
2474
2475 static void
2476list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2477 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2478{
2479 while (numreplaced--)
2480 {
2481 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2482 listitem_remove(l, lis[slicelen + numreplaced]);
2483 }
2484 while (numadded--)
2485 {
2486 listitem_T *next;
2487
2488 next = lastaddedli->li_prev;
2489 listitem_remove(l, lastaddedli);
2490 lastaddedli = next;
2491 }
2492}
2493
2494 static int
2495ListAssSlice(ListObject *self, Py_ssize_t first,
2496 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2497{
2498 PyObject *iterator;
2499 PyObject *item;
2500 listitem_T *li;
2501 listitem_T *lastaddedli = NULL;
2502 listitem_T *next;
2503 typval_T v;
2504 list_T *l = self->list;
2505 PyInt i;
2506 PyInt j;
2507 PyInt numreplaced = 0;
2508 PyInt numadded = 0;
2509 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002510 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002511
2512 size = ListLength(self);
2513
2514 if (l->lv_lock)
2515 {
2516 RAISE_LOCKED_LIST;
2517 return -1;
2518 }
2519
2520 if (step == 0)
2521 {
2522 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2523 return -1;
2524 }
2525
2526 if (step != 1 && slicelen == 0)
2527 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002528 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002529 int ret = 0;
2530
2531 if (obj == NULL)
2532 return 0;
2533
2534 if (!(iterator = PyObject_GetIter(obj)))
2535 return -1;
2536
2537 if ((item = PyIter_Next(iterator)))
2538 {
2539 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002540 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002541 "to extended slice"), 0);
2542 Py_DECREF(item);
2543 ret = -1;
2544 }
2545 Py_DECREF(iterator);
2546 return ret;
2547 }
2548
2549 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002550 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002551 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2552 {
2553 PyErr_NoMemory();
2554 return -1;
2555 }
2556
2557 if (first == size)
2558 li = NULL;
2559 else
2560 {
2561 li = list_find(l, (long) first);
2562 if (li == NULL)
2563 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002564 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002565 (int)first);
2566 if (obj != NULL)
2567 PyMem_Free(lis);
2568 return -1;
2569 }
2570 i = slicelen;
2571 while (i-- && li != NULL)
2572 {
2573 j = step;
2574 next = li;
2575 if (step > 0)
2576 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2577 else
2578 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2579
2580 if (obj == NULL)
2581 listitem_remove(l, li);
2582 else
2583 lis[slicelen - i - 1] = li;
2584
2585 li = next;
2586 }
2587 if (li == NULL && i != -1)
2588 {
2589 PyErr_SET_VIM(N_("internal error: not enough list items"));
2590 if (obj != NULL)
2591 PyMem_Free(lis);
2592 return -1;
2593 }
2594 }
2595
2596 if (obj == NULL)
2597 return 0;
2598
2599 if (!(iterator = PyObject_GetIter(obj)))
2600 {
2601 PyMem_Free(lis);
2602 return -1;
2603 }
2604
2605 i = 0;
2606 while ((item = PyIter_Next(iterator)))
2607 {
2608 if (ConvertFromPyObject(item, &v) == -1)
2609 {
2610 Py_DECREF(iterator);
2611 Py_DECREF(item);
2612 PyMem_Free(lis);
2613 return -1;
2614 }
2615 Py_DECREF(item);
2616 if (list_insert_tv(l, &v, numreplaced < slicelen
2617 ? lis[numreplaced]
2618 : li) == FAIL)
2619 {
2620 clear_tv(&v);
2621 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2622 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2623 PyMem_Free(lis);
2624 return -1;
2625 }
2626 if (numreplaced < slicelen)
2627 {
2628 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002629 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002630 numreplaced++;
2631 }
2632 else
2633 {
2634 if (li)
2635 lastaddedli = li->li_prev;
2636 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002637 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002638 numadded++;
2639 }
2640 clear_tv(&v);
2641 if (step != 1 && i >= slicelen)
2642 {
2643 Py_DECREF(iterator);
2644 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002645 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002646 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002647 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2648 PyMem_Free(lis);
2649 return -1;
2650 }
2651 ++i;
2652 }
2653 Py_DECREF(iterator);
2654
2655 if (step != 1 && i != slicelen)
2656 {
2657 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002658 N_("attempt to assign sequence of size %d to extended slice "
2659 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002660 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2661 PyMem_Free(lis);
2662 return -1;
2663 }
2664
2665 if (PyErr_Occurred())
2666 {
2667 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2668 PyMem_Free(lis);
2669 return -1;
2670 }
2671
2672 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002674 if (step == 1)
2675 for (i = numreplaced; i < slicelen; i++)
2676 listitem_remove(l, lis[i]);
2677
2678 PyMem_Free(lis);
2679
2680 return 0;
2681}
2682
2683 static int
2684ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2685{
2686 typval_T tv;
2687 list_T *l = self->list;
2688 listitem_T *li;
2689 Py_ssize_t length = ListLength(self);
2690
2691 if (l->lv_lock)
2692 {
2693 RAISE_LOCKED_LIST;
2694 return -1;
2695 }
2696 if (index > length || (index == length && obj == NULL))
2697 {
2698 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2699 return -1;
2700 }
2701
2702 if (obj == NULL)
2703 {
2704 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002705 if (li == NULL)
2706 {
2707 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2708 "list item %d"), (int) index);
2709 return -1;
2710 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002711 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002712 clear_tv(&li->li_tv);
2713 vim_free(li);
2714 return 0;
2715 }
2716
2717 if (ConvertFromPyObject(obj, &tv) == -1)
2718 return -1;
2719
2720 if (index == length)
2721 {
2722 if (list_append_tv(l, &tv) == FAIL)
2723 {
2724 clear_tv(&tv);
2725 PyErr_SET_VIM(N_("failed to add item to list"));
2726 return -1;
2727 }
2728 }
2729 else
2730 {
2731 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002732 if (li == NULL)
2733 {
2734 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2735 "list item %d"), (int) index);
2736 return -1;
2737 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002738 clear_tv(&li->li_tv);
2739 copy_tv(&tv, &li->li_tv);
2740 clear_tv(&tv);
2741 }
2742 return 0;
2743}
2744
2745 static Py_ssize_t
2746ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2747{
2748#if PY_MAJOR_VERSION < 3
2749 if (PyInt_Check(idx))
2750 {
2751 long _idx = PyInt_AsLong(idx);
2752 return ListAssIndex(self, _idx, obj);
2753 }
2754 else
2755#endif
2756 if (PyLong_Check(idx))
2757 {
2758 long _idx = PyLong_AsLong(idx);
2759 return ListAssIndex(self, _idx, obj);
2760 }
2761 else if (PySlice_Check(idx))
2762 {
2763 Py_ssize_t start, stop, step, slicelen;
2764
Bram Moolenaar922a4662014-03-30 16:11:43 +02002765 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002766 &start, &stop, &step, &slicelen) < 0)
2767 return -1;
2768 return ListAssSlice(self, start, step, slicelen,
2769 obj);
2770 }
2771 else
2772 {
2773 RAISE_INVALID_INDEX_TYPE(idx);
2774 return -1;
2775 }
2776}
2777
2778 static PyObject *
2779ListConcatInPlace(ListObject *self, PyObject *obj)
2780{
2781 list_T *l = self->list;
2782 PyObject *lookup_dict;
2783
2784 if (l->lv_lock)
2785 {
2786 RAISE_LOCKED_LIST;
2787 return NULL;
2788 }
2789
2790 if (!(lookup_dict = PyDict_New()))
2791 return NULL;
2792
2793 if (list_py_concat(l, obj, lookup_dict) == -1)
2794 {
2795 Py_DECREF(lookup_dict);
2796 return NULL;
2797 }
2798 Py_DECREF(lookup_dict);
2799
2800 Py_INCREF(self);
2801 return (PyObject *)(self);
2802}
2803
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002804typedef struct
2805{
2806 listwatch_T lw;
2807 list_T *list;
2808} listiterinfo_T;
2809
2810 static void
2811ListIterDestruct(listiterinfo_T *lii)
2812{
2813 list_rem_watch(lii->list, &lii->lw);
2814 PyMem_Free(lii);
2815}
2816
2817 static PyObject *
2818ListIterNext(listiterinfo_T **lii)
2819{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002820 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002821
2822 if (!((*lii)->lw.lw_item))
2823 return NULL;
2824
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002825 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002826 return NULL;
2827
2828 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2829
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002830 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002831}
2832
2833 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002834ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002835{
2836 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002837 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002838
2839 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2840 {
2841 PyErr_NoMemory();
2842 return NULL;
2843 }
2844
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002845 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002846 list_add_watch(l, &lii->lw);
2847 lii->lw.lw_item = l->lv_first;
2848 lii->list = l;
2849
2850 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002851 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002852 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002853}
2854
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002855static char *ListAttrs[] = {
2856 "locked",
2857 NULL
2858};
2859
2860 static PyObject *
2861ListDir(PyObject *self)
2862{
2863 return ObjectDir(self, ListAttrs);
2864}
2865
Bram Moolenaar66b79852012-09-21 14:00:35 +02002866 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002867ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002868{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002869 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002871 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002872 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002873 return -1;
2874 }
2875
2876 if (strcmp(name, "locked") == 0)
2877 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002878 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002879 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002880 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002881 return -1;
2882 }
2883 else
2884 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002885 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002886 if (istrue == -1)
2887 return -1;
2888 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002889 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002890 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002891 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002892 }
2893 return 0;
2894 }
2895 else
2896 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002897 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002898 return -1;
2899 }
2900}
2901
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002902static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002903 (lenfunc) ListLength, // sq_length, len(x)
2904 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2905 0, // RangeRepeat, sq_repeat, x*n
2906 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2907 0, // was_sq_slice, x[i:j]
2908 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2909 0, // was_sq_ass_slice, x[i:j]=v
2910 0, // sq_contains
2911 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2912 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002913};
2914
2915static PyMappingMethods ListAsMapping = {
2916 /* mp_length */ (lenfunc) ListLength,
2917 /* mp_subscript */ (binaryfunc) ListItem,
2918 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2919};
2920
Bram Moolenaardb913952012-06-29 12:54:53 +02002921static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002922 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2923 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2924 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002925};
2926
2927typedef struct
2928{
2929 PyObject_HEAD
2930 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002931 int argc;
2932 typval_T *argv;
2933 dict_T *self;
2934 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002935 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002936} FunctionObject;
2937
2938static PyTypeObject FunctionType;
2939
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002940#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2941 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002942
Bram Moolenaardb913952012-06-29 12:54:53 +02002943 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002944FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002945 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002946{
2947 FunctionObject *self;
2948
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002949 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002950 if (self == NULL)
2951 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002952
2953 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002954 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002955 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002956 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002957 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002958 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002959 return NULL;
2960 }
2961 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002962 }
2963 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002964 {
2965 char_u *p;
2966
2967 if ((p = get_expanded_name(name,
2968 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002969 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002970 PyErr_FORMAT(PyExc_ValueError,
2971 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002972 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002973 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002974
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002975 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2976 {
2977 char_u *np;
2978 size_t len = STRLEN(p) + 1;
2979
Bram Moolenaar51e14382019-05-25 20:21:28 +02002980 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002981 {
2982 vim_free(p);
2983 return NULL;
2984 }
2985 mch_memmove(np, "<SNR>", 5);
2986 mch_memmove(np + 5, p + 3, len - 3);
2987 vim_free(p);
2988 self->name = np;
2989 }
2990 else
2991 self->name = p;
2992 }
2993
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002994 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002995 self->argc = argc;
2996 self->argv = argv;
2997 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002998 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002999
3000 if (self->argv || self->self)
3001 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3002
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003003 return (PyObject *)(self);
3004}
3005
3006 static PyObject *
3007FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3008{
3009 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003010 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003011 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003012 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003013 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003014 typval_T selfdicttv;
3015 typval_T argstv;
3016 list_T *argslist = NULL;
3017 dict_T *selfdict = NULL;
3018 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003019 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003020 typval_T *argv = NULL;
3021 typval_T *curtv;
3022 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003023
Bram Moolenaar8110a092016-04-14 15:56:09 +02003024 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003025 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003026 selfdictObject = PyDict_GetItemString(kwargs, "self");
3027 if (selfdictObject != NULL)
3028 {
3029 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3030 return NULL;
3031 selfdict = selfdicttv.vval.v_dict;
3032 }
3033 argsObject = PyDict_GetItemString(kwargs, "args");
3034 if (argsObject != NULL)
3035 {
3036 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3037 {
3038 dict_unref(selfdict);
3039 return NULL;
3040 }
3041 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003042 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003043
3044 argc = argslist->lv_len;
3045 if (argc != 0)
3046 {
3047 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003048 if (argv == NULL)
3049 {
3050 PyErr_NoMemory();
3051 dict_unref(selfdict);
3052 list_unref(argslist);
3053 return NULL;
3054 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003055 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003056 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003057 copy_tv(&li->li_tv, curtv++);
3058 }
3059 list_unref(argslist);
3060 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003061 if (selfdict != NULL)
3062 {
3063 auto_rebind = FALSE;
3064 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3065 if (autoRebindObject != NULL)
3066 {
3067 auto_rebind = PyObject_IsTrue(autoRebindObject);
3068 if (auto_rebind == -1)
3069 {
3070 dict_unref(selfdict);
3071 list_unref(argslist);
3072 return NULL;
3073 }
3074 }
3075 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003076 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003077
Bram Moolenaar389a1792013-06-23 13:00:44 +02003078 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003079 {
3080 dict_unref(selfdict);
3081 while (argc--)
3082 clear_tv(&argv[argc]);
3083 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003084 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003085 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003086
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003087 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003088
Bram Moolenaar389a1792013-06-23 13:00:44 +02003089 PyMem_Free(name);
3090
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003091 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003092}
3093
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003094 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003095FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003096{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003097 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003098 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003099 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003100 for (i = 0; i < self->argc; ++i)
3101 clear_tv(&self->argv[i]);
3102 PyMem_Free(self->argv);
3103 dict_unref(self->self);
3104 if (self->argv || self->self)
3105 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003106
3107 DESTRUCTOR_FINISH(self);
3108}
3109
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003110static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003111 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003112 NULL
3113};
3114
3115 static PyObject *
3116FunctionDir(PyObject *self)
3117{
3118 return ObjectDir(self, FunctionAttrs);
3119}
3120
Bram Moolenaardb913952012-06-29 12:54:53 +02003121 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003122FunctionAttr(FunctionObject *self, char *name)
3123{
3124 list_T *list;
3125 int i;
3126 if (strcmp(name, "name") == 0)
3127 return PyString_FromString((char *)(self->name));
3128 else if (strcmp(name, "args") == 0)
3129 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003130 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003131 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003132
Bram Moolenaar8110a092016-04-14 15:56:09 +02003133 for (i = 0; i < self->argc; ++i)
3134 list_append_tv(list, &self->argv[i]);
3135 return NEW_LIST(list);
3136 }
3137 else if (strcmp(name, "self") == 0)
3138 return self->self == NULL
3139 ? AlwaysNone(NULL)
3140 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003141 else if (strcmp(name, "auto_rebind") == 0)
3142 return self->auto_rebind
3143 ? AlwaysTrue(NULL)
3144 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003145 else if (strcmp(name, "__members__") == 0)
3146 return ObjectDir(NULL, FunctionAttrs);
3147 return NULL;
3148}
3149
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003150/*
3151 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003152 *
3153 * "exported" should be set to true when it is needed to construct a partial
3154 * that may be stored in a variable (i.e. may be freed by Vim).
3155 */
3156 static void
3157set_partial(FunctionObject *self, partial_T *pt, int exported)
3158{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003159 int i;
3160
3161 pt->pt_name = self->name;
3162 if (self->argv)
3163 {
3164 pt->pt_argc = self->argc;
3165 if (exported)
3166 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003167 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003168 for (i = 0; i < pt->pt_argc; ++i)
3169 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3170 }
3171 else
3172 pt->pt_argv = self->argv;
3173 }
3174 else
3175 {
3176 pt->pt_argc = 0;
3177 pt->pt_argv = NULL;
3178 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003179 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003180 pt->pt_dict = self->self;
3181 if (exported && self->self)
3182 ++pt->pt_dict->dv_refcount;
3183 if (exported)
3184 pt->pt_name = vim_strsave(pt->pt_name);
3185 pt->pt_refcount = 1;
3186}
3187
3188 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003189FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003190{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003192 typval_T args;
3193 typval_T selfdicttv;
3194 typval_T rettv;
3195 dict_T *selfdict = NULL;
3196 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003197 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003198 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003199 partial_T pt;
3200 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003201
Bram Moolenaar8110a092016-04-14 15:56:09 +02003202 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003203 return NULL;
3204
3205 if (kwargs != NULL)
3206 {
3207 selfdictObject = PyDict_GetItemString(kwargs, "self");
3208 if (selfdictObject != NULL)
3209 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003210 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003211 {
3212 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003213 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003214 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003215 selfdict = selfdicttv.vval.v_dict;
3216 }
3217 }
3218
Bram Moolenaar8110a092016-04-14 15:56:09 +02003219 if (self->argv || self->self)
3220 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003221 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003222 set_partial(self, &pt, FALSE);
3223 pt_ptr = &pt;
3224 }
3225
Bram Moolenaar71700b82013-05-15 17:49:05 +02003226 Py_BEGIN_ALLOW_THREADS
3227 Python_Lock_Vim();
3228
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003229 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003230 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003231
3232 Python_Release_Vim();
3233 Py_END_ALLOW_THREADS
3234
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003235 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003236 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003237 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003238 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003239 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003240 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003241 }
3242 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003244
Bram Moolenaardb913952012-06-29 12:54:53 +02003245 clear_tv(&args);
3246 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003247 if (selfdict != NULL)
3248 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003249
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003250 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003251}
3252
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003253 static PyObject *
3254FunctionRepr(FunctionObject *self)
3255{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003256 PyObject *ret;
3257 garray_T repr_ga;
3258 int i;
3259 char_u *tofree = NULL;
3260 typval_T tv;
3261 char_u numbuf[NUMBUFLEN];
3262
3263 ga_init2(&repr_ga, (int)sizeof(char), 70);
3264 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3265 if (self->name)
3266 ga_concat(&repr_ga, self->name);
3267 else
3268 ga_concat(&repr_ga, (char_u *)"<NULL>");
3269 ga_append(&repr_ga, '\'');
3270 if (self->argv)
3271 {
3272 ga_concat(&repr_ga, (char_u *)", args=[");
3273 ++emsg_silent;
3274 for (i = 0; i < self->argc; i++)
3275 {
3276 if (i != 0)
3277 ga_concat(&repr_ga, (char_u *)", ");
3278 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3279 get_copyID()));
3280 vim_free(tofree);
3281 }
3282 --emsg_silent;
3283 ga_append(&repr_ga, ']');
3284 }
3285 if (self->self)
3286 {
3287 ga_concat(&repr_ga, (char_u *)", self=");
3288 tv.v_type = VAR_DICT;
3289 tv.vval.v_dict = self->self;
3290 ++emsg_silent;
3291 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3292 --emsg_silent;
3293 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003294 if (self->auto_rebind)
3295 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003296 }
3297 ga_append(&repr_ga, '>');
3298 ret = PyString_FromString((char *)repr_ga.ga_data);
3299 ga_clear(&repr_ga);
3300 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003301}
3302
Bram Moolenaardb913952012-06-29 12:54:53 +02003303static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003304 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3305 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003306};
3307
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003308/*
3309 * Options object
3310 */
3311
3312static PyTypeObject OptionsType;
3313
3314typedef int (*checkfun)(void *);
3315
3316typedef struct
3317{
3318 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003319 int opt_type;
3320 void *from;
3321 checkfun Check;
3322 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003323} OptionsObject;
3324
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003325 static int
3326dummy_check(void *arg UNUSED)
3327{
3328 return 0;
3329}
3330
3331 static PyObject *
3332OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3333{
3334 OptionsObject *self;
3335
Bram Moolenaar774267b2013-05-21 20:51:59 +02003336 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003337 if (self == NULL)
3338 return NULL;
3339
3340 self->opt_type = opt_type;
3341 self->from = from;
3342 self->Check = Check;
3343 self->fromObj = fromObj;
3344 if (fromObj)
3345 Py_INCREF(fromObj);
3346
3347 return (PyObject *)(self);
3348}
3349
3350 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003351OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003352{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003353 PyObject_GC_UnTrack((void *)(self));
3354 Py_XDECREF(self->fromObj);
3355 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003356}
3357
3358 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003359OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003360{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003361 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003362 return 0;
3363}
3364
3365 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003367{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003368 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003369 return 0;
3370}
3371
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003372 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003374{
3375 char_u *key;
3376 int flags;
3377 long numval;
3378 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003379 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003380
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003381 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003382 return NULL;
3383
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003384 if (!(key = StringToChars(keyObject, &todecref)))
3385 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003386
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003387 if (*key == NUL)
3388 {
3389 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003390 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003391 return NULL;
3392 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003393
3394 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003395 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003396
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003397 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003398
3399 if (flags == 0)
3400 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003401 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402 return NULL;
3403 }
3404
3405 if (flags & SOPT_UNSET)
3406 {
3407 Py_INCREF(Py_None);
3408 return Py_None;
3409 }
3410 else if (flags & SOPT_BOOL)
3411 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003412 PyObject *ret;
3413 ret = numval ? Py_True : Py_False;
3414 Py_INCREF(ret);
3415 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003416 }
3417 else if (flags & SOPT_NUM)
3418 return PyInt_FromLong(numval);
3419 else if (flags & SOPT_STRING)
3420 {
3421 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003422 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003423 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003424 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003425 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003426 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003427 else
3428 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003429 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003430 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003431 return NULL;
3432 }
3433 }
3434 else
3435 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003436 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003437 return NULL;
3438 }
3439}
3440
3441 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003442OptionsContains(OptionsObject *self, PyObject *keyObject)
3443{
3444 char_u *key;
3445 PyObject *todecref;
3446
3447 if (!(key = StringToChars(keyObject, &todecref)))
3448 return -1;
3449
3450 if (*key == NUL)
3451 {
3452 Py_XDECREF(todecref);
3453 return 0;
3454 }
3455
3456 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3457 {
3458 Py_XDECREF(todecref);
3459 return 1;
3460 }
3461 else
3462 {
3463 Py_XDECREF(todecref);
3464 return 0;
3465 }
3466}
3467
3468typedef struct
3469{
3470 void *lastoption;
3471 int opt_type;
3472} optiterinfo_T;
3473
3474 static PyObject *
3475OptionsIterNext(optiterinfo_T **oii)
3476{
3477 char_u *name;
3478
3479 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3480 return PyString_FromString((char *)name);
3481
3482 return NULL;
3483}
3484
3485 static PyObject *
3486OptionsIter(OptionsObject *self)
3487{
3488 optiterinfo_T *oii;
3489
3490 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3491 {
3492 PyErr_NoMemory();
3493 return NULL;
3494 }
3495
3496 oii->opt_type = self->opt_type;
3497 oii->lastoption = NULL;
3498
3499 return IterNew(oii,
3500 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003501 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003502}
3503
3504 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003505set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003506{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003507 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003508
3509 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3510 {
3511 if (VimTryEnd())
3512 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003513 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003514 return FAIL;
3515 }
3516 return OK;
3517}
3518
3519 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003520set_option_value_for(
3521 char_u *key,
3522 int numval,
3523 char_u *stringval,
3524 int opt_flags,
3525 int opt_type,
3526 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003527{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003528 win_T *save_curwin = NULL;
3529 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003530 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003531 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003532
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003533 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003534 switch (opt_type)
3535 {
3536 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003537 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003538 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003539 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003540 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003541 if (VimTryEnd())
3542 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003543 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003544 return -1;
3545 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003546 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3547 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003548 break;
3549 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003550 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003551 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003552 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003553 break;
3554 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003555 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003556 break;
3557 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003558 if (set_ret == FAIL)
3559 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003560 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003561}
3562
3563 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003564OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003565{
3566 char_u *key;
3567 int flags;
3568 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003569 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003570 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003571
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003572 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003573 return -1;
3574
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003575 if (!(key = StringToChars(keyObject, &todecref)))
3576 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003577
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003578 if (*key == NUL)
3579 {
3580 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003581 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003582 return -1;
3583 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003584
3585 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003586 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003587
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003588 if (flags == 0)
3589 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003590 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003591 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003592 return -1;
3593 }
3594
3595 if (valObject == NULL)
3596 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003597 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003598 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003599 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003600 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003601 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003602 return -1;
3603 }
3604 else if (!(flags & SOPT_GLOBAL))
3605 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003606 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003607 N_("unable to unset option %s "
3608 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003609 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003610 return -1;
3611 }
3612 else
3613 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003614 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003615 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003616 return 0;
3617 }
3618 }
3619
Bram Moolenaard6e39182013-05-21 18:30:34 +02003620 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003621
3622 if (flags & SOPT_BOOL)
3623 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003624 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003625
Bram Moolenaarb983f752013-05-15 16:11:50 +02003626 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003627 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003628 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003629 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003630 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003631 }
3632 else if (flags & SOPT_NUM)
3633 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003634 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003635
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003636 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003637 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003638 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003639 return -1;
3640 }
3641
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003642 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003643 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003644 }
3645 else
3646 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003647 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003648 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003649
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003650 if ((val = StringToChars(valObject, &todecref2)))
3651 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003652 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003653 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003654 Py_XDECREF(todecref2);
3655 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003656 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003657 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003658 }
3659
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003660 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003661
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003662 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003663}
3664
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003665static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003666 0, // sq_length
3667 0, // sq_concat
3668 0, // sq_repeat
3669 0, // sq_item
3670 0, // sq_slice
3671 0, // sq_ass_item
3672 0, // sq_ass_slice
3673 (objobjproc) OptionsContains, // sq_contains
3674 0, // sq_inplace_concat
3675 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003676};
3677
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003678static PyMappingMethods OptionsAsMapping = {
3679 (lenfunc) NULL,
3680 (binaryfunc) OptionsItem,
3681 (objobjargproc) OptionsAssItem,
3682};
3683
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003684// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003685
3686typedef struct
3687{
3688 PyObject_HEAD
3689 tabpage_T *tab;
3690} TabPageObject;
3691
3692static PyObject *WinListNew(TabPageObject *tabObject);
3693
3694static PyTypeObject TabPageType;
3695
3696 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003697CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003698{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003699 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003700 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003701 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003702 return -1;
3703 }
3704
3705 return 0;
3706}
3707
3708 static PyObject *
3709TabPageNew(tabpage_T *tab)
3710{
3711 TabPageObject *self;
3712
3713 if (TAB_PYTHON_REF(tab))
3714 {
3715 self = TAB_PYTHON_REF(tab);
3716 Py_INCREF(self);
3717 }
3718 else
3719 {
3720 self = PyObject_NEW(TabPageObject, &TabPageType);
3721 if (self == NULL)
3722 return NULL;
3723 self->tab = tab;
3724 TAB_PYTHON_REF(tab) = self;
3725 }
3726
3727 return (PyObject *)(self);
3728}
3729
3730 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003731TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003732{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003733 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3734 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003735
3736 DESTRUCTOR_FINISH(self);
3737}
3738
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003739static char *TabPageAttrs[] = {
3740 "windows", "number", "vars", "window", "valid",
3741 NULL
3742};
3743
3744 static PyObject *
3745TabPageDir(PyObject *self)
3746{
3747 return ObjectDir(self, TabPageAttrs);
3748}
3749
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003750 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003751TabPageAttrValid(TabPageObject *self, char *name)
3752{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003753 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003754
3755 if (strcmp(name, "valid") != 0)
3756 return NULL;
3757
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003758 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3759 Py_INCREF(ret);
3760 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003761}
3762
3763 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003764TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003765{
3766 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003767 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003768 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003769 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003770 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003771 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003772 else if (strcmp(name, "window") == 0)
3773 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003774 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003775 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003776 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003777 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003778 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003779 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003780 else if (strcmp(name, "__members__") == 0)
3781 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003782 return NULL;
3783}
3784
3785 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003786TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003787{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003788 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003789 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003790 else
3791 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003792 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003793
3794 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003795 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3796 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003797 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003798 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003799 }
3800}
3801
3802static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003803 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003804 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3805 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003806};
3807
3808/*
3809 * Window list object
3810 */
3811
3812static PyTypeObject TabListType;
3813static PySequenceMethods TabListAsSeq;
3814
3815typedef struct
3816{
3817 PyObject_HEAD
3818} TabListObject;
3819
3820 static PyInt
3821TabListLength(PyObject *self UNUSED)
3822{
3823 tabpage_T *tp = first_tabpage;
3824 PyInt n = 0;
3825
3826 while (tp != NULL)
3827 {
3828 ++n;
3829 tp = tp->tp_next;
3830 }
3831
3832 return n;
3833}
3834
3835 static PyObject *
3836TabListItem(PyObject *self UNUSED, PyInt n)
3837{
3838 tabpage_T *tp;
3839
3840 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3841 if (n == 0)
3842 return TabPageNew(tp);
3843
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003844 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003845 return NULL;
3846}
3847
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003848/*
3849 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003850 */
3851
3852typedef struct
3853{
3854 PyObject_HEAD
3855 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003856 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003857} WindowObject;
3858
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003859static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003860
3861 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003862CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003863{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003864 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003865 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003866 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003867 return -1;
3868 }
3869
3870 return 0;
3871}
3872
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003873 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003874WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003875{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003876 /*
3877 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003878 * If we add a "w_python*_ref" field to the win_T structure,
3879 * then we can get at it in win_free() in vim. We then
3880 * need to create only ONE Python object per window - if
3881 * we try to create a second, just INCREF the existing one
3882 * and return it. The (single) Python object referring to
3883 * the window is stored in "w_python*_ref".
3884 * On a win_free() we set the Python object's win_T* field
3885 * to an invalid value. We trap all uses of a window
3886 * object, and reject them if the win_T* field is invalid.
3887 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003888 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003889 * w_python_ref and w_python3_ref fields respectively.
3890 */
3891
3892 WindowObject *self;
3893
3894 if (WIN_PYTHON_REF(win))
3895 {
3896 self = WIN_PYTHON_REF(win);
3897 Py_INCREF(self);
3898 }
3899 else
3900 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003901 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003902 if (self == NULL)
3903 return NULL;
3904 self->win = win;
3905 WIN_PYTHON_REF(win) = self;
3906 }
3907
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003908 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3909
Bram Moolenaar971db462013-05-12 18:44:48 +02003910 return (PyObject *)(self);
3911}
3912
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003913 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003914WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003915{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003916 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003917 if (self->win && self->win != INVALID_WINDOW_VALUE)
3918 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003919 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003920 PyObject_GC_Del((void *)(self));
3921}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003922
Bram Moolenaar774267b2013-05-21 20:51:59 +02003923 static int
3924WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3925{
3926 Py_VISIT(((PyObject *)(self->tabObject)));
3927 return 0;
3928}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003929
Bram Moolenaar774267b2013-05-21 20:51:59 +02003930 static int
3931WindowClear(WindowObject *self)
3932{
3933 Py_CLEAR(self->tabObject);
3934 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003935}
3936
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003937 static win_T *
3938get_firstwin(TabPageObject *tabObject)
3939{
3940 if (tabObject)
3941 {
3942 if (CheckTabPage(tabObject))
3943 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003944 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003945 else if (tabObject->tab == curtab)
3946 return firstwin;
3947 else
3948 return tabObject->tab->tp_firstwin;
3949 }
3950 else
3951 return firstwin;
3952}
Bram Moolenaare950f992018-06-10 13:55:55 +02003953
3954// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003955static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003956 "buffer",
3957 "cursor",
3958 "height",
3959 "row",
3960 "width",
3961 "col",
3962 "vars",
3963 "options",
3964 "number",
3965 "tabpage",
3966 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003967 NULL
3968};
3969
3970 static PyObject *
3971WindowDir(PyObject *self)
3972{
3973 return ObjectDir(self, WindowAttrs);
3974}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003975
Bram Moolenaar971db462013-05-12 18:44:48 +02003976 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003977WindowAttrValid(WindowObject *self, char *name)
3978{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003979 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003980
3981 if (strcmp(name, "valid") != 0)
3982 return NULL;
3983
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003984 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3985 Py_INCREF(ret);
3986 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003987}
3988
3989 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003990WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003991{
3992 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003993 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003994 else if (strcmp(name, "cursor") == 0)
3995 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003997
3998 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3999 }
4000 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004001 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004002 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004003 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004004 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004005 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004006 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004007 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004008 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004009 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004010 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004011 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4012 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004013 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004014 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004016 return NULL;
4017 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004018 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004019 }
4020 else if (strcmp(name, "tabpage") == 0)
4021 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004022 Py_INCREF(self->tabObject);
4023 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004024 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004025 else if (strcmp(name, "__members__") == 0)
4026 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004027 else
4028 return NULL;
4029}
4030
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004031 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004032WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004033{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004034 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 return -1;
4036
4037 if (strcmp(name, "buffer") == 0)
4038 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004039 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004040 return -1;
4041 }
4042 else if (strcmp(name, "cursor") == 0)
4043 {
4044 long lnum;
4045 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004047 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004048 return -1;
4049
Bram Moolenaard6e39182013-05-21 18:30:34 +02004050 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004052 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004053 return -1;
4054 }
4055
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004056 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004057 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004058 return -1;
4059
Bram Moolenaard6e39182013-05-21 18:30:34 +02004060 self->win->w_cursor.lnum = lnum;
4061 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004062 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004063 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004064 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004065 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004067 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 return 0;
4069 }
4070 else if (strcmp(name, "height") == 0)
4071 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004072 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073 win_T *savewin;
4074
Bram Moolenaardee2e312013-06-23 16:35:47 +02004075 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 return -1;
4077
4078#ifdef FEAT_GUI
4079 need_mouse_correct = TRUE;
4080#endif
4081 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004082 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004083
4084 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004085 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004087 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 return -1;
4089
4090 return 0;
4091 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 else if (strcmp(name, "width") == 0)
4093 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004094 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 win_T *savewin;
4096
Bram Moolenaardee2e312013-06-23 16:35:47 +02004097 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 return -1;
4099
4100#ifdef FEAT_GUI
4101 need_mouse_correct = TRUE;
4102#endif
4103 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004104 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004105
4106 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004107 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004109 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004110 return -1;
4111
4112 return 0;
4113 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 else
4115 {
4116 PyErr_SetString(PyExc_AttributeError, name);
4117 return -1;
4118 }
4119}
4120
4121 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004122WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004123{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004124 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004125 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126 else
4127 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004128 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129
Bram Moolenaar6d216452013-05-12 19:00:41 +02004130 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004131 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004132 (self));
4133 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004134 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 }
4136}
4137
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004138static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004139 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004140 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4141 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004142};
4143
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004144/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004145 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004147
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004148static PyTypeObject WinListType;
4149static PySequenceMethods WinListAsSeq;
4150
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004151typedef struct
4152{
4153 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004154 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004155} WinListObject;
4156
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004157 static PyObject *
4158WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004159{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004160 WinListObject *self;
4161
4162 self = PyObject_NEW(WinListObject, &WinListType);
4163 self->tabObject = tabObject;
4164 Py_INCREF(tabObject);
4165
4166 return (PyObject *)(self);
4167}
4168
4169 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004170WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004171{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004172 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004173
4174 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004175 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004176 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004177 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004178
4179 DESTRUCTOR_FINISH(self);
4180}
4181
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004182 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004183WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004184{
4185 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 PyInt n = 0;
4187
Bram Moolenaard6e39182013-05-21 18:30:34 +02004188 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004189 return -1;
4190
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004191 while (w != NULL)
4192 {
4193 ++n;
4194 w = W_NEXT(w);
4195 }
4196
4197 return n;
4198}
4199
4200 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004201WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004202{
4203 win_T *w;
4204
Bram Moolenaard6e39182013-05-21 18:30:34 +02004205 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004206 return NULL;
4207
4208 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004209 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004210 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004211
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004212 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004213 return NULL;
4214}
4215
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004216/*
4217 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218 *
4219 * The result is in allocated memory. All internal nulls are replaced by
4220 * newline characters. It is an error for the string to contain newline
4221 * characters.
4222 *
4223 * On errors, the Python exception data is set, and NULL is returned.
4224 */
4225 static char *
4226StringToLine(PyObject *obj)
4227{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004228 char *str;
4229 char *save;
4230 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004231 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004232 PyInt i;
4233 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004234
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004235 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004236 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004237 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4238 || str == NULL)
4239 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004240 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004241 else if (PyUnicode_Check(obj))
4242 {
4243 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4244 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004245
Bram Moolenaardaa27022013-06-24 22:33:30 +02004246 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004247 || str == NULL)
4248 {
4249 Py_DECREF(bytes);
4250 return NULL;
4251 }
4252 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004253 else
4254 {
4255#if PY_MAJOR_VERSION < 3
4256 PyErr_FORMAT(PyExc_TypeError,
4257 N_("expected str() or unicode() instance, but got %s"),
4258 Py_TYPE_NAME(obj));
4259#else
4260 PyErr_FORMAT(PyExc_TypeError,
4261 N_("expected bytes() or str() instance, but got %s"),
4262 Py_TYPE_NAME(obj));
4263#endif
4264 return NULL;
4265 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004266
4267 /*
4268 * Error checking: String must not contain newlines, as we
4269 * are replacing a single line, and we must replace it with
4270 * a single line.
4271 * A trailing newline is removed, so that append(f.readlines()) works.
4272 */
4273 p = memchr(str, '\n', len);
4274 if (p != NULL)
4275 {
4276 if (p == str + len - 1)
4277 --len;
4278 else
4279 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004280 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004281 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004282 return NULL;
4283 }
4284 }
4285
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004286 /*
4287 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004288 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004289 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004290 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004291 if (save == NULL)
4292 {
4293 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004294 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004295 return NULL;
4296 }
4297
4298 for (i = 0; i < len; ++i)
4299 {
4300 if (str[i] == '\0')
4301 save[i] = '\n';
4302 else
4303 save[i] = str[i];
4304 }
4305
4306 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004307 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004308
4309 return save;
4310}
4311
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004312/*
4313 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004314 * in Vim format (1-based). The line is returned as a Python
4315 * string object.
4316 */
4317 static PyObject *
4318GetBufferLine(buf_T *buf, PyInt n)
4319{
4320 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4321}
4322
4323
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004324/*
4325 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004326 * are in Vim format (1-based). The range is from lo up to, but not
4327 * including, hi. The list is returned as a Python list of string objects.
4328 */
4329 static PyObject *
4330GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4331{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004332 PyInt i;
4333 PyInt n = hi - lo;
4334 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004335
4336 if (list == NULL)
4337 return NULL;
4338
4339 for (i = 0; i < n; ++i)
4340 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004341 linenr_T lnum = (linenr_T)(lo + i);
4342 char *text;
4343 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004344
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004345 if (lnum > buf->b_ml.ml_line_count)
4346 text = "";
4347 else
4348 text = (char *)ml_get_buf(buf, lnum, FALSE);
4349 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004350 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004351 {
4352 Py_DECREF(list);
4353 return NULL;
4354 }
4355
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004356 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004357 }
4358
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004359 // The ownership of the Python list is passed to the caller (ie,
4360 // the caller should Py_DECREF() the object when it is finished
4361 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004362
4363 return list;
4364}
4365
4366/*
4367 * Check if deleting lines made the cursor position invalid.
4368 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4369 * deleted).
4370 */
4371 static void
4372py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4373{
4374 if (curwin->w_cursor.lnum >= lo)
4375 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004376 // Adjust the cursor position if it's in/after the changed
4377 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004378 if (curwin->w_cursor.lnum >= hi)
4379 {
4380 curwin->w_cursor.lnum += extra;
4381 check_cursor_col();
4382 }
4383 else if (extra < 0)
4384 {
4385 curwin->w_cursor.lnum = lo;
4386 check_cursor();
4387 }
4388 else
4389 check_cursor_col();
4390 changed_cline_bef_curs();
4391 }
4392 invalidate_botline();
4393}
4394
Bram Moolenaar19e60942011-06-19 00:27:51 +02004395/*
4396 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 * in Vim format (1-based). The replacement line is given as
4398 * a Python string object. The object is checked for validity
4399 * and correct format. Errors are returned as a value of FAIL.
4400 * The return value is OK on success.
4401 * If OK is returned and len_change is not NULL, *len_change
4402 * is set to the change in the buffer length.
4403 */
4404 static int
4405SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4406{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004407 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004408 win_T *save_curwin = NULL;
4409 tabpage_T *save_curtab = NULL;
4410
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004411 // First of all, we check the type of the supplied Python object.
4412 // There are three cases:
4413 // 1. NULL, or None - this is a deletion.
4414 // 2. A string - this is a replacement.
4415 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416 if (line == Py_None || line == NULL)
4417 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004418 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004419 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004421 VimTryStart();
4422
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004424 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004425 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004426 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004427 else
4428 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004429 if (buf == curbuf && (save_curwin != NULL
4430 || save_curbuf.br_buf == NULL))
4431 // Using an existing window for the buffer, adjust the cursor
4432 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004433 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004434 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004435 // Only adjust marks if we managed to switch to a window that
4436 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004437 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 }
4439
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004440 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004442 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 return FAIL;
4444
4445 if (len_change)
4446 *len_change = -1;
4447
4448 return OK;
4449 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004450 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004451 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004452 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453
4454 if (save == NULL)
4455 return FAIL;
4456
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004457 VimTryStart();
4458
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004459 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004460 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004461 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004462
4463 if (u_savesub((linenr_T)n) == FAIL)
4464 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004465 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004466 vim_free(save);
4467 }
4468 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4469 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004470 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004471 vim_free(save);
4472 }
4473 else
4474 changed_bytes((linenr_T)n, 0);
4475
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004476 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004477
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004478 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004479 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004480 check_cursor_col();
4481
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004482 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004483 return FAIL;
4484
4485 if (len_change)
4486 *len_change = 0;
4487
4488 return OK;
4489 }
4490 else
4491 {
4492 PyErr_BadArgument();
4493 return FAIL;
4494 }
4495}
4496
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004497/*
4498 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004499 * Vim format (1-based). The range is from lo up to, but not including, hi.
4500 * The replacement lines are given as a Python list of string objects. The
4501 * list is checked for validity and correct format. Errors are returned as a
4502 * value of FAIL. The return value is OK on success.
4503 * If OK is returned and len_change is not NULL, *len_change
4504 * is set to the change in the buffer length.
4505 */
4506 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004507SetBufferLineList(
4508 buf_T *buf,
4509 PyInt lo,
4510 PyInt hi,
4511 PyObject *list,
4512 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004513{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004514 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004515 win_T *save_curwin = NULL;
4516 tabpage_T *save_curtab = NULL;
4517
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004518 // First of all, we check the type of the supplied Python object.
4519 // There are three cases:
4520 // 1. NULL, or None - this is a deletion.
4521 // 2. A list - this is a replacement.
4522 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004523 if (list == Py_None || list == NULL)
4524 {
4525 PyInt i;
4526 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004527
4528 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004529 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004530 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004531
4532 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004533 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004534 else
4535 {
4536 for (i = 0; i < n; ++i)
4537 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004538 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004539 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004540 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541 break;
4542 }
4543 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004544 if (buf == curbuf && (save_curwin != NULL
4545 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004546 // Using an existing window for the buffer, adjust the cursor
4547 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004548 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004549 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004550 // Only adjust marks if we managed to switch to a window that
4551 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004552 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004553 }
4554
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004555 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004556
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004557 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558 return FAIL;
4559
4560 if (len_change)
4561 *len_change = -n;
4562
4563 return OK;
4564 }
4565 else if (PyList_Check(list))
4566 {
4567 PyInt i;
4568 PyInt new_len = PyList_Size(list);
4569 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004570 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004571 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004572
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004573 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004574 array = NULL;
4575 else
4576 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004577 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004578 if (array == NULL)
4579 {
4580 PyErr_NoMemory();
4581 return FAIL;
4582 }
4583 }
4584
4585 for (i = 0; i < new_len; ++i)
4586 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004587 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004588
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004589 if (!(line = PyList_GetItem(list, i)) ||
4590 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004591 {
4592 while (i)
4593 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004594 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004595 return FAIL;
4596 }
4597 }
4598
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004599 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004600 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004601
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004602 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004603 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004604
4605 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004606 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004607
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004608 // If the size of the range is reducing (ie, new_len < old_len) we
4609 // need to delete some old_len. We do this at the start, by
4610 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004611 if (!PyErr_Occurred())
4612 {
4613 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004614 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004615 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004616 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004617 break;
4618 }
4619 extra -= i;
4620 }
4621
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004622 // For as long as possible, replace the existing old_len with the
4623 // new old_len. This is a more efficient operation, as it requires
4624 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004625 if (!PyErr_Occurred())
4626 {
4627 for (i = 0; i < old_len && i < new_len; ++i)
4628 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4629 == FAIL)
4630 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004631 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004632 break;
4633 }
4634 }
4635 else
4636 i = 0;
4637
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004638 // Now we may need to insert the remaining new old_len. If we do, we
4639 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004640 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004641 if (!PyErr_Occurred())
4642 {
4643 while (i < new_len)
4644 {
4645 if (ml_append((linenr_T)(lo + i - 1),
4646 (char_u *)array[i], 0, FALSE) == FAIL)
4647 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004648 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004649 break;
4650 }
4651 vim_free(array[i]);
4652 ++i;
4653 ++extra;
4654 }
4655 }
4656
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004657 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004658 while (i < new_len)
4659 {
4660 vim_free(array[i]);
4661 ++i;
4662 }
4663
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004664 // Free the array of old_len. All of its contents have now
4665 // been dealt with (either freed, or the responsibility passed
4666 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004667 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004668
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004669 // Adjust marks. Invalidate any which lie in the
4670 // changed range, and move any in the remainder of the buffer.
4671 // Only adjust marks if we managed to switch to a window that holds
4672 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004673 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004674 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004675 (long)MAXLNUM, (long)extra);
4676 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4677
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004678 if (buf == curbuf && (save_curwin != NULL
4679 || save_curbuf.br_buf == NULL))
4680 // Using an existing window for the buffer, adjust the cursor
4681 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004682 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4683
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004684 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004685 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004686
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004687 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004688 return FAIL;
4689
4690 if (len_change)
4691 *len_change = new_len - old_len;
4692
4693 return OK;
4694 }
4695 else
4696 {
4697 PyErr_BadArgument();
4698 return FAIL;
4699 }
4700}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004701
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004702/*
4703 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004704 * The line number is in Vim format (1-based). The lines to be inserted are
4705 * given as a Python list of string objects or as a single string. The lines
4706 * to be added are checked for validity and correct format. Errors are
4707 * returned as a value of FAIL. The return value is OK on success.
4708 * If OK is returned and len_change is not NULL, *len_change
4709 * is set to the change in the buffer length.
4710 */
4711 static int
4712InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4713{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004714 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004715 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004716 tabpage_T *save_curtab = NULL;
4717
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004718 // First of all, we check the type of the supplied Python object.
4719 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004720 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004721 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004722 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723
4724 if (str == NULL)
4725 return FAIL;
4726
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004728 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004729 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004731 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004732 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004734 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004735 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004736 // Only adjust marks if we managed to switch to a window that
4737 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738 appended_lines_mark((linenr_T)n, 1L);
4739
4740 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004741 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742 update_screen(VALID);
4743
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004744 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 return FAIL;
4746
4747 if (len_change)
4748 *len_change = 1;
4749
4750 return OK;
4751 }
4752 else if (PyList_Check(lines))
4753 {
4754 PyInt i;
4755 PyInt size = PyList_Size(lines);
4756 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004757
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004758 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004759 if (array == NULL)
4760 {
4761 PyErr_NoMemory();
4762 return FAIL;
4763 }
4764
4765 for (i = 0; i < size; ++i)
4766 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004767 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004768
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004769 if (!(line = PyList_GetItem(lines, i)) ||
4770 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004771 {
4772 while (i)
4773 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004774 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004775 return FAIL;
4776 }
4777 }
4778
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004780 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004781 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004782
4783 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004784 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004785 else
4786 {
4787 for (i = 0; i < size; ++i)
4788 {
4789 if (ml_append((linenr_T)(n + i),
4790 (char_u *)array[i], 0, FALSE) == FAIL)
4791 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004792 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004793
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004794 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004795 while (i < size)
4796 vim_free(array[i++]);
4797
4798 break;
4799 }
4800 vim_free(array[i]);
4801 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004802 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004803 // Only adjust marks if we managed to switch to a window that
4804 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004805 appended_lines_mark((linenr_T)n, (long)i);
4806 }
4807
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004808 // Free the array of lines. All of its contents have now
4809 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004810 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004811 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004812
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004813 update_screen(VALID);
4814
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004815 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004816 return FAIL;
4817
4818 if (len_change)
4819 *len_change = size;
4820
4821 return OK;
4822 }
4823 else
4824 {
4825 PyErr_BadArgument();
4826 return FAIL;
4827 }
4828}
4829
4830/*
4831 * Common routines for buffers and line ranges
4832 * -------------------------------------------
4833 */
4834
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004835typedef struct
4836{
4837 PyObject_HEAD
4838 buf_T *buf;
4839} BufferObject;
4840
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004841 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004842CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004843{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004844 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004845 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004846 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004847 return -1;
4848 }
4849
4850 return 0;
4851}
4852
4853 static PyObject *
4854RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4855{
4856 if (CheckBuffer(self))
4857 return NULL;
4858
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004859 if (end == -1)
4860 end = self->buf->b_ml.ml_line_count;
4861
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004862 if (n < 0)
4863 n += end - start + 1;
4864
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004865 if (n < 0 || n > end - start)
4866 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004867 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004868 return NULL;
4869 }
4870
4871 return GetBufferLine(self->buf, n+start);
4872}
4873
4874 static PyObject *
4875RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4876{
4877 PyInt size;
4878
4879 if (CheckBuffer(self))
4880 return NULL;
4881
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004882 if (end == -1)
4883 end = self->buf->b_ml.ml_line_count;
4884
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004885 size = end - start + 1;
4886
4887 if (lo < 0)
4888 lo = 0;
4889 else if (lo > size)
4890 lo = size;
4891 if (hi < 0)
4892 hi = 0;
4893 if (hi < lo)
4894 hi = lo;
4895 else if (hi > size)
4896 hi = size;
4897
4898 return GetBufferLineList(self->buf, lo+start, hi+start);
4899}
4900
4901 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004902RBAsItem(
4903 BufferObject *self,
4904 PyInt n,
4905 PyObject *valObject,
4906 PyInt start,
4907 PyInt end,
4908 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004909{
4910 PyInt len_change;
4911
4912 if (CheckBuffer(self))
4913 return -1;
4914
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004915 if (end == -1)
4916 end = self->buf->b_ml.ml_line_count;
4917
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004918 if (n < 0)
4919 n += end - start + 1;
4920
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004921 if (n < 0 || n > end - start)
4922 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004923 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004924 return -1;
4925 }
4926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004927 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004928 return -1;
4929
4930 if (new_end)
4931 *new_end = end + len_change;
4932
4933 return 0;
4934}
4935
Bram Moolenaar19e60942011-06-19 00:27:51 +02004936 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004937RBAsSlice(
4938 BufferObject *self,
4939 PyInt lo,
4940 PyInt hi,
4941 PyObject *valObject,
4942 PyInt start,
4943 PyInt end,
4944 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004945{
4946 PyInt size;
4947 PyInt len_change;
4948
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004949 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004950 if (CheckBuffer(self))
4951 return -1;
4952
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004953 if (end == -1)
4954 end = self->buf->b_ml.ml_line_count;
4955
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004956 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004957 size = end - start + 1;
4958
4959 if (lo < 0)
4960 lo = 0;
4961 else if (lo > size)
4962 lo = size;
4963 if (hi < 0)
4964 hi = 0;
4965 if (hi < lo)
4966 hi = lo;
4967 else if (hi > size)
4968 hi = size;
4969
4970 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004971 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004972 return -1;
4973
4974 if (new_end)
4975 *new_end = end + len_change;
4976
4977 return 0;
4978}
4979
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004980
4981 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004982RBAppend(
4983 BufferObject *self,
4984 PyObject *args,
4985 PyInt start,
4986 PyInt end,
4987 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004988{
4989 PyObject *lines;
4990 PyInt len_change;
4991 PyInt max;
4992 PyInt n;
4993
4994 if (CheckBuffer(self))
4995 return NULL;
4996
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004997 if (end == -1)
4998 end = self->buf->b_ml.ml_line_count;
4999
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005000 max = n = end - start + 1;
5001
5002 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5003 return NULL;
5004
5005 if (n < 0 || n > max)
5006 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005007 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005008 return NULL;
5009 }
5010
5011 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5012 return NULL;
5013
5014 if (new_end)
5015 *new_end = end + len_change;
5016
5017 Py_INCREF(Py_None);
5018 return Py_None;
5019}
5020
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005021// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005022
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005023static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005024static PySequenceMethods RangeAsSeq;
5025static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005026
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005027typedef struct
5028{
5029 PyObject_HEAD
5030 BufferObject *buf;
5031 PyInt start;
5032 PyInt end;
5033} RangeObject;
5034
5035 static PyObject *
5036RangeNew(buf_T *buf, PyInt start, PyInt end)
5037{
5038 BufferObject *bufr;
5039 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005040 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005041 if (self == NULL)
5042 return NULL;
5043
5044 bufr = (BufferObject *)BufferNew(buf);
5045 if (bufr == NULL)
5046 {
5047 Py_DECREF(self);
5048 return NULL;
5049 }
5050 Py_INCREF(bufr);
5051
5052 self->buf = bufr;
5053 self->start = start;
5054 self->end = end;
5055
5056 return (PyObject *)(self);
5057}
5058
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005059 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005061{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005062 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005063 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005064 PyObject_GC_Del((void *)(self));
5065}
5066
5067 static int
5068RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5069{
5070 Py_VISIT(((PyObject *)(self->buf)));
5071 return 0;
5072}
5073
5074 static int
5075RangeClear(RangeObject *self)
5076{
5077 Py_CLEAR(self->buf);
5078 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005079}
5080
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005081 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005082RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005083{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005084 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005085 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005086 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089}
5090
5091 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095}
5096
5097 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005098RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005099{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005100 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101}
5102
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005103static char *RangeAttrs[] = {
5104 "start", "end",
5105 NULL
5106};
5107
5108 static PyObject *
5109RangeDir(PyObject *self)
5110{
5111 return ObjectDir(self, RangeAttrs);
5112}
5113
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005114 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005115RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005117 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005118}
5119
5120 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005123 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005124 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5125 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005126 else
5127 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129
5130 if (name == NULL)
5131 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005132
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005133 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005134 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005135 }
5136}
5137
5138static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005139 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005140 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005141 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5142 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005143};
5144
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005145static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005146static PySequenceMethods BufferAsSeq;
5147static PyMappingMethods BufferAsMapping;
5148
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005149 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005150BufferNew(buf_T *buf)
5151{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005152 /*
5153 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005154 * If we add a "b_python*_ref" field to the buf_T structure,
5155 * then we can get at it in buf_freeall() in vim. We then
5156 * need to create only ONE Python object per buffer - if
5157 * we try to create a second, just INCREF the existing one
5158 * and return it. The (single) Python object referring to
5159 * the buffer is stored in "b_python*_ref".
5160 * Question: what to do on a buf_freeall(). We'll probably
5161 * have to either delete the Python object (DECREF it to
5162 * zero - a bad idea, as it leaves dangling refs!) or
5163 * set the buf_T * value to an invalid value (-1?), which
5164 * means we need checks in all access functions... Bah.
5165 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005166 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005167 * b_python_ref and b_python3_ref fields respectively.
5168 */
5169
5170 BufferObject *self;
5171
5172 if (BUF_PYTHON_REF(buf) != NULL)
5173 {
5174 self = BUF_PYTHON_REF(buf);
5175 Py_INCREF(self);
5176 }
5177 else
5178 {
5179 self = PyObject_NEW(BufferObject, &BufferType);
5180 if (self == NULL)
5181 return NULL;
5182 self->buf = buf;
5183 BUF_PYTHON_REF(buf) = self;
5184 }
5185
5186 return (PyObject *)(self);
5187}
5188
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005189 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005190BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005191{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005192 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5193 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005194
5195 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005196}
5197
Bram Moolenaar971db462013-05-12 18:44:48 +02005198 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005199BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005200{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005201 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005202 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005203 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005204
Bram Moolenaard6e39182013-05-21 18:30:34 +02005205 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005206}
5207
5208 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005209BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005210{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005211 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005212}
5213
5214 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005216{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005217 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005218}
5219
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005220static char *BufferAttrs[] = {
5221 "name", "number", "vars", "options", "valid",
5222 NULL
5223};
5224
5225 static PyObject *
5226BufferDir(PyObject *self)
5227{
5228 return ObjectDir(self, BufferAttrs);
5229}
5230
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005231 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005232BufferAttrValid(BufferObject *self, char *name)
5233{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005234 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005235
5236 if (strcmp(name, "valid") != 0)
5237 return NULL;
5238
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005239 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5240 Py_INCREF(ret);
5241 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005242}
5243
5244 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005245BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005246{
5247 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005248 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005249 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005250 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005251 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005252 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005253 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005254 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005255 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5256 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005257 else if (strcmp(name, "__members__") == 0)
5258 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005259 else
5260 return NULL;
5261}
5262
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005263 static int
5264BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5265{
5266 if (CheckBuffer(self))
5267 return -1;
5268
5269 if (strcmp(name, "name") == 0)
5270 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005271 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005272 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005273 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005274 PyObject *todecref;
5275
5276 if (!(val = StringToChars(valObject, &todecref)))
5277 return -1;
5278
5279 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005280 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005281 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005282 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005283 aucmd_restbuf(&aco);
5284 Py_XDECREF(todecref);
5285 if (VimTryEnd())
5286 return -1;
5287
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005288 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005289 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005290 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005291 return -1;
5292 }
5293 return 0;
5294 }
5295 else
5296 {
5297 PyErr_SetString(PyExc_AttributeError, name);
5298 return -1;
5299 }
5300}
5301
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005302 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005303BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005304{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005305 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005306}
5307
5308 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005309BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005310{
5311 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005312 char_u *pmark;
5313 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005314 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005315 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005316
Bram Moolenaard6e39182013-05-21 18:30:34 +02005317 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005318 return NULL;
5319
Bram Moolenaar389a1792013-06-23 13:00:44 +02005320 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005322
Bram Moolenaar389a1792013-06-23 13:00:44 +02005323 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005324 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005325 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005326 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005327 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005328 return NULL;
5329 }
5330
5331 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005332
5333 Py_XDECREF(todecref);
5334
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005335 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005336 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005337 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005338 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005339 if (VimTryEnd())
5340 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005341
5342 if (posp == NULL)
5343 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005344 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005345 return NULL;
5346 }
5347
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005348 if (posp->lnum <= 0)
5349 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005350 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005351 Py_INCREF(Py_None);
5352 return Py_None;
5353 }
5354
5355 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5356}
5357
5358 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005359BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005360{
5361 PyInt start;
5362 PyInt end;
5363
Bram Moolenaard6e39182013-05-21 18:30:34 +02005364 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005365 return NULL;
5366
5367 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5368 return NULL;
5369
Bram Moolenaard6e39182013-05-21 18:30:34 +02005370 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005371}
5372
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005373 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005374BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005375{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005376 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005377 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005378 else
5379 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005380 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005381
5382 if (name == NULL)
5383 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005384
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005385 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005386 }
5387}
5388
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005389static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005390 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005391 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005392 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005393 {"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 +02005394 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5395 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005396};
5397
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005398/*
5399 * Buffer list object - Implementation
5400 */
5401
5402static PyTypeObject BufMapType;
5403
5404typedef struct
5405{
5406 PyObject_HEAD
5407} BufMapObject;
5408
5409 static PyInt
5410BufMapLength(PyObject *self UNUSED)
5411{
5412 buf_T *b = firstbuf;
5413 PyInt n = 0;
5414
5415 while (b)
5416 {
5417 ++n;
5418 b = b->b_next;
5419 }
5420
5421 return n;
5422}
5423
5424 static PyObject *
5425BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5426{
5427 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005428 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005429
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005430 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005431 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005432
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005433 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005434
5435 if (b)
5436 return BufferNew(b);
5437 else
5438 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005439 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005440 return NULL;
5441 }
5442}
5443
5444 static void
5445BufMapIterDestruct(PyObject *buffer)
5446{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005447 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005448 if (buffer)
5449 {
5450 Py_DECREF(buffer);
5451 }
5452}
5453
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005454 static int
5455BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5456{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005457 if (buffer)
5458 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005459 return 0;
5460}
5461
5462 static int
5463BufMapIterClear(PyObject **buffer)
5464{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005465 if (*buffer)
5466 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005467 return 0;
5468}
5469
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005470 static PyObject *
5471BufMapIterNext(PyObject **buffer)
5472{
5473 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005475
5476 if (!*buffer)
5477 return NULL;
5478
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005479 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005480
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005481 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005482 {
5483 *buffer = NULL;
5484 return NULL;
5485 }
5486
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005487 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005488 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005489 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005490 return NULL;
5491 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005492 // Do not increment reference: we no longer hold it (decref), but whoever
5493 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005494 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005495}
5496
5497 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005498BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005499{
5500 PyObject *buffer;
5501
5502 buffer = BufferNew(firstbuf);
5503 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005504 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005505 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5506 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005507}
5508
5509static PyMappingMethods BufMapAsMapping = {
5510 (lenfunc) BufMapLength,
5511 (binaryfunc) BufMapItem,
5512 (objobjargproc) 0,
5513};
5514
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005515// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005516
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005517static char *CurrentAttrs[] = {
5518 "buffer", "window", "line", "range", "tabpage",
5519 NULL
5520};
5521
5522 static PyObject *
5523CurrentDir(PyObject *self)
5524{
5525 return ObjectDir(self, CurrentAttrs);
5526}
5527
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005528 static PyObject *
5529CurrentGetattr(PyObject *self UNUSED, char *name)
5530{
5531 if (strcmp(name, "buffer") == 0)
5532 return (PyObject *)BufferNew(curbuf);
5533 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005534 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005535 else if (strcmp(name, "tabpage") == 0)
5536 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005537 else if (strcmp(name, "line") == 0)
5538 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5539 else if (strcmp(name, "range") == 0)
5540 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005541 else if (strcmp(name, "__members__") == 0)
5542 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005543 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005544#if PY_MAJOR_VERSION < 3
5545 return Py_FindMethod(WindowMethods, self, name);
5546#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005547 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005548#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005549}
5550
5551 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005552CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005553{
5554 if (strcmp(name, "line") == 0)
5555 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005556 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5557 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005558 return -1;
5559
5560 return 0;
5561 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005562 else if (strcmp(name, "buffer") == 0)
5563 {
5564 int count;
5565
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005566 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005567 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005568 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005569 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005570 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005571 return -1;
5572 }
5573
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005574 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005575 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005576 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005577
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005578 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005579 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5580 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005581 if (VimTryEnd())
5582 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005583 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005584 return -1;
5585 }
5586
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005587 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005588 }
5589 else if (strcmp(name, "window") == 0)
5590 {
5591 int count;
5592
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005593 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005594 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005595 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005596 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005597 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005598 return -1;
5599 }
5600
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005601 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005602 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005603 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005604
5605 if (!count)
5606 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005607 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005608 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005609 return -1;
5610 }
5611
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005612 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005613 win_goto(((WindowObject *)(valObject))->win);
5614 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005615 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005616 if (VimTryEnd())
5617 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005618 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005619 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 return -1;
5621 }
5622
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005623 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005624 }
5625 else if (strcmp(name, "tabpage") == 0)
5626 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005627 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005628 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005629 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005630 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005631 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005632 return -1;
5633 }
5634
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005635 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005636 return -1;
5637
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005638 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005639 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5640 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005641 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005642 if (VimTryEnd())
5643 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005644 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005645 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005646 return -1;
5647 }
5648
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005649 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005650 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005651 else
5652 {
5653 PyErr_SetString(PyExc_AttributeError, name);
5654 return -1;
5655 }
5656}
5657
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005658static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005659 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005660 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5661 { NULL, NULL, 0, NULL}
5662};
5663
Bram Moolenaardb913952012-06-29 12:54:53 +02005664 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005665init_range_cmd(exarg_T *eap)
5666{
5667 RangeStart = eap->line1;
5668 RangeEnd = eap->line2;
5669}
5670
5671 static void
5672init_range_eval(typval_T *rettv UNUSED)
5673{
5674 RangeStart = (PyInt) curwin->w_cursor.lnum;
5675 RangeEnd = RangeStart;
5676}
5677
5678 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005679run_cmd(const char *cmd, void *arg UNUSED
5680#ifdef PY_CAN_RECURSE
5681 , PyGILState_STATE *pygilstate UNUSED
5682#endif
5683 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005684{
Bram Moolenaar41009372013-07-01 22:03:04 +02005685 PyObject *run_ret;
5686 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5687 if (run_ret != NULL)
5688 {
5689 Py_DECREF(run_ret);
5690 }
5691 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5692 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005693 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005694 PyErr_Clear();
5695 }
5696 else
5697 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005698}
5699
5700static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5701static int code_hdr_len = 30;
5702
5703 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005704run_do(const char *cmd, void *arg UNUSED
5705#ifdef PY_CAN_RECURSE
5706 , PyGILState_STATE *pygilstate
5707#endif
5708 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005709{
5710 PyInt lnum;
5711 size_t len;
5712 char *code;
5713 int status;
5714 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005715 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005716 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005717
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005718 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005720 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005721 return;
5722 }
5723
5724 len = code_hdr_len + STRLEN(cmd);
5725 code = PyMem_New(char, len + 1);
5726 memcpy(code, code_hdr, code_hdr_len);
5727 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005728 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5729 status = -1;
5730 if (run_ret != NULL)
5731 {
5732 status = 0;
5733 Py_DECREF(run_ret);
5734 }
5735 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5736 {
5737 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005738 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005739 PyErr_Clear();
5740 return;
5741 }
5742 else
5743 PyErr_PrintEx(1);
5744
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005745 PyMem_Free(code);
5746
5747 if (status)
5748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005749 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005750 return;
5751 }
5752
5753 status = 0;
5754 pymain = PyImport_AddModule("__main__");
5755 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005756#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005757 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005758#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005759
5760 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5761 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005762 PyObject *line;
5763 PyObject *linenr;
5764 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005765
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005766#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005767 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005768#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005769 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005770 if (lnum > curbuf->b_ml.ml_line_count
5771 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005772 goto err;
5773 if (!(linenr = PyInt_FromLong((long) lnum)))
5774 {
5775 Py_DECREF(line);
5776 goto err;
5777 }
5778 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5779 Py_DECREF(line);
5780 Py_DECREF(linenr);
5781 if (!ret)
5782 goto err;
5783
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005784 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005785 if (curbuf != was_curbuf)
5786 {
5787 Py_XDECREF(ret);
5788 goto err;
5789 }
5790
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005791 if (ret != Py_None)
5792 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005793 {
5794 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005795 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005796 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005797
5798 Py_XDECREF(ret);
5799 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005800#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005801 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005802#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005803 }
5804 goto out;
5805err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005806#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005807 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005808#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005809 PyErr_PrintEx(0);
5810 PythonIO_Flush();
5811 status = 1;
5812out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005813#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005814 if (!status)
5815 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005816#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005817 Py_DECREF(pyfunc);
5818 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5819 if (status)
5820 return;
5821 check_cursor();
5822 update_curbuf(NOT_VALID);
5823}
5824
5825 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005826run_eval(const char *cmd, typval_T *rettv
5827#ifdef PY_CAN_RECURSE
5828 , PyGILState_STATE *pygilstate UNUSED
5829#endif
5830 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005831{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005832 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005833
Bram Moolenaar41009372013-07-01 22:03:04 +02005834 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005835 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005836 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005837 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005839 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005840 PyErr_Clear();
5841 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005842 else
5843 {
5844 if (PyErr_Occurred() && !msg_silent)
5845 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005846 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005847 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005848 }
5849 else
5850 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005851 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar86181df2020-05-11 23:14:04 +02005852 emsg(_("E859: Failed to convert returned python object to a Vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005853 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005854 }
5855 PyErr_Clear();
5856}
5857
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005858 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005859set_ref_in_py(const int copyID)
5860{
5861 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005862 list_T *ll;
5863 int i;
5864 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005865 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005866
5867 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005868 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005869 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005870 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5871 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005872 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005873
5874 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005876 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005877 {
5878 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005879 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005880 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005881 }
5882
Bram Moolenaar8110a092016-04-14 15:56:09 +02005883 if (lastfunc != NULL)
5884 {
5885 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5886 {
5887 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005888 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005889 if (func->argc)
5890 for (i = 0; !abort && i < func->argc; ++i)
5891 abort = abort
5892 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5893 }
5894 }
5895
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005896 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005897}
5898
5899 static int
5900set_string_copy(char_u *str, typval_T *tv)
5901{
5902 tv->vval.v_string = vim_strsave(str);
5903 if (tv->vval.v_string == NULL)
5904 {
5905 PyErr_NoMemory();
5906 return -1;
5907 }
5908 return 0;
5909}
5910
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005911 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005912pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005913{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005914 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005915 char_u *key;
5916 dictitem_T *di;
5917 PyObject *keyObject;
5918 PyObject *valObject;
5919 Py_ssize_t iter = 0;
5920
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005921 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005923
5924 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005925 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926
5927 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5928 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005929 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930
Bram Moolenaara03e6312013-05-29 22:49:26 +02005931 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005932 {
5933 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005935 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005937 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005938 {
5939 dict_unref(dict);
5940 return -1;
5941 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005942
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005943 if (*key == NUL)
5944 {
5945 dict_unref(dict);
5946 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005947 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005948 return -1;
5949 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005950
5951 di = dictitem_alloc(key);
5952
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005953 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005954
5955 if (di == NULL)
5956 {
5957 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005958 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959 return -1;
5960 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005962 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 {
5964 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005965 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return -1;
5967 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005968
5969 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005971 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005972 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005974 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975 return -1;
5976 }
5977 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005978
5979 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005980 return 0;
5981}
5982
5983 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005984pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005986 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005987 char_u *key;
5988 dictitem_T *di;
5989 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005990 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005991 PyObject *keyObject;
5992 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005993
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005994 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005995 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005996
5997 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005998 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005999
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006000 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006001 {
6002 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006003 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006004 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006005
6006 if (!(iterator = PyObject_GetIter(list)))
6007 {
6008 dict_unref(dict);
6009 Py_DECREF(list);
6010 return -1;
6011 }
6012 Py_DECREF(list);
6013
6014 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006015 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006016 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006017
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006018 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006019 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006020 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006021 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006022 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006023 return -1;
6024 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006025
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006026 if (*key == NUL)
6027 {
6028 Py_DECREF(keyObject);
6029 Py_DECREF(iterator);
6030 Py_XDECREF(todecref);
6031 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006032 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006033 return -1;
6034 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006035
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006036 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006037 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006038 Py_DECREF(keyObject);
6039 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006040 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006041 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042 return -1;
6043 }
6044
6045 di = dictitem_alloc(key);
6046
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006047 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006048 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006049
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006050 if (di == NULL)
6051 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006052 Py_DECREF(iterator);
6053 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006054 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006055 PyErr_NoMemory();
6056 return -1;
6057 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006058
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006059 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006060 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006061 Py_DECREF(iterator);
6062 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006063 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006064 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006065 return -1;
6066 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006067
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006068 Py_DECREF(valObject);
6069
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006070 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006072 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006073 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006074 dictitem_free(di);
6075 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076 return -1;
6077 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006078 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006079 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006080 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006081 return 0;
6082}
6083
6084 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006085pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006086{
6087 list_T *l;
6088
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006089 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006090 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006091
6092 tv->v_type = VAR_LIST;
6093 tv->vval.v_list = l;
6094
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006095 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006096 {
6097 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006098 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006099 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006100
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006101 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006102 return 0;
6103}
6104
Bram Moolenaardb913952012-06-29 12:54:53 +02006105typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6106
6107 static int
6108convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006109 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006110{
6111 PyObject *capsule;
6112 char hexBuf[sizeof(void *) * 2 + 3];
6113
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006114 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006115
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006116#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006117 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006118#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006119 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006120#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006121 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006122 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006123#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006124 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006125#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006126 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006127#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006128 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6129 {
6130 Py_DECREF(capsule);
6131 tv->v_type = VAR_UNKNOWN;
6132 return -1;
6133 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006134
6135 Py_DECREF(capsule);
6136
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006137 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006138 {
6139 tv->v_type = VAR_UNKNOWN;
6140 return -1;
6141 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006142 // As we are not using copy_tv which increments reference count we must
6143 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006144 if (tv->v_type == VAR_DICT)
6145 ++tv->vval.v_dict->dv_refcount;
6146 else if (tv->v_type == VAR_LIST)
6147 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006148 }
6149 else
6150 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006151 typval_T *v;
6152
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006153#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006154 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006155#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006156 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006157#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006158 copy_tv(v, tv);
6159 }
6160 return 0;
6161}
6162
6163 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006164ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6165{
6166 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006167 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006168
6169 if (!(lookup_dict = PyDict_New()))
6170 return -1;
6171
6172 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6173 {
6174 tv->v_type = VAR_DICT;
6175 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6176 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006177 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006178 }
6179 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006180 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006181 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006182 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006183 else
6184 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006185 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006186 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006187 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006188 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006189 }
6190 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006191 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006192}
6193
6194 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006195ConvertFromPySequence(PyObject *obj, typval_T *tv)
6196{
6197 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006198 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006199
6200 if (!(lookup_dict = PyDict_New()))
6201 return -1;
6202
6203 if (PyType_IsSubtype(obj->ob_type, &ListType))
6204 {
6205 tv->v_type = VAR_LIST;
6206 tv->vval.v_list = (((ListObject *)(obj))->list);
6207 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006208 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006209 }
6210 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006211 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006212 else
6213 {
6214 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006215 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006216 Py_TYPE_NAME(obj));
6217 ret = -1;
6218 }
6219 Py_DECREF(lookup_dict);
6220 return ret;
6221}
6222
6223 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006224ConvertFromPyObject(PyObject *obj, typval_T *tv)
6225{
6226 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006227 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006228
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006229 if (!(lookup_dict = PyDict_New()))
6230 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006231 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006232 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006233 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006234}
6235
6236 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006237_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006238{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006239 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006240 {
6241 tv->v_type = VAR_DICT;
6242 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6243 ++tv->vval.v_dict->dv_refcount;
6244 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006245 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006246 {
6247 tv->v_type = VAR_LIST;
6248 tv->vval.v_list = (((ListObject *)(obj))->list);
6249 ++tv->vval.v_list->lv_refcount;
6250 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006251 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006253 FunctionObject *func = (FunctionObject *) obj;
6254 if (func->self != NULL || func->argv != NULL)
6255 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006256 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6257
Bram Moolenaar8110a092016-04-14 15:56:09 +02006258 set_partial(func, pt, TRUE);
6259 tv->vval.v_partial = pt;
6260 tv->v_type = VAR_PARTIAL;
6261 }
6262 else
6263 {
6264 if (set_string_copy(func->name, tv) == -1)
6265 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006266
Bram Moolenaar8110a092016-04-14 15:56:09 +02006267 tv->v_type = VAR_FUNC;
6268 }
6269 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006270 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006271 else if (PyBytes_Check(obj))
6272 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006273 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006274
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006275 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006276 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006277 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006278 return -1;
6279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006280 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006281 return -1;
6282
6283 tv->v_type = VAR_STRING;
6284 }
6285 else if (PyUnicode_Check(obj))
6286 {
6287 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006288 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006289
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006290 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006291 if (bytes == NULL)
6292 return -1;
6293
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006294 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006295 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006296 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006297 return -1;
6298
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006299 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006300 {
6301 Py_XDECREF(bytes);
6302 return -1;
6303 }
6304 Py_XDECREF(bytes);
6305
6306 tv->v_type = VAR_STRING;
6307 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006308#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006309 else if (PyInt_Check(obj))
6310 {
6311 tv->v_type = VAR_NUMBER;
6312 tv->vval.v_number = (varnumber_T) PyInt_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#endif
6317 else if (PyLong_Check(obj))
6318 {
6319 tv->v_type = VAR_NUMBER;
6320 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006321 if (PyErr_Occurred())
6322 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006323 }
6324 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006325 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006326#ifdef FEAT_FLOAT
6327 else if (PyFloat_Check(obj))
6328 {
6329 tv->v_type = VAR_FLOAT;
6330 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6331 }
6332#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006333 else if (PyObject_HasAttrString(obj, "keys"))
6334 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006335 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006336 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006337 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006338 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006339 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006340 else if (PyNumber_Check(obj))
6341 {
6342 PyObject *num;
6343
6344 if (!(num = PyNumber_Long(obj)))
6345 return -1;
6346
6347 tv->v_type = VAR_NUMBER;
6348 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6349
6350 Py_DECREF(num);
6351 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006352 else if (obj == Py_None)
6353 {
6354 tv->v_type = VAR_SPECIAL;
6355 tv->vval.v_number = VVAL_NONE;
6356 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006357 else
6358 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006359 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006360 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006361 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006362 return -1;
6363 }
6364 return 0;
6365}
6366
6367 static PyObject *
6368ConvertToPyObject(typval_T *tv)
6369{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006370 typval_T *argv;
6371 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006372 if (tv == NULL)
6373 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006374 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006375 return NULL;
6376 }
6377 switch (tv->v_type)
6378 {
6379 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006380 return PyBytes_FromString(tv->vval.v_string == NULL
6381 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006382 case VAR_NUMBER:
6383 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006384 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006385#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006386 return PyFloat_FromDouble((double) tv->vval.v_float);
6387#endif
6388 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006389 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006390 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006391 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006393 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006394 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006395 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006396 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006397 if (tv->vval.v_partial->pt_argc)
6398 {
6399 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6400 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6401 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6402 }
6403 else
6404 argv = NULL;
6405 if (tv->vval.v_partial->pt_dict != NULL)
6406 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006407 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006408 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006409 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006410 tv->vval.v_partial->pt_dict,
6411 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006412 case VAR_BLOB:
6413 return PyBytes_FromStringAndSize(
6414 (char*) tv->vval.v_blob->bv_ga.ga_data,
6415 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006416 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006417 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006419 case VAR_CHANNEL:
6420 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006421 Py_INCREF(Py_None);
6422 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006423 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006424 case VAR_SPECIAL:
6425 switch (tv->vval.v_number)
6426 {
6427 case VVAL_FALSE: return AlwaysFalse(NULL);
6428 case VVAL_TRUE: return AlwaysTrue(NULL);
6429 case VVAL_NONE:
6430 case VVAL_NULL: return AlwaysNone(NULL);
6431 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006432 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006433 return NULL;
6434 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006435 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006436}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006437
6438typedef struct
6439{
6440 PyObject_HEAD
6441} CurrentObject;
6442static PyTypeObject CurrentType;
6443
6444 static void
6445init_structs(void)
6446{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006447 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448 OutputType.tp_name = "vim.message";
6449 OutputType.tp_basicsize = sizeof(OutputObject);
6450 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6451 OutputType.tp_doc = "vim message object";
6452 OutputType.tp_methods = OutputMethods;
6453#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006454 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6455 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006456 OutputType.tp_alloc = call_PyType_GenericAlloc;
6457 OutputType.tp_new = call_PyType_GenericNew;
6458 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006459 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006460#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006461 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6462 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006463 // Disabled, because this causes a crash in test86
6464 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465#endif
6466
Bram Moolenaara80faa82020-04-12 19:37:17 +02006467 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006468 IterType.tp_name = "vim.iter";
6469 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006470 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006471 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006472 IterType.tp_iter = (getiterfunc)IterIter;
6473 IterType.tp_iternext = (iternextfunc)IterNext;
6474 IterType.tp_dealloc = (destructor)IterDestructor;
6475 IterType.tp_traverse = (traverseproc)IterTraverse;
6476 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006477
Bram Moolenaara80faa82020-04-12 19:37:17 +02006478 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006479 BufferType.tp_name = "vim.buffer";
6480 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006481 BufferType.tp_dealloc = (destructor)BufferDestructor;
6482 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483 BufferType.tp_as_sequence = &BufferAsSeq;
6484 BufferType.tp_as_mapping = &BufferAsMapping;
6485 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6486 BufferType.tp_doc = "vim buffer object";
6487 BufferType.tp_methods = BufferMethods;
6488#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006490 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491 BufferType.tp_alloc = call_PyType_GenericAlloc;
6492 BufferType.tp_new = call_PyType_GenericNew;
6493 BufferType.tp_free = call_PyObject_Free;
6494#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006495 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006496 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006497#endif
6498
Bram Moolenaara80faa82020-04-12 19:37:17 +02006499 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006500 WindowType.tp_name = "vim.window";
6501 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 WindowType.tp_dealloc = (destructor)WindowDestructor;
6503 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006504 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006505 WindowType.tp_doc = "vim Window object";
6506 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006507 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6508 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006509#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006510 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6511 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006512 WindowType.tp_alloc = call_PyType_GenericAlloc;
6513 WindowType.tp_new = call_PyType_GenericNew;
6514 WindowType.tp_free = call_PyObject_Free;
6515#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006516 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6517 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006518#endif
6519
Bram Moolenaara80faa82020-04-12 19:37:17 +02006520 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006521 TabPageType.tp_name = "vim.tabpage";
6522 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006523 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6524 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006525 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6526 TabPageType.tp_doc = "vim tab page object";
6527 TabPageType.tp_methods = TabPageMethods;
6528#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006529 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006530 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6531 TabPageType.tp_new = call_PyType_GenericNew;
6532 TabPageType.tp_free = call_PyObject_Free;
6533#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006534 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006535#endif
6536
Bram Moolenaara80faa82020-04-12 19:37:17 +02006537 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006538 BufMapType.tp_name = "vim.bufferlist";
6539 BufMapType.tp_basicsize = sizeof(BufMapObject);
6540 BufMapType.tp_as_mapping = &BufMapAsMapping;
6541 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006542 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006543 BufferType.tp_doc = "vim buffer list";
6544
Bram Moolenaara80faa82020-04-12 19:37:17 +02006545 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006546 WinListType.tp_name = "vim.windowlist";
6547 WinListType.tp_basicsize = sizeof(WinListType);
6548 WinListType.tp_as_sequence = &WinListAsSeq;
6549 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6550 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006551 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006552
Bram Moolenaara80faa82020-04-12 19:37:17 +02006553 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006554 TabListType.tp_name = "vim.tabpagelist";
6555 TabListType.tp_basicsize = sizeof(TabListType);
6556 TabListType.tp_as_sequence = &TabListAsSeq;
6557 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6558 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006559
Bram Moolenaara80faa82020-04-12 19:37:17 +02006560 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006561 RangeType.tp_name = "vim.range";
6562 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006563 RangeType.tp_dealloc = (destructor)RangeDestructor;
6564 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565 RangeType.tp_as_sequence = &RangeAsSeq;
6566 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006567 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568 RangeType.tp_doc = "vim Range object";
6569 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006570 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6571 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006573 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006574 RangeType.tp_alloc = call_PyType_GenericAlloc;
6575 RangeType.tp_new = call_PyType_GenericNew;
6576 RangeType.tp_free = call_PyObject_Free;
6577#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006578 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579#endif
6580
Bram Moolenaara80faa82020-04-12 19:37:17 +02006581 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582 CurrentType.tp_name = "vim.currentdata";
6583 CurrentType.tp_basicsize = sizeof(CurrentObject);
6584 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6585 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006586 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006587#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006588 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6589 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006590#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006591 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6592 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006593#endif
6594
Bram Moolenaara80faa82020-04-12 19:37:17 +02006595 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006596 DictionaryType.tp_name = "vim.dictionary";
6597 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006598 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006599 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006601 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006602 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006603 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006604 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6605 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6606 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006607#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006608 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6609 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006610#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006611 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6612 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006613#endif
6614
Bram Moolenaara80faa82020-04-12 19:37:17 +02006615 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006618 ListType.tp_basicsize = sizeof(ListObject);
6619 ListType.tp_as_sequence = &ListAsSeq;
6620 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006621 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006622 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006624 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006625 ListType.tp_new = (newfunc)ListConstructor;
6626 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006627#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006628 ListType.tp_getattro = (getattrofunc)ListGetattro;
6629 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006630#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006631 ListType.tp_getattr = (getattrfunc)ListGetattr;
6632 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006633#endif
6634
Bram Moolenaara80faa82020-04-12 19:37:17 +02006635 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006636 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006637 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006638 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6639 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006640 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006641 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006642 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006643 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006644 FunctionType.tp_new = (newfunc)FunctionConstructor;
6645 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006646#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006647 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006648#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006649 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006650#endif
6651
Bram Moolenaara80faa82020-04-12 19:37:17 +02006652 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006653 OptionsType.tp_name = "vim.options";
6654 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006655 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006656 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006657 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006658 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006659 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006660 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6661 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6662 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006663
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006664#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006665 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006666 LoaderType.tp_name = "vim.Loader";
6667 LoaderType.tp_basicsize = sizeof(LoaderObject);
6668 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6669 LoaderType.tp_doc = "vim message object";
6670 LoaderType.tp_methods = LoaderMethods;
6671 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006672#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006673
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006674#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006675 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006676 vimmodule.m_name = "vim";
6677 vimmodule.m_doc = "Vim Python interface\n";
6678 vimmodule.m_size = -1;
6679 vimmodule.m_methods = VimMethods;
6680#endif
6681}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006682
6683#define PYTYPE_READY(type) \
6684 if (PyType_Ready(&type)) \
6685 return -1;
6686
6687 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006688init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006689{
6690 PYTYPE_READY(IterType);
6691 PYTYPE_READY(BufferType);
6692 PYTYPE_READY(RangeType);
6693 PYTYPE_READY(WindowType);
6694 PYTYPE_READY(TabPageType);
6695 PYTYPE_READY(BufMapType);
6696 PYTYPE_READY(WinListType);
6697 PYTYPE_READY(TabListType);
6698 PYTYPE_READY(CurrentType);
6699 PYTYPE_READY(DictionaryType);
6700 PYTYPE_READY(ListType);
6701 PYTYPE_READY(FunctionType);
6702 PYTYPE_READY(OptionsType);
6703 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006704#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006705 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006706#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006707 return 0;
6708}
6709
6710 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006711init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006712{
6713 PyObject *path;
6714 PyObject *path_hook;
6715 PyObject *path_hooks;
6716
6717 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6718 return -1;
6719
6720 if (!(path_hooks = PySys_GetObject("path_hooks")))
6721 {
6722 PyErr_Clear();
6723 path_hooks = PyList_New(1);
6724 PyList_SET_ITEM(path_hooks, 0, path_hook);
6725 if (PySys_SetObject("path_hooks", path_hooks))
6726 {
6727 Py_DECREF(path_hooks);
6728 return -1;
6729 }
6730 Py_DECREF(path_hooks);
6731 }
6732 else if (PyList_Check(path_hooks))
6733 {
6734 if (PyList_Append(path_hooks, path_hook))
6735 {
6736 Py_DECREF(path_hook);
6737 return -1;
6738 }
6739 Py_DECREF(path_hook);
6740 }
6741 else
6742 {
6743 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006744 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006745 "You should now do the following:\n"
6746 "- append vim.path_hook to sys.path_hooks\n"
6747 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006748 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006749 Py_DECREF(path_hook);
6750 return 0;
6751 }
6752
6753 if (!(path = PySys_GetObject("path")))
6754 {
6755 PyErr_Clear();
6756 path = PyList_New(1);
6757 Py_INCREF(vim_special_path_object);
6758 PyList_SET_ITEM(path, 0, vim_special_path_object);
6759 if (PySys_SetObject("path", path))
6760 {
6761 Py_DECREF(path);
6762 return -1;
6763 }
6764 Py_DECREF(path);
6765 }
6766 else if (PyList_Check(path))
6767 {
6768 if (PyList_Append(path, vim_special_path_object))
6769 return -1;
6770 }
6771 else
6772 {
6773 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006774 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006775 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006776 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006777 }
6778
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006779 return 0;
6780}
6781
6782static BufMapObject TheBufferMap =
6783{
6784 PyObject_HEAD_INIT(&BufMapType)
6785};
6786
6787static WinListObject TheWindowList =
6788{
6789 PyObject_HEAD_INIT(&WinListType)
6790 NULL
6791};
6792
6793static CurrentObject TheCurrent =
6794{
6795 PyObject_HEAD_INIT(&CurrentType)
6796};
6797
6798static TabListObject TheTabPageList =
6799{
6800 PyObject_HEAD_INIT(&TabListType)
6801};
6802
6803static struct numeric_constant {
6804 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006805 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006806} numeric_constants[] = {
6807 {"VAR_LOCKED", VAR_LOCKED},
6808 {"VAR_FIXED", VAR_FIXED},
6809 {"VAR_SCOPE", VAR_SCOPE},
6810 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6811};
6812
6813static struct object_constant {
6814 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006815 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006816} object_constants[] = {
6817 {"buffers", (PyObject *)(void *)&TheBufferMap},
6818 {"windows", (PyObject *)(void *)&TheWindowList},
6819 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6820 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006821
6822 {"Buffer", (PyObject *)&BufferType},
6823 {"Range", (PyObject *)&RangeType},
6824 {"Window", (PyObject *)&WindowType},
6825 {"TabPage", (PyObject *)&TabPageType},
6826 {"Dictionary", (PyObject *)&DictionaryType},
6827 {"List", (PyObject *)&ListType},
6828 {"Function", (PyObject *)&FunctionType},
6829 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006830#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006831 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006832#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006833};
6834
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006835#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006836 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006837 return -1;
6838
6839#define ADD_CHECKED_OBJECT(m, name, obj) \
6840 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006841 PyObject *valObject = obj; \
6842 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006843 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006844 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006845 }
6846
6847 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006848populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006849{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006850 int i;
6851 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006852 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006853 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006854#if PY_VERSION_HEX >= 0x030700f0
6855 PyObject *dict;
6856 PyObject *cls;
6857#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006858
6859 for (i = 0; i < (int)(sizeof(numeric_constants)
6860 / sizeof(struct numeric_constant));
6861 ++i)
6862 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006863 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006864
6865 for (i = 0; i < (int)(sizeof(object_constants)
6866 / sizeof(struct object_constant));
6867 ++i)
6868 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006869 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006870
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006871 valObject = object_constants[i].valObject;
6872 Py_INCREF(valObject);
6873 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006874 }
6875
6876 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6877 return -1;
6878 ADD_OBJECT(m, "error", VimError);
6879
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006880 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6881 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006882 ADD_CHECKED_OBJECT(m, "options",
6883 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006884
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006885 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006886 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006887 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888
Bram Moolenaar22081f42016-06-01 20:38:34 +02006889#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006890 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006892#else
6893 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6894 return -1;
6895#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896 ADD_OBJECT(m, "_getcwd", py_getcwd)
6897
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006898 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006899 return -1;
6900 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006901 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006902 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006903 if (PyObject_SetAttrString(other_module, "chdir", attr))
6904 {
6905 Py_DECREF(attr);
6906 return -1;
6907 }
6908 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006909
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006910 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006911 {
6912 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006913 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006914 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006915 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6916 {
6917 Py_DECREF(attr);
6918 return -1;
6919 }
6920 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006921 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006922 else
6923 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006924
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006925 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6926 return -1;
6927
6928 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6929
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006930#if PY_VERSION_HEX >= 0x030700f0
6931 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6932 return -1;
6933
6934 dict = PyModule_GetDict(imp);
6935
6936 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6937 {
6938 Py_DECREF(imp);
6939 return -1;
6940 }
6941
6942 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6943 {
6944 Py_DECREF(imp);
6945 return -1;
6946 }
6947
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006948 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6949 {
6950 // find_module() is deprecated, this may stop working in some later
6951 // version.
6952 ADD_OBJECT(m, "_find_module", py_find_module);
6953 }
6954
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006955 Py_DECREF(imp);
6956
6957 ADD_OBJECT(m, "_find_spec", py_find_spec);
6958#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006959 if (!(imp = PyImport_ImportModule("imp")))
6960 return -1;
6961
6962 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6963 {
6964 Py_DECREF(imp);
6965 return -1;
6966 }
6967
6968 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6969 {
6970 Py_DECREF(py_find_module);
6971 Py_DECREF(imp);
6972 return -1;
6973 }
6974
6975 Py_DECREF(imp);
6976
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006977 ADD_OBJECT(m, "_find_module", py_find_module);
6978 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006979#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006980
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006981 return 0;
6982}