blob: d836c7c91b882abe01ece312850e49fc54fe120f [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
378 /* Write each NL separated line. Text after the last NL is kept for
379 * writing later. */
380 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
381 {
382 PyInt len = ptr - str;
383
384 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
385 break;
386
387 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
388 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
389 fn((char_u *)io_ga.ga_data);
390 str = ptr + 1;
391 n -= len + 1;
392 io_ga.ga_len = 0;
393 }
394
395 /* Put the remaining text into io_ga for later printing. */
396 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
397 {
398 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
399 io_ga.ga_len += (int)n;
400 }
401}
402
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200403 static int
404write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200405{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200406 Py_ssize_t len = 0;
407 char *str = NULL;
408 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
411 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200412
413 Py_BEGIN_ALLOW_THREADS
414 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200415 if (error)
416 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100417 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200418 Python_Release_Vim();
419 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200420 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200421
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200422 return 0;
423}
424
425 static PyObject *
426OutputWrite(OutputObject *self, PyObject *string)
427{
428 if (write_output(self, string))
429 return NULL;
430
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200431 Py_INCREF(Py_None);
432 return Py_None;
433}
434
435 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200436OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200437{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200438 PyObject *iterator;
439 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200446 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200447 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200448 Py_DECREF(iterator);
449 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450 return NULL;
451 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200452 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200453 }
454
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(iterator);
456
457 /* Iterator may have finished due to an exception */
458 if (PyErr_Occurred())
459 return NULL;
460
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200461 Py_INCREF(Py_None);
462 return Py_None;
463}
464
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100465 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100466AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100467{
468 /* do nothing */
469 Py_INCREF(Py_None);
470 return Py_None;
471}
472
Bram Moolenaard4247472015-11-02 13:28:59 +0100473 static PyObject *
474AlwaysFalse(PyObject *self UNUSED)
475{
476 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100477 PyObject *ret = Py_False;
478 Py_INCREF(ret);
479 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100480}
481
482 static PyObject *
483AlwaysTrue(PyObject *self UNUSED)
484{
485 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100486 PyObject *ret = Py_True;
487 Py_INCREF(ret);
488 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100489}
490
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200491/***************/
492
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200493static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200494 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200495 {"write", (PyCFunction)OutputWrite, METH_O, ""},
496 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100497 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
498 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
499 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
500 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
501 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
502 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200503 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200504 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200505 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200506};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200507
508static OutputObject Output =
509{
510 PyObject_HEAD_INIT(&OutputType)
511 0,
512 0
513};
514
515static OutputObject Error =
516{
517 PyObject_HEAD_INIT(&OutputType)
518 0,
519 1
520};
521
522 static int
523PythonIO_Init_io(void)
524{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200525 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
526 return -1;
527 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
528 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200529
530 if (PyErr_Occurred())
531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100532 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200533 return -1;
534 }
535
536 return 0;
537}
538
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200539#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200540static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
541
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200542typedef struct
543{
544 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200545 char *fullname;
546 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200547} LoaderObject;
548static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200549
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200550 static void
551LoaderDestructor(LoaderObject *self)
552{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200553 vim_free(self->fullname);
554 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200555 DESTRUCTOR_FINISH(self);
556}
557
558 static PyObject *
559LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
560{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200561 char *fullname = self->fullname;
562 PyObject *result = self->result;
563 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200564
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200565 if (!fullname)
566 {
567 module = result ? result : Py_None;
568 Py_INCREF(module);
569 return module;
570 }
571
572 module = call_load_module(fullname, (int)STRLEN(fullname), result);
573
574 self->fullname = NULL;
575 self->result = module;
576
577 vim_free(fullname);
578 Py_DECREF(result);
579
580 if (!module)
581 {
582 if (PyErr_Occurred())
583 return NULL;
584
585 Py_INCREF(Py_None);
586 return Py_None;
587 }
588
589 Py_INCREF(module);
590 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200591}
592
593static struct PyMethodDef LoaderMethods[] = {
594 /* name, function, calling, doc */
595 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
596 { NULL, NULL, 0, NULL}
597};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200598#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200599
600/* Check to see whether a Vim error has been reported, or a keyboard
601 * interrupt has been detected.
602 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200603
604 static void
605VimTryStart(void)
606{
607 ++trylevel;
608}
609
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200610 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200611VimTryEnd(void)
612{
613 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100614 /* Without this it stops processing all subsequent Vim script commands and
615 * generates strange error messages if I e.g. try calling Test() in a cycle
616 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200617 did_emsg = FALSE;
618 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200619 if (got_int)
620 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100621 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100622 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100623 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200624 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200625 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200626 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100627 else if (msg_list != NULL && *msg_list != NULL)
628 {
629 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100630 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100631
632 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
633
634 if (msg == NULL)
635 {
636 PyErr_NoMemory();
637 return -1;
638 }
639
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100640 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100641
642 free_global_msglist();
643
644 if (should_free)
645 vim_free(msg);
646
647 return -1;
648 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200649 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200650 return (PyErr_Occurred() ? -1 : 0);
651 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200652 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200653 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100654 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200655 return -1;
656 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100657 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200658 else
659 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200660 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200661 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200662 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200663 }
664}
665
666 static int
667VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200668{
669 if (got_int)
670 {
671 PyErr_SetNone(PyExc_KeyboardInterrupt);
672 return 1;
673 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200674 return 0;
675}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200676
677/* Vim module - Implementation
678 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200679
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200680 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200681VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200682{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200683 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200684 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200685 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200686
Bram Moolenaar389a1792013-06-23 13:00:44 +0200687 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688 return NULL;
689
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200690 Py_BEGIN_ALLOW_THREADS
691 Python_Lock_Vim();
692
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200693 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200694 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695 update_screen(VALID);
696
697 Python_Release_Vim();
698 Py_END_ALLOW_THREADS
699
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200700 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200701 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200703 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200705 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200706 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200707 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200708}
709
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200710/*
711 * Function to translate a typval_T into a PyObject; this will recursively
712 * translate lists/dictionaries into their Python equivalents.
713 *
714 * The depth parameter is to avoid infinite recursion, set it to 1 when
715 * you call VimToPython.
716 */
717 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200718VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200719{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200720 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200722 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200723
724 /* Avoid infinite recursion */
725 if (depth > 100)
726 {
727 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200728 ret = Py_None;
729 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730 }
731
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200732 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200733 * then and we can use it again. */
734 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
735 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
736 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200737 sprintf(ptrBuf, "%p",
738 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
739 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200740
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200741 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200742 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200743 Py_INCREF(ret);
744 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 }
746 }
747
748 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200749 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200750 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200751 else if (our_tv->v_type == VAR_NUMBER)
752 {
753 char buf[NUMBUFLEN];
754
755 /* For backwards compatibility numbers are stored as strings. */
756 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200757 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100759#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200760 else if (our_tv->v_type == VAR_FLOAT)
761 {
762 char buf[NUMBUFLEN];
763
764 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200765 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200766 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100767#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768 else if (our_tv->v_type == VAR_LIST)
769 {
770 list_T *list = our_tv->vval.v_list;
771 listitem_T *curr;
772
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200773 if (list == NULL)
774 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200775
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200776 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200777 return NULL;
778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200779 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200781 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200782 return NULL;
783 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200784
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
786 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200787 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200788 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200789 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200790 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200792 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200793 {
794 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 return NULL;
797 }
798 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200799 }
800 }
801 else if (our_tv->v_type == VAR_DICT)
802 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100804 hashtab_T *ht;
805 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200806 hashitem_T *hi;
807 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100808
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200809 if (our_tv->vval.v_dict == NULL)
810 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100811 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200812
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200813 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200814 return NULL;
815
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200816 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200817 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200818 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200819 return NULL;
820 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200821
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100822 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200823 for (hi = ht->ht_array; todo > 0; ++hi)
824 {
825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200826 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200827 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200828
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200829 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200830 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200831 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200832 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200833 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200834 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200835 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
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 Py_DECREF(newObj);
839 return NULL;
840 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841 }
842 }
843 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100844 else if (our_tv->v_type == VAR_SPECIAL)
845 {
846 if (our_tv->vval.v_number == VVAL_FALSE)
847 {
848 ret = Py_False;
849 Py_INCREF(ret);
850 }
851 else if (our_tv->vval.v_number == VVAL_TRUE)
852 {
853 ret = Py_True;
854 Py_INCREF(ret);
855 }
856 else
857 {
858 Py_INCREF(Py_None);
859 ret = Py_None;
860 }
861 return ret;
862 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100863 else if (our_tv->v_type == VAR_BLOB)
864 ret = PyBytes_FromStringAndSize(
865 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
866 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200867 else
868 {
869 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200870 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200871 }
872
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200873 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200875
876 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200877VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200879 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200880 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200881 PyObject *string;
882 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200883 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200884 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885
Bram Moolenaar389a1792013-06-23 13:00:44 +0200886 if (!PyArg_ParseTuple(args, "O", &string))
887 return NULL;
888
889 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200890 return NULL;
891
892 Py_BEGIN_ALLOW_THREADS
893 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200894 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200895 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200896 Python_Release_Vim();
897 Py_END_ALLOW_THREADS
898
Bram Moolenaar389a1792013-06-23 13:00:44 +0200899 Py_XDECREF(todecref);
900
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200901 if (VimTryEnd())
902 return NULL;
903
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200904 if (our_tv == NULL)
905 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200906 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200907 return NULL;
908 }
909
910 /* Convert the Vim type into a Python type. Create a dictionary that's
911 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200912 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200913 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200914 else
915 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200916 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200917 Py_DECREF(lookup_dict);
918 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200919
920
921 Py_BEGIN_ALLOW_THREADS
922 Python_Lock_Vim();
923 free_tv(our_tv);
924 Python_Release_Vim();
925 Py_END_ALLOW_THREADS
926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200928}
929
Bram Moolenaardb913952012-06-29 12:54:53 +0200930static PyObject *ConvertToPyObject(typval_T *);
931
932 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200933VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200934{
Bram Moolenaardb913952012-06-29 12:54:53 +0200935 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200936 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200937 char_u *expr;
938 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200939
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200941 return NULL;
942
943 Py_BEGIN_ALLOW_THREADS
944 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200945 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200946 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200947 Python_Release_Vim();
948 Py_END_ALLOW_THREADS
949
Bram Moolenaar389a1792013-06-23 13:00:44 +0200950 Py_XDECREF(todecref);
951
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200952 if (VimTryEnd())
953 return NULL;
954
Bram Moolenaardb913952012-06-29 12:54:53 +0200955 if (our_tv == NULL)
956 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200957 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200958 return NULL;
959 }
960
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200961 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 Py_BEGIN_ALLOW_THREADS
963 Python_Lock_Vim();
964 free_tv(our_tv);
965 Python_Release_Vim();
966 Py_END_ALLOW_THREADS
967
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200969}
970
971 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200972VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200973{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200974 char_u *str;
975 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200976 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200977
Bram Moolenaar389a1792013-06-23 13:00:44 +0200978 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200979 return NULL;
980
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200981 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200982
983 Py_XDECREF(todecref);
984
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200985 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200986}
987
Bram Moolenaarf4258302013-06-02 18:20:17 +0200988 static PyObject *
989_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
990{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200991 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200992 PyObject *newwd;
993 PyObject *todecref;
994 char_u *new_dir;
995
Bram Moolenaard4209d22013-06-05 20:34:15 +0200996 if (_chdir == NULL)
997 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200998 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200999 return NULL;
1000
1001 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1002 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001003 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001004 return NULL;
1005 }
1006
1007 if (!(new_dir = StringToChars(newwd, &todecref)))
1008 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001009 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001010 Py_DECREF(newwd);
1011 return NULL;
1012 }
1013
1014 VimTryStart();
1015
1016 if (vim_chdir(new_dir))
1017 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001018 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001019 Py_DECREF(newwd);
1020 Py_XDECREF(todecref);
1021
1022 if (VimTryEnd())
1023 return NULL;
1024
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001025 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001026 return NULL;
1027 }
1028
1029 Py_DECREF(newwd);
1030 Py_XDECREF(todecref);
1031
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001032 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001033
1034 if (VimTryEnd())
1035 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001036 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001037 return NULL;
1038 }
1039
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001040 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001041}
1042
1043 static PyObject *
1044VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1045{
1046 return _VimChdir(py_chdir, args, kwargs);
1047}
1048
1049 static PyObject *
1050VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1051{
1052 return _VimChdir(py_fchdir, args, kwargs);
1053}
1054
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001055typedef struct {
1056 PyObject *callable;
1057 PyObject *result;
1058} map_rtp_data;
1059
1060 static void
1061map_rtp_callback(char_u *path, void *_data)
1062{
1063 void **data = (void **) _data;
1064 PyObject *pathObject;
1065 map_rtp_data *mr_data = *((map_rtp_data **) data);
1066
Bram Moolenaar41009372013-07-01 22:03:04 +02001067 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001068 {
1069 *data = NULL;
1070 return;
1071 }
1072
1073 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1074 pathObject, NULL);
1075
1076 Py_DECREF(pathObject);
1077
1078 if (!mr_data->result || mr_data->result != Py_None)
1079 *data = NULL;
1080 else
1081 {
1082 Py_DECREF(mr_data->result);
1083 mr_data->result = NULL;
1084 }
1085}
1086
1087 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001088VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001089{
1090 map_rtp_data data;
1091
Bram Moolenaar389a1792013-06-23 13:00:44 +02001092 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001093 data.result = NULL;
1094
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001095 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001096
1097 if (data.result == NULL)
1098 {
1099 if (PyErr_Occurred())
1100 return NULL;
1101 else
1102 {
1103 Py_INCREF(Py_None);
1104 return Py_None;
1105 }
1106 }
1107 return data.result;
1108}
1109
1110/*
1111 * _vim_runtimepath_ special path implementation.
1112 */
1113
1114 static void
1115map_finder_callback(char_u *path, void *_data)
1116{
1117 void **data = (void **) _data;
1118 PyObject *list = *((PyObject **) data);
1119 PyObject *pathObject1, *pathObject2;
1120 char *pathbuf;
1121 size_t pathlen;
1122
1123 pathlen = STRLEN(path);
1124
1125#if PY_MAJOR_VERSION < 3
1126# define PY_MAIN_DIR_STRING "python2"
1127#else
1128# define PY_MAIN_DIR_STRING "python3"
1129#endif
1130#define PY_ALTERNATE_DIR_STRING "pythonx"
1131
1132#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1133 if (!(pathbuf = PyMem_New(char,
1134 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1135 {
1136 PyErr_NoMemory();
1137 *data = NULL;
1138 return;
1139 }
1140
1141 mch_memmove(pathbuf, path, pathlen + 1);
1142 add_pathsep((char_u *) pathbuf);
1143
1144 pathlen = STRLEN(pathbuf);
1145 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1146 PYTHONX_STRING_LENGTH + 1);
1147
1148 if (!(pathObject1 = PyString_FromString(pathbuf)))
1149 {
1150 *data = NULL;
1151 PyMem_Free(pathbuf);
1152 return;
1153 }
1154
1155 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1156 PYTHONX_STRING_LENGTH + 1);
1157
1158 if (!(pathObject2 = PyString_FromString(pathbuf)))
1159 {
1160 Py_DECREF(pathObject1);
1161 PyMem_Free(pathbuf);
1162 *data = NULL;
1163 return;
1164 }
1165
1166 PyMem_Free(pathbuf);
1167
1168 if (PyList_Append(list, pathObject1)
1169 || PyList_Append(list, pathObject2))
1170 *data = NULL;
1171
1172 Py_DECREF(pathObject1);
1173 Py_DECREF(pathObject2);
1174}
1175
1176 static PyObject *
1177Vim_GetPaths(PyObject *self UNUSED)
1178{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001179 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001180
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001181 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001182 return NULL;
1183
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001184 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001185
1186 if (PyErr_Occurred())
1187 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001188 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001189 return NULL;
1190 }
1191
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001192 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001193}
1194
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001195#if PY_VERSION_HEX >= 0x030700f0
1196 static PyObject *
1197FinderFindSpec(PyObject *self, PyObject *args)
1198{
1199 char *fullname;
1200 PyObject *paths;
1201 PyObject *target = Py_None;
1202 PyObject *spec;
1203
1204 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1205 return NULL;
1206
1207 if (!(paths = Vim_GetPaths(self)))
1208 return NULL;
1209
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001210 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001211
1212 Py_DECREF(paths);
1213
1214 if (!spec)
1215 {
1216 if (PyErr_Occurred())
1217 return NULL;
1218
1219 Py_INCREF(Py_None);
1220 return Py_None;
1221 }
1222
1223 return spec;
1224}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001225
1226 static PyObject *
1227FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1228{
1229 // Apparently returning None works.
1230 Py_INCREF(Py_None);
1231 return Py_None;
1232}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001233#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001234 static PyObject *
1235call_load_module(char *name, int len, PyObject *find_module_result)
1236{
1237 PyObject *fd, *pathname, *description;
1238
Bram Moolenaarc476e522013-06-23 13:46:40 +02001239 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001240 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001241 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001242 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001243 Py_TYPE_NAME(find_module_result));
1244 return NULL;
1245 }
1246 if (PyTuple_GET_SIZE(find_module_result) != 3)
1247 {
1248 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001249 N_("expected 3-tuple as imp.find_module() result, but got "
1250 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001251 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001252 return NULL;
1253 }
1254
1255 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1256 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1257 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1258 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001259 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001260 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001261 return NULL;
1262 }
1263
1264 return PyObject_CallFunction(py_load_module,
1265 "s#OOO", name, len, fd, pathname, description);
1266}
1267
1268 static PyObject *
1269find_module(char *fullname, char *tail, PyObject *new_path)
1270{
1271 PyObject *find_module_result;
1272 PyObject *module;
1273 char *dot;
1274
Bram Moolenaar41009372013-07-01 22:03:04 +02001275 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001276 {
1277 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001278 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001279 * first component
1280 */
1281 PyObject *newest_path;
1282 int partlen = (int) (dot - 1 - tail);
1283
1284 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1285 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001286 {
1287 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1288 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001289 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001290 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001291
1292 if (!(module = call_load_module(
1293 fullname,
1294 ((int) (tail - fullname)) + partlen,
1295 find_module_result)))
1296 {
1297 Py_DECREF(find_module_result);
1298 return NULL;
1299 }
1300
1301 Py_DECREF(find_module_result);
1302
1303 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1304 {
1305 Py_DECREF(module);
1306 return NULL;
1307 }
1308
1309 Py_DECREF(module);
1310
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001311 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001312
1313 Py_DECREF(newest_path);
1314
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001315 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001316 }
1317 else
1318 {
1319 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1320 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001321 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001322 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1323 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001324 return NULL;
1325 }
1326
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001327 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001328 }
1329}
1330
1331 static PyObject *
1332FinderFindModule(PyObject *self, PyObject *args)
1333{
1334 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001335 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001336 PyObject *new_path;
1337 LoaderObject *loader;
1338
1339 if (!PyArg_ParseTuple(args, "s", &fullname))
1340 return NULL;
1341
1342 if (!(new_path = Vim_GetPaths(self)))
1343 return NULL;
1344
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001345 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001346
1347 Py_DECREF(new_path);
1348
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001349 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001350 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001351 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001352 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001353
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001354 Py_INCREF(Py_None);
1355 return Py_None;
1356 }
1357
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001358 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001359 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001360 Py_DECREF(result);
1361 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001362 return NULL;
1363 }
1364
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001365 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1366 {
1367 vim_free(fullname);
1368 Py_DECREF(result);
1369 return NULL;
1370 }
1371
1372 loader->fullname = fullname;
1373 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001374
1375 return (PyObject *) loader;
1376}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001377#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001378
1379 static PyObject *
1380VimPathHook(PyObject *self UNUSED, PyObject *args)
1381{
1382 char *path;
1383
1384 if (PyArg_ParseTuple(args, "s", &path)
1385 && STRCMP(path, vim_special_path) == 0)
1386 {
1387 Py_INCREF(vim_module);
1388 return vim_module;
1389 }
1390
1391 PyErr_Clear();
1392 PyErr_SetNone(PyExc_ImportError);
1393 return NULL;
1394}
1395
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001396/*
1397 * Vim module - Definitions
1398 */
1399
1400static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001401 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001402 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001403 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001404 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1405 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001406 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1407 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001408 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001409#if PY_VERSION_HEX >= 0x030700f0
1410 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001411#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001412 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001413 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1414 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1415 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001416};
1417
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001418/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001419 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001420 */
1421
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001422static PyTypeObject IterType;
1423
1424typedef PyObject *(*nextfun)(void **);
1425typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001426typedef int (*traversefun)(void *, visitproc, void *);
1427typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001428
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001429/* Main purpose of this object is removing the need for do python
1430 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1431 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001432
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001433typedef struct
1434{
1435 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001436 void *cur;
1437 nextfun next;
1438 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001439 traversefun traverse;
1440 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001441} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001442
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001443 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001444IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1445 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001446{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001447 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001448
Bram Moolenaar774267b2013-05-21 20:51:59 +02001449 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001450 self->cur = start;
1451 self->next = next;
1452 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001453 self->traverse = traverse;
1454 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001455
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001456 return (PyObject *)(self);
1457}
1458
1459 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001460IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001461{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001462 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001463 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001464 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001465}
1466
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001467 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001468IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001469{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001470 if (self->traverse != NULL)
1471 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001472 else
1473 return 0;
1474}
1475
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001476/* Mac OSX defines clear() somewhere. */
1477#ifdef clear
1478# undef clear
1479#endif
1480
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001481 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001482IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001483{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001484 if (self->clear != NULL)
1485 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001486 else
1487 return 0;
1488}
1489
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001490 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001491IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001492{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001493 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001494}
1495
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001496 static PyObject *
1497IterIter(PyObject *self)
1498{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001499 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001500 return self;
1501}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001502
Bram Moolenaardb913952012-06-29 12:54:53 +02001503typedef struct pylinkedlist_S {
1504 struct pylinkedlist_S *pll_next;
1505 struct pylinkedlist_S *pll_prev;
1506 PyObject *pll_obj;
1507} pylinkedlist_T;
1508
1509static pylinkedlist_T *lastdict = NULL;
1510static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001511static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001512
1513 static void
1514pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1515{
1516 if (ref->pll_prev == NULL)
1517 {
1518 if (ref->pll_next == NULL)
1519 {
1520 *last = NULL;
1521 return;
1522 }
1523 }
1524 else
1525 ref->pll_prev->pll_next = ref->pll_next;
1526
1527 if (ref->pll_next == NULL)
1528 *last = ref->pll_prev;
1529 else
1530 ref->pll_next->pll_prev = ref->pll_prev;
1531}
1532
1533 static void
1534pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1535{
1536 if (*last == NULL)
1537 ref->pll_prev = NULL;
1538 else
1539 {
1540 (*last)->pll_next = ref;
1541 ref->pll_prev = *last;
1542 }
1543 ref->pll_next = NULL;
1544 ref->pll_obj = self;
1545 *last = ref;
1546}
1547
1548static PyTypeObject DictionaryType;
1549
1550typedef struct
1551{
1552 PyObject_HEAD
1553 dict_T *dict;
1554 pylinkedlist_T ref;
1555} DictionaryObject;
1556
Bram Moolenaara9922d62013-05-30 13:01:18 +02001557static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1558
1559#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1560
Bram Moolenaardb913952012-06-29 12:54:53 +02001561 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001563{
1564 DictionaryObject *self;
1565
Bram Moolenaara9922d62013-05-30 13:01:18 +02001566 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001567 if (self == NULL)
1568 return NULL;
1569 self->dict = dict;
1570 ++dict->dv_refcount;
1571
1572 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1573
1574 return (PyObject *)(self);
1575}
1576
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001578py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001579{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001580 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001582 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001583 {
1584 PyErr_NoMemory();
1585 return NULL;
1586 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001587 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001588
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001589 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590}
1591
1592 static PyObject *
1593DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1594{
1595 DictionaryObject *self;
1596 dict_T *dict;
1597
1598 if (!(dict = py_dict_alloc()))
1599 return NULL;
1600
1601 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1602
1603 --dict->dv_refcount;
1604
1605 if (kwargs || PyTuple_Size(args))
1606 {
1607 PyObject *tmp;
1608 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1609 {
1610 Py_DECREF(self);
1611 return NULL;
1612 }
1613
1614 Py_DECREF(tmp);
1615 }
1616
1617 return (PyObject *)(self);
1618}
1619
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001620 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001621DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001622{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001623 pyll_remove(&self->ref, &lastdict);
1624 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001625
1626 DESTRUCTOR_FINISH(self);
1627}
1628
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001629static char *DictionaryAttrs[] = {
1630 "locked", "scope",
1631 NULL
1632};
1633
1634 static PyObject *
1635DictionaryDir(PyObject *self)
1636{
1637 return ObjectDir(self, DictionaryAttrs);
1638}
1639
Bram Moolenaardb913952012-06-29 12:54:53 +02001640 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001641DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001642{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001643 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001644 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001645 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001646 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001647 return -1;
1648 }
1649
1650 if (strcmp(name, "locked") == 0)
1651 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001652 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001653 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001654 PyErr_SET_STRING(PyExc_TypeError,
1655 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001656 return -1;
1657 }
1658 else
1659 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001660 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001661 if (istrue == -1)
1662 return -1;
1663 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001664 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001665 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001666 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001667 }
1668 return 0;
1669 }
1670 else
1671 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001672 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001673 return -1;
1674 }
1675}
1676
1677 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001678DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001679{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001680 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001681}
1682
Bram Moolenaara9922d62013-05-30 13:01:18 +02001683#define DICT_FLAG_HAS_DEFAULT 0x01
1684#define DICT_FLAG_POP 0x02
1685#define DICT_FLAG_NONE_DEFAULT 0x04
1686#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1687#define DICT_FLAG_RETURN_PAIR 0x10
1688
Bram Moolenaardb913952012-06-29 12:54:53 +02001689 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001690_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001691{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692 PyObject *keyObject;
1693 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001694 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001695 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001696 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697 dict_T *dict = self->dict;
1698 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001699 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001700
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701 if (flags & DICT_FLAG_HAS_DEFAULT)
1702 {
1703 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1704 return NULL;
1705 }
1706 else
1707 keyObject = args;
1708
1709 if (flags & DICT_FLAG_RETURN_BOOL)
1710 defObject = Py_False;
1711
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001712 if (!(key = StringToChars(keyObject, &todecref)))
1713 return NULL;
1714
1715 if (*key == NUL)
1716 {
1717 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001718 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001719 return NULL;
1720 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001721
Bram Moolenaara9922d62013-05-30 13:01:18 +02001722 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001723
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001724 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001725
Bram Moolenaara9922d62013-05-30 13:01:18 +02001726 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001727 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001728 if (defObject)
1729 {
1730 Py_INCREF(defObject);
1731 return defObject;
1732 }
1733 else
1734 {
1735 PyErr_SetObject(PyExc_KeyError, keyObject);
1736 return NULL;
1737 }
1738 }
1739 else if (flags & DICT_FLAG_RETURN_BOOL)
1740 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001741 ret = Py_True;
1742 Py_INCREF(ret);
1743 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001744 }
1745
1746 di = dict_lookup(hi);
1747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001748 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001749 return NULL;
1750
1751 if (flags & DICT_FLAG_POP)
1752 {
1753 if (dict->dv_lock)
1754 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001755 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001756 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001757 return NULL;
1758 }
1759
1760 hash_remove(&dict->dv_hashtab, hi);
1761 dictitem_free(di);
1762 }
1763
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001764 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001765}
1766
1767 static PyObject *
1768DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1769{
1770 return _DictionaryItem(self, keyObject, 0);
1771}
1772
1773 static int
1774DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1775{
1776 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001777 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001779 if (rObj == NULL)
1780 return -1;
1781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001782 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001783
Bram Moolenaardee2e312013-06-23 16:35:47 +02001784 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001786 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001787}
1788
1789typedef struct
1790{
1791 hashitem_T *ht_array;
1792 long_u ht_used;
1793 hashtab_T *ht;
1794 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001795 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796} dictiterinfo_T;
1797
1798 static PyObject *
1799DictionaryIterNext(dictiterinfo_T **dii)
1800{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001801 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001802
1803 if (!(*dii)->todo)
1804 return NULL;
1805
1806 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1807 (*dii)->ht->ht_used != (*dii)->ht_used)
1808 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001809 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001810 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001811 return NULL;
1812 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001813
Bram Moolenaara9922d62013-05-30 13:01:18 +02001814 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1815 ++((*dii)->hi);
1816
1817 --((*dii)->todo);
1818
Bram Moolenaar41009372013-07-01 22:03:04 +02001819 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001820 return NULL;
1821
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001822 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001823}
1824
1825 static PyObject *
1826DictionaryIter(DictionaryObject *self)
1827{
1828 dictiterinfo_T *dii;
1829 hashtab_T *ht;
1830
1831 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1832 {
1833 PyErr_NoMemory();
1834 return NULL;
1835 }
1836
1837 ht = &self->dict->dv_hashtab;
1838 dii->ht_array = ht->ht_array;
1839 dii->ht_used = ht->ht_used;
1840 dii->ht = ht;
1841 dii->hi = dii->ht_array;
1842 dii->todo = dii->ht_used;
1843
1844 return IterNew(dii,
1845 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1846 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001847}
1848
1849 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001850DictionaryAssItem(
1851 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001852{
1853 char_u *key;
1854 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001855 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001857 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001858
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001859 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001860 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001861 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001862 return -1;
1863 }
1864
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001865 if (!(key = StringToChars(keyObject, &todecref)))
1866 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001867
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001868 if (*key == NUL)
1869 {
1870 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001871 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001872 return -1;
1873 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001874
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001875 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001876
1877 if (valObject == NULL)
1878 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001879 hashitem_T *hi;
1880
Bram Moolenaardb913952012-06-29 12:54:53 +02001881 if (di == NULL)
1882 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001883 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001884 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001885 return -1;
1886 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001887 hi = hash_find(&dict->dv_hashtab, di->di_key);
1888 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001889 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001890 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001891 return 0;
1892 }
1893
1894 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001895 {
1896 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001897 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001898 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001899
1900 if (di == NULL)
1901 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001902 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001903 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001904 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001905 PyErr_NoMemory();
1906 return -1;
1907 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001908 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001909
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001910 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 {
1912 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001913 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001914 RAISE_KEY_ADD_FAIL(key);
1915 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001916 return -1;
1917 }
1918 }
1919 else
1920 clear_tv(&di->di_tv);
1921
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001922 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001923
1924 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001925 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001926 return 0;
1927}
1928
Bram Moolenaara9922d62013-05-30 13:01:18 +02001929typedef PyObject *(*hi_to_py)(hashitem_T *);
1930
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001932DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001933{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001934 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001935 long_u todo = dict->dv_hashtab.ht_used;
1936 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001937 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001938 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001941 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1943 {
1944 if (!HASHITEM_EMPTY(hi))
1945 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001946 if (!(newObj = hiconvert(hi)))
1947 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001948 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 return NULL;
1950 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001951 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001952 --todo;
1953 ++i;
1954 }
1955 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001956 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001957}
1958
Bram Moolenaara9922d62013-05-30 13:01:18 +02001959 static PyObject *
1960dict_key(hashitem_T *hi)
1961{
1962 return PyBytes_FromString((char *)(hi->hi_key));
1963}
1964
1965 static PyObject *
1966DictionaryListKeys(DictionaryObject *self)
1967{
1968 return DictionaryListObjects(self, dict_key);
1969}
1970
1971 static PyObject *
1972dict_val(hashitem_T *hi)
1973{
1974 dictitem_T *di;
1975
1976 di = dict_lookup(hi);
1977 return ConvertToPyObject(&di->di_tv);
1978}
1979
1980 static PyObject *
1981DictionaryListValues(DictionaryObject *self)
1982{
1983 return DictionaryListObjects(self, dict_val);
1984}
1985
1986 static PyObject *
1987dict_item(hashitem_T *hi)
1988{
1989 PyObject *keyObject;
1990 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001991 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001992
1993 if (!(keyObject = dict_key(hi)))
1994 return NULL;
1995
1996 if (!(valObject = dict_val(hi)))
1997 {
1998 Py_DECREF(keyObject);
1999 return NULL;
2000 }
2001
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002002 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002003
2004 Py_DECREF(keyObject);
2005 Py_DECREF(valObject);
2006
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002007 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002008}
2009
2010 static PyObject *
2011DictionaryListItems(DictionaryObject *self)
2012{
2013 return DictionaryListObjects(self, dict_item);
2014}
2015
2016 static PyObject *
2017DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2018{
2019 dict_T *dict = self->dict;
2020
2021 if (dict->dv_lock)
2022 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002023 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002024 return NULL;
2025 }
2026
2027 if (kwargs)
2028 {
2029 typval_T tv;
2030
2031 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2032 return NULL;
2033
2034 VimTryStart();
2035 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2036 clear_tv(&tv);
2037 if (VimTryEnd())
2038 return NULL;
2039 }
2040 else
2041 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002042 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002043
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002044 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045 return NULL;
2046
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002047 if (obj == NULL)
2048 {
2049 Py_INCREF(Py_None);
2050 return Py_None;
2051 }
2052
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002053 if (PyObject_HasAttrString(obj, "keys"))
2054 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 else
2056 {
2057 PyObject *iterator;
2058 PyObject *item;
2059
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002060 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002061 return NULL;
2062
2063 while ((item = PyIter_Next(iterator)))
2064 {
2065 PyObject *fast;
2066 PyObject *keyObject;
2067 PyObject *valObject;
2068 PyObject *todecref;
2069 char_u *key;
2070 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002071 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002072
2073 if (!(fast = PySequence_Fast(item, "")))
2074 {
2075 Py_DECREF(iterator);
2076 Py_DECREF(item);
2077 return NULL;
2078 }
2079
2080 Py_DECREF(item);
2081
2082 if (PySequence_Fast_GET_SIZE(fast) != 2)
2083 {
2084 Py_DECREF(iterator);
2085 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002086 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002087 N_("expected sequence element of size 2, "
2088 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002089 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002090 return NULL;
2091 }
2092
2093 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2094
2095 if (!(key = StringToChars(keyObject, &todecref)))
2096 {
2097 Py_DECREF(iterator);
2098 Py_DECREF(fast);
2099 return NULL;
2100 }
2101
2102 di = dictitem_alloc(key);
2103
2104 Py_XDECREF(todecref);
2105
2106 if (di == NULL)
2107 {
2108 Py_DECREF(fast);
2109 Py_DECREF(iterator);
2110 PyErr_NoMemory();
2111 return NULL;
2112 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002113 di->di_tv.v_type = VAR_UNKNOWN;
2114
2115 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2116
2117 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2118 {
2119 Py_DECREF(iterator);
2120 Py_DECREF(fast);
2121 dictitem_free(di);
2122 return NULL;
2123 }
2124
2125 Py_DECREF(fast);
2126
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002127 hi = hash_find(&dict->dv_hashtab, di->di_key);
2128 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002129 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002130 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002131 Py_DECREF(iterator);
2132 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002133 return NULL;
2134 }
2135 }
2136
2137 Py_DECREF(iterator);
2138
2139 /* Iterator may have finished due to an exception */
2140 if (PyErr_Occurred())
2141 return NULL;
2142 }
2143 }
2144 Py_INCREF(Py_None);
2145 return Py_None;
2146}
2147
2148 static PyObject *
2149DictionaryGet(DictionaryObject *self, PyObject *args)
2150{
2151 return _DictionaryItem(self, args,
2152 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2153}
2154
2155 static PyObject *
2156DictionaryPop(DictionaryObject *self, PyObject *args)
2157{
2158 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2159}
2160
2161 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002162DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002163{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002164 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002165 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002166 PyObject *valObject;
2167 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002168
Bram Moolenaarde71b562013-06-02 17:41:54 +02002169 if (self->dict->dv_hashtab.ht_used == 0)
2170 {
2171 PyErr_SetNone(PyExc_KeyError);
2172 return NULL;
2173 }
2174
2175 hi = self->dict->dv_hashtab.ht_array;
2176 while (HASHITEM_EMPTY(hi))
2177 ++hi;
2178
2179 di = dict_lookup(hi);
2180
2181 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002182 return NULL;
2183
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002184 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002185 {
2186 Py_DECREF(valObject);
2187 return NULL;
2188 }
2189
2190 hash_remove(&self->dict->dv_hashtab, hi);
2191 dictitem_free(di);
2192
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002193 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002194}
2195
2196 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002197DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002198{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002199 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2200}
2201
2202static PySequenceMethods DictionaryAsSeq = {
2203 0, /* sq_length */
2204 0, /* sq_concat */
2205 0, /* sq_repeat */
2206 0, /* sq_item */
2207 0, /* sq_slice */
2208 0, /* sq_ass_item */
2209 0, /* sq_ass_slice */
2210 (objobjproc) DictionaryContains, /* sq_contains */
2211 0, /* sq_inplace_concat */
2212 0, /* sq_inplace_repeat */
2213};
2214
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002215static PyMappingMethods DictionaryAsMapping = {
2216 (lenfunc) DictionaryLength,
2217 (binaryfunc) DictionaryItem,
2218 (objobjargproc) DictionaryAssItem,
2219};
2220
Bram Moolenaardb913952012-06-29 12:54:53 +02002221static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002222 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002223 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2224 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2225 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2226 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2227 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002228 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002229 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002230 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2231 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002232};
2233
2234static PyTypeObject ListType;
2235
2236typedef struct
2237{
2238 PyObject_HEAD
2239 list_T *list;
2240 pylinkedlist_T ref;
2241} ListObject;
2242
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002243#define NEW_LIST(list) ListNew(&ListType, list)
2244
Bram Moolenaardb913952012-06-29 12:54:53 +02002245 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002246ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002247{
2248 ListObject *self;
2249
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002250 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002251 if (self == NULL)
2252 return NULL;
2253 self->list = list;
2254 ++list->lv_refcount;
2255
2256 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2257
2258 return (PyObject *)(self);
2259}
2260
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002261 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002262py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002263{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002264 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002265
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002266 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002267 {
2268 PyErr_NoMemory();
2269 return NULL;
2270 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002271 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002272
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002273 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002274}
2275
2276 static int
2277list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2278{
2279 PyObject *iterator;
2280 PyObject *item;
2281 listitem_T *li;
2282
2283 if (!(iterator = PyObject_GetIter(obj)))
2284 return -1;
2285
2286 while ((item = PyIter_Next(iterator)))
2287 {
2288 if (!(li = listitem_alloc()))
2289 {
2290 PyErr_NoMemory();
2291 Py_DECREF(item);
2292 Py_DECREF(iterator);
2293 return -1;
2294 }
2295 li->li_tv.v_lock = 0;
2296 li->li_tv.v_type = VAR_UNKNOWN;
2297
2298 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2299 {
2300 Py_DECREF(item);
2301 Py_DECREF(iterator);
2302 listitem_free(li);
2303 return -1;
2304 }
2305
2306 Py_DECREF(item);
2307
2308 list_append(l, li);
2309 }
2310
2311 Py_DECREF(iterator);
2312
2313 /* Iterator may have finished due to an exception */
2314 if (PyErr_Occurred())
2315 return -1;
2316
2317 return 0;
2318}
2319
2320 static PyObject *
2321ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2322{
2323 list_T *list;
2324 PyObject *obj = NULL;
2325
2326 if (kwargs)
2327 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002328 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002329 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002330 return NULL;
2331 }
2332
2333 if (!PyArg_ParseTuple(args, "|O", &obj))
2334 return NULL;
2335
2336 if (!(list = py_list_alloc()))
2337 return NULL;
2338
2339 if (obj)
2340 {
2341 PyObject *lookup_dict;
2342
2343 if (!(lookup_dict = PyDict_New()))
2344 {
2345 list_unref(list);
2346 return NULL;
2347 }
2348
2349 if (list_py_concat(list, obj, lookup_dict) == -1)
2350 {
2351 Py_DECREF(lookup_dict);
2352 list_unref(list);
2353 return NULL;
2354 }
2355
2356 Py_DECREF(lookup_dict);
2357 }
2358
2359 return ListNew(subtype, list);
2360}
2361
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002362 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002363ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002364{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002365 pyll_remove(&self->ref, &lastlist);
2366 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002367
2368 DESTRUCTOR_FINISH(self);
2369}
2370
Bram Moolenaardb913952012-06-29 12:54:53 +02002371 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002372ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002373{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002374 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002375}
2376
2377 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002378ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002379{
2380 listitem_T *li;
2381
Bram Moolenaard6e39182013-05-21 18:30:34 +02002382 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002383 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002384 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002385 return NULL;
2386 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002387 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002388 if (li == NULL)
2389 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002390 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002391 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002392 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002393 return NULL;
2394 }
2395 return ConvertToPyObject(&li->li_tv);
2396}
2397
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002399ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2400 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002401{
2402 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002403 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002404
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002405 if (step == 0)
2406 {
2407 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2408 return NULL;
2409 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002410
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002411 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002412 if (list == NULL)
2413 return NULL;
2414
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002415 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002417 PyObject *item;
2418
2419 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002420 if (item == NULL)
2421 {
2422 Py_DECREF(list);
2423 return NULL;
2424 }
2425
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002426 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002427 }
2428
2429 return list;
2430}
2431
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002432 static PyObject *
2433ListItem(ListObject *self, PyObject* idx)
2434{
2435#if PY_MAJOR_VERSION < 3
2436 if (PyInt_Check(idx))
2437 {
2438 long _idx = PyInt_AsLong(idx);
2439 return ListIndex(self, _idx);
2440 }
2441 else
2442#endif
2443 if (PyLong_Check(idx))
2444 {
2445 long _idx = PyLong_AsLong(idx);
2446 return ListIndex(self, _idx);
2447 }
2448 else if (PySlice_Check(idx))
2449 {
2450 Py_ssize_t start, stop, step, slicelen;
2451
Bram Moolenaar922a4662014-03-30 16:11:43 +02002452 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002453 &start, &stop, &step, &slicelen) < 0)
2454 return NULL;
2455 return ListSlice(self, start, step, slicelen);
2456 }
2457 else
2458 {
2459 RAISE_INVALID_INDEX_TYPE(idx);
2460 return NULL;
2461 }
2462}
2463
2464 static void
2465list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2466 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2467{
2468 while (numreplaced--)
2469 {
2470 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2471 listitem_remove(l, lis[slicelen + numreplaced]);
2472 }
2473 while (numadded--)
2474 {
2475 listitem_T *next;
2476
2477 next = lastaddedli->li_prev;
2478 listitem_remove(l, lastaddedli);
2479 lastaddedli = next;
2480 }
2481}
2482
2483 static int
2484ListAssSlice(ListObject *self, Py_ssize_t first,
2485 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2486{
2487 PyObject *iterator;
2488 PyObject *item;
2489 listitem_T *li;
2490 listitem_T *lastaddedli = NULL;
2491 listitem_T *next;
2492 typval_T v;
2493 list_T *l = self->list;
2494 PyInt i;
2495 PyInt j;
2496 PyInt numreplaced = 0;
2497 PyInt numadded = 0;
2498 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002499 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002500
2501 size = ListLength(self);
2502
2503 if (l->lv_lock)
2504 {
2505 RAISE_LOCKED_LIST;
2506 return -1;
2507 }
2508
2509 if (step == 0)
2510 {
2511 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2512 return -1;
2513 }
2514
2515 if (step != 1 && slicelen == 0)
2516 {
2517 /* Nothing to do. Only error out if obj has some items. */
2518 int ret = 0;
2519
2520 if (obj == NULL)
2521 return 0;
2522
2523 if (!(iterator = PyObject_GetIter(obj)))
2524 return -1;
2525
2526 if ((item = PyIter_Next(iterator)))
2527 {
2528 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002529 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002530 "to extended slice"), 0);
2531 Py_DECREF(item);
2532 ret = -1;
2533 }
2534 Py_DECREF(iterator);
2535 return ret;
2536 }
2537
2538 if (obj != NULL)
2539 /* XXX May allocate zero bytes. */
2540 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2541 {
2542 PyErr_NoMemory();
2543 return -1;
2544 }
2545
2546 if (first == size)
2547 li = NULL;
2548 else
2549 {
2550 li = list_find(l, (long) first);
2551 if (li == NULL)
2552 {
2553 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2554 (int)first);
2555 if (obj != NULL)
2556 PyMem_Free(lis);
2557 return -1;
2558 }
2559 i = slicelen;
2560 while (i-- && li != NULL)
2561 {
2562 j = step;
2563 next = li;
2564 if (step > 0)
2565 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2566 else
2567 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2568
2569 if (obj == NULL)
2570 listitem_remove(l, li);
2571 else
2572 lis[slicelen - i - 1] = li;
2573
2574 li = next;
2575 }
2576 if (li == NULL && i != -1)
2577 {
2578 PyErr_SET_VIM(N_("internal error: not enough list items"));
2579 if (obj != NULL)
2580 PyMem_Free(lis);
2581 return -1;
2582 }
2583 }
2584
2585 if (obj == NULL)
2586 return 0;
2587
2588 if (!(iterator = PyObject_GetIter(obj)))
2589 {
2590 PyMem_Free(lis);
2591 return -1;
2592 }
2593
2594 i = 0;
2595 while ((item = PyIter_Next(iterator)))
2596 {
2597 if (ConvertFromPyObject(item, &v) == -1)
2598 {
2599 Py_DECREF(iterator);
2600 Py_DECREF(item);
2601 PyMem_Free(lis);
2602 return -1;
2603 }
2604 Py_DECREF(item);
2605 if (list_insert_tv(l, &v, numreplaced < slicelen
2606 ? lis[numreplaced]
2607 : li) == FAIL)
2608 {
2609 clear_tv(&v);
2610 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2611 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2612 PyMem_Free(lis);
2613 return -1;
2614 }
2615 if (numreplaced < slicelen)
2616 {
2617 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002618 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002619 numreplaced++;
2620 }
2621 else
2622 {
2623 if (li)
2624 lastaddedli = li->li_prev;
2625 else
2626 lastaddedli = l->lv_last;
2627 numadded++;
2628 }
2629 clear_tv(&v);
2630 if (step != 1 && i >= slicelen)
2631 {
2632 Py_DECREF(iterator);
2633 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002634 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002635 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002636 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2637 PyMem_Free(lis);
2638 return -1;
2639 }
2640 ++i;
2641 }
2642 Py_DECREF(iterator);
2643
2644 if (step != 1 && i != slicelen)
2645 {
2646 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002647 N_("attempt to assign sequence of size %d to extended slice "
2648 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002649 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2650 PyMem_Free(lis);
2651 return -1;
2652 }
2653
2654 if (PyErr_Occurred())
2655 {
2656 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2657 PyMem_Free(lis);
2658 return -1;
2659 }
2660
2661 for (i = 0; i < numreplaced; i++)
2662 listitem_free(lis[i]);
2663 if (step == 1)
2664 for (i = numreplaced; i < slicelen; i++)
2665 listitem_remove(l, lis[i]);
2666
2667 PyMem_Free(lis);
2668
2669 return 0;
2670}
2671
2672 static int
2673ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2674{
2675 typval_T tv;
2676 list_T *l = self->list;
2677 listitem_T *li;
2678 Py_ssize_t length = ListLength(self);
2679
2680 if (l->lv_lock)
2681 {
2682 RAISE_LOCKED_LIST;
2683 return -1;
2684 }
2685 if (index > length || (index == length && obj == NULL))
2686 {
2687 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2688 return -1;
2689 }
2690
2691 if (obj == NULL)
2692 {
2693 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002694 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002695 clear_tv(&li->li_tv);
2696 vim_free(li);
2697 return 0;
2698 }
2699
2700 if (ConvertFromPyObject(obj, &tv) == -1)
2701 return -1;
2702
2703 if (index == length)
2704 {
2705 if (list_append_tv(l, &tv) == FAIL)
2706 {
2707 clear_tv(&tv);
2708 PyErr_SET_VIM(N_("failed to add item to list"));
2709 return -1;
2710 }
2711 }
2712 else
2713 {
2714 li = list_find(l, (long) index);
2715 clear_tv(&li->li_tv);
2716 copy_tv(&tv, &li->li_tv);
2717 clear_tv(&tv);
2718 }
2719 return 0;
2720}
2721
2722 static Py_ssize_t
2723ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2724{
2725#if PY_MAJOR_VERSION < 3
2726 if (PyInt_Check(idx))
2727 {
2728 long _idx = PyInt_AsLong(idx);
2729 return ListAssIndex(self, _idx, obj);
2730 }
2731 else
2732#endif
2733 if (PyLong_Check(idx))
2734 {
2735 long _idx = PyLong_AsLong(idx);
2736 return ListAssIndex(self, _idx, obj);
2737 }
2738 else if (PySlice_Check(idx))
2739 {
2740 Py_ssize_t start, stop, step, slicelen;
2741
Bram Moolenaar922a4662014-03-30 16:11:43 +02002742 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002743 &start, &stop, &step, &slicelen) < 0)
2744 return -1;
2745 return ListAssSlice(self, start, step, slicelen,
2746 obj);
2747 }
2748 else
2749 {
2750 RAISE_INVALID_INDEX_TYPE(idx);
2751 return -1;
2752 }
2753}
2754
2755 static PyObject *
2756ListConcatInPlace(ListObject *self, PyObject *obj)
2757{
2758 list_T *l = self->list;
2759 PyObject *lookup_dict;
2760
2761 if (l->lv_lock)
2762 {
2763 RAISE_LOCKED_LIST;
2764 return NULL;
2765 }
2766
2767 if (!(lookup_dict = PyDict_New()))
2768 return NULL;
2769
2770 if (list_py_concat(l, obj, lookup_dict) == -1)
2771 {
2772 Py_DECREF(lookup_dict);
2773 return NULL;
2774 }
2775 Py_DECREF(lookup_dict);
2776
2777 Py_INCREF(self);
2778 return (PyObject *)(self);
2779}
2780
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002781typedef struct
2782{
2783 listwatch_T lw;
2784 list_T *list;
2785} listiterinfo_T;
2786
2787 static void
2788ListIterDestruct(listiterinfo_T *lii)
2789{
2790 list_rem_watch(lii->list, &lii->lw);
2791 PyMem_Free(lii);
2792}
2793
2794 static PyObject *
2795ListIterNext(listiterinfo_T **lii)
2796{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002797 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002798
2799 if (!((*lii)->lw.lw_item))
2800 return NULL;
2801
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002802 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002803 return NULL;
2804
2805 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2806
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002807 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002808}
2809
2810 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002811ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002812{
2813 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002814 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002815
2816 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2817 {
2818 PyErr_NoMemory();
2819 return NULL;
2820 }
2821
2822 list_add_watch(l, &lii->lw);
2823 lii->lw.lw_item = l->lv_first;
2824 lii->list = l;
2825
2826 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002827 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2828 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002829}
2830
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002831static char *ListAttrs[] = {
2832 "locked",
2833 NULL
2834};
2835
2836 static PyObject *
2837ListDir(PyObject *self)
2838{
2839 return ObjectDir(self, ListAttrs);
2840}
2841
Bram Moolenaar66b79852012-09-21 14:00:35 +02002842 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002843ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002844{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002845 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002846 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002847 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002848 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002849 return -1;
2850 }
2851
2852 if (strcmp(name, "locked") == 0)
2853 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002854 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002855 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002856 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002857 return -1;
2858 }
2859 else
2860 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002861 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002862 if (istrue == -1)
2863 return -1;
2864 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002865 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002866 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002867 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002868 }
2869 return 0;
2870 }
2871 else
2872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002873 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002874 return -1;
2875 }
2876}
2877
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002878static PySequenceMethods ListAsSeq = {
2879 (lenfunc) ListLength, /* sq_length, len(x) */
2880 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2881 0, /* RangeRepeat, sq_repeat, x*n */
2882 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2883 0, /* was_sq_slice, x[i:j] */
2884 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2885 0, /* was_sq_ass_slice, x[i:j]=v */
2886 0, /* sq_contains */
2887 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2888 0, /* sq_inplace_repeat */
2889};
2890
2891static PyMappingMethods ListAsMapping = {
2892 /* mp_length */ (lenfunc) ListLength,
2893 /* mp_subscript */ (binaryfunc) ListItem,
2894 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2895};
2896
Bram Moolenaardb913952012-06-29 12:54:53 +02002897static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002898 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2899 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2900 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002901};
2902
2903typedef struct
2904{
2905 PyObject_HEAD
2906 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002907 int argc;
2908 typval_T *argv;
2909 dict_T *self;
2910 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002911 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002912} FunctionObject;
2913
2914static PyTypeObject FunctionType;
2915
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002916#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2917 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002918
Bram Moolenaardb913952012-06-29 12:54:53 +02002919 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002920FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002921 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002922{
2923 FunctionObject *self;
2924
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002925 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002926 if (self == NULL)
2927 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002928
2929 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002930 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002931 if (!translated_function_exists(name))
2932 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002933 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002934 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002935 return NULL;
2936 }
2937 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002938 }
2939 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002940 {
2941 char_u *p;
2942
2943 if ((p = get_expanded_name(name,
2944 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002945 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002946 PyErr_FORMAT(PyExc_ValueError,
2947 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002948 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002949 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002950
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002951 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2952 {
2953 char_u *np;
2954 size_t len = STRLEN(p) + 1;
2955
Bram Moolenaar51e14382019-05-25 20:21:28 +02002956 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002957 {
2958 vim_free(p);
2959 return NULL;
2960 }
2961 mch_memmove(np, "<SNR>", 5);
2962 mch_memmove(np + 5, p + 3, len - 3);
2963 vim_free(p);
2964 self->name = np;
2965 }
2966 else
2967 self->name = p;
2968 }
2969
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002970 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002971 self->argc = argc;
2972 self->argv = argv;
2973 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002974 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002975
2976 if (self->argv || self->self)
2977 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2978
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002979 return (PyObject *)(self);
2980}
2981
2982 static PyObject *
2983FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2984{
2985 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002986 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002987 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002988 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002989 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002990 typval_T selfdicttv;
2991 typval_T argstv;
2992 list_T *argslist = NULL;
2993 dict_T *selfdict = NULL;
2994 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002995 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002996 typval_T *argv = NULL;
2997 typval_T *curtv;
2998 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002999
Bram Moolenaar8110a092016-04-14 15:56:09 +02003000 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003001 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003002 selfdictObject = PyDict_GetItemString(kwargs, "self");
3003 if (selfdictObject != NULL)
3004 {
3005 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3006 return NULL;
3007 selfdict = selfdicttv.vval.v_dict;
3008 }
3009 argsObject = PyDict_GetItemString(kwargs, "args");
3010 if (argsObject != NULL)
3011 {
3012 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3013 {
3014 dict_unref(selfdict);
3015 return NULL;
3016 }
3017 argslist = argstv.vval.v_list;
3018
3019 argc = argslist->lv_len;
3020 if (argc != 0)
3021 {
3022 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003023 if (argv == NULL)
3024 {
3025 PyErr_NoMemory();
3026 dict_unref(selfdict);
3027 list_unref(argslist);
3028 return NULL;
3029 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003030 curtv = argv;
3031 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3032 copy_tv(&li->li_tv, curtv++);
3033 }
3034 list_unref(argslist);
3035 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003036 if (selfdict != NULL)
3037 {
3038 auto_rebind = FALSE;
3039 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3040 if (autoRebindObject != NULL)
3041 {
3042 auto_rebind = PyObject_IsTrue(autoRebindObject);
3043 if (auto_rebind == -1)
3044 {
3045 dict_unref(selfdict);
3046 list_unref(argslist);
3047 return NULL;
3048 }
3049 }
3050 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003051 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003052
Bram Moolenaar389a1792013-06-23 13:00:44 +02003053 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003054 {
3055 dict_unref(selfdict);
3056 while (argc--)
3057 clear_tv(&argv[argc]);
3058 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003059 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003060 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003061
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003062 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003063
Bram Moolenaar389a1792013-06-23 13:00:44 +02003064 PyMem_Free(name);
3065
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003066 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003067}
3068
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003069 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003070FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003071{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003072 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003073 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003074 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003075 for (i = 0; i < self->argc; ++i)
3076 clear_tv(&self->argv[i]);
3077 PyMem_Free(self->argv);
3078 dict_unref(self->self);
3079 if (self->argv || self->self)
3080 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003081
3082 DESTRUCTOR_FINISH(self);
3083}
3084
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003085static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003086 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003087 NULL
3088};
3089
3090 static PyObject *
3091FunctionDir(PyObject *self)
3092{
3093 return ObjectDir(self, FunctionAttrs);
3094}
3095
Bram Moolenaardb913952012-06-29 12:54:53 +02003096 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003097FunctionAttr(FunctionObject *self, char *name)
3098{
3099 list_T *list;
3100 int i;
3101 if (strcmp(name, "name") == 0)
3102 return PyString_FromString((char *)(self->name));
3103 else if (strcmp(name, "args") == 0)
3104 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003105 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003106 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003107
Bram Moolenaar8110a092016-04-14 15:56:09 +02003108 for (i = 0; i < self->argc; ++i)
3109 list_append_tv(list, &self->argv[i]);
3110 return NEW_LIST(list);
3111 }
3112 else if (strcmp(name, "self") == 0)
3113 return self->self == NULL
3114 ? AlwaysNone(NULL)
3115 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003116 else if (strcmp(name, "auto_rebind") == 0)
3117 return self->auto_rebind
3118 ? AlwaysTrue(NULL)
3119 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003120 else if (strcmp(name, "__members__") == 0)
3121 return ObjectDir(NULL, FunctionAttrs);
3122 return NULL;
3123}
3124
3125/* Populate partial_T given function object.
3126 *
3127 * "exported" should be set to true when it is needed to construct a partial
3128 * that may be stored in a variable (i.e. may be freed by Vim).
3129 */
3130 static void
3131set_partial(FunctionObject *self, partial_T *pt, int exported)
3132{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003133 int i;
3134
3135 pt->pt_name = self->name;
3136 if (self->argv)
3137 {
3138 pt->pt_argc = self->argc;
3139 if (exported)
3140 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003141 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003142 for (i = 0; i < pt->pt_argc; ++i)
3143 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3144 }
3145 else
3146 pt->pt_argv = self->argv;
3147 }
3148 else
3149 {
3150 pt->pt_argc = 0;
3151 pt->pt_argv = NULL;
3152 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003153 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003154 pt->pt_dict = self->self;
3155 if (exported && self->self)
3156 ++pt->pt_dict->dv_refcount;
3157 if (exported)
3158 pt->pt_name = vim_strsave(pt->pt_name);
3159 pt->pt_refcount = 1;
3160}
3161
3162 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003163FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003164{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003165 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003166 typval_T args;
3167 typval_T selfdicttv;
3168 typval_T rettv;
3169 dict_T *selfdict = NULL;
3170 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003171 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003172 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003173 partial_T pt;
3174 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003175
Bram Moolenaar8110a092016-04-14 15:56:09 +02003176 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003177 return NULL;
3178
3179 if (kwargs != NULL)
3180 {
3181 selfdictObject = PyDict_GetItemString(kwargs, "self");
3182 if (selfdictObject != NULL)
3183 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003184 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003185 {
3186 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003187 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003188 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003189 selfdict = selfdicttv.vval.v_dict;
3190 }
3191 }
3192
Bram Moolenaar8110a092016-04-14 15:56:09 +02003193 if (self->argv || self->self)
3194 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003195 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003196 set_partial(self, &pt, FALSE);
3197 pt_ptr = &pt;
3198 }
3199
Bram Moolenaar71700b82013-05-15 17:49:05 +02003200 Py_BEGIN_ALLOW_THREADS
3201 Python_Lock_Vim();
3202
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003203 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003204 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003205
3206 Python_Release_Vim();
3207 Py_END_ALLOW_THREADS
3208
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003209 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003210 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003211 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003212 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003213 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003214 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003215 }
3216 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003217 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003218
Bram Moolenaardb913952012-06-29 12:54:53 +02003219 clear_tv(&args);
3220 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003221 if (selfdict != NULL)
3222 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003223
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003224 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003225}
3226
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003227 static PyObject *
3228FunctionRepr(FunctionObject *self)
3229{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003230 PyObject *ret;
3231 garray_T repr_ga;
3232 int i;
3233 char_u *tofree = NULL;
3234 typval_T tv;
3235 char_u numbuf[NUMBUFLEN];
3236
3237 ga_init2(&repr_ga, (int)sizeof(char), 70);
3238 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3239 if (self->name)
3240 ga_concat(&repr_ga, self->name);
3241 else
3242 ga_concat(&repr_ga, (char_u *)"<NULL>");
3243 ga_append(&repr_ga, '\'');
3244 if (self->argv)
3245 {
3246 ga_concat(&repr_ga, (char_u *)", args=[");
3247 ++emsg_silent;
3248 for (i = 0; i < self->argc; i++)
3249 {
3250 if (i != 0)
3251 ga_concat(&repr_ga, (char_u *)", ");
3252 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3253 get_copyID()));
3254 vim_free(tofree);
3255 }
3256 --emsg_silent;
3257 ga_append(&repr_ga, ']');
3258 }
3259 if (self->self)
3260 {
3261 ga_concat(&repr_ga, (char_u *)", self=");
3262 tv.v_type = VAR_DICT;
3263 tv.vval.v_dict = self->self;
3264 ++emsg_silent;
3265 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3266 --emsg_silent;
3267 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003268 if (self->auto_rebind)
3269 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003270 }
3271 ga_append(&repr_ga, '>');
3272 ret = PyString_FromString((char *)repr_ga.ga_data);
3273 ga_clear(&repr_ga);
3274 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003275}
3276
Bram Moolenaardb913952012-06-29 12:54:53 +02003277static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003278 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3279 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003280};
3281
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003282/*
3283 * Options object
3284 */
3285
3286static PyTypeObject OptionsType;
3287
3288typedef int (*checkfun)(void *);
3289
3290typedef struct
3291{
3292 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003293 int opt_type;
3294 void *from;
3295 checkfun Check;
3296 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003297} OptionsObject;
3298
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003299 static int
3300dummy_check(void *arg UNUSED)
3301{
3302 return 0;
3303}
3304
3305 static PyObject *
3306OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3307{
3308 OptionsObject *self;
3309
Bram Moolenaar774267b2013-05-21 20:51:59 +02003310 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003311 if (self == NULL)
3312 return NULL;
3313
3314 self->opt_type = opt_type;
3315 self->from = from;
3316 self->Check = Check;
3317 self->fromObj = fromObj;
3318 if (fromObj)
3319 Py_INCREF(fromObj);
3320
3321 return (PyObject *)(self);
3322}
3323
3324 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003325OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003326{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003327 PyObject_GC_UnTrack((void *)(self));
3328 Py_XDECREF(self->fromObj);
3329 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003330}
3331
3332 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003334{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003335 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003336 return 0;
3337}
3338
3339 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003340OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003342 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003343 return 0;
3344}
3345
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003346 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003347OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003348{
3349 char_u *key;
3350 int flags;
3351 long numval;
3352 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003353 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003354
Bram Moolenaard6e39182013-05-21 18:30:34 +02003355 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003356 return NULL;
3357
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003358 if (!(key = StringToChars(keyObject, &todecref)))
3359 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003360
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003361 if (*key == NUL)
3362 {
3363 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003364 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003365 return NULL;
3366 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003367
3368 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003369 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003370
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003371 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003372
3373 if (flags == 0)
3374 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003375 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003376 return NULL;
3377 }
3378
3379 if (flags & SOPT_UNSET)
3380 {
3381 Py_INCREF(Py_None);
3382 return Py_None;
3383 }
3384 else if (flags & SOPT_BOOL)
3385 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003386 PyObject *ret;
3387 ret = numval ? Py_True : Py_False;
3388 Py_INCREF(ret);
3389 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003390 }
3391 else if (flags & SOPT_NUM)
3392 return PyInt_FromLong(numval);
3393 else if (flags & SOPT_STRING)
3394 {
3395 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003396 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003397 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003398 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003399 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003400 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003401 else
3402 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003403 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003404 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003405 return NULL;
3406 }
3407 }
3408 else
3409 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003410 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003411 return NULL;
3412 }
3413}
3414
3415 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003416OptionsContains(OptionsObject *self, PyObject *keyObject)
3417{
3418 char_u *key;
3419 PyObject *todecref;
3420
3421 if (!(key = StringToChars(keyObject, &todecref)))
3422 return -1;
3423
3424 if (*key == NUL)
3425 {
3426 Py_XDECREF(todecref);
3427 return 0;
3428 }
3429
3430 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3431 {
3432 Py_XDECREF(todecref);
3433 return 1;
3434 }
3435 else
3436 {
3437 Py_XDECREF(todecref);
3438 return 0;
3439 }
3440}
3441
3442typedef struct
3443{
3444 void *lastoption;
3445 int opt_type;
3446} optiterinfo_T;
3447
3448 static PyObject *
3449OptionsIterNext(optiterinfo_T **oii)
3450{
3451 char_u *name;
3452
3453 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3454 return PyString_FromString((char *)name);
3455
3456 return NULL;
3457}
3458
3459 static PyObject *
3460OptionsIter(OptionsObject *self)
3461{
3462 optiterinfo_T *oii;
3463
3464 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3465 {
3466 PyErr_NoMemory();
3467 return NULL;
3468 }
3469
3470 oii->opt_type = self->opt_type;
3471 oii->lastoption = NULL;
3472
3473 return IterNew(oii,
3474 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3475 NULL, NULL);
3476}
3477
3478 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003479set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003480{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003481 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003482
3483 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3484 {
3485 if (VimTryEnd())
3486 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003487 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003488 return FAIL;
3489 }
3490 return OK;
3491}
3492
3493 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003494set_option_value_for(
3495 char_u *key,
3496 int numval,
3497 char_u *stringval,
3498 int opt_flags,
3499 int opt_type,
3500 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003501{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003502 win_T *save_curwin = NULL;
3503 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003504 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003505 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003506
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003507 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003508 switch (opt_type)
3509 {
3510 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003511 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003512 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003513 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003514 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003515 if (VimTryEnd())
3516 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003517 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003518 return -1;
3519 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003520 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3521 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003522 break;
3523 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003524 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003525 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003526 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003527 break;
3528 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003529 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003530 break;
3531 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003532 if (set_ret == FAIL)
3533 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003534 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003535}
3536
3537 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003538OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003539{
3540 char_u *key;
3541 int flags;
3542 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003543 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003544 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003545
Bram Moolenaard6e39182013-05-21 18:30:34 +02003546 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003547 return -1;
3548
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003549 if (!(key = StringToChars(keyObject, &todecref)))
3550 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003551
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003552 if (*key == NUL)
3553 {
3554 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003555 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003556 return -1;
3557 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003558
3559 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003560 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003561
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003562 if (flags == 0)
3563 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003564 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003565 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003566 return -1;
3567 }
3568
3569 if (valObject == NULL)
3570 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003571 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003572 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003573 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003574 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003575 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003576 return -1;
3577 }
3578 else if (!(flags & SOPT_GLOBAL))
3579 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003580 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003581 N_("unable to unset option %s "
3582 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003583 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003584 return -1;
3585 }
3586 else
3587 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003588 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003589 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003590 return 0;
3591 }
3592 }
3593
Bram Moolenaard6e39182013-05-21 18:30:34 +02003594 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003595
3596 if (flags & SOPT_BOOL)
3597 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003598 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003599
Bram Moolenaarb983f752013-05-15 16:11:50 +02003600 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003601 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003602 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003603 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003604 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003605 }
3606 else if (flags & SOPT_NUM)
3607 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003608 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003609
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003610 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003611 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003612 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003613 return -1;
3614 }
3615
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003616 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003617 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003618 }
3619 else
3620 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003621 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003622 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003623
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003624 if ((val = StringToChars(valObject, &todecref2)))
3625 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003626 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003627 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003628 Py_XDECREF(todecref2);
3629 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003630 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003632 }
3633
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003634 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003635
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003636 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003637}
3638
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003639static PySequenceMethods OptionsAsSeq = {
3640 0, /* sq_length */
3641 0, /* sq_concat */
3642 0, /* sq_repeat */
3643 0, /* sq_item */
3644 0, /* sq_slice */
3645 0, /* sq_ass_item */
3646 0, /* sq_ass_slice */
3647 (objobjproc) OptionsContains, /* sq_contains */
3648 0, /* sq_inplace_concat */
3649 0, /* sq_inplace_repeat */
3650};
3651
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003652static PyMappingMethods OptionsAsMapping = {
3653 (lenfunc) NULL,
3654 (binaryfunc) OptionsItem,
3655 (objobjargproc) OptionsAssItem,
3656};
3657
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003658/* Tabpage object
3659 */
3660
3661typedef struct
3662{
3663 PyObject_HEAD
3664 tabpage_T *tab;
3665} TabPageObject;
3666
3667static PyObject *WinListNew(TabPageObject *tabObject);
3668
3669static PyTypeObject TabPageType;
3670
3671 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003672CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003673{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003674 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003675 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003676 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003677 return -1;
3678 }
3679
3680 return 0;
3681}
3682
3683 static PyObject *
3684TabPageNew(tabpage_T *tab)
3685{
3686 TabPageObject *self;
3687
3688 if (TAB_PYTHON_REF(tab))
3689 {
3690 self = TAB_PYTHON_REF(tab);
3691 Py_INCREF(self);
3692 }
3693 else
3694 {
3695 self = PyObject_NEW(TabPageObject, &TabPageType);
3696 if (self == NULL)
3697 return NULL;
3698 self->tab = tab;
3699 TAB_PYTHON_REF(tab) = self;
3700 }
3701
3702 return (PyObject *)(self);
3703}
3704
3705 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003706TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003707{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003708 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3709 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003710
3711 DESTRUCTOR_FINISH(self);
3712}
3713
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003714static char *TabPageAttrs[] = {
3715 "windows", "number", "vars", "window", "valid",
3716 NULL
3717};
3718
3719 static PyObject *
3720TabPageDir(PyObject *self)
3721{
3722 return ObjectDir(self, TabPageAttrs);
3723}
3724
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003725 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003726TabPageAttrValid(TabPageObject *self, char *name)
3727{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003728 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003729
3730 if (strcmp(name, "valid") != 0)
3731 return NULL;
3732
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003733 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3734 Py_INCREF(ret);
3735 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003736}
3737
3738 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003739TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003740{
3741 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003742 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003743 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003744 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003745 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003746 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003747 else if (strcmp(name, "window") == 0)
3748 {
3749 /* For current tab window.c does not bother to set or update tp_curwin
3750 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003751 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003752 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003753 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003754 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003755 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003756 else if (strcmp(name, "__members__") == 0)
3757 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003758 return NULL;
3759}
3760
3761 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003762TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003763{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003764 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003765 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003766 else
3767 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003768 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003769
3770 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003771 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3772 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003773 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003774 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003775 }
3776}
3777
3778static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003779 /* name, function, calling, documentation */
3780 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3781 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003782};
3783
3784/*
3785 * Window list object
3786 */
3787
3788static PyTypeObject TabListType;
3789static PySequenceMethods TabListAsSeq;
3790
3791typedef struct
3792{
3793 PyObject_HEAD
3794} TabListObject;
3795
3796 static PyInt
3797TabListLength(PyObject *self UNUSED)
3798{
3799 tabpage_T *tp = first_tabpage;
3800 PyInt n = 0;
3801
3802 while (tp != NULL)
3803 {
3804 ++n;
3805 tp = tp->tp_next;
3806 }
3807
3808 return n;
3809}
3810
3811 static PyObject *
3812TabListItem(PyObject *self UNUSED, PyInt n)
3813{
3814 tabpage_T *tp;
3815
3816 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3817 if (n == 0)
3818 return TabPageNew(tp);
3819
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003820 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003821 return NULL;
3822}
3823
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003824/*
3825 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003826 */
3827
3828typedef struct
3829{
3830 PyObject_HEAD
3831 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003832 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003833} WindowObject;
3834
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003835static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003836
3837 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003838CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003839{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003840 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003842 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003843 return -1;
3844 }
3845
3846 return 0;
3847}
3848
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003849 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003850WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003851{
3852 /* We need to handle deletion of windows underneath us.
3853 * If we add a "w_python*_ref" field to the win_T structure,
3854 * then we can get at it in win_free() in vim. We then
3855 * need to create only ONE Python object per window - if
3856 * we try to create a second, just INCREF the existing one
3857 * and return it. The (single) Python object referring to
3858 * the window is stored in "w_python*_ref".
3859 * On a win_free() we set the Python object's win_T* field
3860 * to an invalid value. We trap all uses of a window
3861 * object, and reject them if the win_T* field is invalid.
3862 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003863 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003864 * w_python_ref and w_python3_ref fields respectively.
3865 */
3866
3867 WindowObject *self;
3868
3869 if (WIN_PYTHON_REF(win))
3870 {
3871 self = WIN_PYTHON_REF(win);
3872 Py_INCREF(self);
3873 }
3874 else
3875 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003876 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003877 if (self == NULL)
3878 return NULL;
3879 self->win = win;
3880 WIN_PYTHON_REF(win) = self;
3881 }
3882
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003883 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3884
Bram Moolenaar971db462013-05-12 18:44:48 +02003885 return (PyObject *)(self);
3886}
3887
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003888 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003889WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003890{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003891 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003892 if (self->win && self->win != INVALID_WINDOW_VALUE)
3893 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003894 Py_XDECREF(((PyObject *)(self->tabObject)));
3895 PyObject_GC_Del((void *)(self));
3896}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003897
Bram Moolenaar774267b2013-05-21 20:51:59 +02003898 static int
3899WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3900{
3901 Py_VISIT(((PyObject *)(self->tabObject)));
3902 return 0;
3903}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003904
Bram Moolenaar774267b2013-05-21 20:51:59 +02003905 static int
3906WindowClear(WindowObject *self)
3907{
3908 Py_CLEAR(self->tabObject);
3909 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003910}
3911
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003912 static win_T *
3913get_firstwin(TabPageObject *tabObject)
3914{
3915 if (tabObject)
3916 {
3917 if (CheckTabPage(tabObject))
3918 return NULL;
3919 /* For current tab window.c does not bother to set or update tp_firstwin
3920 */
3921 else if (tabObject->tab == curtab)
3922 return firstwin;
3923 else
3924 return tabObject->tab->tp_firstwin;
3925 }
3926 else
3927 return firstwin;
3928}
Bram Moolenaare950f992018-06-10 13:55:55 +02003929
3930// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003931static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003932 "buffer",
3933 "cursor",
3934 "height",
3935 "row",
3936 "width",
3937 "col",
3938 "vars",
3939 "options",
3940 "number",
3941 "tabpage",
3942 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003943 NULL
3944};
3945
3946 static PyObject *
3947WindowDir(PyObject *self)
3948{
3949 return ObjectDir(self, WindowAttrs);
3950}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003951
Bram Moolenaar971db462013-05-12 18:44:48 +02003952 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003953WindowAttrValid(WindowObject *self, char *name)
3954{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003955 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003956
3957 if (strcmp(name, "valid") != 0)
3958 return NULL;
3959
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003960 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3961 Py_INCREF(ret);
3962 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003963}
3964
3965 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003966WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003967{
3968 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003969 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003970 else if (strcmp(name, "cursor") == 0)
3971 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003972 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003973
3974 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3975 }
3976 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003977 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003978 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003979 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003980 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003981 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003982 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003983 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003984 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003985 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003986 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003987 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3988 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003989 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003990 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003991 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003992 return NULL;
3993 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003994 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003995 }
3996 else if (strcmp(name, "tabpage") == 0)
3997 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003998 Py_INCREF(self->tabObject);
3999 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004000 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004001 else if (strcmp(name, "__members__") == 0)
4002 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004003 else
4004 return NULL;
4005}
4006
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004007 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004008WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004009{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004010 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011 return -1;
4012
4013 if (strcmp(name, "buffer") == 0)
4014 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004015 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004016 return -1;
4017 }
4018 else if (strcmp(name, "cursor") == 0)
4019 {
4020 long lnum;
4021 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004023 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004024 return -1;
4025
Bram Moolenaard6e39182013-05-21 18:30:34 +02004026 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004027 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004028 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004029 return -1;
4030 }
4031
4032 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004033 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 return -1;
4035
Bram Moolenaard6e39182013-05-21 18:30:34 +02004036 self->win->w_cursor.lnum = lnum;
4037 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004038 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004039 self->win->w_cursor.coladd = 0;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004040 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004043 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044 return 0;
4045 }
4046 else if (strcmp(name, "height") == 0)
4047 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004048 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049 win_T *savewin;
4050
Bram Moolenaardee2e312013-06-23 16:35:47 +02004051 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 return -1;
4053
4054#ifdef FEAT_GUI
4055 need_mouse_correct = TRUE;
4056#endif
4057 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004058 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004059
4060 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004061 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004063 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004064 return -1;
4065
4066 return 0;
4067 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 else if (strcmp(name, "width") == 0)
4069 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004070 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071 win_T *savewin;
4072
Bram Moolenaardee2e312013-06-23 16:35:47 +02004073 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 return -1;
4075
4076#ifdef FEAT_GUI
4077 need_mouse_correct = TRUE;
4078#endif
4079 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004080 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004081
4082 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004083 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004084 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004085 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 return -1;
4087
4088 return 0;
4089 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090 else
4091 {
4092 PyErr_SetString(PyExc_AttributeError, name);
4093 return -1;
4094 }
4095}
4096
4097 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004098WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004100 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004101 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102 else
4103 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004104 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105
Bram Moolenaar6d216452013-05-12 19:00:41 +02004106 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004107 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108 (self));
4109 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004110 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 }
4112}
4113
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004114static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004115 /* name, function, calling, documentation */
4116 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4117 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004118};
4119
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004120/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004121 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004123
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004124static PyTypeObject WinListType;
4125static PySequenceMethods WinListAsSeq;
4126
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004127typedef struct
4128{
4129 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004130 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004131} WinListObject;
4132
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004133 static PyObject *
4134WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004136 WinListObject *self;
4137
4138 self = PyObject_NEW(WinListObject, &WinListType);
4139 self->tabObject = tabObject;
4140 Py_INCREF(tabObject);
4141
4142 return (PyObject *)(self);
4143}
4144
4145 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004146WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004147{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004148 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004149
4150 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004151 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004152 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004153 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004154
4155 DESTRUCTOR_FINISH(self);
4156}
4157
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004158 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004159WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004160{
4161 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004162 PyInt n = 0;
4163
Bram Moolenaard6e39182013-05-21 18:30:34 +02004164 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004165 return -1;
4166
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167 while (w != NULL)
4168 {
4169 ++n;
4170 w = W_NEXT(w);
4171 }
4172
4173 return n;
4174}
4175
4176 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004177WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004178{
4179 win_T *w;
4180
Bram Moolenaard6e39182013-05-21 18:30:34 +02004181 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004182 return NULL;
4183
4184 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004185 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004186 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004187
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004188 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004189 return NULL;
4190}
4191
4192/* Convert a Python string into a Vim line.
4193 *
4194 * The result is in allocated memory. All internal nulls are replaced by
4195 * newline characters. It is an error for the string to contain newline
4196 * characters.
4197 *
4198 * On errors, the Python exception data is set, and NULL is returned.
4199 */
4200 static char *
4201StringToLine(PyObject *obj)
4202{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004203 char *str;
4204 char *save;
4205 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004206 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004207 PyInt i;
4208 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004209
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004210 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004211 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004212 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4213 || str == NULL)
4214 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004215 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004216 else if (PyUnicode_Check(obj))
4217 {
4218 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4219 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004220
Bram Moolenaardaa27022013-06-24 22:33:30 +02004221 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004222 || str == NULL)
4223 {
4224 Py_DECREF(bytes);
4225 return NULL;
4226 }
4227 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004228 else
4229 {
4230#if PY_MAJOR_VERSION < 3
4231 PyErr_FORMAT(PyExc_TypeError,
4232 N_("expected str() or unicode() instance, but got %s"),
4233 Py_TYPE_NAME(obj));
4234#else
4235 PyErr_FORMAT(PyExc_TypeError,
4236 N_("expected bytes() or str() instance, but got %s"),
4237 Py_TYPE_NAME(obj));
4238#endif
4239 return NULL;
4240 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004241
4242 /*
4243 * Error checking: String must not contain newlines, as we
4244 * are replacing a single line, and we must replace it with
4245 * a single line.
4246 * A trailing newline is removed, so that append(f.readlines()) works.
4247 */
4248 p = memchr(str, '\n', len);
4249 if (p != NULL)
4250 {
4251 if (p == str + len - 1)
4252 --len;
4253 else
4254 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004255 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004256 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004257 return NULL;
4258 }
4259 }
4260
4261 /* Create a copy of the string, with internal nulls replaced by
4262 * newline characters, as is the vim convention.
4263 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004264 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004265 if (save == NULL)
4266 {
4267 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004268 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269 return NULL;
4270 }
4271
4272 for (i = 0; i < len; ++i)
4273 {
4274 if (str[i] == '\0')
4275 save[i] = '\n';
4276 else
4277 save[i] = str[i];
4278 }
4279
4280 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004281 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004282
4283 return save;
4284}
4285
4286/* Get a line from the specified buffer. The line number is
4287 * in Vim format (1-based). The line is returned as a Python
4288 * string object.
4289 */
4290 static PyObject *
4291GetBufferLine(buf_T *buf, PyInt n)
4292{
4293 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4294}
4295
4296
4297/* Get a list of lines from the specified buffer. The line numbers
4298 * are in Vim format (1-based). The range is from lo up to, but not
4299 * including, hi. The list is returned as a Python list of string objects.
4300 */
4301 static PyObject *
4302GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4303{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004304 PyInt i;
4305 PyInt n = hi - lo;
4306 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307
4308 if (list == NULL)
4309 return NULL;
4310
4311 for (i = 0; i < n; ++i)
4312 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004313 PyObject *string = LineToString(
4314 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004315
4316 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004317 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004318 {
4319 Py_DECREF(list);
4320 return NULL;
4321 }
4322
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004323 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004324 }
4325
4326 /* The ownership of the Python list is passed to the caller (ie,
4327 * the caller should Py_DECREF() the object when it is finished
4328 * with it).
4329 */
4330
4331 return list;
4332}
4333
4334/*
4335 * Check if deleting lines made the cursor position invalid.
4336 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4337 * deleted).
4338 */
4339 static void
4340py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4341{
4342 if (curwin->w_cursor.lnum >= lo)
4343 {
4344 /* Adjust the cursor position if it's in/after the changed
4345 * lines. */
4346 if (curwin->w_cursor.lnum >= hi)
4347 {
4348 curwin->w_cursor.lnum += extra;
4349 check_cursor_col();
4350 }
4351 else if (extra < 0)
4352 {
4353 curwin->w_cursor.lnum = lo;
4354 check_cursor();
4355 }
4356 else
4357 check_cursor_col();
4358 changed_cline_bef_curs();
4359 }
4360 invalidate_botline();
4361}
4362
Bram Moolenaar19e60942011-06-19 00:27:51 +02004363/*
4364 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004365 * in Vim format (1-based). The replacement line is given as
4366 * a Python string object. The object is checked for validity
4367 * and correct format. Errors are returned as a value of FAIL.
4368 * The return value is OK on success.
4369 * If OK is returned and len_change is not NULL, *len_change
4370 * is set to the change in the buffer length.
4371 */
4372 static int
4373SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4374{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004375 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004376 win_T *save_curwin = NULL;
4377 tabpage_T *save_curtab = NULL;
4378
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004379 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004380 * There are three cases:
4381 * 1. NULL, or None - this is a deletion.
4382 * 2. A string - this is a replacement.
4383 * 3. Anything else - this is an error.
4384 */
4385 if (line == Py_None || line == NULL)
4386 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004387 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004388 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004389
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004390 VimTryStart();
4391
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004392 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004393 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004394 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004395 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396 else
4397 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004398 if (buf == curbuf && (save_curwin != NULL
4399 || save_curbuf.br_buf == NULL))
4400 // Using an existing window for the buffer, adjust the cursor
4401 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004402 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004403 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004404 /* Only adjust marks if we managed to switch to a window that
4405 * holds the buffer, otherwise line numbers will be invalid. */
4406 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407 }
4408
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004409 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004410
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004411 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412 return FAIL;
4413
4414 if (len_change)
4415 *len_change = -1;
4416
4417 return OK;
4418 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004419 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004421 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422
4423 if (save == NULL)
4424 return FAIL;
4425
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004426 VimTryStart();
4427
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428 /* We do not need to free "save" if ml_replace() consumes it. */
4429 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004430 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431
4432 if (u_savesub((linenr_T)n) == FAIL)
4433 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004434 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435 vim_free(save);
4436 }
4437 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4438 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004439 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440 vim_free(save);
4441 }
4442 else
4443 changed_bytes((linenr_T)n, 0);
4444
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004445 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446
4447 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004448 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004449 check_cursor_col();
4450
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004451 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004452 return FAIL;
4453
4454 if (len_change)
4455 *len_change = 0;
4456
4457 return OK;
4458 }
4459 else
4460 {
4461 PyErr_BadArgument();
4462 return FAIL;
4463 }
4464}
4465
Bram Moolenaar19e60942011-06-19 00:27:51 +02004466/* Replace a range of lines in the specified buffer. The line numbers are in
4467 * Vim format (1-based). The range is from lo up to, but not including, hi.
4468 * The replacement lines are given as a Python list of string objects. The
4469 * list is checked for validity and correct format. Errors are returned as a
4470 * value of FAIL. The return value is OK on success.
4471 * If OK is returned and len_change is not NULL, *len_change
4472 * is set to the change in the buffer length.
4473 */
4474 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004475SetBufferLineList(
4476 buf_T *buf,
4477 PyInt lo,
4478 PyInt hi,
4479 PyObject *list,
4480 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004481{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004482 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004483 win_T *save_curwin = NULL;
4484 tabpage_T *save_curtab = NULL;
4485
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004486 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004487 * There are three cases:
4488 * 1. NULL, or None - this is a deletion.
4489 * 2. A list - this is a replacement.
4490 * 3. Anything else - this is an error.
4491 */
4492 if (list == Py_None || list == NULL)
4493 {
4494 PyInt i;
4495 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004496
4497 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004498 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004499 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004500
4501 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004502 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004503 else
4504 {
4505 for (i = 0; i < n; ++i)
4506 {
4507 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4508 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004509 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004510 break;
4511 }
4512 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004513 if (buf == curbuf && (save_curwin != NULL
4514 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004515 /* Using an existing window for the buffer, adjust the cursor
4516 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004517 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004518 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004519 /* Only adjust marks if we managed to switch to a window that
4520 * holds the buffer, otherwise line numbers will be invalid. */
4521 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004522 }
4523
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004524 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004525
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004526 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004527 return FAIL;
4528
4529 if (len_change)
4530 *len_change = -n;
4531
4532 return OK;
4533 }
4534 else if (PyList_Check(list))
4535 {
4536 PyInt i;
4537 PyInt new_len = PyList_Size(list);
4538 PyInt old_len = hi - lo;
4539 PyInt extra = 0; /* lines added to text, can be negative */
4540 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541
4542 if (new_len == 0) /* avoid allocating zero bytes */
4543 array = NULL;
4544 else
4545 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004546 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004547 if (array == NULL)
4548 {
4549 PyErr_NoMemory();
4550 return FAIL;
4551 }
4552 }
4553
4554 for (i = 0; i < new_len; ++i)
4555 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004556 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004557
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004558 if (!(line = PyList_GetItem(list, i)) ||
4559 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004560 {
4561 while (i)
4562 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004563 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004564 return FAIL;
4565 }
4566 }
4567
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004568 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004569 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004570
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004571 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004572 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004573
4574 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004575 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004576
4577 /* If the size of the range is reducing (ie, new_len < old_len) we
4578 * need to delete some old_len. We do this at the start, by
4579 * repeatedly deleting line "lo".
4580 */
4581 if (!PyErr_Occurred())
4582 {
4583 for (i = 0; i < old_len - new_len; ++i)
4584 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4585 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004586 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004587 break;
4588 }
4589 extra -= i;
4590 }
4591
4592 /* For as long as possible, replace the existing old_len with the
4593 * new old_len. This is a more efficient operation, as it requires
4594 * less memory allocation and freeing.
4595 */
4596 if (!PyErr_Occurred())
4597 {
4598 for (i = 0; i < old_len && i < new_len; ++i)
4599 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4600 == FAIL)
4601 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004602 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004603 break;
4604 }
4605 }
4606 else
4607 i = 0;
4608
4609 /* Now we may need to insert the remaining new old_len. If we do, we
4610 * must free the strings as we finish with them (we can't pass the
4611 * responsibility to vim in this case).
4612 */
4613 if (!PyErr_Occurred())
4614 {
4615 while (i < new_len)
4616 {
4617 if (ml_append((linenr_T)(lo + i - 1),
4618 (char_u *)array[i], 0, FALSE) == FAIL)
4619 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004620 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004621 break;
4622 }
4623 vim_free(array[i]);
4624 ++i;
4625 ++extra;
4626 }
4627 }
4628
4629 /* Free any left-over old_len, as a result of an error */
4630 while (i < new_len)
4631 {
4632 vim_free(array[i]);
4633 ++i;
4634 }
4635
4636 /* Free the array of old_len. All of its contents have now
4637 * been dealt with (either freed, or the responsibility passed
4638 * to vim.
4639 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004640 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004641
4642 /* Adjust marks. Invalidate any which lie in the
4643 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004644 * Only adjust marks if we managed to switch to a window that holds
4645 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004646 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004647 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004648 (long)MAXLNUM, (long)extra);
4649 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4650
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004651 if (buf == curbuf && (save_curwin != NULL
4652 || save_curbuf.br_buf == NULL))
4653 // Using an existing window for the buffer, adjust the cursor
4654 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004655 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4656
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004657 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004658 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004659
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004660 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004661 return FAIL;
4662
4663 if (len_change)
4664 *len_change = new_len - old_len;
4665
4666 return OK;
4667 }
4668 else
4669 {
4670 PyErr_BadArgument();
4671 return FAIL;
4672 }
4673}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004674
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004675/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004676 * The line number is in Vim format (1-based). The lines to be inserted are
4677 * given as a Python list of string objects or as a single string. The lines
4678 * to be added are checked for validity and correct format. Errors are
4679 * returned as a value of FAIL. The return value is OK on success.
4680 * If OK is returned and len_change is not NULL, *len_change
4681 * is set to the change in the buffer length.
4682 */
4683 static int
4684InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4685{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004686 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004687 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004688 tabpage_T *save_curtab = NULL;
4689
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004690 /* First of all, we check the type of the supplied Python object.
4691 * It must be a string or a list, or the call is in error.
4692 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004693 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004695 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004696
4697 if (str == NULL)
4698 return FAIL;
4699
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004700 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004701 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004702 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004703
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004704 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004705 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004707 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004708 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004709 /* Only adjust marks if we managed to switch to a window that
4710 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711 appended_lines_mark((linenr_T)n, 1L);
4712
4713 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004714 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004715 update_screen(VALID);
4716
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004717 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004718 return FAIL;
4719
4720 if (len_change)
4721 *len_change = 1;
4722
4723 return OK;
4724 }
4725 else if (PyList_Check(lines))
4726 {
4727 PyInt i;
4728 PyInt size = PyList_Size(lines);
4729 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004731 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732 if (array == NULL)
4733 {
4734 PyErr_NoMemory();
4735 return FAIL;
4736 }
4737
4738 for (i = 0; i < size; ++i)
4739 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004740 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004742 if (!(line = PyList_GetItem(lines, i)) ||
4743 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004744 {
4745 while (i)
4746 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004747 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004748 return FAIL;
4749 }
4750 }
4751
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004753 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004754 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004755
4756 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004757 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004758 else
4759 {
4760 for (i = 0; i < size; ++i)
4761 {
4762 if (ml_append((linenr_T)(n + i),
4763 (char_u *)array[i], 0, FALSE) == FAIL)
4764 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004765 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004766
4767 /* Free the rest of the lines */
4768 while (i < size)
4769 vim_free(array[i++]);
4770
4771 break;
4772 }
4773 vim_free(array[i]);
4774 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004775 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004776 /* Only adjust marks if we managed to switch to a window that
4777 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004778 appended_lines_mark((linenr_T)n, (long)i);
4779 }
4780
4781 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004782 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004783 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004784 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004785
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 update_screen(VALID);
4787
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004788 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004789 return FAIL;
4790
4791 if (len_change)
4792 *len_change = size;
4793
4794 return OK;
4795 }
4796 else
4797 {
4798 PyErr_BadArgument();
4799 return FAIL;
4800 }
4801}
4802
4803/*
4804 * Common routines for buffers and line ranges
4805 * -------------------------------------------
4806 */
4807
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004808typedef struct
4809{
4810 PyObject_HEAD
4811 buf_T *buf;
4812} BufferObject;
4813
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004814 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004815CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004816{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004817 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004818 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004819 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004820 return -1;
4821 }
4822
4823 return 0;
4824}
4825
4826 static PyObject *
4827RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4828{
4829 if (CheckBuffer(self))
4830 return NULL;
4831
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004832 if (end == -1)
4833 end = self->buf->b_ml.ml_line_count;
4834
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004835 if (n < 0)
4836 n += end - start + 1;
4837
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004838 if (n < 0 || n > end - start)
4839 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004840 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004841 return NULL;
4842 }
4843
4844 return GetBufferLine(self->buf, n+start);
4845}
4846
4847 static PyObject *
4848RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4849{
4850 PyInt size;
4851
4852 if (CheckBuffer(self))
4853 return NULL;
4854
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004855 if (end == -1)
4856 end = self->buf->b_ml.ml_line_count;
4857
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004858 size = end - start + 1;
4859
4860 if (lo < 0)
4861 lo = 0;
4862 else if (lo > size)
4863 lo = size;
4864 if (hi < 0)
4865 hi = 0;
4866 if (hi < lo)
4867 hi = lo;
4868 else if (hi > size)
4869 hi = size;
4870
4871 return GetBufferLineList(self->buf, lo+start, hi+start);
4872}
4873
4874 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004875RBAsItem(
4876 BufferObject *self,
4877 PyInt n,
4878 PyObject *valObject,
4879 PyInt start,
4880 PyInt end,
4881 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004882{
4883 PyInt len_change;
4884
4885 if (CheckBuffer(self))
4886 return -1;
4887
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004888 if (end == -1)
4889 end = self->buf->b_ml.ml_line_count;
4890
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004891 if (n < 0)
4892 n += end - start + 1;
4893
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004894 if (n < 0 || n > end - start)
4895 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004896 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004897 return -1;
4898 }
4899
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004900 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004901 return -1;
4902
4903 if (new_end)
4904 *new_end = end + len_change;
4905
4906 return 0;
4907}
4908
Bram Moolenaar19e60942011-06-19 00:27:51 +02004909 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004910RBAsSlice(
4911 BufferObject *self,
4912 PyInt lo,
4913 PyInt hi,
4914 PyObject *valObject,
4915 PyInt start,
4916 PyInt end,
4917 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004918{
4919 PyInt size;
4920 PyInt len_change;
4921
4922 /* Self must be a valid buffer */
4923 if (CheckBuffer(self))
4924 return -1;
4925
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004926 if (end == -1)
4927 end = self->buf->b_ml.ml_line_count;
4928
Bram Moolenaar19e60942011-06-19 00:27:51 +02004929 /* Sort out the slice range */
4930 size = end - start + 1;
4931
4932 if (lo < 0)
4933 lo = 0;
4934 else if (lo > size)
4935 lo = size;
4936 if (hi < 0)
4937 hi = 0;
4938 if (hi < lo)
4939 hi = lo;
4940 else if (hi > size)
4941 hi = size;
4942
4943 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004944 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004945 return -1;
4946
4947 if (new_end)
4948 *new_end = end + len_change;
4949
4950 return 0;
4951}
4952
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004953
4954 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004955RBAppend(
4956 BufferObject *self,
4957 PyObject *args,
4958 PyInt start,
4959 PyInt end,
4960 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004961{
4962 PyObject *lines;
4963 PyInt len_change;
4964 PyInt max;
4965 PyInt n;
4966
4967 if (CheckBuffer(self))
4968 return NULL;
4969
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004970 if (end == -1)
4971 end = self->buf->b_ml.ml_line_count;
4972
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004973 max = n = end - start + 1;
4974
4975 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4976 return NULL;
4977
4978 if (n < 0 || n > max)
4979 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004980 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004981 return NULL;
4982 }
4983
4984 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4985 return NULL;
4986
4987 if (new_end)
4988 *new_end = end + len_change;
4989
4990 Py_INCREF(Py_None);
4991 return Py_None;
4992}
4993
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004994/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004995 */
4996
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004997static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004998static PySequenceMethods RangeAsSeq;
4999static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005000
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005001typedef struct
5002{
5003 PyObject_HEAD
5004 BufferObject *buf;
5005 PyInt start;
5006 PyInt end;
5007} RangeObject;
5008
5009 static PyObject *
5010RangeNew(buf_T *buf, PyInt start, PyInt end)
5011{
5012 BufferObject *bufr;
5013 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005014 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005015 if (self == NULL)
5016 return NULL;
5017
5018 bufr = (BufferObject *)BufferNew(buf);
5019 if (bufr == NULL)
5020 {
5021 Py_DECREF(self);
5022 return NULL;
5023 }
5024 Py_INCREF(bufr);
5025
5026 self->buf = bufr;
5027 self->start = start;
5028 self->end = end;
5029
5030 return (PyObject *)(self);
5031}
5032
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005033 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005034RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005035{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005036 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005037 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005038 PyObject_GC_Del((void *)(self));
5039}
5040
5041 static int
5042RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5043{
5044 Py_VISIT(((PyObject *)(self->buf)));
5045 return 0;
5046}
5047
5048 static int
5049RangeClear(RangeObject *self)
5050{
5051 Py_CLEAR(self->buf);
5052 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005053}
5054
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005055 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005056RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005057{
5058 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005059 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060 return -1; /* ??? */
5061
Bram Moolenaard6e39182013-05-21 18:30:34 +02005062 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005063}
5064
5065 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005067{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005068 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005069}
5070
5071 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005072RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005073{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005074 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005075}
5076
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005077static char *RangeAttrs[] = {
5078 "start", "end",
5079 NULL
5080};
5081
5082 static PyObject *
5083RangeDir(PyObject *self)
5084{
5085 return ObjectDir(self, RangeAttrs);
5086}
5087
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005090{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005091 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005092}
5093
5094 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005095RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005096{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005097 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005098 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5099 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100 else
5101 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103
5104 if (name == NULL)
5105 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005107 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005108 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005109 }
5110}
5111
5112static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005113 /* name, function, calling, documentation */
5114 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005115 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5116 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005117};
5118
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005119static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120static PySequenceMethods BufferAsSeq;
5121static PyMappingMethods BufferAsMapping;
5122
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005123 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005124BufferNew(buf_T *buf)
5125{
5126 /* We need to handle deletion of buffers underneath us.
5127 * If we add a "b_python*_ref" field to the buf_T structure,
5128 * then we can get at it in buf_freeall() in vim. We then
5129 * need to create only ONE Python object per buffer - if
5130 * we try to create a second, just INCREF the existing one
5131 * and return it. The (single) Python object referring to
5132 * the buffer is stored in "b_python*_ref".
5133 * Question: what to do on a buf_freeall(). We'll probably
5134 * have to either delete the Python object (DECREF it to
5135 * zero - a bad idea, as it leaves dangling refs!) or
5136 * set the buf_T * value to an invalid value (-1?), which
5137 * means we need checks in all access functions... Bah.
5138 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005140 * b_python_ref and b_python3_ref fields respectively.
5141 */
5142
5143 BufferObject *self;
5144
5145 if (BUF_PYTHON_REF(buf) != NULL)
5146 {
5147 self = BUF_PYTHON_REF(buf);
5148 Py_INCREF(self);
5149 }
5150 else
5151 {
5152 self = PyObject_NEW(BufferObject, &BufferType);
5153 if (self == NULL)
5154 return NULL;
5155 self->buf = buf;
5156 BUF_PYTHON_REF(buf) = self;
5157 }
5158
5159 return (PyObject *)(self);
5160}
5161
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005162 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005163BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005164{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005165 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5166 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005167
5168 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005169}
5170
Bram Moolenaar971db462013-05-12 18:44:48 +02005171 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005173{
5174 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005176 return -1; /* ??? */
5177
Bram Moolenaard6e39182013-05-21 18:30:34 +02005178 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005179}
5180
5181 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005182BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005183{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005184 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005185}
5186
5187 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005188BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005189{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005190 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005191}
5192
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005193static char *BufferAttrs[] = {
5194 "name", "number", "vars", "options", "valid",
5195 NULL
5196};
5197
5198 static PyObject *
5199BufferDir(PyObject *self)
5200{
5201 return ObjectDir(self, BufferAttrs);
5202}
5203
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005204 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005205BufferAttrValid(BufferObject *self, char *name)
5206{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005207 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005208
5209 if (strcmp(name, "valid") != 0)
5210 return NULL;
5211
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005212 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5213 Py_INCREF(ret);
5214 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005215}
5216
5217 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005218BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005219{
5220 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005221 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005222 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005223 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005224 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005225 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005226 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005227 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005228 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5229 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005230 else if (strcmp(name, "__members__") == 0)
5231 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 else
5233 return NULL;
5234}
5235
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005236 static int
5237BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5238{
5239 if (CheckBuffer(self))
5240 return -1;
5241
5242 if (strcmp(name, "name") == 0)
5243 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005244 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005245 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005246 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005247 PyObject *todecref;
5248
5249 if (!(val = StringToChars(valObject, &todecref)))
5250 return -1;
5251
5252 VimTryStart();
5253 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5254 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005255 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005256 aucmd_restbuf(&aco);
5257 Py_XDECREF(todecref);
5258 if (VimTryEnd())
5259 return -1;
5260
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005261 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005262 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005263 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005264 return -1;
5265 }
5266 return 0;
5267 }
5268 else
5269 {
5270 PyErr_SetString(PyExc_AttributeError, name);
5271 return -1;
5272 }
5273}
5274
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005275 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005276BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005277{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005278 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005279}
5280
5281 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005282BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005283{
5284 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005285 char_u *pmark;
5286 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005287 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005288 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005289
Bram Moolenaard6e39182013-05-21 18:30:34 +02005290 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005291 return NULL;
5292
Bram Moolenaar389a1792013-06-23 13:00:44 +02005293 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005294 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005295
Bram Moolenaar389a1792013-06-23 13:00:44 +02005296 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005297 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005298 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005299 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005300 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005301 return NULL;
5302 }
5303
5304 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005305
5306 Py_XDECREF(todecref);
5307
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005308 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005309 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005310 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005311 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005312 if (VimTryEnd())
5313 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005314
5315 if (posp == NULL)
5316 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005317 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005318 return NULL;
5319 }
5320
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321 if (posp->lnum <= 0)
5322 {
5323 /* Or raise an error? */
5324 Py_INCREF(Py_None);
5325 return Py_None;
5326 }
5327
5328 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5329}
5330
5331 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005332BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005333{
5334 PyInt start;
5335 PyInt end;
5336
Bram Moolenaard6e39182013-05-21 18:30:34 +02005337 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005338 return NULL;
5339
5340 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5341 return NULL;
5342
Bram Moolenaard6e39182013-05-21 18:30:34 +02005343 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005344}
5345
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005346 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005347BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005348{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005349 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005350 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005351 else
5352 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005353 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005354
5355 if (name == NULL)
5356 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005357
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005358 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005359 }
5360}
5361
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005362static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005363 /* name, function, calling, documentation */
5364 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005365 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005366 {"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 +02005367 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5368 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005369};
5370
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005371/*
5372 * Buffer list object - Implementation
5373 */
5374
5375static PyTypeObject BufMapType;
5376
5377typedef struct
5378{
5379 PyObject_HEAD
5380} BufMapObject;
5381
5382 static PyInt
5383BufMapLength(PyObject *self UNUSED)
5384{
5385 buf_T *b = firstbuf;
5386 PyInt n = 0;
5387
5388 while (b)
5389 {
5390 ++n;
5391 b = b->b_next;
5392 }
5393
5394 return n;
5395}
5396
5397 static PyObject *
5398BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5399{
5400 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005401 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005402
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005403 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005404 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005405
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005406 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005407
5408 if (b)
5409 return BufferNew(b);
5410 else
5411 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005412 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005413 return NULL;
5414 }
5415}
5416
5417 static void
5418BufMapIterDestruct(PyObject *buffer)
5419{
5420 /* Iteration was stopped before all buffers were processed */
5421 if (buffer)
5422 {
5423 Py_DECREF(buffer);
5424 }
5425}
5426
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005427 static int
5428BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5429{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005430 if (buffer)
5431 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005432 return 0;
5433}
5434
5435 static int
5436BufMapIterClear(PyObject **buffer)
5437{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005438 if (*buffer)
5439 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005440 return 0;
5441}
5442
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005443 static PyObject *
5444BufMapIterNext(PyObject **buffer)
5445{
5446 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005447 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005448
5449 if (!*buffer)
5450 return NULL;
5451
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005452 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005453
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005454 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005455 {
5456 *buffer = NULL;
5457 return NULL;
5458 }
5459
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005460 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005461 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005462 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005463 return NULL;
5464 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005465 /* Do not increment reference: we no longer hold it (decref), but whoever
5466 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005467 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005468}
5469
5470 static PyObject *
5471BufMapIter(PyObject *self UNUSED)
5472{
5473 PyObject *buffer;
5474
5475 buffer = BufferNew(firstbuf);
5476 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005477 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5478 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005479}
5480
5481static PyMappingMethods BufMapAsMapping = {
5482 (lenfunc) BufMapLength,
5483 (binaryfunc) BufMapItem,
5484 (objobjargproc) 0,
5485};
5486
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005487/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005488 */
5489
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005490static char *CurrentAttrs[] = {
5491 "buffer", "window", "line", "range", "tabpage",
5492 NULL
5493};
5494
5495 static PyObject *
5496CurrentDir(PyObject *self)
5497{
5498 return ObjectDir(self, CurrentAttrs);
5499}
5500
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005501 static PyObject *
5502CurrentGetattr(PyObject *self UNUSED, char *name)
5503{
5504 if (strcmp(name, "buffer") == 0)
5505 return (PyObject *)BufferNew(curbuf);
5506 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005507 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005508 else if (strcmp(name, "tabpage") == 0)
5509 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005510 else if (strcmp(name, "line") == 0)
5511 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5512 else if (strcmp(name, "range") == 0)
5513 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005514 else if (strcmp(name, "__members__") == 0)
5515 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005516 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005517#if PY_MAJOR_VERSION < 3
5518 return Py_FindMethod(WindowMethods, self, name);
5519#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005520 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005521#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005522}
5523
5524 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005525CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005526{
5527 if (strcmp(name, "line") == 0)
5528 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005529 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5530 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005531 return -1;
5532
5533 return 0;
5534 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005535 else if (strcmp(name, "buffer") == 0)
5536 {
5537 int count;
5538
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005539 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005540 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005541 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005542 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005543 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005544 return -1;
5545 }
5546
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005547 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005548 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005549 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005550
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005551 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005552 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5553 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005554 if (VimTryEnd())
5555 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005556 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005557 return -1;
5558 }
5559
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005560 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005561 }
5562 else if (strcmp(name, "window") == 0)
5563 {
5564 int count;
5565
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005566 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005567 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005568 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005569 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005570 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005571 return -1;
5572 }
5573
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005574 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005575 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005576 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005577
5578 if (!count)
5579 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005580 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005581 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 return -1;
5583 }
5584
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005585 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005586 win_goto(((WindowObject *)(valObject))->win);
5587 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005588 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005589 if (VimTryEnd())
5590 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005591 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005592 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005593 return -1;
5594 }
5595
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005596 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005597 }
5598 else if (strcmp(name, "tabpage") == 0)
5599 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005600 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005601 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005602 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005603 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005604 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005605 return -1;
5606 }
5607
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005608 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005609 return -1;
5610
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005611 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005612 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5613 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005614 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005615 if (VimTryEnd())
5616 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005617 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005618 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005619 return -1;
5620 }
5621
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005622 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005623 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005624 else
5625 {
5626 PyErr_SetString(PyExc_AttributeError, name);
5627 return -1;
5628 }
5629}
5630
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005631static struct PyMethodDef CurrentMethods[] = {
5632 /* name, function, calling, documentation */
5633 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5634 { NULL, NULL, 0, NULL}
5635};
5636
Bram Moolenaardb913952012-06-29 12:54:53 +02005637 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005638init_range_cmd(exarg_T *eap)
5639{
5640 RangeStart = eap->line1;
5641 RangeEnd = eap->line2;
5642}
5643
5644 static void
5645init_range_eval(typval_T *rettv UNUSED)
5646{
5647 RangeStart = (PyInt) curwin->w_cursor.lnum;
5648 RangeEnd = RangeStart;
5649}
5650
5651 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005652run_cmd(const char *cmd, void *arg UNUSED
5653#ifdef PY_CAN_RECURSE
5654 , PyGILState_STATE *pygilstate UNUSED
5655#endif
5656 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005657{
Bram Moolenaar41009372013-07-01 22:03:04 +02005658 PyObject *run_ret;
5659 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5660 if (run_ret != NULL)
5661 {
5662 Py_DECREF(run_ret);
5663 }
5664 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005666 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005667 PyErr_Clear();
5668 }
5669 else
5670 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005671}
5672
5673static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5674static int code_hdr_len = 30;
5675
5676 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005677run_do(const char *cmd, void *arg UNUSED
5678#ifdef PY_CAN_RECURSE
5679 , PyGILState_STATE *pygilstate
5680#endif
5681 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005682{
5683 PyInt lnum;
5684 size_t len;
5685 char *code;
5686 int status;
5687 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005688 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005689 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005690
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005691 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005692 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005693 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005694 return;
5695 }
5696
5697 len = code_hdr_len + STRLEN(cmd);
5698 code = PyMem_New(char, len + 1);
5699 memcpy(code, code_hdr, code_hdr_len);
5700 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005701 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5702 status = -1;
5703 if (run_ret != NULL)
5704 {
5705 status = 0;
5706 Py_DECREF(run_ret);
5707 }
5708 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5709 {
5710 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005711 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005712 PyErr_Clear();
5713 return;
5714 }
5715 else
5716 PyErr_PrintEx(1);
5717
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005718 PyMem_Free(code);
5719
5720 if (status)
5721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005722 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005723 return;
5724 }
5725
5726 status = 0;
5727 pymain = PyImport_AddModule("__main__");
5728 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005729#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005730 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005731#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005732
5733 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5734 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005735 PyObject *line;
5736 PyObject *linenr;
5737 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005738
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005739#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005740 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005741#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005742 /* Check the line number, the command my have deleted lines. */
5743 if (lnum > curbuf->b_ml.ml_line_count
5744 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005745 goto err;
5746 if (!(linenr = PyInt_FromLong((long) lnum)))
5747 {
5748 Py_DECREF(line);
5749 goto err;
5750 }
5751 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5752 Py_DECREF(line);
5753 Py_DECREF(linenr);
5754 if (!ret)
5755 goto err;
5756
Bram Moolenaara58883b2017-01-29 21:31:09 +01005757 /* Check that the command didn't switch to another buffer. */
5758 if (curbuf != was_curbuf)
5759 {
5760 Py_XDECREF(ret);
5761 goto err;
5762 }
5763
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764 if (ret != Py_None)
5765 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005766 {
5767 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005768 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005769 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005770
5771 Py_XDECREF(ret);
5772 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005773#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005774 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005775#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005776 }
5777 goto out;
5778err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005779#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005780 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005781#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005782 PyErr_PrintEx(0);
5783 PythonIO_Flush();
5784 status = 1;
5785out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005786#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005787 if (!status)
5788 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005789#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005790 Py_DECREF(pyfunc);
5791 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5792 if (status)
5793 return;
5794 check_cursor();
5795 update_curbuf(NOT_VALID);
5796}
5797
5798 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005799run_eval(const char *cmd, typval_T *rettv
5800#ifdef PY_CAN_RECURSE
5801 , PyGILState_STATE *pygilstate UNUSED
5802#endif
5803 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005804{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005805 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806
Bram Moolenaar41009372013-07-01 22:03:04 +02005807 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005808 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005809 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005810 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005812 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005813 PyErr_Clear();
5814 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005815 else
5816 {
5817 if (PyErr_Occurred() && !msg_silent)
5818 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005819 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005820 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005821 }
5822 else
5823 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005824 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005825 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005826 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005827 }
5828 PyErr_Clear();
5829}
5830
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005831 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005832set_ref_in_py(const int copyID)
5833{
5834 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005835 list_T *ll;
5836 int i;
5837 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005838 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005839
5840 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005841 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005842 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005843 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5844 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005845 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005846
5847 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005848 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005849 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005850 {
5851 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005852 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005853 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005854 }
5855
Bram Moolenaar8110a092016-04-14 15:56:09 +02005856 if (lastfunc != NULL)
5857 {
5858 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5859 {
5860 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005861 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005862 if (func->argc)
5863 for (i = 0; !abort && i < func->argc; ++i)
5864 abort = abort
5865 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5866 }
5867 }
5868
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005869 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005870}
5871
5872 static int
5873set_string_copy(char_u *str, typval_T *tv)
5874{
5875 tv->vval.v_string = vim_strsave(str);
5876 if (tv->vval.v_string == NULL)
5877 {
5878 PyErr_NoMemory();
5879 return -1;
5880 }
5881 return 0;
5882}
5883
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005884 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005885pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005886{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005887 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005888 char_u *key;
5889 dictitem_T *di;
5890 PyObject *keyObject;
5891 PyObject *valObject;
5892 Py_ssize_t iter = 0;
5893
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005894 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005895 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005896
5897 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005898 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005899
5900 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5901 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005902 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005903
Bram Moolenaara03e6312013-05-29 22:49:26 +02005904 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005905 {
5906 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005907 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005908 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005909
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005910 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005911 {
5912 dict_unref(dict);
5913 return -1;
5914 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005915
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005916 if (*key == NUL)
5917 {
5918 dict_unref(dict);
5919 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005920 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005921 return -1;
5922 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005923
5924 di = dictitem_alloc(key);
5925
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005926 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005927
5928 if (di == NULL)
5929 {
5930 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005931 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005932 return -1;
5933 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005935 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936 {
5937 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005938 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939 return -1;
5940 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005941
5942 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005943 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005944 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005945 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005947 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005948 return -1;
5949 }
5950 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005951
5952 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 return 0;
5954}
5955
5956 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005957pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005958{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005959 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005960 char_u *key;
5961 dictitem_T *di;
5962 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005963 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005964 PyObject *keyObject;
5965 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005967 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969
5970 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005971 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005972
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005973 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005974 {
5975 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005977 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005978
5979 if (!(iterator = PyObject_GetIter(list)))
5980 {
5981 dict_unref(dict);
5982 Py_DECREF(list);
5983 return -1;
5984 }
5985 Py_DECREF(list);
5986
5987 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005988 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005989 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005990
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005991 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005992 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005993 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005994 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005995 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005996 return -1;
5997 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005998
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005999 if (*key == NUL)
6000 {
6001 Py_DECREF(keyObject);
6002 Py_DECREF(iterator);
6003 Py_XDECREF(todecref);
6004 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006005 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006006 return -1;
6007 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006008
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006009 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006010 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006011 Py_DECREF(keyObject);
6012 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006013 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006014 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006015 return -1;
6016 }
6017
6018 di = dictitem_alloc(key);
6019
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006020 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006021 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006022
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006023 if (di == NULL)
6024 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006025 Py_DECREF(iterator);
6026 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006027 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006028 PyErr_NoMemory();
6029 return -1;
6030 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006031
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006032 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006033 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006034 Py_DECREF(iterator);
6035 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006036 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006037 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006038 return -1;
6039 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006040
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006041 Py_DECREF(valObject);
6042
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006043 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006044 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006045 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006046 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006047 dictitem_free(di);
6048 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006049 return -1;
6050 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006051 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006052 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006053 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006054 return 0;
6055}
6056
6057 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006058pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006059{
6060 list_T *l;
6061
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006062 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006063 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064
6065 tv->v_type = VAR_LIST;
6066 tv->vval.v_list = l;
6067
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006068 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006069 {
6070 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006072 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006073
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006074 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006075 return 0;
6076}
6077
Bram Moolenaardb913952012-06-29 12:54:53 +02006078typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6079
6080 static int
6081convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006082 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006083{
6084 PyObject *capsule;
6085 char hexBuf[sizeof(void *) * 2 + 3];
6086
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006087 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006088
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006089#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006090 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006091#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006092 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006093#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006094 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006095 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006096#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006097 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006098#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006099 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006100#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006101 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6102 {
6103 Py_DECREF(capsule);
6104 tv->v_type = VAR_UNKNOWN;
6105 return -1;
6106 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006107
6108 Py_DECREF(capsule);
6109
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006110 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006111 {
6112 tv->v_type = VAR_UNKNOWN;
6113 return -1;
6114 }
6115 /* As we are not using copy_tv which increments reference count we must
6116 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006117 if (tv->v_type == VAR_DICT)
6118 ++tv->vval.v_dict->dv_refcount;
6119 else if (tv->v_type == VAR_LIST)
6120 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006121 }
6122 else
6123 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006124 typval_T *v;
6125
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006126#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006127 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006128#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006129 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006130#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006131 copy_tv(v, tv);
6132 }
6133 return 0;
6134}
6135
6136 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006137ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6138{
6139 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006140 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006141
6142 if (!(lookup_dict = PyDict_New()))
6143 return -1;
6144
6145 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6146 {
6147 tv->v_type = VAR_DICT;
6148 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6149 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006150 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006151 }
6152 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006153 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006154 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006155 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006156 else
6157 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006158 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006159 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006160 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006161 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006162 }
6163 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006164 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006165}
6166
6167 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006168ConvertFromPySequence(PyObject *obj, typval_T *tv)
6169{
6170 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006171 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006172
6173 if (!(lookup_dict = PyDict_New()))
6174 return -1;
6175
6176 if (PyType_IsSubtype(obj->ob_type, &ListType))
6177 {
6178 tv->v_type = VAR_LIST;
6179 tv->vval.v_list = (((ListObject *)(obj))->list);
6180 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006181 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006182 }
6183 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006184 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006185 else
6186 {
6187 PyErr_FORMAT(PyExc_TypeError,
6188 N_("unable to convert %s to vim list"),
6189 Py_TYPE_NAME(obj));
6190 ret = -1;
6191 }
6192 Py_DECREF(lookup_dict);
6193 return ret;
6194}
6195
6196 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006197ConvertFromPyObject(PyObject *obj, typval_T *tv)
6198{
6199 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006200 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006201
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006202 if (!(lookup_dict = PyDict_New()))
6203 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006204 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006205 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006206 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006207}
6208
6209 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006210_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006211{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006212 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006213 {
6214 tv->v_type = VAR_DICT;
6215 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6216 ++tv->vval.v_dict->dv_refcount;
6217 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006218 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006219 {
6220 tv->v_type = VAR_LIST;
6221 tv->vval.v_list = (((ListObject *)(obj))->list);
6222 ++tv->vval.v_list->lv_refcount;
6223 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006224 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006225 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006226 FunctionObject *func = (FunctionObject *) obj;
6227 if (func->self != NULL || func->argv != NULL)
6228 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006229 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6230
Bram Moolenaar8110a092016-04-14 15:56:09 +02006231 set_partial(func, pt, TRUE);
6232 tv->vval.v_partial = pt;
6233 tv->v_type = VAR_PARTIAL;
6234 }
6235 else
6236 {
6237 if (set_string_copy(func->name, tv) == -1)
6238 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006239
Bram Moolenaar8110a092016-04-14 15:56:09 +02006240 tv->v_type = VAR_FUNC;
6241 }
6242 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006243 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006244 else if (PyBytes_Check(obj))
6245 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006246 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006247
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006248 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006249 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006250 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 return -1;
6252
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006253 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006254 return -1;
6255
6256 tv->v_type = VAR_STRING;
6257 }
6258 else if (PyUnicode_Check(obj))
6259 {
6260 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006261 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006262
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006263 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006264 if (bytes == NULL)
6265 return -1;
6266
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006267 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006268 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006269 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006270 return -1;
6271
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006272 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006273 {
6274 Py_XDECREF(bytes);
6275 return -1;
6276 }
6277 Py_XDECREF(bytes);
6278
6279 tv->v_type = VAR_STRING;
6280 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006281#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006282 else if (PyInt_Check(obj))
6283 {
6284 tv->v_type = VAR_NUMBER;
6285 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006286 if (PyErr_Occurred())
6287 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006288 }
6289#endif
6290 else if (PyLong_Check(obj))
6291 {
6292 tv->v_type = VAR_NUMBER;
6293 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006294 if (PyErr_Occurred())
6295 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006296 }
6297 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006298 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006299#ifdef FEAT_FLOAT
6300 else if (PyFloat_Check(obj))
6301 {
6302 tv->v_type = VAR_FLOAT;
6303 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6304 }
6305#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006306 else if (PyObject_HasAttrString(obj, "keys"))
6307 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006308 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006309 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006310 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006311 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006312 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006313 else if (PyNumber_Check(obj))
6314 {
6315 PyObject *num;
6316
6317 if (!(num = PyNumber_Long(obj)))
6318 return -1;
6319
6320 tv->v_type = VAR_NUMBER;
6321 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6322
6323 Py_DECREF(num);
6324 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006325 else if (obj == Py_None)
6326 {
6327 tv->v_type = VAR_SPECIAL;
6328 tv->vval.v_number = VVAL_NONE;
6329 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006330 else
6331 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006332 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006333 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006334 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006335 return -1;
6336 }
6337 return 0;
6338}
6339
6340 static PyObject *
6341ConvertToPyObject(typval_T *tv)
6342{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006343 typval_T *argv;
6344 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006345 if (tv == NULL)
6346 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006347 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006348 return NULL;
6349 }
6350 switch (tv->v_type)
6351 {
6352 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006353 return PyBytes_FromString(tv->vval.v_string == NULL
6354 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006355 case VAR_NUMBER:
6356 return PyLong_FromLong((long) tv->vval.v_number);
6357#ifdef FEAT_FLOAT
6358 case VAR_FLOAT:
6359 return PyFloat_FromDouble((double) tv->vval.v_float);
6360#endif
6361 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006362 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006363 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006364 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006365 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006366 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006367 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006368 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006369 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006370 if (tv->vval.v_partial->pt_argc)
6371 {
6372 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6373 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6374 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6375 }
6376 else
6377 argv = NULL;
6378 if (tv->vval.v_partial->pt_dict != NULL)
6379 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006380 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006381 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006382 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006383 tv->vval.v_partial->pt_dict,
6384 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006385 case VAR_BLOB:
6386 return PyBytes_FromStringAndSize(
6387 (char*) tv->vval.v_blob->bv_ga.ga_data,
6388 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006389 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006390 case VAR_CHANNEL:
6391 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 Py_INCREF(Py_None);
6393 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006394 case VAR_SPECIAL:
6395 switch (tv->vval.v_number)
6396 {
6397 case VVAL_FALSE: return AlwaysFalse(NULL);
6398 case VVAL_TRUE: return AlwaysTrue(NULL);
6399 case VVAL_NONE:
6400 case VVAL_NULL: return AlwaysNone(NULL);
6401 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006402 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006403 return NULL;
6404 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006405 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006406}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006407
6408typedef struct
6409{
6410 PyObject_HEAD
6411} CurrentObject;
6412static PyTypeObject CurrentType;
6413
6414 static void
6415init_structs(void)
6416{
6417 vim_memset(&OutputType, 0, sizeof(OutputType));
6418 OutputType.tp_name = "vim.message";
6419 OutputType.tp_basicsize = sizeof(OutputObject);
6420 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6421 OutputType.tp_doc = "vim message object";
6422 OutputType.tp_methods = OutputMethods;
6423#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006424 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6425 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006426 OutputType.tp_alloc = call_PyType_GenericAlloc;
6427 OutputType.tp_new = call_PyType_GenericNew;
6428 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006429 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006430#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006431 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6432 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006433 // Disabled, because this causes a crash in test86
6434 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006435#endif
6436
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006437 vim_memset(&IterType, 0, sizeof(IterType));
6438 IterType.tp_name = "vim.iter";
6439 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006440 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006441 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006442 IterType.tp_iter = (getiterfunc)IterIter;
6443 IterType.tp_iternext = (iternextfunc)IterNext;
6444 IterType.tp_dealloc = (destructor)IterDestructor;
6445 IterType.tp_traverse = (traverseproc)IterTraverse;
6446 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006447
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448 vim_memset(&BufferType, 0, sizeof(BufferType));
6449 BufferType.tp_name = "vim.buffer";
6450 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006451 BufferType.tp_dealloc = (destructor)BufferDestructor;
6452 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006453 BufferType.tp_as_sequence = &BufferAsSeq;
6454 BufferType.tp_as_mapping = &BufferAsMapping;
6455 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6456 BufferType.tp_doc = "vim buffer object";
6457 BufferType.tp_methods = BufferMethods;
6458#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006459 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006460 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006461 BufferType.tp_alloc = call_PyType_GenericAlloc;
6462 BufferType.tp_new = call_PyType_GenericNew;
6463 BufferType.tp_free = call_PyObject_Free;
6464#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006465 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006466 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467#endif
6468
6469 vim_memset(&WindowType, 0, sizeof(WindowType));
6470 WindowType.tp_name = "vim.window";
6471 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006472 WindowType.tp_dealloc = (destructor)WindowDestructor;
6473 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006474 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006475 WindowType.tp_doc = "vim Window object";
6476 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006477 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6478 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006479#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006480 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6481 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006482 WindowType.tp_alloc = call_PyType_GenericAlloc;
6483 WindowType.tp_new = call_PyType_GenericNew;
6484 WindowType.tp_free = call_PyObject_Free;
6485#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6487 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488#endif
6489
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006490 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6491 TabPageType.tp_name = "vim.tabpage";
6492 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006493 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6494 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006495 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6496 TabPageType.tp_doc = "vim tab page object";
6497 TabPageType.tp_methods = TabPageMethods;
6498#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006500 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6501 TabPageType.tp_new = call_PyType_GenericNew;
6502 TabPageType.tp_free = call_PyObject_Free;
6503#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006504 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006505#endif
6506
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006507 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6508 BufMapType.tp_name = "vim.bufferlist";
6509 BufMapType.tp_basicsize = sizeof(BufMapObject);
6510 BufMapType.tp_as_mapping = &BufMapAsMapping;
6511 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006512 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006513 BufferType.tp_doc = "vim buffer list";
6514
6515 vim_memset(&WinListType, 0, sizeof(WinListType));
6516 WinListType.tp_name = "vim.windowlist";
6517 WinListType.tp_basicsize = sizeof(WinListType);
6518 WinListType.tp_as_sequence = &WinListAsSeq;
6519 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6520 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006522
6523 vim_memset(&TabListType, 0, sizeof(TabListType));
6524 TabListType.tp_name = "vim.tabpagelist";
6525 TabListType.tp_basicsize = sizeof(TabListType);
6526 TabListType.tp_as_sequence = &TabListAsSeq;
6527 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6528 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006529
6530 vim_memset(&RangeType, 0, sizeof(RangeType));
6531 RangeType.tp_name = "vim.range";
6532 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006533 RangeType.tp_dealloc = (destructor)RangeDestructor;
6534 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006535 RangeType.tp_as_sequence = &RangeAsSeq;
6536 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006537 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006538 RangeType.tp_doc = "vim Range object";
6539 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006540 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6541 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006542#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006543 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006544 RangeType.tp_alloc = call_PyType_GenericAlloc;
6545 RangeType.tp_new = call_PyType_GenericNew;
6546 RangeType.tp_free = call_PyObject_Free;
6547#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006548 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006549#endif
6550
6551 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6552 CurrentType.tp_name = "vim.currentdata";
6553 CurrentType.tp_basicsize = sizeof(CurrentObject);
6554 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6555 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006556 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006557#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006558 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6559 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006561 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6562 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006563#endif
6564
6565 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6566 DictionaryType.tp_name = "vim.dictionary";
6567 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006568 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006569 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006570 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006571 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6573 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006574 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6575 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6576 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006577#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006578 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6579 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006580#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006581 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6582 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006583#endif
6584
6585 vim_memset(&ListType, 0, sizeof(ListType));
6586 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006587 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588 ListType.tp_basicsize = sizeof(ListObject);
6589 ListType.tp_as_sequence = &ListAsSeq;
6590 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006591 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592 ListType.tp_doc = "list pushing modifications to vim structure";
6593 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006594 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006595 ListType.tp_new = (newfunc)ListConstructor;
6596 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006597#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006598 ListType.tp_getattro = (getattrofunc)ListGetattro;
6599 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006601 ListType.tp_getattr = (getattrfunc)ListGetattr;
6602 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006603#endif
6604
6605 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006606 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006607 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006608 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6609 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006610 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006611 FunctionType.tp_doc = "object that calls vim function";
6612 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006613 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006614 FunctionType.tp_new = (newfunc)FunctionConstructor;
6615 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006618#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006619 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006620#endif
6621
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006622 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6623 OptionsType.tp_name = "vim.options";
6624 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006625 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006626 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006627 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006628 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006629 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006630 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6631 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6632 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006633
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006634#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006635 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6636 LoaderType.tp_name = "vim.Loader";
6637 LoaderType.tp_basicsize = sizeof(LoaderObject);
6638 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6639 LoaderType.tp_doc = "vim message object";
6640 LoaderType.tp_methods = LoaderMethods;
6641 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006642#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006643
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006644#if PY_MAJOR_VERSION >= 3
6645 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6646 vimmodule.m_name = "vim";
6647 vimmodule.m_doc = "Vim Python interface\n";
6648 vimmodule.m_size = -1;
6649 vimmodule.m_methods = VimMethods;
6650#endif
6651}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006652
6653#define PYTYPE_READY(type) \
6654 if (PyType_Ready(&type)) \
6655 return -1;
6656
6657 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006658init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006659{
6660 PYTYPE_READY(IterType);
6661 PYTYPE_READY(BufferType);
6662 PYTYPE_READY(RangeType);
6663 PYTYPE_READY(WindowType);
6664 PYTYPE_READY(TabPageType);
6665 PYTYPE_READY(BufMapType);
6666 PYTYPE_READY(WinListType);
6667 PYTYPE_READY(TabListType);
6668 PYTYPE_READY(CurrentType);
6669 PYTYPE_READY(DictionaryType);
6670 PYTYPE_READY(ListType);
6671 PYTYPE_READY(FunctionType);
6672 PYTYPE_READY(OptionsType);
6673 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006674#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006675 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006676#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006677 return 0;
6678}
6679
6680 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006681init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006682{
6683 PyObject *path;
6684 PyObject *path_hook;
6685 PyObject *path_hooks;
6686
6687 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6688 return -1;
6689
6690 if (!(path_hooks = PySys_GetObject("path_hooks")))
6691 {
6692 PyErr_Clear();
6693 path_hooks = PyList_New(1);
6694 PyList_SET_ITEM(path_hooks, 0, path_hook);
6695 if (PySys_SetObject("path_hooks", path_hooks))
6696 {
6697 Py_DECREF(path_hooks);
6698 return -1;
6699 }
6700 Py_DECREF(path_hooks);
6701 }
6702 else if (PyList_Check(path_hooks))
6703 {
6704 if (PyList_Append(path_hooks, path_hook))
6705 {
6706 Py_DECREF(path_hook);
6707 return -1;
6708 }
6709 Py_DECREF(path_hook);
6710 }
6711 else
6712 {
6713 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006714 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006715 "You should now do the following:\n"
6716 "- append vim.path_hook to sys.path_hooks\n"
6717 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6718 VimTryEnd(); /* Discard the error */
6719 Py_DECREF(path_hook);
6720 return 0;
6721 }
6722
6723 if (!(path = PySys_GetObject("path")))
6724 {
6725 PyErr_Clear();
6726 path = PyList_New(1);
6727 Py_INCREF(vim_special_path_object);
6728 PyList_SET_ITEM(path, 0, vim_special_path_object);
6729 if (PySys_SetObject("path", path))
6730 {
6731 Py_DECREF(path);
6732 return -1;
6733 }
6734 Py_DECREF(path);
6735 }
6736 else if (PyList_Check(path))
6737 {
6738 if (PyList_Append(path, vim_special_path_object))
6739 return -1;
6740 }
6741 else
6742 {
6743 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006744 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006745 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6746 VimTryEnd(); /* Discard the error */
6747 }
6748
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006749 return 0;
6750}
6751
6752static BufMapObject TheBufferMap =
6753{
6754 PyObject_HEAD_INIT(&BufMapType)
6755};
6756
6757static WinListObject TheWindowList =
6758{
6759 PyObject_HEAD_INIT(&WinListType)
6760 NULL
6761};
6762
6763static CurrentObject TheCurrent =
6764{
6765 PyObject_HEAD_INIT(&CurrentType)
6766};
6767
6768static TabListObject TheTabPageList =
6769{
6770 PyObject_HEAD_INIT(&TabListType)
6771};
6772
6773static struct numeric_constant {
6774 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006775 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006776} numeric_constants[] = {
6777 {"VAR_LOCKED", VAR_LOCKED},
6778 {"VAR_FIXED", VAR_FIXED},
6779 {"VAR_SCOPE", VAR_SCOPE},
6780 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6781};
6782
6783static struct object_constant {
6784 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006785 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006786} object_constants[] = {
6787 {"buffers", (PyObject *)(void *)&TheBufferMap},
6788 {"windows", (PyObject *)(void *)&TheWindowList},
6789 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6790 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006791
6792 {"Buffer", (PyObject *)&BufferType},
6793 {"Range", (PyObject *)&RangeType},
6794 {"Window", (PyObject *)&WindowType},
6795 {"TabPage", (PyObject *)&TabPageType},
6796 {"Dictionary", (PyObject *)&DictionaryType},
6797 {"List", (PyObject *)&ListType},
6798 {"Function", (PyObject *)&FunctionType},
6799 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006800#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006801 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006802#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006803};
6804
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006805#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006806 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006807 return -1;
6808
6809#define ADD_CHECKED_OBJECT(m, name, obj) \
6810 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006811 PyObject *valObject = obj; \
6812 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006813 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006814 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006815 }
6816
6817 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006818populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006819{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006820 int i;
6821 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006822 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006823 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006824#if PY_VERSION_HEX >= 0x030700f0
6825 PyObject *dict;
6826 PyObject *cls;
6827#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006828
6829 for (i = 0; i < (int)(sizeof(numeric_constants)
6830 / sizeof(struct numeric_constant));
6831 ++i)
6832 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006833 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006834
6835 for (i = 0; i < (int)(sizeof(object_constants)
6836 / sizeof(struct object_constant));
6837 ++i)
6838 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006839 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006841 valObject = object_constants[i].valObject;
6842 Py_INCREF(valObject);
6843 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006844 }
6845
6846 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6847 return -1;
6848 ADD_OBJECT(m, "error", VimError);
6849
Bram Moolenaara9922d62013-05-30 13:01:18 +02006850 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6851 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006852 ADD_CHECKED_OBJECT(m, "options",
6853 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006854
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006855 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006856 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006857 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006858
Bram Moolenaar22081f42016-06-01 20:38:34 +02006859#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006860 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006861 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006862#else
6863 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6864 return -1;
6865#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006866 ADD_OBJECT(m, "_getcwd", py_getcwd)
6867
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006868 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006869 return -1;
6870 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006871 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006872 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006873 if (PyObject_SetAttrString(other_module, "chdir", attr))
6874 {
6875 Py_DECREF(attr);
6876 return -1;
6877 }
6878 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006879
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006880 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006881 {
6882 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006883 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006884 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006885 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6886 {
6887 Py_DECREF(attr);
6888 return -1;
6889 }
6890 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006892 else
6893 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006895 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6896 return -1;
6897
6898 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6899
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006900#if PY_VERSION_HEX >= 0x030700f0
6901 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6902 return -1;
6903
6904 dict = PyModule_GetDict(imp);
6905
6906 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6907 {
6908 Py_DECREF(imp);
6909 return -1;
6910 }
6911
6912 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6913 {
6914 Py_DECREF(imp);
6915 return -1;
6916 }
6917
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006918 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6919 {
6920 // find_module() is deprecated, this may stop working in some later
6921 // version.
6922 ADD_OBJECT(m, "_find_module", py_find_module);
6923 }
6924
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006925 Py_DECREF(imp);
6926
6927 ADD_OBJECT(m, "_find_spec", py_find_spec);
6928#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006929 if (!(imp = PyImport_ImportModule("imp")))
6930 return -1;
6931
6932 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6933 {
6934 Py_DECREF(imp);
6935 return -1;
6936 }
6937
6938 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6939 {
6940 Py_DECREF(py_find_module);
6941 Py_DECREF(imp);
6942 return -1;
6943 }
6944
6945 Py_DECREF(imp);
6946
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006947 ADD_OBJECT(m, "_find_module", py_find_module);
6948 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006949#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006950
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006951 return 0;
6952}