blob: ee848f9f8bddc88e646ed49edf62bb795f5e4325 [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
19typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
20#endif
21
Bram Moolenaar264b74f2019-01-24 17:18:42 +010022#define ENC_OPT ((char *)p_enc)
Bram Moolenaard620aa92013-05-17 16:40:06 +020023#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020024
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020025static const char *vim_special_path = "_vim_path_";
26
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020027#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020028#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020029#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010030#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
31#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
32#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020033
34#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
35 ? "(NULL)" \
36 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020039 N_("empty keys are not allowed"))
40#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
41#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
42#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
43#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
44#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
45#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020046#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020047 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020048#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020049 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020050 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020051
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020052#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
53#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020054#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020055
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020056typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020057typedef void (*runner)(const char *, void *
58#ifdef PY_CAN_RECURSE
59 , PyGILState_STATE *
60#endif
61 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020062
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020063static int ConvertFromPyObject(PyObject *, typval_T *);
64static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020065static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020066static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020067static PyObject *WindowNew(win_T *, tabpage_T *);
68static PyObject *BufferNew (buf_T *);
69static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020070
71static PyInt RangeStart;
72static PyInt RangeEnd;
73
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020074static PyObject *globals;
75
Bram Moolenaarf4258302013-06-02 18:20:17 +020076static PyObject *py_chdir;
77static PyObject *py_fchdir;
78static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020079static PyObject *vim_module;
80static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020081
Bram Moolenaar79a494d2018-07-22 04:30:21 +020082#if PY_VERSION_HEX >= 0x030700f0
83static PyObject *py_find_spec;
84#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020085static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020086#endif
Bram Moolenaarb999ba22019-02-14 13:28:45 +010087static PyObject *py_find_module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +020088
89static PyObject *VimError;
90
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020091/*
92 * obtain a lock on the Vim data structures
93 */
94 static void
95Python_Lock_Vim(void)
96{
97}
98
99/*
100 * release a lock on the Vim data structures
101 */
102 static void
103Python_Release_Vim(void)
104{
105}
106
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200107/*
108 * The "todecref" argument holds a pointer to PyObject * that must be
109 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
110 * was needed to generate returned value is object.
111 *
112 * Use Py_XDECREF to decrement reference count.
113 */
114 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200115StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200116{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200117 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200118
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200119 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120 {
121
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200122 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
123 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200124 return NULL;
125
126 *todecref = NULL;
127 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200128 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200130 PyObject *bytes;
131
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200132 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 return NULL;
134
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200135 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
136 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200137 {
138 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200139 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200140 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200141
142 *todecref = bytes;
143 }
144 else
145 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200146#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200147 PyErr_FORMAT(PyExc_TypeError,
148 N_("expected str() or unicode() instance, but got %s"),
149 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200151 PyErr_FORMAT(PyExc_TypeError,
152 N_("expected bytes() or str() instance, but got %s"),
153 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200154#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200155 return NULL;
156 }
157
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200158 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200159}
160
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200161#define NUMBER_LONG 1
162#define NUMBER_INT 2
163#define NUMBER_NATURAL 4
164#define NUMBER_UNSIGNED 8
165
166 static int
167NumberToLong(PyObject *obj, long *result, int flags)
168{
169#if PY_MAJOR_VERSION < 3
170 if (PyInt_Check(obj))
171 {
172 *result = PyInt_AsLong(obj);
173 if (PyErr_Occurred())
174 return -1;
175 }
176 else
177#endif
178 if (PyLong_Check(obj))
179 {
180 *result = PyLong_AsLong(obj);
181 if (PyErr_Occurred())
182 return -1;
183 }
184 else if (PyNumber_Check(obj))
185 {
186 PyObject *num;
187
188 if (!(num = PyNumber_Long(obj)))
189 return -1;
190
191 *result = PyLong_AsLong(num);
192
193 Py_DECREF(num);
194
195 if (PyErr_Occurred())
196 return -1;
197 }
198 else
199 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200200#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200201 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200202 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200203 "coercing to long(), but got %s"),
204 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200205#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200207 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 "but got %s"),
209 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200210#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200211 return -1;
212 }
213
214 if (flags & NUMBER_INT)
215 {
216 if (*result > INT_MAX)
217 {
218 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200219 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200220 return -1;
221 }
222 else if (*result < INT_MIN)
223 {
224 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200225 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200226 return -1;
227 }
228 }
229
230 if (flags & NUMBER_NATURAL)
231 {
232 if (*result <= 0)
233 {
234 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100235 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200236 return -1;
237 }
238 }
239 else if (flags & NUMBER_UNSIGNED)
240 {
241 if (*result < 0)
242 {
243 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200244 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200245 return -1;
246 }
247 }
248
249 return 0;
250}
251
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200252 static int
253add_string(PyObject *list, char *s)
254{
255 PyObject *string;
256
257 if (!(string = PyString_FromString(s)))
258 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200259
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200260 if (PyList_Append(list, string))
261 {
262 Py_DECREF(string);
263 return -1;
264 }
265
266 Py_DECREF(string);
267 return 0;
268}
269
270 static PyObject *
271ObjectDir(PyObject *self, char **attributes)
272{
273 PyMethodDef *method;
274 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200275 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200277 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200278 return NULL;
279
280 if (self)
281 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200282 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200284 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200285 return NULL;
286 }
287
288 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200291 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200292 return NULL;
293 }
294
295#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200298 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200299 return NULL;
300 }
301#endif
302
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200303 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200304}
305
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200306/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200307 */
308
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200309/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200310typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311
312static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313
314typedef struct
315{
316 PyObject_HEAD
317 long softspace;
318 long error;
319} OutputObject;
320
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200321static char *OutputAttrs[] = {
322 "softspace",
323 NULL
324};
325
326 static PyObject *
327OutputDir(PyObject *self)
328{
329 return ObjectDir(self, OutputAttrs);
330}
331
Bram Moolenaar77045652012-09-21 13:46:06 +0200332 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200333OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200334{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200335 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200337 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200338 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200339 return -1;
340 }
341
342 if (strcmp(name, "softspace") == 0)
343 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200344 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200345 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200346 return 0;
347 }
348
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200349 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
351}
352
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200353/* Buffer IO, we write one whole line at a time. */
354static garray_T io_ga = {0, 0, 1, 80, NULL};
355static writefn old_fn = NULL;
356
357 static void
358PythonIO_Flush(void)
359{
360 if (old_fn != NULL && io_ga.ga_len > 0)
361 {
362 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
363 old_fn((char_u *)io_ga.ga_data);
364 }
365 io_ga.ga_len = 0;
366}
367
368 static void
369writer(writefn fn, char_u *str, PyInt n)
370{
371 char_u *ptr;
372
373 /* Flush when switching output function. */
374 if (fn != old_fn)
375 PythonIO_Flush();
376 old_fn = fn;
377
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200378 // Write each NL separated line. Text after the last NL is kept for
379 // writing later.
380 // For normal messages: Do not output when "got_int" was set. This avoids
381 // a loop gone crazy flooding the terminal with messages. Also for when
382 // "q" is pressed at the more-prompt.
383 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
384 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200399 // Put the remaining text into io_ga for later printing.
400 if (n > 0 && (fn == (writefn)emsg || !got_int)
401 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200420 if (error)
421 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100422 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423 Python_Release_Vim();
424 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200425 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200426
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200427 return 0;
428}
429
430 static PyObject *
431OutputWrite(OutputObject *self, PyObject *string)
432{
433 if (write_output(self, string))
434 return NULL;
435
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200436 Py_INCREF(Py_None);
437 return Py_None;
438}
439
440 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200441OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 PyObject *iterator;
444 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200449 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200451 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200452 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200453 Py_DECREF(iterator);
454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 return NULL;
456 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200458 }
459
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200460 Py_DECREF(iterator);
461
462 /* Iterator may have finished due to an exception */
463 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{
473 /* do nothing */
474 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{
481 /* 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{
490 /* 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 Moolenaar182dc4f2013-05-21 19:01:55 +0200499 /* 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[] = {
599 /* name, function, calling, doc */
600 {"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
605/* Check to see whether a Vim error has been reported, or a keyboard
606 * interrupt has been detected.
607 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200608
609 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 Moolenaarb544f3c2017-02-23 19:03:28 +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
621 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200622 did_emsg = FALSE;
623 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200624 if (got_int)
625 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100626 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100627 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100628 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200629 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200630 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200631 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100632 else if (msg_list != NULL && *msg_list != NULL)
633 {
634 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100635 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100636
637 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
638
639 if (msg == NULL)
640 {
641 PyErr_NoMemory();
642 return -1;
643 }
644
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100645 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100646
647 free_global_msglist();
648
649 if (should_free)
650 vim_free(msg);
651
652 return -1;
653 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200654 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200655 return (PyErr_Occurred() ? -1 : 0);
656 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200657 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200658 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100659 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200660 return -1;
661 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100662 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200663 else
664 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200665 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200666 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200667 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200668 }
669}
670
671 static int
672VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200673{
674 if (got_int)
675 {
676 PyErr_SetNone(PyExc_KeyboardInterrupt);
677 return 1;
678 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200679 return 0;
680}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200681
682/* Vim module - Implementation
683 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200684
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200685 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200686VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200688 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200690 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691
Bram Moolenaar389a1792013-06-23 13:00:44 +0200692 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 return NULL;
694
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695 Py_BEGIN_ALLOW_THREADS
696 Python_Lock_Vim();
697
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200698 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200699 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700 update_screen(VALID);
701
702 Python_Release_Vim();
703 Py_END_ALLOW_THREADS
704
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200705 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200706 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200711 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713}
714
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715/*
716 * Function to translate a typval_T into a PyObject; this will recursively
717 * translate lists/dictionaries into their Python equivalents.
718 *
719 * The depth parameter is to avoid infinite recursion, set it to 1 when
720 * you call VimToPython.
721 */
722 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200723VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200724{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200727 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728
729 /* Avoid infinite recursion */
730 if (depth > 100)
731 {
732 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200733 ret = Py_None;
734 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735 }
736
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200737 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200738 * then and we can use it again. */
739 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
740 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
741 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200742 sprintf(ptrBuf, "%p",
743 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
744 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200745
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200747 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 Py_INCREF(ret);
749 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750 }
751 }
752
753 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200754 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200755 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200756 else if (our_tv->v_type == VAR_NUMBER)
757 {
758 char buf[NUMBUFLEN];
759
760 /* For backwards compatibility numbers are stored as strings. */
761 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200762 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100764#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 else if (our_tv->v_type == VAR_FLOAT)
766 {
767 char buf[NUMBUFLEN];
768
769 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200770 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100772#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 else if (our_tv->v_type == VAR_LIST)
774 {
775 list_T *list = our_tv->vval.v_list;
776 listitem_T *curr;
777
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200778 if (list == NULL)
779 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200781 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200782 return NULL;
783
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200785 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200786 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200787 return NULL;
788 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200789
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200790 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
791 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200792 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200793 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200794 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200795 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200796 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200797 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200798 {
799 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200800 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200801 return NULL;
802 }
803 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200804 }
805 }
806 else if (our_tv->v_type == VAR_DICT)
807 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200808
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100809 hashtab_T *ht;
810 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200811 hashitem_T *hi;
812 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100813
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200814 if (our_tv->vval.v_dict == NULL)
815 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100816 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200817
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200818 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200819 return NULL;
820
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200821 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200822 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200823 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200824 return NULL;
825 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200826
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100827 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200828 for (hi = ht->ht_array; todo > 0; ++hi)
829 {
830 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200831 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200832 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200833
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200834 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200835 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200836 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200837 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200838 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200840 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200841 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200842 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200843 Py_DECREF(newObj);
844 return NULL;
845 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200846 }
847 }
848 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100849 else if (our_tv->v_type == VAR_SPECIAL)
850 {
851 if (our_tv->vval.v_number == VVAL_FALSE)
852 {
853 ret = Py_False;
854 Py_INCREF(ret);
855 }
856 else if (our_tv->vval.v_number == VVAL_TRUE)
857 {
858 ret = Py_True;
859 Py_INCREF(ret);
860 }
861 else
862 {
863 Py_INCREF(Py_None);
864 ret = Py_None;
865 }
866 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
915 /* 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
1137#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1138 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 Moolenaarc09a6d62013-06-10 21:27:29 +02001406 /* 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 Moolenaar389a1792013-06-23 13:00:44 +02001409 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1410 {"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 Moolenaar9e74e302013-05-17 21:20:17 +02001434/* 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 Moolenaar3d0c52d2013-05-12 19:45:35 +02001437
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001438typedef struct
1439{
1440 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001441 void *cur;
1442 nextfun next;
1443 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001444 traversefun traverse;
1445 clearfun clear;
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,
1450 clearfun clear)
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 Moolenaarca8a4df2010-07-31 19:54:14 +02001460
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001461 return (PyObject *)(self);
1462}
1463
1464 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001465IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001466{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001467 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001468 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001469 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001470}
1471
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001472 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001473IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001474{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001475 if (self->traverse != NULL)
1476 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001477 else
1478 return 0;
1479}
1480
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001481/* Mac OSX defines clear() somewhere. */
1482#ifdef clear
1483# undef clear
1484#endif
1485
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001486 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001487IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001488{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001489 if (self->clear != NULL)
1490 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001491 else
1492 return 0;
1493}
1494
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001495 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001496IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001497{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001498 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001499}
1500
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001501 static PyObject *
1502IterIter(PyObject *self)
1503{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001504 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001505 return self;
1506}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001507
Bram Moolenaardb913952012-06-29 12:54:53 +02001508typedef struct pylinkedlist_S {
1509 struct pylinkedlist_S *pll_next;
1510 struct pylinkedlist_S *pll_prev;
1511 PyObject *pll_obj;
1512} pylinkedlist_T;
1513
1514static pylinkedlist_T *lastdict = NULL;
1515static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001516static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001517
1518 static void
1519pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1520{
1521 if (ref->pll_prev == NULL)
1522 {
1523 if (ref->pll_next == NULL)
1524 {
1525 *last = NULL;
1526 return;
1527 }
1528 }
1529 else
1530 ref->pll_prev->pll_next = ref->pll_next;
1531
1532 if (ref->pll_next == NULL)
1533 *last = ref->pll_prev;
1534 else
1535 ref->pll_next->pll_prev = ref->pll_prev;
1536}
1537
1538 static void
1539pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1540{
1541 if (*last == NULL)
1542 ref->pll_prev = NULL;
1543 else
1544 {
1545 (*last)->pll_next = ref;
1546 ref->pll_prev = *last;
1547 }
1548 ref->pll_next = NULL;
1549 ref->pll_obj = self;
1550 *last = ref;
1551}
1552
1553static PyTypeObject DictionaryType;
1554
1555typedef struct
1556{
1557 PyObject_HEAD
1558 dict_T *dict;
1559 pylinkedlist_T ref;
1560} DictionaryObject;
1561
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1563
1564#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1565
Bram Moolenaardb913952012-06-29 12:54:53 +02001566 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001567DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001568{
1569 DictionaryObject *self;
1570
Bram Moolenaara9922d62013-05-30 13:01:18 +02001571 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001572 if (self == NULL)
1573 return NULL;
1574 self->dict = dict;
1575 ++dict->dv_refcount;
1576
1577 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1578
1579 return (PyObject *)(self);
1580}
1581
Bram Moolenaara9922d62013-05-30 13:01:18 +02001582 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001583py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001584{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001585 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001586
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001587 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001588 {
1589 PyErr_NoMemory();
1590 return NULL;
1591 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001592 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001593
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001594 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001595}
1596
1597 static PyObject *
1598DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1599{
1600 DictionaryObject *self;
1601 dict_T *dict;
1602
1603 if (!(dict = py_dict_alloc()))
1604 return NULL;
1605
1606 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1607
1608 --dict->dv_refcount;
1609
1610 if (kwargs || PyTuple_Size(args))
1611 {
1612 PyObject *tmp;
1613 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1614 {
1615 Py_DECREF(self);
1616 return NULL;
1617 }
1618
1619 Py_DECREF(tmp);
1620 }
1621
1622 return (PyObject *)(self);
1623}
1624
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001625 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001626DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001627{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001628 pyll_remove(&self->ref, &lastdict);
1629 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001630
1631 DESTRUCTOR_FINISH(self);
1632}
1633
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001634static char *DictionaryAttrs[] = {
1635 "locked", "scope",
1636 NULL
1637};
1638
1639 static PyObject *
1640DictionaryDir(PyObject *self)
1641{
1642 return ObjectDir(self, DictionaryAttrs);
1643}
1644
Bram Moolenaardb913952012-06-29 12:54:53 +02001645 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001646DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001647{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001648 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001649 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001650 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001651 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001652 return -1;
1653 }
1654
1655 if (strcmp(name, "locked") == 0)
1656 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001657 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001658 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001659 PyErr_SET_STRING(PyExc_TypeError,
1660 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001661 return -1;
1662 }
1663 else
1664 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001665 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001666 if (istrue == -1)
1667 return -1;
1668 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001669 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001670 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001671 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001672 }
1673 return 0;
1674 }
1675 else
1676 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001677 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001678 return -1;
1679 }
1680}
1681
1682 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001683DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001684{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001685 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001686}
1687
Bram Moolenaara9922d62013-05-30 13:01:18 +02001688#define DICT_FLAG_HAS_DEFAULT 0x01
1689#define DICT_FLAG_POP 0x02
1690#define DICT_FLAG_NONE_DEFAULT 0x04
1691#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1692#define DICT_FLAG_RETURN_PAIR 0x10
1693
Bram Moolenaardb913952012-06-29 12:54:53 +02001694 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001695_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001696{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697 PyObject *keyObject;
1698 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001699 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001700 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001701 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702 dict_T *dict = self->dict;
1703 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001704 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001705
Bram Moolenaara9922d62013-05-30 13:01:18 +02001706 if (flags & DICT_FLAG_HAS_DEFAULT)
1707 {
1708 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1709 return NULL;
1710 }
1711 else
1712 keyObject = args;
1713
1714 if (flags & DICT_FLAG_RETURN_BOOL)
1715 defObject = Py_False;
1716
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001717 if (!(key = StringToChars(keyObject, &todecref)))
1718 return NULL;
1719
1720 if (*key == NUL)
1721 {
1722 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001723 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001724 return NULL;
1725 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001726
Bram Moolenaara9922d62013-05-30 13:01:18 +02001727 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001728
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001729 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001730
Bram Moolenaara9922d62013-05-30 13:01:18 +02001731 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001732 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001733 if (defObject)
1734 {
1735 Py_INCREF(defObject);
1736 return defObject;
1737 }
1738 else
1739 {
1740 PyErr_SetObject(PyExc_KeyError, keyObject);
1741 return NULL;
1742 }
1743 }
1744 else if (flags & DICT_FLAG_RETURN_BOOL)
1745 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001746 ret = Py_True;
1747 Py_INCREF(ret);
1748 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001749 }
1750
1751 di = dict_lookup(hi);
1752
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001753 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001754 return NULL;
1755
1756 if (flags & DICT_FLAG_POP)
1757 {
1758 if (dict->dv_lock)
1759 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001760 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001761 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001762 return NULL;
1763 }
1764
1765 hash_remove(&dict->dv_hashtab, hi);
1766 dictitem_free(di);
1767 }
1768
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001769 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001770}
1771
1772 static PyObject *
1773DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1774{
1775 return _DictionaryItem(self, keyObject, 0);
1776}
1777
1778 static int
1779DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1780{
1781 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001782 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001783
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001784 if (rObj == NULL)
1785 return -1;
1786
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001787 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788
Bram Moolenaardee2e312013-06-23 16:35:47 +02001789 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001790
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001791 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792}
1793
1794typedef struct
1795{
1796 hashitem_T *ht_array;
1797 long_u ht_used;
1798 hashtab_T *ht;
1799 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001800 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001801} dictiterinfo_T;
1802
1803 static PyObject *
1804DictionaryIterNext(dictiterinfo_T **dii)
1805{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001806 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001807
1808 if (!(*dii)->todo)
1809 return NULL;
1810
1811 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1812 (*dii)->ht->ht_used != (*dii)->ht_used)
1813 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001814 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001815 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001816 return NULL;
1817 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001818
Bram Moolenaara9922d62013-05-30 13:01:18 +02001819 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1820 ++((*dii)->hi);
1821
1822 --((*dii)->todo);
1823
Bram Moolenaar41009372013-07-01 22:03:04 +02001824 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001825 return NULL;
1826
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001827 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001828}
1829
1830 static PyObject *
1831DictionaryIter(DictionaryObject *self)
1832{
1833 dictiterinfo_T *dii;
1834 hashtab_T *ht;
1835
1836 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1837 {
1838 PyErr_NoMemory();
1839 return NULL;
1840 }
1841
1842 ht = &self->dict->dv_hashtab;
1843 dii->ht_array = ht->ht_array;
1844 dii->ht_used = ht->ht_used;
1845 dii->ht = ht;
1846 dii->hi = dii->ht_array;
1847 dii->todo = dii->ht_used;
1848
1849 return IterNew(dii,
1850 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1851 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001852}
1853
1854 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001855DictionaryAssItem(
1856 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001857{
1858 char_u *key;
1859 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001860 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001861 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001862 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001863
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001864 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001865 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001866 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001867 return -1;
1868 }
1869
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001870 if (!(key = StringToChars(keyObject, &todecref)))
1871 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001872
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001873 if (*key == NUL)
1874 {
1875 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001876 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001877 return -1;
1878 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001879
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001880 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001881
1882 if (valObject == NULL)
1883 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001884 hashitem_T *hi;
1885
Bram Moolenaardb913952012-06-29 12:54:53 +02001886 if (di == NULL)
1887 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001888 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001889 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001890 return -1;
1891 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001892 hi = hash_find(&dict->dv_hashtab, di->di_key);
1893 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001894 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001895 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001896 return 0;
1897 }
1898
1899 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001900 {
1901 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001902 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001903 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001904
1905 if (di == NULL)
1906 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001907 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001908 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001909 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001910 PyErr_NoMemory();
1911 return -1;
1912 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001913 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001914
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001915 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001916 {
1917 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001918 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001919 RAISE_KEY_ADD_FAIL(key);
1920 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001921 return -1;
1922 }
1923 }
1924 else
1925 clear_tv(&di->di_tv);
1926
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001927 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001928
1929 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001930 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 return 0;
1932}
1933
Bram Moolenaara9922d62013-05-30 13:01:18 +02001934typedef PyObject *(*hi_to_py)(hashitem_T *);
1935
Bram Moolenaardb913952012-06-29 12:54:53 +02001936 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001937DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001938{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001939 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001940 long_u todo = dict->dv_hashtab.ht_used;
1941 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001942 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001943 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001944 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001945
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001946 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001947 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1948 {
1949 if (!HASHITEM_EMPTY(hi))
1950 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001951 if (!(newObj = hiconvert(hi)))
1952 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001953 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001954 return NULL;
1955 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001956 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001957 --todo;
1958 ++i;
1959 }
1960 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001961 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001962}
1963
Bram Moolenaara9922d62013-05-30 13:01:18 +02001964 static PyObject *
1965dict_key(hashitem_T *hi)
1966{
1967 return PyBytes_FromString((char *)(hi->hi_key));
1968}
1969
1970 static PyObject *
1971DictionaryListKeys(DictionaryObject *self)
1972{
1973 return DictionaryListObjects(self, dict_key);
1974}
1975
1976 static PyObject *
1977dict_val(hashitem_T *hi)
1978{
1979 dictitem_T *di;
1980
1981 di = dict_lookup(hi);
1982 return ConvertToPyObject(&di->di_tv);
1983}
1984
1985 static PyObject *
1986DictionaryListValues(DictionaryObject *self)
1987{
1988 return DictionaryListObjects(self, dict_val);
1989}
1990
1991 static PyObject *
1992dict_item(hashitem_T *hi)
1993{
1994 PyObject *keyObject;
1995 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001996 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001997
1998 if (!(keyObject = dict_key(hi)))
1999 return NULL;
2000
2001 if (!(valObject = dict_val(hi)))
2002 {
2003 Py_DECREF(keyObject);
2004 return NULL;
2005 }
2006
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002007 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002008
2009 Py_DECREF(keyObject);
2010 Py_DECREF(valObject);
2011
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002012 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002013}
2014
2015 static PyObject *
2016DictionaryListItems(DictionaryObject *self)
2017{
2018 return DictionaryListObjects(self, dict_item);
2019}
2020
2021 static PyObject *
2022DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2023{
2024 dict_T *dict = self->dict;
2025
2026 if (dict->dv_lock)
2027 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002028 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002029 return NULL;
2030 }
2031
2032 if (kwargs)
2033 {
2034 typval_T tv;
2035
2036 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2037 return NULL;
2038
2039 VimTryStart();
2040 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2041 clear_tv(&tv);
2042 if (VimTryEnd())
2043 return NULL;
2044 }
2045 else
2046 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002047 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002048
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002049 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002050 return NULL;
2051
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002052 if (obj == NULL)
2053 {
2054 Py_INCREF(Py_None);
2055 return Py_None;
2056 }
2057
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002058 if (PyObject_HasAttrString(obj, "keys"))
2059 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002060 else
2061 {
2062 PyObject *iterator;
2063 PyObject *item;
2064
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002065 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002066 return NULL;
2067
2068 while ((item = PyIter_Next(iterator)))
2069 {
2070 PyObject *fast;
2071 PyObject *keyObject;
2072 PyObject *valObject;
2073 PyObject *todecref;
2074 char_u *key;
2075 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002076 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002077
2078 if (!(fast = PySequence_Fast(item, "")))
2079 {
2080 Py_DECREF(iterator);
2081 Py_DECREF(item);
2082 return NULL;
2083 }
2084
2085 Py_DECREF(item);
2086
2087 if (PySequence_Fast_GET_SIZE(fast) != 2)
2088 {
2089 Py_DECREF(iterator);
2090 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002091 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002092 N_("expected sequence element of size 2, "
2093 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002094 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002095 return NULL;
2096 }
2097
2098 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2099
2100 if (!(key = StringToChars(keyObject, &todecref)))
2101 {
2102 Py_DECREF(iterator);
2103 Py_DECREF(fast);
2104 return NULL;
2105 }
2106
2107 di = dictitem_alloc(key);
2108
2109 Py_XDECREF(todecref);
2110
2111 if (di == NULL)
2112 {
2113 Py_DECREF(fast);
2114 Py_DECREF(iterator);
2115 PyErr_NoMemory();
2116 return NULL;
2117 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002118 di->di_tv.v_type = VAR_UNKNOWN;
2119
2120 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2121
2122 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2123 {
2124 Py_DECREF(iterator);
2125 Py_DECREF(fast);
2126 dictitem_free(di);
2127 return NULL;
2128 }
2129
2130 Py_DECREF(fast);
2131
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002132 hi = hash_find(&dict->dv_hashtab, di->di_key);
2133 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002134 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002135 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002136 Py_DECREF(iterator);
2137 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002138 return NULL;
2139 }
2140 }
2141
2142 Py_DECREF(iterator);
2143
2144 /* Iterator may have finished due to an exception */
2145 if (PyErr_Occurred())
2146 return NULL;
2147 }
2148 }
2149 Py_INCREF(Py_None);
2150 return Py_None;
2151}
2152
2153 static PyObject *
2154DictionaryGet(DictionaryObject *self, PyObject *args)
2155{
2156 return _DictionaryItem(self, args,
2157 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2158}
2159
2160 static PyObject *
2161DictionaryPop(DictionaryObject *self, PyObject *args)
2162{
2163 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2164}
2165
2166 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002167DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002168{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002169 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002170 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002171 PyObject *valObject;
2172 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002173
Bram Moolenaarde71b562013-06-02 17:41:54 +02002174 if (self->dict->dv_hashtab.ht_used == 0)
2175 {
2176 PyErr_SetNone(PyExc_KeyError);
2177 return NULL;
2178 }
2179
2180 hi = self->dict->dv_hashtab.ht_array;
2181 while (HASHITEM_EMPTY(hi))
2182 ++hi;
2183
2184 di = dict_lookup(hi);
2185
2186 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002187 return NULL;
2188
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002189 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002190 {
2191 Py_DECREF(valObject);
2192 return NULL;
2193 }
2194
2195 hash_remove(&self->dict->dv_hashtab, hi);
2196 dictitem_free(di);
2197
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002198 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002199}
2200
2201 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002202DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002203{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002204 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2205}
2206
2207static PySequenceMethods DictionaryAsSeq = {
2208 0, /* sq_length */
2209 0, /* sq_concat */
2210 0, /* sq_repeat */
2211 0, /* sq_item */
2212 0, /* sq_slice */
2213 0, /* sq_ass_item */
2214 0, /* sq_ass_slice */
2215 (objobjproc) DictionaryContains, /* sq_contains */
2216 0, /* sq_inplace_concat */
2217 0, /* sq_inplace_repeat */
2218};
2219
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002220static PyMappingMethods DictionaryAsMapping = {
2221 (lenfunc) DictionaryLength,
2222 (binaryfunc) DictionaryItem,
2223 (objobjargproc) DictionaryAssItem,
2224};
2225
Bram Moolenaardb913952012-06-29 12:54:53 +02002226static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002227 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002228 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2229 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2230 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2231 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2232 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002233 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002234 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002235 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2236 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002237};
2238
2239static PyTypeObject ListType;
2240
2241typedef struct
2242{
2243 PyObject_HEAD
2244 list_T *list;
2245 pylinkedlist_T ref;
2246} ListObject;
2247
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002248#define NEW_LIST(list) ListNew(&ListType, list)
2249
Bram Moolenaardb913952012-06-29 12:54:53 +02002250 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002251ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002252{
2253 ListObject *self;
2254
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002255 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002256 if (self == NULL)
2257 return NULL;
2258 self->list = list;
2259 ++list->lv_refcount;
2260
2261 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2262
2263 return (PyObject *)(self);
2264}
2265
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002266 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002267py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002268{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002269 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002270
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002271 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002272 {
2273 PyErr_NoMemory();
2274 return NULL;
2275 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002276 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002277
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002278 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002279}
2280
2281 static int
2282list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2283{
2284 PyObject *iterator;
2285 PyObject *item;
2286 listitem_T *li;
2287
2288 if (!(iterator = PyObject_GetIter(obj)))
2289 return -1;
2290
2291 while ((item = PyIter_Next(iterator)))
2292 {
2293 if (!(li = listitem_alloc()))
2294 {
2295 PyErr_NoMemory();
2296 Py_DECREF(item);
2297 Py_DECREF(iterator);
2298 return -1;
2299 }
2300 li->li_tv.v_lock = 0;
2301 li->li_tv.v_type = VAR_UNKNOWN;
2302
2303 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2304 {
2305 Py_DECREF(item);
2306 Py_DECREF(iterator);
2307 listitem_free(li);
2308 return -1;
2309 }
2310
2311 Py_DECREF(item);
2312
2313 list_append(l, li);
2314 }
2315
2316 Py_DECREF(iterator);
2317
2318 /* Iterator may have finished due to an exception */
2319 if (PyErr_Occurred())
2320 return -1;
2321
2322 return 0;
2323}
2324
2325 static PyObject *
2326ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2327{
2328 list_T *list;
2329 PyObject *obj = NULL;
2330
2331 if (kwargs)
2332 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002333 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002334 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002335 return NULL;
2336 }
2337
2338 if (!PyArg_ParseTuple(args, "|O", &obj))
2339 return NULL;
2340
2341 if (!(list = py_list_alloc()))
2342 return NULL;
2343
2344 if (obj)
2345 {
2346 PyObject *lookup_dict;
2347
2348 if (!(lookup_dict = PyDict_New()))
2349 {
2350 list_unref(list);
2351 return NULL;
2352 }
2353
2354 if (list_py_concat(list, obj, lookup_dict) == -1)
2355 {
2356 Py_DECREF(lookup_dict);
2357 list_unref(list);
2358 return NULL;
2359 }
2360
2361 Py_DECREF(lookup_dict);
2362 }
2363
2364 return ListNew(subtype, list);
2365}
2366
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002367 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002368ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002369{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002370 pyll_remove(&self->ref, &lastlist);
2371 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002372
2373 DESTRUCTOR_FINISH(self);
2374}
2375
Bram Moolenaardb913952012-06-29 12:54:53 +02002376 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002377ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002378{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002379 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002380}
2381
2382 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002383ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002384{
2385 listitem_T *li;
2386
Bram Moolenaard6e39182013-05-21 18:30:34 +02002387 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002388 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002389 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002390 return NULL;
2391 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002392 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002393 if (li == NULL)
2394 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002395 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002396 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002397 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 return NULL;
2399 }
2400 return ConvertToPyObject(&li->li_tv);
2401}
2402
Bram Moolenaardb913952012-06-29 12:54:53 +02002403 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002404ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2405 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002406{
2407 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002409
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002410 if (step == 0)
2411 {
2412 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2413 return NULL;
2414 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002415
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002416 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002417 if (list == NULL)
2418 return NULL;
2419
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002420 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002421 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002422 PyObject *item;
2423
2424 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002425 if (item == NULL)
2426 {
2427 Py_DECREF(list);
2428 return NULL;
2429 }
2430
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002431 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002432 }
2433
2434 return list;
2435}
2436
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002437 static PyObject *
2438ListItem(ListObject *self, PyObject* idx)
2439{
2440#if PY_MAJOR_VERSION < 3
2441 if (PyInt_Check(idx))
2442 {
2443 long _idx = PyInt_AsLong(idx);
2444 return ListIndex(self, _idx);
2445 }
2446 else
2447#endif
2448 if (PyLong_Check(idx))
2449 {
2450 long _idx = PyLong_AsLong(idx);
2451 return ListIndex(self, _idx);
2452 }
2453 else if (PySlice_Check(idx))
2454 {
2455 Py_ssize_t start, stop, step, slicelen;
2456
Bram Moolenaar922a4662014-03-30 16:11:43 +02002457 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002458 &start, &stop, &step, &slicelen) < 0)
2459 return NULL;
2460 return ListSlice(self, start, step, slicelen);
2461 }
2462 else
2463 {
2464 RAISE_INVALID_INDEX_TYPE(idx);
2465 return NULL;
2466 }
2467}
2468
2469 static void
2470list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2471 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2472{
2473 while (numreplaced--)
2474 {
2475 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2476 listitem_remove(l, lis[slicelen + numreplaced]);
2477 }
2478 while (numadded--)
2479 {
2480 listitem_T *next;
2481
2482 next = lastaddedli->li_prev;
2483 listitem_remove(l, lastaddedli);
2484 lastaddedli = next;
2485 }
2486}
2487
2488 static int
2489ListAssSlice(ListObject *self, Py_ssize_t first,
2490 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2491{
2492 PyObject *iterator;
2493 PyObject *item;
2494 listitem_T *li;
2495 listitem_T *lastaddedli = NULL;
2496 listitem_T *next;
2497 typval_T v;
2498 list_T *l = self->list;
2499 PyInt i;
2500 PyInt j;
2501 PyInt numreplaced = 0;
2502 PyInt numadded = 0;
2503 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002504 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002505
2506 size = ListLength(self);
2507
2508 if (l->lv_lock)
2509 {
2510 RAISE_LOCKED_LIST;
2511 return -1;
2512 }
2513
2514 if (step == 0)
2515 {
2516 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2517 return -1;
2518 }
2519
2520 if (step != 1 && slicelen == 0)
2521 {
2522 /* Nothing to do. Only error out if obj has some items. */
2523 int ret = 0;
2524
2525 if (obj == NULL)
2526 return 0;
2527
2528 if (!(iterator = PyObject_GetIter(obj)))
2529 return -1;
2530
2531 if ((item = PyIter_Next(iterator)))
2532 {
2533 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002534 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002535 "to extended slice"), 0);
2536 Py_DECREF(item);
2537 ret = -1;
2538 }
2539 Py_DECREF(iterator);
2540 return ret;
2541 }
2542
2543 if (obj != NULL)
2544 /* XXX May allocate zero bytes. */
2545 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2546 {
2547 PyErr_NoMemory();
2548 return -1;
2549 }
2550
2551 if (first == size)
2552 li = NULL;
2553 else
2554 {
2555 li = list_find(l, (long) first);
2556 if (li == NULL)
2557 {
2558 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2559 (int)first);
2560 if (obj != NULL)
2561 PyMem_Free(lis);
2562 return -1;
2563 }
2564 i = slicelen;
2565 while (i-- && li != NULL)
2566 {
2567 j = step;
2568 next = li;
2569 if (step > 0)
2570 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2571 else
2572 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2573
2574 if (obj == NULL)
2575 listitem_remove(l, li);
2576 else
2577 lis[slicelen - i - 1] = li;
2578
2579 li = next;
2580 }
2581 if (li == NULL && i != -1)
2582 {
2583 PyErr_SET_VIM(N_("internal error: not enough list items"));
2584 if (obj != NULL)
2585 PyMem_Free(lis);
2586 return -1;
2587 }
2588 }
2589
2590 if (obj == NULL)
2591 return 0;
2592
2593 if (!(iterator = PyObject_GetIter(obj)))
2594 {
2595 PyMem_Free(lis);
2596 return -1;
2597 }
2598
2599 i = 0;
2600 while ((item = PyIter_Next(iterator)))
2601 {
2602 if (ConvertFromPyObject(item, &v) == -1)
2603 {
2604 Py_DECREF(iterator);
2605 Py_DECREF(item);
2606 PyMem_Free(lis);
2607 return -1;
2608 }
2609 Py_DECREF(item);
2610 if (list_insert_tv(l, &v, numreplaced < slicelen
2611 ? lis[numreplaced]
2612 : li) == FAIL)
2613 {
2614 clear_tv(&v);
2615 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2616 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2617 PyMem_Free(lis);
2618 return -1;
2619 }
2620 if (numreplaced < slicelen)
2621 {
2622 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002623 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002624 numreplaced++;
2625 }
2626 else
2627 {
2628 if (li)
2629 lastaddedli = li->li_prev;
2630 else
2631 lastaddedli = l->lv_last;
2632 numadded++;
2633 }
2634 clear_tv(&v);
2635 if (step != 1 && i >= slicelen)
2636 {
2637 Py_DECREF(iterator);
2638 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002639 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002640 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002641 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2642 PyMem_Free(lis);
2643 return -1;
2644 }
2645 ++i;
2646 }
2647 Py_DECREF(iterator);
2648
2649 if (step != 1 && i != slicelen)
2650 {
2651 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002652 N_("attempt to assign sequence of size %d to extended slice "
2653 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002654 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2655 PyMem_Free(lis);
2656 return -1;
2657 }
2658
2659 if (PyErr_Occurred())
2660 {
2661 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2662 PyMem_Free(lis);
2663 return -1;
2664 }
2665
2666 for (i = 0; i < numreplaced; i++)
2667 listitem_free(lis[i]);
2668 if (step == 1)
2669 for (i = numreplaced; i < slicelen; i++)
2670 listitem_remove(l, lis[i]);
2671
2672 PyMem_Free(lis);
2673
2674 return 0;
2675}
2676
2677 static int
2678ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2679{
2680 typval_T tv;
2681 list_T *l = self->list;
2682 listitem_T *li;
2683 Py_ssize_t length = ListLength(self);
2684
2685 if (l->lv_lock)
2686 {
2687 RAISE_LOCKED_LIST;
2688 return -1;
2689 }
2690 if (index > length || (index == length && obj == NULL))
2691 {
2692 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2693 return -1;
2694 }
2695
2696 if (obj == NULL)
2697 {
2698 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002699 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002700 clear_tv(&li->li_tv);
2701 vim_free(li);
2702 return 0;
2703 }
2704
2705 if (ConvertFromPyObject(obj, &tv) == -1)
2706 return -1;
2707
2708 if (index == length)
2709 {
2710 if (list_append_tv(l, &tv) == FAIL)
2711 {
2712 clear_tv(&tv);
2713 PyErr_SET_VIM(N_("failed to add item to list"));
2714 return -1;
2715 }
2716 }
2717 else
2718 {
2719 li = list_find(l, (long) index);
2720 clear_tv(&li->li_tv);
2721 copy_tv(&tv, &li->li_tv);
2722 clear_tv(&tv);
2723 }
2724 return 0;
2725}
2726
2727 static Py_ssize_t
2728ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2729{
2730#if PY_MAJOR_VERSION < 3
2731 if (PyInt_Check(idx))
2732 {
2733 long _idx = PyInt_AsLong(idx);
2734 return ListAssIndex(self, _idx, obj);
2735 }
2736 else
2737#endif
2738 if (PyLong_Check(idx))
2739 {
2740 long _idx = PyLong_AsLong(idx);
2741 return ListAssIndex(self, _idx, obj);
2742 }
2743 else if (PySlice_Check(idx))
2744 {
2745 Py_ssize_t start, stop, step, slicelen;
2746
Bram Moolenaar922a4662014-03-30 16:11:43 +02002747 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002748 &start, &stop, &step, &slicelen) < 0)
2749 return -1;
2750 return ListAssSlice(self, start, step, slicelen,
2751 obj);
2752 }
2753 else
2754 {
2755 RAISE_INVALID_INDEX_TYPE(idx);
2756 return -1;
2757 }
2758}
2759
2760 static PyObject *
2761ListConcatInPlace(ListObject *self, PyObject *obj)
2762{
2763 list_T *l = self->list;
2764 PyObject *lookup_dict;
2765
2766 if (l->lv_lock)
2767 {
2768 RAISE_LOCKED_LIST;
2769 return NULL;
2770 }
2771
2772 if (!(lookup_dict = PyDict_New()))
2773 return NULL;
2774
2775 if (list_py_concat(l, obj, lookup_dict) == -1)
2776 {
2777 Py_DECREF(lookup_dict);
2778 return NULL;
2779 }
2780 Py_DECREF(lookup_dict);
2781
2782 Py_INCREF(self);
2783 return (PyObject *)(self);
2784}
2785
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002786typedef struct
2787{
2788 listwatch_T lw;
2789 list_T *list;
2790} listiterinfo_T;
2791
2792 static void
2793ListIterDestruct(listiterinfo_T *lii)
2794{
2795 list_rem_watch(lii->list, &lii->lw);
2796 PyMem_Free(lii);
2797}
2798
2799 static PyObject *
2800ListIterNext(listiterinfo_T **lii)
2801{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002802 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002803
2804 if (!((*lii)->lw.lw_item))
2805 return NULL;
2806
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002807 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002808 return NULL;
2809
2810 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2811
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002812 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002813}
2814
2815 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002816ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002817{
2818 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002819 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002820
2821 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2822 {
2823 PyErr_NoMemory();
2824 return NULL;
2825 }
2826
2827 list_add_watch(l, &lii->lw);
2828 lii->lw.lw_item = l->lv_first;
2829 lii->list = l;
2830
2831 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002832 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2833 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002834}
2835
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002836static char *ListAttrs[] = {
2837 "locked",
2838 NULL
2839};
2840
2841 static PyObject *
2842ListDir(PyObject *self)
2843{
2844 return ObjectDir(self, ListAttrs);
2845}
2846
Bram Moolenaar66b79852012-09-21 14:00:35 +02002847 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002848ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002849{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002850 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002851 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002852 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002853 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002854 return -1;
2855 }
2856
2857 if (strcmp(name, "locked") == 0)
2858 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002859 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002860 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002861 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002862 return -1;
2863 }
2864 else
2865 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002866 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002867 if (istrue == -1)
2868 return -1;
2869 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002870 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002871 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002872 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002873 }
2874 return 0;
2875 }
2876 else
2877 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002878 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002879 return -1;
2880 }
2881}
2882
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002883static PySequenceMethods ListAsSeq = {
2884 (lenfunc) ListLength, /* sq_length, len(x) */
2885 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2886 0, /* RangeRepeat, sq_repeat, x*n */
2887 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2888 0, /* was_sq_slice, x[i:j] */
2889 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2890 0, /* was_sq_ass_slice, x[i:j]=v */
2891 0, /* sq_contains */
2892 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2893 0, /* sq_inplace_repeat */
2894};
2895
2896static PyMappingMethods ListAsMapping = {
2897 /* mp_length */ (lenfunc) ListLength,
2898 /* mp_subscript */ (binaryfunc) ListItem,
2899 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2900};
2901
Bram Moolenaardb913952012-06-29 12:54:53 +02002902static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002903 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2904 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2905 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002906};
2907
2908typedef struct
2909{
2910 PyObject_HEAD
2911 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002912 int argc;
2913 typval_T *argv;
2914 dict_T *self;
2915 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002916 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002917} FunctionObject;
2918
2919static PyTypeObject FunctionType;
2920
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002921#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2922 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002923
Bram Moolenaardb913952012-06-29 12:54:53 +02002924 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002925FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002926 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002927{
2928 FunctionObject *self;
2929
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002930 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002931 if (self == NULL)
2932 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002933
2934 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002935 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002936 if (!translated_function_exists(name))
2937 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002938 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002939 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002940 return NULL;
2941 }
2942 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002943 }
2944 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002945 {
2946 char_u *p;
2947
2948 if ((p = get_expanded_name(name,
2949 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002950 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002951 PyErr_FORMAT(PyExc_ValueError,
2952 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002953 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002954 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002955
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002956 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2957 {
2958 char_u *np;
2959 size_t len = STRLEN(p) + 1;
2960
Bram Moolenaar51e14382019-05-25 20:21:28 +02002961 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002962 {
2963 vim_free(p);
2964 return NULL;
2965 }
2966 mch_memmove(np, "<SNR>", 5);
2967 mch_memmove(np + 5, p + 3, len - 3);
2968 vim_free(p);
2969 self->name = np;
2970 }
2971 else
2972 self->name = p;
2973 }
2974
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002975 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002976 self->argc = argc;
2977 self->argv = argv;
2978 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002979 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002980
2981 if (self->argv || self->self)
2982 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2983
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002984 return (PyObject *)(self);
2985}
2986
2987 static PyObject *
2988FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2989{
2990 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002991 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002992 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002993 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002994 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002995 typval_T selfdicttv;
2996 typval_T argstv;
2997 list_T *argslist = NULL;
2998 dict_T *selfdict = NULL;
2999 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003000 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003001 typval_T *argv = NULL;
3002 typval_T *curtv;
3003 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003004
Bram Moolenaar8110a092016-04-14 15:56:09 +02003005 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003006 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003007 selfdictObject = PyDict_GetItemString(kwargs, "self");
3008 if (selfdictObject != NULL)
3009 {
3010 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3011 return NULL;
3012 selfdict = selfdicttv.vval.v_dict;
3013 }
3014 argsObject = PyDict_GetItemString(kwargs, "args");
3015 if (argsObject != NULL)
3016 {
3017 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3018 {
3019 dict_unref(selfdict);
3020 return NULL;
3021 }
3022 argslist = argstv.vval.v_list;
3023
3024 argc = argslist->lv_len;
3025 if (argc != 0)
3026 {
3027 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003028 if (argv == NULL)
3029 {
3030 PyErr_NoMemory();
3031 dict_unref(selfdict);
3032 list_unref(argslist);
3033 return NULL;
3034 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003035 curtv = argv;
3036 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3037 copy_tv(&li->li_tv, curtv++);
3038 }
3039 list_unref(argslist);
3040 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003041 if (selfdict != NULL)
3042 {
3043 auto_rebind = FALSE;
3044 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3045 if (autoRebindObject != NULL)
3046 {
3047 auto_rebind = PyObject_IsTrue(autoRebindObject);
3048 if (auto_rebind == -1)
3049 {
3050 dict_unref(selfdict);
3051 list_unref(argslist);
3052 return NULL;
3053 }
3054 }
3055 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003056 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003057
Bram Moolenaar389a1792013-06-23 13:00:44 +02003058 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003059 {
3060 dict_unref(selfdict);
3061 while (argc--)
3062 clear_tv(&argv[argc]);
3063 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003064 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003065 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003066
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003067 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003068
Bram Moolenaar389a1792013-06-23 13:00:44 +02003069 PyMem_Free(name);
3070
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003071 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003072}
3073
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003074 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003075FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003076{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003077 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003078 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003079 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003080 for (i = 0; i < self->argc; ++i)
3081 clear_tv(&self->argv[i]);
3082 PyMem_Free(self->argv);
3083 dict_unref(self->self);
3084 if (self->argv || self->self)
3085 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003086
3087 DESTRUCTOR_FINISH(self);
3088}
3089
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003090static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003091 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003092 NULL
3093};
3094
3095 static PyObject *
3096FunctionDir(PyObject *self)
3097{
3098 return ObjectDir(self, FunctionAttrs);
3099}
3100
Bram Moolenaardb913952012-06-29 12:54:53 +02003101 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003102FunctionAttr(FunctionObject *self, char *name)
3103{
3104 list_T *list;
3105 int i;
3106 if (strcmp(name, "name") == 0)
3107 return PyString_FromString((char *)(self->name));
3108 else if (strcmp(name, "args") == 0)
3109 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003110 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003111 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003112
Bram Moolenaar8110a092016-04-14 15:56:09 +02003113 for (i = 0; i < self->argc; ++i)
3114 list_append_tv(list, &self->argv[i]);
3115 return NEW_LIST(list);
3116 }
3117 else if (strcmp(name, "self") == 0)
3118 return self->self == NULL
3119 ? AlwaysNone(NULL)
3120 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003121 else if (strcmp(name, "auto_rebind") == 0)
3122 return self->auto_rebind
3123 ? AlwaysTrue(NULL)
3124 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003125 else if (strcmp(name, "__members__") == 0)
3126 return ObjectDir(NULL, FunctionAttrs);
3127 return NULL;
3128}
3129
3130/* Populate partial_T given function object.
3131 *
3132 * "exported" should be set to true when it is needed to construct a partial
3133 * that may be stored in a variable (i.e. may be freed by Vim).
3134 */
3135 static void
3136set_partial(FunctionObject *self, partial_T *pt, int exported)
3137{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003138 int i;
3139
3140 pt->pt_name = self->name;
3141 if (self->argv)
3142 {
3143 pt->pt_argc = self->argc;
3144 if (exported)
3145 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003146 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003147 for (i = 0; i < pt->pt_argc; ++i)
3148 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3149 }
3150 else
3151 pt->pt_argv = self->argv;
3152 }
3153 else
3154 {
3155 pt->pt_argc = 0;
3156 pt->pt_argv = NULL;
3157 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003158 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003159 pt->pt_dict = self->self;
3160 if (exported && self->self)
3161 ++pt->pt_dict->dv_refcount;
3162 if (exported)
3163 pt->pt_name = vim_strsave(pt->pt_name);
3164 pt->pt_refcount = 1;
3165}
3166
3167 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003168FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003169{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003170 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003171 typval_T args;
3172 typval_T selfdicttv;
3173 typval_T rettv;
3174 dict_T *selfdict = NULL;
3175 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003176 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003177 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003178 partial_T pt;
3179 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003180
Bram Moolenaar8110a092016-04-14 15:56:09 +02003181 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003182 return NULL;
3183
3184 if (kwargs != NULL)
3185 {
3186 selfdictObject = PyDict_GetItemString(kwargs, "self");
3187 if (selfdictObject != NULL)
3188 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003189 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003190 {
3191 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003192 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003193 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003194 selfdict = selfdicttv.vval.v_dict;
3195 }
3196 }
3197
Bram Moolenaar8110a092016-04-14 15:56:09 +02003198 if (self->argv || self->self)
3199 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003200 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003201 set_partial(self, &pt, FALSE);
3202 pt_ptr = &pt;
3203 }
3204
Bram Moolenaar71700b82013-05-15 17:49:05 +02003205 Py_BEGIN_ALLOW_THREADS
3206 Python_Lock_Vim();
3207
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003208 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003209 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003210
3211 Python_Release_Vim();
3212 Py_END_ALLOW_THREADS
3213
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003214 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003215 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003216 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003217 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003218 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003219 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003220 }
3221 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003222 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003223
Bram Moolenaardb913952012-06-29 12:54:53 +02003224 clear_tv(&args);
3225 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003226 if (selfdict != NULL)
3227 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003228
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003229 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003230}
3231
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003232 static PyObject *
3233FunctionRepr(FunctionObject *self)
3234{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003235 PyObject *ret;
3236 garray_T repr_ga;
3237 int i;
3238 char_u *tofree = NULL;
3239 typval_T tv;
3240 char_u numbuf[NUMBUFLEN];
3241
3242 ga_init2(&repr_ga, (int)sizeof(char), 70);
3243 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3244 if (self->name)
3245 ga_concat(&repr_ga, self->name);
3246 else
3247 ga_concat(&repr_ga, (char_u *)"<NULL>");
3248 ga_append(&repr_ga, '\'');
3249 if (self->argv)
3250 {
3251 ga_concat(&repr_ga, (char_u *)", args=[");
3252 ++emsg_silent;
3253 for (i = 0; i < self->argc; i++)
3254 {
3255 if (i != 0)
3256 ga_concat(&repr_ga, (char_u *)", ");
3257 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3258 get_copyID()));
3259 vim_free(tofree);
3260 }
3261 --emsg_silent;
3262 ga_append(&repr_ga, ']');
3263 }
3264 if (self->self)
3265 {
3266 ga_concat(&repr_ga, (char_u *)", self=");
3267 tv.v_type = VAR_DICT;
3268 tv.vval.v_dict = self->self;
3269 ++emsg_silent;
3270 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3271 --emsg_silent;
3272 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003273 if (self->auto_rebind)
3274 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003275 }
3276 ga_append(&repr_ga, '>');
3277 ret = PyString_FromString((char *)repr_ga.ga_data);
3278 ga_clear(&repr_ga);
3279 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003280}
3281
Bram Moolenaardb913952012-06-29 12:54:53 +02003282static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003283 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3284 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003285};
3286
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003287/*
3288 * Options object
3289 */
3290
3291static PyTypeObject OptionsType;
3292
3293typedef int (*checkfun)(void *);
3294
3295typedef struct
3296{
3297 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003298 int opt_type;
3299 void *from;
3300 checkfun Check;
3301 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003302} OptionsObject;
3303
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003304 static int
3305dummy_check(void *arg UNUSED)
3306{
3307 return 0;
3308}
3309
3310 static PyObject *
3311OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3312{
3313 OptionsObject *self;
3314
Bram Moolenaar774267b2013-05-21 20:51:59 +02003315 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003316 if (self == NULL)
3317 return NULL;
3318
3319 self->opt_type = opt_type;
3320 self->from = from;
3321 self->Check = Check;
3322 self->fromObj = fromObj;
3323 if (fromObj)
3324 Py_INCREF(fromObj);
3325
3326 return (PyObject *)(self);
3327}
3328
3329 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003330OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003331{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003332 PyObject_GC_UnTrack((void *)(self));
3333 Py_XDECREF(self->fromObj);
3334 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003335}
3336
3337 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003338OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003339{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003340 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341 return 0;
3342}
3343
3344 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003345OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003346{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003347 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003348 return 0;
3349}
3350
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003351 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003352OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003353{
3354 char_u *key;
3355 int flags;
3356 long numval;
3357 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003358 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003359
Bram Moolenaard6e39182013-05-21 18:30:34 +02003360 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003361 return NULL;
3362
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003363 if (!(key = StringToChars(keyObject, &todecref)))
3364 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003365
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003366 if (*key == NUL)
3367 {
3368 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003369 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003370 return NULL;
3371 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003372
3373 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003374 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003375
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003376 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003377
3378 if (flags == 0)
3379 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003380 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003381 return NULL;
3382 }
3383
3384 if (flags & SOPT_UNSET)
3385 {
3386 Py_INCREF(Py_None);
3387 return Py_None;
3388 }
3389 else if (flags & SOPT_BOOL)
3390 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003391 PyObject *ret;
3392 ret = numval ? Py_True : Py_False;
3393 Py_INCREF(ret);
3394 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003395 }
3396 else if (flags & SOPT_NUM)
3397 return PyInt_FromLong(numval);
3398 else if (flags & SOPT_STRING)
3399 {
3400 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003401 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003402 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003403 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003404 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003405 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003406 else
3407 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003408 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003409 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003410 return NULL;
3411 }
3412 }
3413 else
3414 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003415 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003416 return NULL;
3417 }
3418}
3419
3420 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003421OptionsContains(OptionsObject *self, PyObject *keyObject)
3422{
3423 char_u *key;
3424 PyObject *todecref;
3425
3426 if (!(key = StringToChars(keyObject, &todecref)))
3427 return -1;
3428
3429 if (*key == NUL)
3430 {
3431 Py_XDECREF(todecref);
3432 return 0;
3433 }
3434
3435 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3436 {
3437 Py_XDECREF(todecref);
3438 return 1;
3439 }
3440 else
3441 {
3442 Py_XDECREF(todecref);
3443 return 0;
3444 }
3445}
3446
3447typedef struct
3448{
3449 void *lastoption;
3450 int opt_type;
3451} optiterinfo_T;
3452
3453 static PyObject *
3454OptionsIterNext(optiterinfo_T **oii)
3455{
3456 char_u *name;
3457
3458 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3459 return PyString_FromString((char *)name);
3460
3461 return NULL;
3462}
3463
3464 static PyObject *
3465OptionsIter(OptionsObject *self)
3466{
3467 optiterinfo_T *oii;
3468
3469 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3470 {
3471 PyErr_NoMemory();
3472 return NULL;
3473 }
3474
3475 oii->opt_type = self->opt_type;
3476 oii->lastoption = NULL;
3477
3478 return IterNew(oii,
3479 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3480 NULL, NULL);
3481}
3482
3483 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003484set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003485{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003486 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003487
3488 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3489 {
3490 if (VimTryEnd())
3491 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003492 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003493 return FAIL;
3494 }
3495 return OK;
3496}
3497
3498 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003499set_option_value_for(
3500 char_u *key,
3501 int numval,
3502 char_u *stringval,
3503 int opt_flags,
3504 int opt_type,
3505 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003506{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003507 win_T *save_curwin = NULL;
3508 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003509 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003510 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003511
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003512 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003513 switch (opt_type)
3514 {
3515 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003516 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003517 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003518 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003519 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003520 if (VimTryEnd())
3521 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003522 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003523 return -1;
3524 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003525 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3526 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003527 break;
3528 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003529 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003530 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003531 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003532 break;
3533 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003534 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003535 break;
3536 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003537 if (set_ret == FAIL)
3538 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003539 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540}
3541
3542 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003543OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003544{
3545 char_u *key;
3546 int flags;
3547 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003548 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003549 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003550
Bram Moolenaard6e39182013-05-21 18:30:34 +02003551 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003552 return -1;
3553
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003554 if (!(key = StringToChars(keyObject, &todecref)))
3555 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003556
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003557 if (*key == NUL)
3558 {
3559 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003560 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003561 return -1;
3562 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003563
3564 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003565 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003566
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003567 if (flags == 0)
3568 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003569 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003570 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003571 return -1;
3572 }
3573
3574 if (valObject == NULL)
3575 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003576 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003578 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003579 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003580 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003581 return -1;
3582 }
3583 else if (!(flags & SOPT_GLOBAL))
3584 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003585 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003586 N_("unable to unset option %s "
3587 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003588 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003589 return -1;
3590 }
3591 else
3592 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003593 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003594 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003595 return 0;
3596 }
3597 }
3598
Bram Moolenaard6e39182013-05-21 18:30:34 +02003599 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003600
3601 if (flags & SOPT_BOOL)
3602 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003603 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003604
Bram Moolenaarb983f752013-05-15 16:11:50 +02003605 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003606 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003607 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003608 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003609 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003610 }
3611 else if (flags & SOPT_NUM)
3612 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003613 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003615 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003616 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003617 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003618 return -1;
3619 }
3620
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003621 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003622 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003623 }
3624 else
3625 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003626 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003627 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003628
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003629 if ((val = StringToChars(valObject, &todecref2)))
3630 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003632 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003633 Py_XDECREF(todecref2);
3634 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003635 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003636 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003637 }
3638
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003639 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003640
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003641 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003642}
3643
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003644static PySequenceMethods OptionsAsSeq = {
3645 0, /* sq_length */
3646 0, /* sq_concat */
3647 0, /* sq_repeat */
3648 0, /* sq_item */
3649 0, /* sq_slice */
3650 0, /* sq_ass_item */
3651 0, /* sq_ass_slice */
3652 (objobjproc) OptionsContains, /* sq_contains */
3653 0, /* sq_inplace_concat */
3654 0, /* sq_inplace_repeat */
3655};
3656
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003657static PyMappingMethods OptionsAsMapping = {
3658 (lenfunc) NULL,
3659 (binaryfunc) OptionsItem,
3660 (objobjargproc) OptionsAssItem,
3661};
3662
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003663/* Tabpage object
3664 */
3665
3666typedef struct
3667{
3668 PyObject_HEAD
3669 tabpage_T *tab;
3670} TabPageObject;
3671
3672static PyObject *WinListNew(TabPageObject *tabObject);
3673
3674static PyTypeObject TabPageType;
3675
3676 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003677CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003678{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003679 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003680 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003681 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003682 return -1;
3683 }
3684
3685 return 0;
3686}
3687
3688 static PyObject *
3689TabPageNew(tabpage_T *tab)
3690{
3691 TabPageObject *self;
3692
3693 if (TAB_PYTHON_REF(tab))
3694 {
3695 self = TAB_PYTHON_REF(tab);
3696 Py_INCREF(self);
3697 }
3698 else
3699 {
3700 self = PyObject_NEW(TabPageObject, &TabPageType);
3701 if (self == NULL)
3702 return NULL;
3703 self->tab = tab;
3704 TAB_PYTHON_REF(tab) = self;
3705 }
3706
3707 return (PyObject *)(self);
3708}
3709
3710 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003711TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003712{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003713 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3714 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003715
3716 DESTRUCTOR_FINISH(self);
3717}
3718
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003719static char *TabPageAttrs[] = {
3720 "windows", "number", "vars", "window", "valid",
3721 NULL
3722};
3723
3724 static PyObject *
3725TabPageDir(PyObject *self)
3726{
3727 return ObjectDir(self, TabPageAttrs);
3728}
3729
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003730 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003731TabPageAttrValid(TabPageObject *self, char *name)
3732{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003733 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003734
3735 if (strcmp(name, "valid") != 0)
3736 return NULL;
3737
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003738 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3739 Py_INCREF(ret);
3740 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003741}
3742
3743 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003744TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003745{
3746 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003747 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003748 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003749 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003750 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003751 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003752 else if (strcmp(name, "window") == 0)
3753 {
3754 /* For current tab window.c does not bother to set or update tp_curwin
3755 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003756 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003757 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003758 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003759 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003760 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003761 else if (strcmp(name, "__members__") == 0)
3762 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003763 return NULL;
3764}
3765
3766 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003767TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003768{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003769 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003770 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771 else
3772 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003773 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774
3775 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003776 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3777 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003778 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003779 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003780 }
3781}
3782
3783static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003784 /* name, function, calling, documentation */
3785 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3786 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003787};
3788
3789/*
3790 * Window list object
3791 */
3792
3793static PyTypeObject TabListType;
3794static PySequenceMethods TabListAsSeq;
3795
3796typedef struct
3797{
3798 PyObject_HEAD
3799} TabListObject;
3800
3801 static PyInt
3802TabListLength(PyObject *self UNUSED)
3803{
3804 tabpage_T *tp = first_tabpage;
3805 PyInt n = 0;
3806
3807 while (tp != NULL)
3808 {
3809 ++n;
3810 tp = tp->tp_next;
3811 }
3812
3813 return n;
3814}
3815
3816 static PyObject *
3817TabListItem(PyObject *self UNUSED, PyInt n)
3818{
3819 tabpage_T *tp;
3820
3821 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3822 if (n == 0)
3823 return TabPageNew(tp);
3824
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003825 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003826 return NULL;
3827}
3828
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003829/*
3830 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003831 */
3832
3833typedef struct
3834{
3835 PyObject_HEAD
3836 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003837 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003838} WindowObject;
3839
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003840static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841
3842 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003843CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003845 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003846 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003847 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003848 return -1;
3849 }
3850
3851 return 0;
3852}
3853
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003854 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003855WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003856{
3857 /* We need to handle deletion of windows underneath us.
3858 * If we add a "w_python*_ref" field to the win_T structure,
3859 * then we can get at it in win_free() in vim. We then
3860 * need to create only ONE Python object per window - if
3861 * we try to create a second, just INCREF the existing one
3862 * and return it. The (single) Python object referring to
3863 * the window is stored in "w_python*_ref".
3864 * On a win_free() we set the Python object's win_T* field
3865 * to an invalid value. We trap all uses of a window
3866 * object, and reject them if the win_T* field is invalid.
3867 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003868 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003869 * w_python_ref and w_python3_ref fields respectively.
3870 */
3871
3872 WindowObject *self;
3873
3874 if (WIN_PYTHON_REF(win))
3875 {
3876 self = WIN_PYTHON_REF(win);
3877 Py_INCREF(self);
3878 }
3879 else
3880 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003881 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003882 if (self == NULL)
3883 return NULL;
3884 self->win = win;
3885 WIN_PYTHON_REF(win) = self;
3886 }
3887
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003888 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3889
Bram Moolenaar971db462013-05-12 18:44:48 +02003890 return (PyObject *)(self);
3891}
3892
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003893 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003894WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003895{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003896 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897 if (self->win && self->win != INVALID_WINDOW_VALUE)
3898 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003899 Py_XDECREF(((PyObject *)(self->tabObject)));
3900 PyObject_GC_Del((void *)(self));
3901}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003902
Bram Moolenaar774267b2013-05-21 20:51:59 +02003903 static int
3904WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3905{
3906 Py_VISIT(((PyObject *)(self->tabObject)));
3907 return 0;
3908}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003909
Bram Moolenaar774267b2013-05-21 20:51:59 +02003910 static int
3911WindowClear(WindowObject *self)
3912{
3913 Py_CLEAR(self->tabObject);
3914 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003915}
3916
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003917 static win_T *
3918get_firstwin(TabPageObject *tabObject)
3919{
3920 if (tabObject)
3921 {
3922 if (CheckTabPage(tabObject))
3923 return NULL;
3924 /* For current tab window.c does not bother to set or update tp_firstwin
3925 */
3926 else if (tabObject->tab == curtab)
3927 return firstwin;
3928 else
3929 return tabObject->tab->tp_firstwin;
3930 }
3931 else
3932 return firstwin;
3933}
Bram Moolenaare950f992018-06-10 13:55:55 +02003934
3935// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003936static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003937 "buffer",
3938 "cursor",
3939 "height",
3940 "row",
3941 "width",
3942 "col",
3943 "vars",
3944 "options",
3945 "number",
3946 "tabpage",
3947 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003948 NULL
3949};
3950
3951 static PyObject *
3952WindowDir(PyObject *self)
3953{
3954 return ObjectDir(self, WindowAttrs);
3955}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003956
Bram Moolenaar971db462013-05-12 18:44:48 +02003957 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003958WindowAttrValid(WindowObject *self, char *name)
3959{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003960 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003961
3962 if (strcmp(name, "valid") != 0)
3963 return NULL;
3964
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003965 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3966 Py_INCREF(ret);
3967 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003968}
3969
3970 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003971WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003972{
3973 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003974 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003975 else if (strcmp(name, "cursor") == 0)
3976 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003977 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003978
3979 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3980 }
3981 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003982 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003983 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003984 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003985 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003986 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003987 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003988 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003989 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003990 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003991 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3993 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003994 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003995 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003997 return NULL;
3998 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004000 }
4001 else if (strcmp(name, "tabpage") == 0)
4002 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004003 Py_INCREF(self->tabObject);
4004 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004005 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004006 else if (strcmp(name, "__members__") == 0)
4007 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004008 else
4009 return NULL;
4010}
4011
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004012 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004013WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004014{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004016 return -1;
4017
4018 if (strcmp(name, "buffer") == 0)
4019 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004020 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004021 return -1;
4022 }
4023 else if (strcmp(name, "cursor") == 0)
4024 {
4025 long lnum;
4026 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004027
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004028 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004029 return -1;
4030
Bram Moolenaard6e39182013-05-21 18:30:34 +02004031 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004032 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004033 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 return -1;
4035 }
4036
4037 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004038 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039 return -1;
4040
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 self->win->w_cursor.lnum = lnum;
4042 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004043 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004044 self->win->w_cursor.coladd = 0;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004045 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004046 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004047
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004048 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049 return 0;
4050 }
4051 else if (strcmp(name, "height") == 0)
4052 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004053 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 win_T *savewin;
4055
Bram Moolenaardee2e312013-06-23 16:35:47 +02004056 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 return -1;
4058
4059#ifdef FEAT_GUI
4060 need_mouse_correct = TRUE;
4061#endif
4062 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004063 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004064
4065 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004066 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004068 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 return -1;
4070
4071 return 0;
4072 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073 else if (strcmp(name, "width") == 0)
4074 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004075 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 win_T *savewin;
4077
Bram Moolenaardee2e312013-06-23 16:35:47 +02004078 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 return -1;
4080
4081#ifdef FEAT_GUI
4082 need_mouse_correct = TRUE;
4083#endif
4084 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004085 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004086
4087 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004088 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004090 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 return -1;
4092
4093 return 0;
4094 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 else
4096 {
4097 PyErr_SetString(PyExc_AttributeError, name);
4098 return -1;
4099 }
4100}
4101
4102 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004103WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004104{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004105 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004106 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 else
4108 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004109 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004110
Bram Moolenaar6d216452013-05-12 19:00:41 +02004111 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004112 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 (self));
4114 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004115 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 }
4117}
4118
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004119static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004120 /* name, function, calling, documentation */
4121 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4122 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004123};
4124
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004126 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004128
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004129static PyTypeObject WinListType;
4130static PySequenceMethods WinListAsSeq;
4131
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004132typedef struct
4133{
4134 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004135 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004136} WinListObject;
4137
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004138 static PyObject *
4139WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004140{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004141 WinListObject *self;
4142
4143 self = PyObject_NEW(WinListObject, &WinListType);
4144 self->tabObject = tabObject;
4145 Py_INCREF(tabObject);
4146
4147 return (PyObject *)(self);
4148}
4149
4150 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004151WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004152{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004153 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004154
4155 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004156 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004157 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004158 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004159
4160 DESTRUCTOR_FINISH(self);
4161}
4162
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004163 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004164WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004165{
4166 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167 PyInt n = 0;
4168
Bram Moolenaard6e39182013-05-21 18:30:34 +02004169 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004170 return -1;
4171
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004172 while (w != NULL)
4173 {
4174 ++n;
4175 w = W_NEXT(w);
4176 }
4177
4178 return n;
4179}
4180
4181 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004182WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004183{
4184 win_T *w;
4185
Bram Moolenaard6e39182013-05-21 18:30:34 +02004186 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004187 return NULL;
4188
4189 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004191 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004193 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004194 return NULL;
4195}
4196
4197/* Convert a Python string into a Vim line.
4198 *
4199 * The result is in allocated memory. All internal nulls are replaced by
4200 * newline characters. It is an error for the string to contain newline
4201 * characters.
4202 *
4203 * On errors, the Python exception data is set, and NULL is returned.
4204 */
4205 static char *
4206StringToLine(PyObject *obj)
4207{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004208 char *str;
4209 char *save;
4210 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004211 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004212 PyInt i;
4213 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004215 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004217 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4218 || str == NULL)
4219 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004220 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004221 else if (PyUnicode_Check(obj))
4222 {
4223 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4224 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004225
Bram Moolenaardaa27022013-06-24 22:33:30 +02004226 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004227 || str == NULL)
4228 {
4229 Py_DECREF(bytes);
4230 return NULL;
4231 }
4232 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004233 else
4234 {
4235#if PY_MAJOR_VERSION < 3
4236 PyErr_FORMAT(PyExc_TypeError,
4237 N_("expected str() or unicode() instance, but got %s"),
4238 Py_TYPE_NAME(obj));
4239#else
4240 PyErr_FORMAT(PyExc_TypeError,
4241 N_("expected bytes() or str() instance, but got %s"),
4242 Py_TYPE_NAME(obj));
4243#endif
4244 return NULL;
4245 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004246
4247 /*
4248 * Error checking: String must not contain newlines, as we
4249 * are replacing a single line, and we must replace it with
4250 * a single line.
4251 * A trailing newline is removed, so that append(f.readlines()) works.
4252 */
4253 p = memchr(str, '\n', len);
4254 if (p != NULL)
4255 {
4256 if (p == str + len - 1)
4257 --len;
4258 else
4259 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004260 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004261 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004262 return NULL;
4263 }
4264 }
4265
4266 /* Create a copy of the string, with internal nulls replaced by
4267 * newline characters, as is the vim convention.
4268 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004269 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270 if (save == NULL)
4271 {
4272 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004273 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004274 return NULL;
4275 }
4276
4277 for (i = 0; i < len; ++i)
4278 {
4279 if (str[i] == '\0')
4280 save[i] = '\n';
4281 else
4282 save[i] = str[i];
4283 }
4284
4285 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004286 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004287
4288 return save;
4289}
4290
4291/* Get a line from the specified buffer. The line number is
4292 * in Vim format (1-based). The line is returned as a Python
4293 * string object.
4294 */
4295 static PyObject *
4296GetBufferLine(buf_T *buf, PyInt n)
4297{
4298 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4299}
4300
4301
4302/* Get a list of lines from the specified buffer. The line numbers
4303 * are in Vim format (1-based). The range is from lo up to, but not
4304 * including, hi. The list is returned as a Python list of string objects.
4305 */
4306 static PyObject *
4307GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4308{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004309 PyInt i;
4310 PyInt n = hi - lo;
4311 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004312
4313 if (list == NULL)
4314 return NULL;
4315
4316 for (i = 0; i < n; ++i)
4317 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004318 PyObject *string = LineToString(
4319 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004320
4321 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004322 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004323 {
4324 Py_DECREF(list);
4325 return NULL;
4326 }
4327
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004328 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004329 }
4330
4331 /* The ownership of the Python list is passed to the caller (ie,
4332 * the caller should Py_DECREF() the object when it is finished
4333 * with it).
4334 */
4335
4336 return list;
4337}
4338
4339/*
4340 * Check if deleting lines made the cursor position invalid.
4341 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4342 * deleted).
4343 */
4344 static void
4345py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4346{
4347 if (curwin->w_cursor.lnum >= lo)
4348 {
4349 /* Adjust the cursor position if it's in/after the changed
4350 * lines. */
4351 if (curwin->w_cursor.lnum >= hi)
4352 {
4353 curwin->w_cursor.lnum += extra;
4354 check_cursor_col();
4355 }
4356 else if (extra < 0)
4357 {
4358 curwin->w_cursor.lnum = lo;
4359 check_cursor();
4360 }
4361 else
4362 check_cursor_col();
4363 changed_cline_bef_curs();
4364 }
4365 invalidate_botline();
4366}
4367
Bram Moolenaar19e60942011-06-19 00:27:51 +02004368/*
4369 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004370 * in Vim format (1-based). The replacement line is given as
4371 * a Python string object. The object is checked for validity
4372 * and correct format. Errors are returned as a value of FAIL.
4373 * The return value is OK on success.
4374 * If OK is returned and len_change is not NULL, *len_change
4375 * is set to the change in the buffer length.
4376 */
4377 static int
4378SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4379{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004380 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004381 win_T *save_curwin = NULL;
4382 tabpage_T *save_curtab = NULL;
4383
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004384 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004385 * There are three cases:
4386 * 1. NULL, or None - this is a deletion.
4387 * 2. A string - this is a replacement.
4388 * 3. Anything else - this is an error.
4389 */
4390 if (line == Py_None || line == NULL)
4391 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004392 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004393 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004394
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004395 VimTryStart();
4396
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004398 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004399 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004400 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 else
4402 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004403 if (buf == curbuf && (save_curwin != NULL
4404 || save_curbuf.br_buf == NULL))
4405 // Using an existing window for the buffer, adjust the cursor
4406 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004408 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004409 /* Only adjust marks if we managed to switch to a window that
4410 * holds the buffer, otherwise line numbers will be invalid. */
4411 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412 }
4413
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004414 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004415
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004416 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004417 return FAIL;
4418
4419 if (len_change)
4420 *len_change = -1;
4421
4422 return OK;
4423 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004424 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004426 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004427
4428 if (save == NULL)
4429 return FAIL;
4430
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004431 VimTryStart();
4432
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004433 /* We do not need to free "save" if ml_replace() consumes it. */
4434 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004435 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004436
4437 if (u_savesub((linenr_T)n) == FAIL)
4438 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004439 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440 vim_free(save);
4441 }
4442 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4443 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004444 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 vim_free(save);
4446 }
4447 else
4448 changed_bytes((linenr_T)n, 0);
4449
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004450 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004451
4452 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004453 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004454 check_cursor_col();
4455
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004456 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004457 return FAIL;
4458
4459 if (len_change)
4460 *len_change = 0;
4461
4462 return OK;
4463 }
4464 else
4465 {
4466 PyErr_BadArgument();
4467 return FAIL;
4468 }
4469}
4470
Bram Moolenaar19e60942011-06-19 00:27:51 +02004471/* Replace a range of lines in the specified buffer. The line numbers are in
4472 * Vim format (1-based). The range is from lo up to, but not including, hi.
4473 * The replacement lines are given as a Python list of string objects. The
4474 * list is checked for validity and correct format. Errors are returned as a
4475 * value of FAIL. The return value is OK on success.
4476 * If OK is returned and len_change is not NULL, *len_change
4477 * is set to the change in the buffer length.
4478 */
4479 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004480SetBufferLineList(
4481 buf_T *buf,
4482 PyInt lo,
4483 PyInt hi,
4484 PyObject *list,
4485 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004486{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004487 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004488 win_T *save_curwin = NULL;
4489 tabpage_T *save_curtab = NULL;
4490
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004491 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004492 * There are three cases:
4493 * 1. NULL, or None - this is a deletion.
4494 * 2. A list - this is a replacement.
4495 * 3. Anything else - this is an error.
4496 */
4497 if (list == Py_None || list == NULL)
4498 {
4499 PyInt i;
4500 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004501
4502 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004503 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004504 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004505
4506 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004507 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004508 else
4509 {
4510 for (i = 0; i < n; ++i)
4511 {
4512 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4513 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004514 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004515 break;
4516 }
4517 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004518 if (buf == curbuf && (save_curwin != NULL
4519 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004520 /* Using an existing window for the buffer, adjust the cursor
4521 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004522 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004523 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004524 /* Only adjust marks if we managed to switch to a window that
4525 * holds the buffer, otherwise line numbers will be invalid. */
4526 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004527 }
4528
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004529 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004530
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004531 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004532 return FAIL;
4533
4534 if (len_change)
4535 *len_change = -n;
4536
4537 return OK;
4538 }
4539 else if (PyList_Check(list))
4540 {
4541 PyInt i;
4542 PyInt new_len = PyList_Size(list);
4543 PyInt old_len = hi - lo;
4544 PyInt extra = 0; /* lines added to text, can be negative */
4545 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004546
4547 if (new_len == 0) /* avoid allocating zero bytes */
4548 array = NULL;
4549 else
4550 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004551 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004552 if (array == NULL)
4553 {
4554 PyErr_NoMemory();
4555 return FAIL;
4556 }
4557 }
4558
4559 for (i = 0; i < new_len; ++i)
4560 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004561 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004562
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004563 if (!(line = PyList_GetItem(list, i)) ||
4564 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004565 {
4566 while (i)
4567 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004568 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004569 return FAIL;
4570 }
4571 }
4572
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004573 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004574 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004575
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004576 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004577 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004578
4579 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004580 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004581
4582 /* If the size of the range is reducing (ie, new_len < old_len) we
4583 * need to delete some old_len. We do this at the start, by
4584 * repeatedly deleting line "lo".
4585 */
4586 if (!PyErr_Occurred())
4587 {
4588 for (i = 0; i < old_len - new_len; ++i)
4589 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4590 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004591 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004592 break;
4593 }
4594 extra -= i;
4595 }
4596
4597 /* For as long as possible, replace the existing old_len with the
4598 * new old_len. This is a more efficient operation, as it requires
4599 * less memory allocation and freeing.
4600 */
4601 if (!PyErr_Occurred())
4602 {
4603 for (i = 0; i < old_len && i < new_len; ++i)
4604 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4605 == FAIL)
4606 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004607 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004608 break;
4609 }
4610 }
4611 else
4612 i = 0;
4613
4614 /* Now we may need to insert the remaining new old_len. If we do, we
4615 * must free the strings as we finish with them (we can't pass the
4616 * responsibility to vim in this case).
4617 */
4618 if (!PyErr_Occurred())
4619 {
4620 while (i < new_len)
4621 {
4622 if (ml_append((linenr_T)(lo + i - 1),
4623 (char_u *)array[i], 0, FALSE) == FAIL)
4624 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004625 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004626 break;
4627 }
4628 vim_free(array[i]);
4629 ++i;
4630 ++extra;
4631 }
4632 }
4633
4634 /* Free any left-over old_len, as a result of an error */
4635 while (i < new_len)
4636 {
4637 vim_free(array[i]);
4638 ++i;
4639 }
4640
4641 /* Free the array of old_len. All of its contents have now
4642 * been dealt with (either freed, or the responsibility passed
4643 * to vim.
4644 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004645 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004646
4647 /* Adjust marks. Invalidate any which lie in the
4648 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004649 * Only adjust marks if we managed to switch to a window that holds
4650 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004651 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004652 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004653 (long)MAXLNUM, (long)extra);
4654 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4655
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004656 if (buf == curbuf && (save_curwin != NULL
4657 || save_curbuf.br_buf == NULL))
4658 // Using an existing window for the buffer, adjust the cursor
4659 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004660 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4661
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004662 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004663 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004664
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004665 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004666 return FAIL;
4667
4668 if (len_change)
4669 *len_change = new_len - old_len;
4670
4671 return OK;
4672 }
4673 else
4674 {
4675 PyErr_BadArgument();
4676 return FAIL;
4677 }
4678}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004679
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004680/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004681 * The line number is in Vim format (1-based). The lines to be inserted are
4682 * given as a Python list of string objects or as a single string. The lines
4683 * to be added are checked for validity and correct format. Errors are
4684 * returned as a value of FAIL. The return value is OK on success.
4685 * If OK is returned and len_change is not NULL, *len_change
4686 * is set to the change in the buffer length.
4687 */
4688 static int
4689InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4690{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004691 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004692 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004693 tabpage_T *save_curtab = NULL;
4694
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004695 /* First of all, we check the type of the supplied Python object.
4696 * It must be a string or a list, or the call is in error.
4697 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004698 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004699 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004700 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004701
4702 if (str == NULL)
4703 return FAIL;
4704
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004705 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004706 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004707 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004709 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004710 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004712 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004713 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004714 /* Only adjust marks if we managed to switch to a window that
4715 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004716 appended_lines_mark((linenr_T)n, 1L);
4717
4718 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004719 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004720 update_screen(VALID);
4721
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004722 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723 return FAIL;
4724
4725 if (len_change)
4726 *len_change = 1;
4727
4728 return OK;
4729 }
4730 else if (PyList_Check(lines))
4731 {
4732 PyInt i;
4733 PyInt size = PyList_Size(lines);
4734 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004736 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004737 if (array == NULL)
4738 {
4739 PyErr_NoMemory();
4740 return FAIL;
4741 }
4742
4743 for (i = 0; i < size; ++i)
4744 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004745 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004746
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004747 if (!(line = PyList_GetItem(lines, i)) ||
4748 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 {
4750 while (i)
4751 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004752 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004753 return FAIL;
4754 }
4755 }
4756
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004757 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004758 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004759 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004760
4761 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004762 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763 else
4764 {
4765 for (i = 0; i < size; ++i)
4766 {
4767 if (ml_append((linenr_T)(n + i),
4768 (char_u *)array[i], 0, FALSE) == FAIL)
4769 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004770 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004771
4772 /* Free the rest of the lines */
4773 while (i < size)
4774 vim_free(array[i++]);
4775
4776 break;
4777 }
4778 vim_free(array[i]);
4779 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004780 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004781 /* Only adjust marks if we managed to switch to a window that
4782 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 appended_lines_mark((linenr_T)n, (long)i);
4784 }
4785
4786 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004787 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004788 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004789 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004790
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004791 update_screen(VALID);
4792
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004793 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004794 return FAIL;
4795
4796 if (len_change)
4797 *len_change = size;
4798
4799 return OK;
4800 }
4801 else
4802 {
4803 PyErr_BadArgument();
4804 return FAIL;
4805 }
4806}
4807
4808/*
4809 * Common routines for buffers and line ranges
4810 * -------------------------------------------
4811 */
4812
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004813typedef struct
4814{
4815 PyObject_HEAD
4816 buf_T *buf;
4817} BufferObject;
4818
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004819 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004820CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004821{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004822 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004823 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004824 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004825 return -1;
4826 }
4827
4828 return 0;
4829}
4830
4831 static PyObject *
4832RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4833{
4834 if (CheckBuffer(self))
4835 return NULL;
4836
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004837 if (end == -1)
4838 end = self->buf->b_ml.ml_line_count;
4839
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004840 if (n < 0)
4841 n += end - start + 1;
4842
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004843 if (n < 0 || n > end - start)
4844 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004845 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004846 return NULL;
4847 }
4848
4849 return GetBufferLine(self->buf, n+start);
4850}
4851
4852 static PyObject *
4853RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4854{
4855 PyInt size;
4856
4857 if (CheckBuffer(self))
4858 return NULL;
4859
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004860 if (end == -1)
4861 end = self->buf->b_ml.ml_line_count;
4862
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004863 size = end - start + 1;
4864
4865 if (lo < 0)
4866 lo = 0;
4867 else if (lo > size)
4868 lo = size;
4869 if (hi < 0)
4870 hi = 0;
4871 if (hi < lo)
4872 hi = lo;
4873 else if (hi > size)
4874 hi = size;
4875
4876 return GetBufferLineList(self->buf, lo+start, hi+start);
4877}
4878
4879 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004880RBAsItem(
4881 BufferObject *self,
4882 PyInt n,
4883 PyObject *valObject,
4884 PyInt start,
4885 PyInt end,
4886 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004887{
4888 PyInt len_change;
4889
4890 if (CheckBuffer(self))
4891 return -1;
4892
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004893 if (end == -1)
4894 end = self->buf->b_ml.ml_line_count;
4895
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004896 if (n < 0)
4897 n += end - start + 1;
4898
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004899 if (n < 0 || n > end - start)
4900 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004901 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004902 return -1;
4903 }
4904
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004905 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004906 return -1;
4907
4908 if (new_end)
4909 *new_end = end + len_change;
4910
4911 return 0;
4912}
4913
Bram Moolenaar19e60942011-06-19 00:27:51 +02004914 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004915RBAsSlice(
4916 BufferObject *self,
4917 PyInt lo,
4918 PyInt hi,
4919 PyObject *valObject,
4920 PyInt start,
4921 PyInt end,
4922 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004923{
4924 PyInt size;
4925 PyInt len_change;
4926
4927 /* Self must be a valid buffer */
4928 if (CheckBuffer(self))
4929 return -1;
4930
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004931 if (end == -1)
4932 end = self->buf->b_ml.ml_line_count;
4933
Bram Moolenaar19e60942011-06-19 00:27:51 +02004934 /* Sort out the slice range */
4935 size = end - start + 1;
4936
4937 if (lo < 0)
4938 lo = 0;
4939 else if (lo > size)
4940 lo = size;
4941 if (hi < 0)
4942 hi = 0;
4943 if (hi < lo)
4944 hi = lo;
4945 else if (hi > size)
4946 hi = size;
4947
4948 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004949 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004950 return -1;
4951
4952 if (new_end)
4953 *new_end = end + len_change;
4954
4955 return 0;
4956}
4957
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004958
4959 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004960RBAppend(
4961 BufferObject *self,
4962 PyObject *args,
4963 PyInt start,
4964 PyInt end,
4965 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004966{
4967 PyObject *lines;
4968 PyInt len_change;
4969 PyInt max;
4970 PyInt n;
4971
4972 if (CheckBuffer(self))
4973 return NULL;
4974
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004975 if (end == -1)
4976 end = self->buf->b_ml.ml_line_count;
4977
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004978 max = n = end - start + 1;
4979
4980 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4981 return NULL;
4982
4983 if (n < 0 || n > max)
4984 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004985 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004986 return NULL;
4987 }
4988
4989 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4990 return NULL;
4991
4992 if (new_end)
4993 *new_end = end + len_change;
4994
4995 Py_INCREF(Py_None);
4996 return Py_None;
4997}
4998
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004999/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005000 */
5001
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005002static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005003static PySequenceMethods RangeAsSeq;
5004static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005005
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005006typedef struct
5007{
5008 PyObject_HEAD
5009 BufferObject *buf;
5010 PyInt start;
5011 PyInt end;
5012} RangeObject;
5013
5014 static PyObject *
5015RangeNew(buf_T *buf, PyInt start, PyInt end)
5016{
5017 BufferObject *bufr;
5018 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005019 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005020 if (self == NULL)
5021 return NULL;
5022
5023 bufr = (BufferObject *)BufferNew(buf);
5024 if (bufr == NULL)
5025 {
5026 Py_DECREF(self);
5027 return NULL;
5028 }
5029 Py_INCREF(bufr);
5030
5031 self->buf = bufr;
5032 self->start = start;
5033 self->end = end;
5034
5035 return (PyObject *)(self);
5036}
5037
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005038 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005039RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005040{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005041 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005042 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005043 PyObject_GC_Del((void *)(self));
5044}
5045
5046 static int
5047RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5048{
5049 Py_VISIT(((PyObject *)(self->buf)));
5050 return 0;
5051}
5052
5053 static int
5054RangeClear(RangeObject *self)
5055{
5056 Py_CLEAR(self->buf);
5057 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005058}
5059
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005061RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005062{
5063 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005064 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005065 return -1; /* ??? */
5066
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005068}
5069
5070 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005071RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005072{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005073 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005074}
5075
5076 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005077RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005078{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005079 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005080}
5081
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005082static char *RangeAttrs[] = {
5083 "start", "end",
5084 NULL
5085};
5086
5087 static PyObject *
5088RangeDir(PyObject *self)
5089{
5090 return ObjectDir(self, RangeAttrs);
5091}
5092
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097}
5098
5099 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005100RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005103 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5104 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005105 else
5106 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005107 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005108
5109 if (name == NULL)
5110 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005111
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005112 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005113 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005114 }
5115}
5116
5117static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005118 /* name, function, calling, documentation */
5119 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005120 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5121 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122};
5123
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005125static PySequenceMethods BufferAsSeq;
5126static PyMappingMethods BufferAsMapping;
5127
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005128 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005129BufferNew(buf_T *buf)
5130{
5131 /* We need to handle deletion of buffers underneath us.
5132 * If we add a "b_python*_ref" field to the buf_T structure,
5133 * then we can get at it in buf_freeall() in vim. We then
5134 * need to create only ONE Python object per buffer - if
5135 * we try to create a second, just INCREF the existing one
5136 * and return it. The (single) Python object referring to
5137 * the buffer is stored in "b_python*_ref".
5138 * Question: what to do on a buf_freeall(). We'll probably
5139 * have to either delete the Python object (DECREF it to
5140 * zero - a bad idea, as it leaves dangling refs!) or
5141 * set the buf_T * value to an invalid value (-1?), which
5142 * means we need checks in all access functions... Bah.
5143 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005144 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005145 * b_python_ref and b_python3_ref fields respectively.
5146 */
5147
5148 BufferObject *self;
5149
5150 if (BUF_PYTHON_REF(buf) != NULL)
5151 {
5152 self = BUF_PYTHON_REF(buf);
5153 Py_INCREF(self);
5154 }
5155 else
5156 {
5157 self = PyObject_NEW(BufferObject, &BufferType);
5158 if (self == NULL)
5159 return NULL;
5160 self->buf = buf;
5161 BUF_PYTHON_REF(buf) = self;
5162 }
5163
5164 return (PyObject *)(self);
5165}
5166
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005167 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005168BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005169{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005170 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5171 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005172
5173 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005174}
5175
Bram Moolenaar971db462013-05-12 18:44:48 +02005176 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005177BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005178{
5179 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005180 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005181 return -1; /* ??? */
5182
Bram Moolenaard6e39182013-05-21 18:30:34 +02005183 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005184}
5185
5186 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005187BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005188{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005189 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005190}
5191
5192 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005193BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005194{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005196}
5197
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005198static char *BufferAttrs[] = {
5199 "name", "number", "vars", "options", "valid",
5200 NULL
5201};
5202
5203 static PyObject *
5204BufferDir(PyObject *self)
5205{
5206 return ObjectDir(self, BufferAttrs);
5207}
5208
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005209 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005210BufferAttrValid(BufferObject *self, char *name)
5211{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005212 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005213
5214 if (strcmp(name, "valid") != 0)
5215 return NULL;
5216
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005217 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5218 Py_INCREF(ret);
5219 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005220}
5221
5222 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005223BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005224{
5225 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005226 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005227 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005228 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005229 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005230 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005231 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005233 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5234 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005235 else if (strcmp(name, "__members__") == 0)
5236 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005237 else
5238 return NULL;
5239}
5240
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005241 static int
5242BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5243{
5244 if (CheckBuffer(self))
5245 return -1;
5246
5247 if (strcmp(name, "name") == 0)
5248 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005249 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005250 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005251 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005252 PyObject *todecref;
5253
5254 if (!(val = StringToChars(valObject, &todecref)))
5255 return -1;
5256
5257 VimTryStart();
5258 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5259 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005260 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005261 aucmd_restbuf(&aco);
5262 Py_XDECREF(todecref);
5263 if (VimTryEnd())
5264 return -1;
5265
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005266 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005267 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005268 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005269 return -1;
5270 }
5271 return 0;
5272 }
5273 else
5274 {
5275 PyErr_SetString(PyExc_AttributeError, name);
5276 return -1;
5277 }
5278}
5279
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005280 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005281BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005282{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005283 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005284}
5285
5286 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005287BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005288{
5289 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005290 char_u *pmark;
5291 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005292 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005293 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005294
Bram Moolenaard6e39182013-05-21 18:30:34 +02005295 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005296 return NULL;
5297
Bram Moolenaar389a1792013-06-23 13:00:44 +02005298 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005299 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005300
Bram Moolenaar389a1792013-06-23 13:00:44 +02005301 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005302 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005303 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005304 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005305 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005306 return NULL;
5307 }
5308
5309 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005310
5311 Py_XDECREF(todecref);
5312
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005313 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005314 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005316 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005317 if (VimTryEnd())
5318 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005319
5320 if (posp == NULL)
5321 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005322 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005323 return NULL;
5324 }
5325
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005326 if (posp->lnum <= 0)
5327 {
5328 /* Or raise an error? */
5329 Py_INCREF(Py_None);
5330 return Py_None;
5331 }
5332
5333 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5334}
5335
5336 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005337BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005338{
5339 PyInt start;
5340 PyInt end;
5341
Bram Moolenaard6e39182013-05-21 18:30:34 +02005342 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005343 return NULL;
5344
5345 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5346 return NULL;
5347
Bram Moolenaard6e39182013-05-21 18:30:34 +02005348 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005349}
5350
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005351 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005352BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005353{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005354 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005355 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005356 else
5357 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005358 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005359
5360 if (name == NULL)
5361 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005362
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005363 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005364 }
5365}
5366
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005367static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005368 /* name, function, calling, documentation */
5369 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005370 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005371 {"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 +02005372 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5373 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005374};
5375
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005376/*
5377 * Buffer list object - Implementation
5378 */
5379
5380static PyTypeObject BufMapType;
5381
5382typedef struct
5383{
5384 PyObject_HEAD
5385} BufMapObject;
5386
5387 static PyInt
5388BufMapLength(PyObject *self UNUSED)
5389{
5390 buf_T *b = firstbuf;
5391 PyInt n = 0;
5392
5393 while (b)
5394 {
5395 ++n;
5396 b = b->b_next;
5397 }
5398
5399 return n;
5400}
5401
5402 static PyObject *
5403BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5404{
5405 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005406 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005407
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005408 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005409 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005410
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005411 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005412
5413 if (b)
5414 return BufferNew(b);
5415 else
5416 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005417 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005418 return NULL;
5419 }
5420}
5421
5422 static void
5423BufMapIterDestruct(PyObject *buffer)
5424{
5425 /* Iteration was stopped before all buffers were processed */
5426 if (buffer)
5427 {
5428 Py_DECREF(buffer);
5429 }
5430}
5431
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005432 static int
5433BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5434{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005435 if (buffer)
5436 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005437 return 0;
5438}
5439
5440 static int
5441BufMapIterClear(PyObject **buffer)
5442{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005443 if (*buffer)
5444 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005445 return 0;
5446}
5447
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005448 static PyObject *
5449BufMapIterNext(PyObject **buffer)
5450{
5451 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005452 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005453
5454 if (!*buffer)
5455 return NULL;
5456
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005457 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005458
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005459 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005460 {
5461 *buffer = NULL;
5462 return NULL;
5463 }
5464
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005465 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005466 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005467 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005468 return NULL;
5469 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005470 /* Do not increment reference: we no longer hold it (decref), but whoever
5471 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005472 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005473}
5474
5475 static PyObject *
5476BufMapIter(PyObject *self UNUSED)
5477{
5478 PyObject *buffer;
5479
5480 buffer = BufferNew(firstbuf);
5481 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005482 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5483 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005484}
5485
5486static PyMappingMethods BufMapAsMapping = {
5487 (lenfunc) BufMapLength,
5488 (binaryfunc) BufMapItem,
5489 (objobjargproc) 0,
5490};
5491
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005492/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005493 */
5494
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005495static char *CurrentAttrs[] = {
5496 "buffer", "window", "line", "range", "tabpage",
5497 NULL
5498};
5499
5500 static PyObject *
5501CurrentDir(PyObject *self)
5502{
5503 return ObjectDir(self, CurrentAttrs);
5504}
5505
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005506 static PyObject *
5507CurrentGetattr(PyObject *self UNUSED, char *name)
5508{
5509 if (strcmp(name, "buffer") == 0)
5510 return (PyObject *)BufferNew(curbuf);
5511 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005512 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005513 else if (strcmp(name, "tabpage") == 0)
5514 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005515 else if (strcmp(name, "line") == 0)
5516 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5517 else if (strcmp(name, "range") == 0)
5518 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005519 else if (strcmp(name, "__members__") == 0)
5520 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005521 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005522#if PY_MAJOR_VERSION < 3
5523 return Py_FindMethod(WindowMethods, self, name);
5524#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005525 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005526#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005527}
5528
5529 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005530CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005531{
5532 if (strcmp(name, "line") == 0)
5533 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005534 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5535 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005536 return -1;
5537
5538 return 0;
5539 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005540 else if (strcmp(name, "buffer") == 0)
5541 {
5542 int count;
5543
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005544 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005545 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005546 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005547 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005548 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005549 return -1;
5550 }
5551
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005552 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005553 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005554 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005555
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005556 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005557 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5558 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005559 if (VimTryEnd())
5560 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005561 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005562 return -1;
5563 }
5564
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005565 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005566 }
5567 else if (strcmp(name, "window") == 0)
5568 {
5569 int count;
5570
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005572 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005573 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005574 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005575 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005576 return -1;
5577 }
5578
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005579 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005580 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005582
5583 if (!count)
5584 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005585 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005586 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005587 return -1;
5588 }
5589
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005590 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005591 win_goto(((WindowObject *)(valObject))->win);
5592 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005593 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005594 if (VimTryEnd())
5595 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005596 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005597 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005598 return -1;
5599 }
5600
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005601 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005602 }
5603 else if (strcmp(name, "tabpage") == 0)
5604 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005606 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005607 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005608 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005609 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005610 return -1;
5611 }
5612
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005613 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005614 return -1;
5615
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005616 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005617 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5618 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005619 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005620 if (VimTryEnd())
5621 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005622 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005623 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005624 return -1;
5625 }
5626
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005627 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005628 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005629 else
5630 {
5631 PyErr_SetString(PyExc_AttributeError, name);
5632 return -1;
5633 }
5634}
5635
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005636static struct PyMethodDef CurrentMethods[] = {
5637 /* name, function, calling, documentation */
5638 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5639 { NULL, NULL, 0, NULL}
5640};
5641
Bram Moolenaardb913952012-06-29 12:54:53 +02005642 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005643init_range_cmd(exarg_T *eap)
5644{
5645 RangeStart = eap->line1;
5646 RangeEnd = eap->line2;
5647}
5648
5649 static void
5650init_range_eval(typval_T *rettv UNUSED)
5651{
5652 RangeStart = (PyInt) curwin->w_cursor.lnum;
5653 RangeEnd = RangeStart;
5654}
5655
5656 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005657run_cmd(const char *cmd, void *arg UNUSED
5658#ifdef PY_CAN_RECURSE
5659 , PyGILState_STATE *pygilstate UNUSED
5660#endif
5661 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005662{
Bram Moolenaar41009372013-07-01 22:03:04 +02005663 PyObject *run_ret;
5664 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5665 if (run_ret != NULL)
5666 {
5667 Py_DECREF(run_ret);
5668 }
5669 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005671 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005672 PyErr_Clear();
5673 }
5674 else
5675 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005676}
5677
5678static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5679static int code_hdr_len = 30;
5680
5681 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005682run_do(const char *cmd, void *arg UNUSED
5683#ifdef PY_CAN_RECURSE
5684 , PyGILState_STATE *pygilstate
5685#endif
5686 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005687{
5688 PyInt lnum;
5689 size_t len;
5690 char *code;
5691 int status;
5692 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005693 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005694 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005695
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005696 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005698 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005699 return;
5700 }
5701
5702 len = code_hdr_len + STRLEN(cmd);
5703 code = PyMem_New(char, len + 1);
5704 memcpy(code, code_hdr, code_hdr_len);
5705 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005706 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5707 status = -1;
5708 if (run_ret != NULL)
5709 {
5710 status = 0;
5711 Py_DECREF(run_ret);
5712 }
5713 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5714 {
5715 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005716 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005717 PyErr_Clear();
5718 return;
5719 }
5720 else
5721 PyErr_PrintEx(1);
5722
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005723 PyMem_Free(code);
5724
5725 if (status)
5726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005727 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005728 return;
5729 }
5730
5731 status = 0;
5732 pymain = PyImport_AddModule("__main__");
5733 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005734#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005735 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005736#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005737
5738 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5739 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005740 PyObject *line;
5741 PyObject *linenr;
5742 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005743
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005744#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005745 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005746#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005747 /* Check the line number, the command my have deleted lines. */
5748 if (lnum > curbuf->b_ml.ml_line_count
5749 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005750 goto err;
5751 if (!(linenr = PyInt_FromLong((long) lnum)))
5752 {
5753 Py_DECREF(line);
5754 goto err;
5755 }
5756 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5757 Py_DECREF(line);
5758 Py_DECREF(linenr);
5759 if (!ret)
5760 goto err;
5761
Bram Moolenaara58883b2017-01-29 21:31:09 +01005762 /* Check that the command didn't switch to another buffer. */
5763 if (curbuf != was_curbuf)
5764 {
5765 Py_XDECREF(ret);
5766 goto err;
5767 }
5768
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005769 if (ret != Py_None)
5770 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005771 {
5772 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005773 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005774 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005775
5776 Py_XDECREF(ret);
5777 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005778#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005779 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005780#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781 }
5782 goto out;
5783err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005784#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005785 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005786#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005787 PyErr_PrintEx(0);
5788 PythonIO_Flush();
5789 status = 1;
5790out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005791#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005792 if (!status)
5793 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005794#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005795 Py_DECREF(pyfunc);
5796 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5797 if (status)
5798 return;
5799 check_cursor();
5800 update_curbuf(NOT_VALID);
5801}
5802
5803 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005804run_eval(const char *cmd, typval_T *rettv
5805#ifdef PY_CAN_RECURSE
5806 , PyGILState_STATE *pygilstate UNUSED
5807#endif
5808 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005809{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005810 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005811
Bram Moolenaar41009372013-07-01 22:03:04 +02005812 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005813 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005814 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005815 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005817 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005818 PyErr_Clear();
5819 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005820 else
5821 {
5822 if (PyErr_Occurred() && !msg_silent)
5823 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005824 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005825 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005826 }
5827 else
5828 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005829 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005830 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005831 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005832 }
5833 PyErr_Clear();
5834}
5835
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005836 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005837set_ref_in_py(const int copyID)
5838{
5839 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005840 list_T *ll;
5841 int i;
5842 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005843 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005844
5845 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005847 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005848 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5849 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005850 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005851
5852 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005853 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005854 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005855 {
5856 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005857 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005858 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005859 }
5860
Bram Moolenaar8110a092016-04-14 15:56:09 +02005861 if (lastfunc != NULL)
5862 {
5863 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5864 {
5865 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005866 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005867 if (func->argc)
5868 for (i = 0; !abort && i < func->argc; ++i)
5869 abort = abort
5870 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5871 }
5872 }
5873
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005874 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005875}
5876
5877 static int
5878set_string_copy(char_u *str, typval_T *tv)
5879{
5880 tv->vval.v_string = vim_strsave(str);
5881 if (tv->vval.v_string == NULL)
5882 {
5883 PyErr_NoMemory();
5884 return -1;
5885 }
5886 return 0;
5887}
5888
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005889 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005890pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005891{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005892 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005893 char_u *key;
5894 dictitem_T *di;
5895 PyObject *keyObject;
5896 PyObject *valObject;
5897 Py_ssize_t iter = 0;
5898
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005899 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005900 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901
5902 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005903 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005904
5905 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5906 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005907 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005908
Bram Moolenaara03e6312013-05-29 22:49:26 +02005909 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005910 {
5911 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005912 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005913 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005914
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005915 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005916 {
5917 dict_unref(dict);
5918 return -1;
5919 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005920
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005921 if (*key == NUL)
5922 {
5923 dict_unref(dict);
5924 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005925 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005926 return -1;
5927 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005928
5929 di = dictitem_alloc(key);
5930
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005931 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005932
5933 if (di == NULL)
5934 {
5935 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005936 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937 return -1;
5938 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005940 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941 {
5942 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005943 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005944 return -1;
5945 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005946
5947 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005948 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005949 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005950 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005952 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 return -1;
5954 }
5955 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005956
5957 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005958 return 0;
5959}
5960
5961 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005962pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005964 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965 char_u *key;
5966 dictitem_T *di;
5967 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005968 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969 PyObject *keyObject;
5970 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005972 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005974
5975 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005976 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005978 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005979 {
5980 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005982 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005983
5984 if (!(iterator = PyObject_GetIter(list)))
5985 {
5986 dict_unref(dict);
5987 Py_DECREF(list);
5988 return -1;
5989 }
5990 Py_DECREF(list);
5991
5992 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005993 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005994 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005995
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005996 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005997 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005998 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005999 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006000 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006001 return -1;
6002 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006003
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006004 if (*key == NUL)
6005 {
6006 Py_DECREF(keyObject);
6007 Py_DECREF(iterator);
6008 Py_XDECREF(todecref);
6009 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006010 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006011 return -1;
6012 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006013
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006014 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006015 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006016 Py_DECREF(keyObject);
6017 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006018 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006019 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006020 return -1;
6021 }
6022
6023 di = dictitem_alloc(key);
6024
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006025 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006026 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006027
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006028 if (di == NULL)
6029 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006030 Py_DECREF(iterator);
6031 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006032 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006033 PyErr_NoMemory();
6034 return -1;
6035 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006036
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006037 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006038 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006039 Py_DECREF(iterator);
6040 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006041 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006042 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006043 return -1;
6044 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006045
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006046 Py_DECREF(valObject);
6047
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006048 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006049 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006050 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006051 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006052 dictitem_free(di);
6053 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006054 return -1;
6055 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006056 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006057 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006058 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006059 return 0;
6060}
6061
6062 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006063pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064{
6065 list_T *l;
6066
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006067 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006069
6070 tv->v_type = VAR_LIST;
6071 tv->vval.v_list = l;
6072
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006073 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006074 {
6075 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006077 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006078
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006080 return 0;
6081}
6082
Bram Moolenaardb913952012-06-29 12:54:53 +02006083typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6084
6085 static int
6086convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006087 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006088{
6089 PyObject *capsule;
6090 char hexBuf[sizeof(void *) * 2 + 3];
6091
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006092 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006093
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006094#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006095 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006096#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006097 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006098#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006099 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006100 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006101#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006102 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006103#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006104 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006105#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006106 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6107 {
6108 Py_DECREF(capsule);
6109 tv->v_type = VAR_UNKNOWN;
6110 return -1;
6111 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006112
6113 Py_DECREF(capsule);
6114
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006115 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006116 {
6117 tv->v_type = VAR_UNKNOWN;
6118 return -1;
6119 }
6120 /* As we are not using copy_tv which increments reference count we must
6121 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006122 if (tv->v_type == VAR_DICT)
6123 ++tv->vval.v_dict->dv_refcount;
6124 else if (tv->v_type == VAR_LIST)
6125 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006126 }
6127 else
6128 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006129 typval_T *v;
6130
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006131#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006132 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006133#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006134 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006135#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006136 copy_tv(v, tv);
6137 }
6138 return 0;
6139}
6140
6141 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006142ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6143{
6144 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006145 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006146
6147 if (!(lookup_dict = PyDict_New()))
6148 return -1;
6149
6150 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6151 {
6152 tv->v_type = VAR_DICT;
6153 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6154 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006155 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006156 }
6157 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006158 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006159 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006160 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006161 else
6162 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006163 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006164 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006165 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006166 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006167 }
6168 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006169 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006170}
6171
6172 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006173ConvertFromPySequence(PyObject *obj, typval_T *tv)
6174{
6175 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006176 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006177
6178 if (!(lookup_dict = PyDict_New()))
6179 return -1;
6180
6181 if (PyType_IsSubtype(obj->ob_type, &ListType))
6182 {
6183 tv->v_type = VAR_LIST;
6184 tv->vval.v_list = (((ListObject *)(obj))->list);
6185 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006186 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006187 }
6188 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006189 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006190 else
6191 {
6192 PyErr_FORMAT(PyExc_TypeError,
6193 N_("unable to convert %s to vim list"),
6194 Py_TYPE_NAME(obj));
6195 ret = -1;
6196 }
6197 Py_DECREF(lookup_dict);
6198 return ret;
6199}
6200
6201 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006202ConvertFromPyObject(PyObject *obj, typval_T *tv)
6203{
6204 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006205 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006206
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006207 if (!(lookup_dict = PyDict_New()))
6208 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006209 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006210 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006211 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006212}
6213
6214 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006215_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006216{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006217 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006218 {
6219 tv->v_type = VAR_DICT;
6220 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6221 ++tv->vval.v_dict->dv_refcount;
6222 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006223 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006224 {
6225 tv->v_type = VAR_LIST;
6226 tv->vval.v_list = (((ListObject *)(obj))->list);
6227 ++tv->vval.v_list->lv_refcount;
6228 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006229 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006230 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006231 FunctionObject *func = (FunctionObject *) obj;
6232 if (func->self != NULL || func->argv != NULL)
6233 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006234 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6235
Bram Moolenaar8110a092016-04-14 15:56:09 +02006236 set_partial(func, pt, TRUE);
6237 tv->vval.v_partial = pt;
6238 tv->v_type = VAR_PARTIAL;
6239 }
6240 else
6241 {
6242 if (set_string_copy(func->name, tv) == -1)
6243 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006244
Bram Moolenaar8110a092016-04-14 15:56:09 +02006245 tv->v_type = VAR_FUNC;
6246 }
6247 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006248 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006249 else if (PyBytes_Check(obj))
6250 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006251 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006252
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006253 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006254 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006255 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 return -1;
6257
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006258 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 return -1;
6260
6261 tv->v_type = VAR_STRING;
6262 }
6263 else if (PyUnicode_Check(obj))
6264 {
6265 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006266 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006267
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006268 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006269 if (bytes == NULL)
6270 return -1;
6271
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006272 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006273 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006274 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006275 return -1;
6276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006277 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006278 {
6279 Py_XDECREF(bytes);
6280 return -1;
6281 }
6282 Py_XDECREF(bytes);
6283
6284 tv->v_type = VAR_STRING;
6285 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006286#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006287 else if (PyInt_Check(obj))
6288 {
6289 tv->v_type = VAR_NUMBER;
6290 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006291 if (PyErr_Occurred())
6292 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006293 }
6294#endif
6295 else if (PyLong_Check(obj))
6296 {
6297 tv->v_type = VAR_NUMBER;
6298 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006299 if (PyErr_Occurred())
6300 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006301 }
6302 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006303 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006304#ifdef FEAT_FLOAT
6305 else if (PyFloat_Check(obj))
6306 {
6307 tv->v_type = VAR_FLOAT;
6308 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6309 }
6310#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006311 else if (PyObject_HasAttrString(obj, "keys"))
6312 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006313 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006314 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006315 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006316 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006317 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006318 else if (PyNumber_Check(obj))
6319 {
6320 PyObject *num;
6321
6322 if (!(num = PyNumber_Long(obj)))
6323 return -1;
6324
6325 tv->v_type = VAR_NUMBER;
6326 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6327
6328 Py_DECREF(num);
6329 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006330 else if (obj == Py_None)
6331 {
6332 tv->v_type = VAR_SPECIAL;
6333 tv->vval.v_number = VVAL_NONE;
6334 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006335 else
6336 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006337 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006338 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006339 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006340 return -1;
6341 }
6342 return 0;
6343}
6344
6345 static PyObject *
6346ConvertToPyObject(typval_T *tv)
6347{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006348 typval_T *argv;
6349 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006350 if (tv == NULL)
6351 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006352 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006353 return NULL;
6354 }
6355 switch (tv->v_type)
6356 {
6357 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006358 return PyBytes_FromString(tv->vval.v_string == NULL
6359 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006360 case VAR_NUMBER:
6361 return PyLong_FromLong((long) tv->vval.v_number);
6362#ifdef FEAT_FLOAT
6363 case VAR_FLOAT:
6364 return PyFloat_FromDouble((double) tv->vval.v_float);
6365#endif
6366 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006367 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006368 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006369 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006370 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006371 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006372 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006373 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006374 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006375 if (tv->vval.v_partial->pt_argc)
6376 {
6377 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6378 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6379 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6380 }
6381 else
6382 argv = NULL;
6383 if (tv->vval.v_partial->pt_dict != NULL)
6384 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006385 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006386 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006387 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006388 tv->vval.v_partial->pt_dict,
6389 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006390 case VAR_BLOB:
6391 return PyBytes_FromStringAndSize(
6392 (char*) tv->vval.v_blob->bv_ga.ga_data,
6393 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006394 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006395 case VAR_CHANNEL:
6396 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006397 Py_INCREF(Py_None);
6398 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006399 case VAR_SPECIAL:
6400 switch (tv->vval.v_number)
6401 {
6402 case VVAL_FALSE: return AlwaysFalse(NULL);
6403 case VVAL_TRUE: return AlwaysTrue(NULL);
6404 case VVAL_NONE:
6405 case VVAL_NULL: return AlwaysNone(NULL);
6406 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006407 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006408 return NULL;
6409 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006410 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006411}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006412
6413typedef struct
6414{
6415 PyObject_HEAD
6416} CurrentObject;
6417static PyTypeObject CurrentType;
6418
6419 static void
6420init_structs(void)
6421{
6422 vim_memset(&OutputType, 0, sizeof(OutputType));
6423 OutputType.tp_name = "vim.message";
6424 OutputType.tp_basicsize = sizeof(OutputObject);
6425 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6426 OutputType.tp_doc = "vim message object";
6427 OutputType.tp_methods = OutputMethods;
6428#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006429 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6430 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006431 OutputType.tp_alloc = call_PyType_GenericAlloc;
6432 OutputType.tp_new = call_PyType_GenericNew;
6433 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006434 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006435#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006436 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6437 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006438 // Disabled, because this causes a crash in test86
6439 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006440#endif
6441
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006442 vim_memset(&IterType, 0, sizeof(IterType));
6443 IterType.tp_name = "vim.iter";
6444 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006445 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006446 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006447 IterType.tp_iter = (getiterfunc)IterIter;
6448 IterType.tp_iternext = (iternextfunc)IterNext;
6449 IterType.tp_dealloc = (destructor)IterDestructor;
6450 IterType.tp_traverse = (traverseproc)IterTraverse;
6451 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006452
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006453 vim_memset(&BufferType, 0, sizeof(BufferType));
6454 BufferType.tp_name = "vim.buffer";
6455 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006456 BufferType.tp_dealloc = (destructor)BufferDestructor;
6457 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006458 BufferType.tp_as_sequence = &BufferAsSeq;
6459 BufferType.tp_as_mapping = &BufferAsMapping;
6460 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6461 BufferType.tp_doc = "vim buffer object";
6462 BufferType.tp_methods = BufferMethods;
6463#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006464 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006465 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006466 BufferType.tp_alloc = call_PyType_GenericAlloc;
6467 BufferType.tp_new = call_PyType_GenericNew;
6468 BufferType.tp_free = call_PyObject_Free;
6469#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006470 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006471 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006472#endif
6473
6474 vim_memset(&WindowType, 0, sizeof(WindowType));
6475 WindowType.tp_name = "vim.window";
6476 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006477 WindowType.tp_dealloc = (destructor)WindowDestructor;
6478 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006479 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006480 WindowType.tp_doc = "vim Window object";
6481 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006482 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6483 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006484#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006485 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6486 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006487 WindowType.tp_alloc = call_PyType_GenericAlloc;
6488 WindowType.tp_new = call_PyType_GenericNew;
6489 WindowType.tp_free = call_PyObject_Free;
6490#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006491 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6492 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006493#endif
6494
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006495 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6496 TabPageType.tp_name = "vim.tabpage";
6497 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006498 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6499 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006500 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6501 TabPageType.tp_doc = "vim tab page object";
6502 TabPageType.tp_methods = TabPageMethods;
6503#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006504 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006505 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6506 TabPageType.tp_new = call_PyType_GenericNew;
6507 TabPageType.tp_free = call_PyObject_Free;
6508#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006509 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006510#endif
6511
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006512 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6513 BufMapType.tp_name = "vim.bufferlist";
6514 BufMapType.tp_basicsize = sizeof(BufMapObject);
6515 BufMapType.tp_as_mapping = &BufMapAsMapping;
6516 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006517 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006518 BufferType.tp_doc = "vim buffer list";
6519
6520 vim_memset(&WinListType, 0, sizeof(WinListType));
6521 WinListType.tp_name = "vim.windowlist";
6522 WinListType.tp_basicsize = sizeof(WinListType);
6523 WinListType.tp_as_sequence = &WinListAsSeq;
6524 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6525 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006526 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006527
6528 vim_memset(&TabListType, 0, sizeof(TabListType));
6529 TabListType.tp_name = "vim.tabpagelist";
6530 TabListType.tp_basicsize = sizeof(TabListType);
6531 TabListType.tp_as_sequence = &TabListAsSeq;
6532 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6533 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006534
6535 vim_memset(&RangeType, 0, sizeof(RangeType));
6536 RangeType.tp_name = "vim.range";
6537 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006538 RangeType.tp_dealloc = (destructor)RangeDestructor;
6539 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006540 RangeType.tp_as_sequence = &RangeAsSeq;
6541 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006542 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006543 RangeType.tp_doc = "vim Range object";
6544 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006545 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6546 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006547#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006548 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006549 RangeType.tp_alloc = call_PyType_GenericAlloc;
6550 RangeType.tp_new = call_PyType_GenericNew;
6551 RangeType.tp_free = call_PyObject_Free;
6552#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006553 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006554#endif
6555
6556 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6557 CurrentType.tp_name = "vim.currentdata";
6558 CurrentType.tp_basicsize = sizeof(CurrentObject);
6559 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6560 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006561 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006562#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006563 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6564 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006566 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6567 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568#endif
6569
6570 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6571 DictionaryType.tp_name = "vim.dictionary";
6572 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006573 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006574 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006575 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006576 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006577 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6578 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006579 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6580 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6581 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006583 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6584 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006585#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006586 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6587 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588#endif
6589
6590 vim_memset(&ListType, 0, sizeof(ListType));
6591 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006592 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006593 ListType.tp_basicsize = sizeof(ListObject);
6594 ListType.tp_as_sequence = &ListAsSeq;
6595 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006596 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006597 ListType.tp_doc = "list pushing modifications to vim structure";
6598 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006599 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006600 ListType.tp_new = (newfunc)ListConstructor;
6601 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006602#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 ListType.tp_getattro = (getattrofunc)ListGetattro;
6604 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006605#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006606 ListType.tp_getattr = (getattrfunc)ListGetattr;
6607 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608#endif
6609
6610 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006611 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006612 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006613 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6614 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006615 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616 FunctionType.tp_doc = "object that calls vim function";
6617 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006618 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006619 FunctionType.tp_new = (newfunc)FunctionConstructor;
6620 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006621#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006622 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006624 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006625#endif
6626
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006627 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6628 OptionsType.tp_name = "vim.options";
6629 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006630 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006631 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006632 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006633 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006634 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006635 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6636 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6637 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006638
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006639#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006640 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6641 LoaderType.tp_name = "vim.Loader";
6642 LoaderType.tp_basicsize = sizeof(LoaderObject);
6643 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6644 LoaderType.tp_doc = "vim message object";
6645 LoaderType.tp_methods = LoaderMethods;
6646 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006647#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006648
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006649#if PY_MAJOR_VERSION >= 3
6650 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6651 vimmodule.m_name = "vim";
6652 vimmodule.m_doc = "Vim Python interface\n";
6653 vimmodule.m_size = -1;
6654 vimmodule.m_methods = VimMethods;
6655#endif
6656}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006657
6658#define PYTYPE_READY(type) \
6659 if (PyType_Ready(&type)) \
6660 return -1;
6661
6662 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006663init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006664{
6665 PYTYPE_READY(IterType);
6666 PYTYPE_READY(BufferType);
6667 PYTYPE_READY(RangeType);
6668 PYTYPE_READY(WindowType);
6669 PYTYPE_READY(TabPageType);
6670 PYTYPE_READY(BufMapType);
6671 PYTYPE_READY(WinListType);
6672 PYTYPE_READY(TabListType);
6673 PYTYPE_READY(CurrentType);
6674 PYTYPE_READY(DictionaryType);
6675 PYTYPE_READY(ListType);
6676 PYTYPE_READY(FunctionType);
6677 PYTYPE_READY(OptionsType);
6678 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006679#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006680 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006681#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006682 return 0;
6683}
6684
6685 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006686init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006687{
6688 PyObject *path;
6689 PyObject *path_hook;
6690 PyObject *path_hooks;
6691
6692 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6693 return -1;
6694
6695 if (!(path_hooks = PySys_GetObject("path_hooks")))
6696 {
6697 PyErr_Clear();
6698 path_hooks = PyList_New(1);
6699 PyList_SET_ITEM(path_hooks, 0, path_hook);
6700 if (PySys_SetObject("path_hooks", path_hooks))
6701 {
6702 Py_DECREF(path_hooks);
6703 return -1;
6704 }
6705 Py_DECREF(path_hooks);
6706 }
6707 else if (PyList_Check(path_hooks))
6708 {
6709 if (PyList_Append(path_hooks, path_hook))
6710 {
6711 Py_DECREF(path_hook);
6712 return -1;
6713 }
6714 Py_DECREF(path_hook);
6715 }
6716 else
6717 {
6718 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006719 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006720 "You should now do the following:\n"
6721 "- append vim.path_hook to sys.path_hooks\n"
6722 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6723 VimTryEnd(); /* Discard the error */
6724 Py_DECREF(path_hook);
6725 return 0;
6726 }
6727
6728 if (!(path = PySys_GetObject("path")))
6729 {
6730 PyErr_Clear();
6731 path = PyList_New(1);
6732 Py_INCREF(vim_special_path_object);
6733 PyList_SET_ITEM(path, 0, vim_special_path_object);
6734 if (PySys_SetObject("path", path))
6735 {
6736 Py_DECREF(path);
6737 return -1;
6738 }
6739 Py_DECREF(path);
6740 }
6741 else if (PyList_Check(path))
6742 {
6743 if (PyList_Append(path, vim_special_path_object))
6744 return -1;
6745 }
6746 else
6747 {
6748 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006749 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006750 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6751 VimTryEnd(); /* Discard the error */
6752 }
6753
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006754 return 0;
6755}
6756
6757static BufMapObject TheBufferMap =
6758{
6759 PyObject_HEAD_INIT(&BufMapType)
6760};
6761
6762static WinListObject TheWindowList =
6763{
6764 PyObject_HEAD_INIT(&WinListType)
6765 NULL
6766};
6767
6768static CurrentObject TheCurrent =
6769{
6770 PyObject_HEAD_INIT(&CurrentType)
6771};
6772
6773static TabListObject TheTabPageList =
6774{
6775 PyObject_HEAD_INIT(&TabListType)
6776};
6777
6778static struct numeric_constant {
6779 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006780 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006781} numeric_constants[] = {
6782 {"VAR_LOCKED", VAR_LOCKED},
6783 {"VAR_FIXED", VAR_FIXED},
6784 {"VAR_SCOPE", VAR_SCOPE},
6785 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6786};
6787
6788static struct object_constant {
6789 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006790 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006791} object_constants[] = {
6792 {"buffers", (PyObject *)(void *)&TheBufferMap},
6793 {"windows", (PyObject *)(void *)&TheWindowList},
6794 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6795 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006796
6797 {"Buffer", (PyObject *)&BufferType},
6798 {"Range", (PyObject *)&RangeType},
6799 {"Window", (PyObject *)&WindowType},
6800 {"TabPage", (PyObject *)&TabPageType},
6801 {"Dictionary", (PyObject *)&DictionaryType},
6802 {"List", (PyObject *)&ListType},
6803 {"Function", (PyObject *)&FunctionType},
6804 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006805#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006806 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006807#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006808};
6809
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006810#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006811 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006812 return -1;
6813
6814#define ADD_CHECKED_OBJECT(m, name, obj) \
6815 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006816 PyObject *valObject = obj; \
6817 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006818 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006819 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006820 }
6821
6822 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006823populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006824{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006825 int i;
6826 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006827 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006828 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006829#if PY_VERSION_HEX >= 0x030700f0
6830 PyObject *dict;
6831 PyObject *cls;
6832#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006833
6834 for (i = 0; i < (int)(sizeof(numeric_constants)
6835 / sizeof(struct numeric_constant));
6836 ++i)
6837 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006838 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006839
6840 for (i = 0; i < (int)(sizeof(object_constants)
6841 / sizeof(struct object_constant));
6842 ++i)
6843 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006844 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006845
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006846 valObject = object_constants[i].valObject;
6847 Py_INCREF(valObject);
6848 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006849 }
6850
6851 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6852 return -1;
6853 ADD_OBJECT(m, "error", VimError);
6854
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006855 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6856 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006857 ADD_CHECKED_OBJECT(m, "options",
6858 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006859
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006860 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006861 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006862 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006863
Bram Moolenaar22081f42016-06-01 20:38:34 +02006864#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006865 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006866 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006867#else
6868 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6869 return -1;
6870#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006871 ADD_OBJECT(m, "_getcwd", py_getcwd)
6872
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006873 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006874 return -1;
6875 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006876 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006877 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006878 if (PyObject_SetAttrString(other_module, "chdir", attr))
6879 {
6880 Py_DECREF(attr);
6881 return -1;
6882 }
6883 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006884
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006885 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006886 {
6887 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006888 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006890 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6891 {
6892 Py_DECREF(attr);
6893 return -1;
6894 }
6895 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006897 else
6898 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006899
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006900 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6901 return -1;
6902
6903 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6904
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006905#if PY_VERSION_HEX >= 0x030700f0
6906 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6907 return -1;
6908
6909 dict = PyModule_GetDict(imp);
6910
6911 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6912 {
6913 Py_DECREF(imp);
6914 return -1;
6915 }
6916
6917 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6918 {
6919 Py_DECREF(imp);
6920 return -1;
6921 }
6922
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006923 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6924 {
6925 // find_module() is deprecated, this may stop working in some later
6926 // version.
6927 ADD_OBJECT(m, "_find_module", py_find_module);
6928 }
6929
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006930 Py_DECREF(imp);
6931
6932 ADD_OBJECT(m, "_find_spec", py_find_spec);
6933#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006934 if (!(imp = PyImport_ImportModule("imp")))
6935 return -1;
6936
6937 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6938 {
6939 Py_DECREF(imp);
6940 return -1;
6941 }
6942
6943 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6944 {
6945 Py_DECREF(py_find_module);
6946 Py_DECREF(imp);
6947 return -1;
6948 }
6949
6950 Py_DECREF(imp);
6951
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006952 ADD_OBJECT(m, "_find_module", py_find_module);
6953 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006954#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006955
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006956 return 0;
6957}