blob: bc33a80a2027558c858f064da4631f613b08d860 [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 {
3141 pt->pt_argv = (typval_T *)alloc_clear(
Bram Moolenaar51e14382019-05-25 20:21:28 +02003142 sizeof(typval_T) * self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003143 for (i = 0; i < pt->pt_argc; ++i)
3144 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3145 }
3146 else
3147 pt->pt_argv = self->argv;
3148 }
3149 else
3150 {
3151 pt->pt_argc = 0;
3152 pt->pt_argv = NULL;
3153 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003154 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003155 pt->pt_dict = self->self;
3156 if (exported && self->self)
3157 ++pt->pt_dict->dv_refcount;
3158 if (exported)
3159 pt->pt_name = vim_strsave(pt->pt_name);
3160 pt->pt_refcount = 1;
3161}
3162
3163 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003164FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003165{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003166 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003167 typval_T args;
3168 typval_T selfdicttv;
3169 typval_T rettv;
3170 dict_T *selfdict = NULL;
3171 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003172 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003173 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003174 partial_T pt;
3175 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003176
Bram Moolenaar8110a092016-04-14 15:56:09 +02003177 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003178 return NULL;
3179
3180 if (kwargs != NULL)
3181 {
3182 selfdictObject = PyDict_GetItemString(kwargs, "self");
3183 if (selfdictObject != NULL)
3184 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003185 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003186 {
3187 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003188 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003189 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003190 selfdict = selfdicttv.vval.v_dict;
3191 }
3192 }
3193
Bram Moolenaar8110a092016-04-14 15:56:09 +02003194 if (self->argv || self->self)
3195 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003196 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003197 set_partial(self, &pt, FALSE);
3198 pt_ptr = &pt;
3199 }
3200
Bram Moolenaar71700b82013-05-15 17:49:05 +02003201 Py_BEGIN_ALLOW_THREADS
3202 Python_Lock_Vim();
3203
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003204 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003205 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003206
3207 Python_Release_Vim();
3208 Py_END_ALLOW_THREADS
3209
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003210 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003211 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003212 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003213 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003214 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003215 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003216 }
3217 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003218 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003219
Bram Moolenaardb913952012-06-29 12:54:53 +02003220 clear_tv(&args);
3221 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003222 if (selfdict != NULL)
3223 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003224
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003225 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003226}
3227
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003228 static PyObject *
3229FunctionRepr(FunctionObject *self)
3230{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003231 PyObject *ret;
3232 garray_T repr_ga;
3233 int i;
3234 char_u *tofree = NULL;
3235 typval_T tv;
3236 char_u numbuf[NUMBUFLEN];
3237
3238 ga_init2(&repr_ga, (int)sizeof(char), 70);
3239 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3240 if (self->name)
3241 ga_concat(&repr_ga, self->name);
3242 else
3243 ga_concat(&repr_ga, (char_u *)"<NULL>");
3244 ga_append(&repr_ga, '\'');
3245 if (self->argv)
3246 {
3247 ga_concat(&repr_ga, (char_u *)", args=[");
3248 ++emsg_silent;
3249 for (i = 0; i < self->argc; i++)
3250 {
3251 if (i != 0)
3252 ga_concat(&repr_ga, (char_u *)", ");
3253 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3254 get_copyID()));
3255 vim_free(tofree);
3256 }
3257 --emsg_silent;
3258 ga_append(&repr_ga, ']');
3259 }
3260 if (self->self)
3261 {
3262 ga_concat(&repr_ga, (char_u *)", self=");
3263 tv.v_type = VAR_DICT;
3264 tv.vval.v_dict = self->self;
3265 ++emsg_silent;
3266 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3267 --emsg_silent;
3268 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003269 if (self->auto_rebind)
3270 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003271 }
3272 ga_append(&repr_ga, '>');
3273 ret = PyString_FromString((char *)repr_ga.ga_data);
3274 ga_clear(&repr_ga);
3275 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003276}
3277
Bram Moolenaardb913952012-06-29 12:54:53 +02003278static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003279 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3280 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003281};
3282
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003283/*
3284 * Options object
3285 */
3286
3287static PyTypeObject OptionsType;
3288
3289typedef int (*checkfun)(void *);
3290
3291typedef struct
3292{
3293 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003294 int opt_type;
3295 void *from;
3296 checkfun Check;
3297 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003298} OptionsObject;
3299
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003300 static int
3301dummy_check(void *arg UNUSED)
3302{
3303 return 0;
3304}
3305
3306 static PyObject *
3307OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3308{
3309 OptionsObject *self;
3310
Bram Moolenaar774267b2013-05-21 20:51:59 +02003311 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003312 if (self == NULL)
3313 return NULL;
3314
3315 self->opt_type = opt_type;
3316 self->from = from;
3317 self->Check = Check;
3318 self->fromObj = fromObj;
3319 if (fromObj)
3320 Py_INCREF(fromObj);
3321
3322 return (PyObject *)(self);
3323}
3324
3325 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003326OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003327{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003328 PyObject_GC_UnTrack((void *)(self));
3329 Py_XDECREF(self->fromObj);
3330 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003331}
3332
3333 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003335{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003337 return 0;
3338}
3339
3340 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003341OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003342{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003343 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003344 return 0;
3345}
3346
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003347 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003348OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003349{
3350 char_u *key;
3351 int flags;
3352 long numval;
3353 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003354 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003355
Bram Moolenaard6e39182013-05-21 18:30:34 +02003356 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003357 return NULL;
3358
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003359 if (!(key = StringToChars(keyObject, &todecref)))
3360 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003361
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003362 if (*key == NUL)
3363 {
3364 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003365 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003366 return NULL;
3367 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003368
3369 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003371
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003372 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003373
3374 if (flags == 0)
3375 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003376 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003377 return NULL;
3378 }
3379
3380 if (flags & SOPT_UNSET)
3381 {
3382 Py_INCREF(Py_None);
3383 return Py_None;
3384 }
3385 else if (flags & SOPT_BOOL)
3386 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003387 PyObject *ret;
3388 ret = numval ? Py_True : Py_False;
3389 Py_INCREF(ret);
3390 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003391 }
3392 else if (flags & SOPT_NUM)
3393 return PyInt_FromLong(numval);
3394 else if (flags & SOPT_STRING)
3395 {
3396 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003397 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003398 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003399 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003400 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003401 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402 else
3403 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003404 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003405 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003406 return NULL;
3407 }
3408 }
3409 else
3410 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003411 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003412 return NULL;
3413 }
3414}
3415
3416 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003417OptionsContains(OptionsObject *self, PyObject *keyObject)
3418{
3419 char_u *key;
3420 PyObject *todecref;
3421
3422 if (!(key = StringToChars(keyObject, &todecref)))
3423 return -1;
3424
3425 if (*key == NUL)
3426 {
3427 Py_XDECREF(todecref);
3428 return 0;
3429 }
3430
3431 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3432 {
3433 Py_XDECREF(todecref);
3434 return 1;
3435 }
3436 else
3437 {
3438 Py_XDECREF(todecref);
3439 return 0;
3440 }
3441}
3442
3443typedef struct
3444{
3445 void *lastoption;
3446 int opt_type;
3447} optiterinfo_T;
3448
3449 static PyObject *
3450OptionsIterNext(optiterinfo_T **oii)
3451{
3452 char_u *name;
3453
3454 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3455 return PyString_FromString((char *)name);
3456
3457 return NULL;
3458}
3459
3460 static PyObject *
3461OptionsIter(OptionsObject *self)
3462{
3463 optiterinfo_T *oii;
3464
3465 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3466 {
3467 PyErr_NoMemory();
3468 return NULL;
3469 }
3470
3471 oii->opt_type = self->opt_type;
3472 oii->lastoption = NULL;
3473
3474 return IterNew(oii,
3475 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3476 NULL, NULL);
3477}
3478
3479 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003480set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003481{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003482 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003483
3484 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3485 {
3486 if (VimTryEnd())
3487 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003488 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003489 return FAIL;
3490 }
3491 return OK;
3492}
3493
3494 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003495set_option_value_for(
3496 char_u *key,
3497 int numval,
3498 char_u *stringval,
3499 int opt_flags,
3500 int opt_type,
3501 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003502{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003503 win_T *save_curwin = NULL;
3504 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003505 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003506 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003507
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003508 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003509 switch (opt_type)
3510 {
3511 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003512 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003513 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003514 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003515 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003516 if (VimTryEnd())
3517 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003518 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003519 return -1;
3520 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003521 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3522 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003523 break;
3524 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003525 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003526 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003527 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003528 break;
3529 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003530 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003531 break;
3532 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003533 if (set_ret == FAIL)
3534 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003535 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003536}
3537
3538 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003539OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540{
3541 char_u *key;
3542 int flags;
3543 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003544 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003545 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003546
Bram Moolenaard6e39182013-05-21 18:30:34 +02003547 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003548 return -1;
3549
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003550 if (!(key = StringToChars(keyObject, &todecref)))
3551 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003552
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003553 if (*key == NUL)
3554 {
3555 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003556 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003557 return -1;
3558 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003559
3560 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003561 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003562
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003563 if (flags == 0)
3564 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003565 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003566 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003567 return -1;
3568 }
3569
3570 if (valObject == NULL)
3571 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003572 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003573 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003574 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003575 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003576 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577 return -1;
3578 }
3579 else if (!(flags & SOPT_GLOBAL))
3580 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003581 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003582 N_("unable to unset option %s "
3583 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003584 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003585 return -1;
3586 }
3587 else
3588 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003589 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003590 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003591 return 0;
3592 }
3593 }
3594
Bram Moolenaard6e39182013-05-21 18:30:34 +02003595 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003596
3597 if (flags & SOPT_BOOL)
3598 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003599 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003600
Bram Moolenaarb983f752013-05-15 16:11:50 +02003601 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003602 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003603 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003604 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003605 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003606 }
3607 else if (flags & SOPT_NUM)
3608 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003609 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003610
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003611 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003612 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003613 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614 return -1;
3615 }
3616
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003617 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003619 }
3620 else
3621 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003622 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003623 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003624
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003625 if ((val = StringToChars(valObject, &todecref2)))
3626 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003627 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003628 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003629 Py_XDECREF(todecref2);
3630 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003631 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003632 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003633 }
3634
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003635 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003636
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003637 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003638}
3639
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003640static PySequenceMethods OptionsAsSeq = {
3641 0, /* sq_length */
3642 0, /* sq_concat */
3643 0, /* sq_repeat */
3644 0, /* sq_item */
3645 0, /* sq_slice */
3646 0, /* sq_ass_item */
3647 0, /* sq_ass_slice */
3648 (objobjproc) OptionsContains, /* sq_contains */
3649 0, /* sq_inplace_concat */
3650 0, /* sq_inplace_repeat */
3651};
3652
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003653static PyMappingMethods OptionsAsMapping = {
3654 (lenfunc) NULL,
3655 (binaryfunc) OptionsItem,
3656 (objobjargproc) OptionsAssItem,
3657};
3658
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003659/* Tabpage object
3660 */
3661
3662typedef struct
3663{
3664 PyObject_HEAD
3665 tabpage_T *tab;
3666} TabPageObject;
3667
3668static PyObject *WinListNew(TabPageObject *tabObject);
3669
3670static PyTypeObject TabPageType;
3671
3672 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003673CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003674{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003675 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003676 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003677 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003678 return -1;
3679 }
3680
3681 return 0;
3682}
3683
3684 static PyObject *
3685TabPageNew(tabpage_T *tab)
3686{
3687 TabPageObject *self;
3688
3689 if (TAB_PYTHON_REF(tab))
3690 {
3691 self = TAB_PYTHON_REF(tab);
3692 Py_INCREF(self);
3693 }
3694 else
3695 {
3696 self = PyObject_NEW(TabPageObject, &TabPageType);
3697 if (self == NULL)
3698 return NULL;
3699 self->tab = tab;
3700 TAB_PYTHON_REF(tab) = self;
3701 }
3702
3703 return (PyObject *)(self);
3704}
3705
3706 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003707TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003708{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003709 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3710 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003711
3712 DESTRUCTOR_FINISH(self);
3713}
3714
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003715static char *TabPageAttrs[] = {
3716 "windows", "number", "vars", "window", "valid",
3717 NULL
3718};
3719
3720 static PyObject *
3721TabPageDir(PyObject *self)
3722{
3723 return ObjectDir(self, TabPageAttrs);
3724}
3725
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003726 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003727TabPageAttrValid(TabPageObject *self, char *name)
3728{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003729 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003730
3731 if (strcmp(name, "valid") != 0)
3732 return NULL;
3733
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003734 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3735 Py_INCREF(ret);
3736 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003737}
3738
3739 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003740TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003741{
3742 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003743 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003744 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003745 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003746 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003747 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003748 else if (strcmp(name, "window") == 0)
3749 {
3750 /* For current tab window.c does not bother to set or update tp_curwin
3751 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003752 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003753 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003754 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003755 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003756 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003757 else if (strcmp(name, "__members__") == 0)
3758 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003759 return NULL;
3760}
3761
3762 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003763TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003764{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003766 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003767 else
3768 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003769 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003770
3771 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003772 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3773 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003775 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003776 }
3777}
3778
3779static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003780 /* name, function, calling, documentation */
3781 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3782 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783};
3784
3785/*
3786 * Window list object
3787 */
3788
3789static PyTypeObject TabListType;
3790static PySequenceMethods TabListAsSeq;
3791
3792typedef struct
3793{
3794 PyObject_HEAD
3795} TabListObject;
3796
3797 static PyInt
3798TabListLength(PyObject *self UNUSED)
3799{
3800 tabpage_T *tp = first_tabpage;
3801 PyInt n = 0;
3802
3803 while (tp != NULL)
3804 {
3805 ++n;
3806 tp = tp->tp_next;
3807 }
3808
3809 return n;
3810}
3811
3812 static PyObject *
3813TabListItem(PyObject *self UNUSED, PyInt n)
3814{
3815 tabpage_T *tp;
3816
3817 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3818 if (n == 0)
3819 return TabPageNew(tp);
3820
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003821 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003822 return NULL;
3823}
3824
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003825/*
3826 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003827 */
3828
3829typedef struct
3830{
3831 PyObject_HEAD
3832 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003833 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003834} WindowObject;
3835
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003836static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003837
3838 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003839CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003840{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003841 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003842 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003843 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844 return -1;
3845 }
3846
3847 return 0;
3848}
3849
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003850 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003851WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003852{
3853 /* We need to handle deletion of windows underneath us.
3854 * If we add a "w_python*_ref" field to the win_T structure,
3855 * then we can get at it in win_free() in vim. We then
3856 * need to create only ONE Python object per window - if
3857 * we try to create a second, just INCREF the existing one
3858 * and return it. The (single) Python object referring to
3859 * the window is stored in "w_python*_ref".
3860 * On a win_free() we set the Python object's win_T* field
3861 * to an invalid value. We trap all uses of a window
3862 * object, and reject them if the win_T* field is invalid.
3863 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003864 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003865 * w_python_ref and w_python3_ref fields respectively.
3866 */
3867
3868 WindowObject *self;
3869
3870 if (WIN_PYTHON_REF(win))
3871 {
3872 self = WIN_PYTHON_REF(win);
3873 Py_INCREF(self);
3874 }
3875 else
3876 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003877 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003878 if (self == NULL)
3879 return NULL;
3880 self->win = win;
3881 WIN_PYTHON_REF(win) = self;
3882 }
3883
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003884 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3885
Bram Moolenaar971db462013-05-12 18:44:48 +02003886 return (PyObject *)(self);
3887}
3888
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003889 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003890WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003891{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003892 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003893 if (self->win && self->win != INVALID_WINDOW_VALUE)
3894 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003895 Py_XDECREF(((PyObject *)(self->tabObject)));
3896 PyObject_GC_Del((void *)(self));
3897}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003898
Bram Moolenaar774267b2013-05-21 20:51:59 +02003899 static int
3900WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3901{
3902 Py_VISIT(((PyObject *)(self->tabObject)));
3903 return 0;
3904}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003905
Bram Moolenaar774267b2013-05-21 20:51:59 +02003906 static int
3907WindowClear(WindowObject *self)
3908{
3909 Py_CLEAR(self->tabObject);
3910 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003911}
3912
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003913 static win_T *
3914get_firstwin(TabPageObject *tabObject)
3915{
3916 if (tabObject)
3917 {
3918 if (CheckTabPage(tabObject))
3919 return NULL;
3920 /* For current tab window.c does not bother to set or update tp_firstwin
3921 */
3922 else if (tabObject->tab == curtab)
3923 return firstwin;
3924 else
3925 return tabObject->tab->tp_firstwin;
3926 }
3927 else
3928 return firstwin;
3929}
Bram Moolenaare950f992018-06-10 13:55:55 +02003930
3931// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003932static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003933 "buffer",
3934 "cursor",
3935 "height",
3936 "row",
3937 "width",
3938 "col",
3939 "vars",
3940 "options",
3941 "number",
3942 "tabpage",
3943 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003944 NULL
3945};
3946
3947 static PyObject *
3948WindowDir(PyObject *self)
3949{
3950 return ObjectDir(self, WindowAttrs);
3951}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003952
Bram Moolenaar971db462013-05-12 18:44:48 +02003953 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003954WindowAttrValid(WindowObject *self, char *name)
3955{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003956 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003957
3958 if (strcmp(name, "valid") != 0)
3959 return NULL;
3960
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003961 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3962 Py_INCREF(ret);
3963 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003964}
3965
3966 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003967WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003968{
3969 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003970 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003971 else if (strcmp(name, "cursor") == 0)
3972 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003973 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003974
3975 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3976 }
3977 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003978 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003979 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003981 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003982 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003983 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003984 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003985 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003986 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003987 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3989 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003990 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003991 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003993 return NULL;
3994 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003995 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003996 }
3997 else if (strcmp(name, "tabpage") == 0)
3998 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 Py_INCREF(self->tabObject);
4000 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004001 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004002 else if (strcmp(name, "__members__") == 0)
4003 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004004 else
4005 return NULL;
4006}
4007
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004009WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004011 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004012 return -1;
4013
4014 if (strcmp(name, "buffer") == 0)
4015 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004016 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004017 return -1;
4018 }
4019 else if (strcmp(name, "cursor") == 0)
4020 {
4021 long lnum;
4022 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004023
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004024 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004025 return -1;
4026
Bram Moolenaard6e39182013-05-21 18:30:34 +02004027 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004028 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004029 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030 return -1;
4031 }
4032
4033 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004034 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 return -1;
4036
Bram Moolenaard6e39182013-05-21 18:30:34 +02004037 self->win->w_cursor.lnum = lnum;
4038 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004039 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004040 self->win->w_cursor.coladd = 0;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004041 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004042 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004044 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004045 return 0;
4046 }
4047 else if (strcmp(name, "height") == 0)
4048 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004049 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004050 win_T *savewin;
4051
Bram Moolenaardee2e312013-06-23 16:35:47 +02004052 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004053 return -1;
4054
4055#ifdef FEAT_GUI
4056 need_mouse_correct = TRUE;
4057#endif
4058 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004059 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004060
4061 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004062 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004063 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004064 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 return -1;
4066
4067 return 0;
4068 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 else if (strcmp(name, "width") == 0)
4070 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004071 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072 win_T *savewin;
4073
Bram Moolenaardee2e312013-06-23 16:35:47 +02004074 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004075 return -1;
4076
4077#ifdef FEAT_GUI
4078 need_mouse_correct = TRUE;
4079#endif
4080 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004081 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004082
4083 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004084 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004086 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 return -1;
4088
4089 return 0;
4090 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 else
4092 {
4093 PyErr_SetString(PyExc_AttributeError, name);
4094 return -1;
4095 }
4096}
4097
4098 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004099WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004100{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004101 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004102 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103 else
4104 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004105 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106
Bram Moolenaar6d216452013-05-12 19:00:41 +02004107 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004108 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 (self));
4110 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004111 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 }
4113}
4114
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004115static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004116 /* name, function, calling, documentation */
4117 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4118 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004119};
4120
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004121/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004122 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004123 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004124
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004125static PyTypeObject WinListType;
4126static PySequenceMethods WinListAsSeq;
4127
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004128typedef struct
4129{
4130 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004131 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004132} WinListObject;
4133
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004134 static PyObject *
4135WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004137 WinListObject *self;
4138
4139 self = PyObject_NEW(WinListObject, &WinListType);
4140 self->tabObject = tabObject;
4141 Py_INCREF(tabObject);
4142
4143 return (PyObject *)(self);
4144}
4145
4146 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004147WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004148{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004149 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004150
4151 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004152 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004153 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004154 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004155
4156 DESTRUCTOR_FINISH(self);
4157}
4158
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004159 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004160WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004161{
4162 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004163 PyInt n = 0;
4164
Bram Moolenaard6e39182013-05-21 18:30:34 +02004165 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004166 return -1;
4167
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004168 while (w != NULL)
4169 {
4170 ++n;
4171 w = W_NEXT(w);
4172 }
4173
4174 return n;
4175}
4176
4177 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004178WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004179{
4180 win_T *w;
4181
Bram Moolenaard6e39182013-05-21 18:30:34 +02004182 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004183 return NULL;
4184
4185 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004187 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004188
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004189 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190 return NULL;
4191}
4192
4193/* Convert a Python string into a Vim line.
4194 *
4195 * The result is in allocated memory. All internal nulls are replaced by
4196 * newline characters. It is an error for the string to contain newline
4197 * characters.
4198 *
4199 * On errors, the Python exception data is set, and NULL is returned.
4200 */
4201 static char *
4202StringToLine(PyObject *obj)
4203{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004204 char *str;
4205 char *save;
4206 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004207 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004208 PyInt i;
4209 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004210
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004211 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004213 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4214 || str == NULL)
4215 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004217 else if (PyUnicode_Check(obj))
4218 {
4219 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4220 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004221
Bram Moolenaardaa27022013-06-24 22:33:30 +02004222 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004223 || str == NULL)
4224 {
4225 Py_DECREF(bytes);
4226 return NULL;
4227 }
4228 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004229 else
4230 {
4231#if PY_MAJOR_VERSION < 3
4232 PyErr_FORMAT(PyExc_TypeError,
4233 N_("expected str() or unicode() instance, but got %s"),
4234 Py_TYPE_NAME(obj));
4235#else
4236 PyErr_FORMAT(PyExc_TypeError,
4237 N_("expected bytes() or str() instance, but got %s"),
4238 Py_TYPE_NAME(obj));
4239#endif
4240 return NULL;
4241 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004242
4243 /*
4244 * Error checking: String must not contain newlines, as we
4245 * are replacing a single line, and we must replace it with
4246 * a single line.
4247 * A trailing newline is removed, so that append(f.readlines()) works.
4248 */
4249 p = memchr(str, '\n', len);
4250 if (p != NULL)
4251 {
4252 if (p == str + len - 1)
4253 --len;
4254 else
4255 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004256 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004257 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004258 return NULL;
4259 }
4260 }
4261
4262 /* Create a copy of the string, with internal nulls replaced by
4263 * newline characters, as is the vim convention.
4264 */
Bram Moolenaar51e14382019-05-25 20:21:28 +02004265 save = (char *)alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004266 if (save == NULL)
4267 {
4268 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004269 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270 return NULL;
4271 }
4272
4273 for (i = 0; i < len; ++i)
4274 {
4275 if (str[i] == '\0')
4276 save[i] = '\n';
4277 else
4278 save[i] = str[i];
4279 }
4280
4281 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004282 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004283
4284 return save;
4285}
4286
4287/* Get a line from the specified buffer. The line number is
4288 * in Vim format (1-based). The line is returned as a Python
4289 * string object.
4290 */
4291 static PyObject *
4292GetBufferLine(buf_T *buf, PyInt n)
4293{
4294 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4295}
4296
4297
4298/* Get a list of lines from the specified buffer. The line numbers
4299 * are in Vim format (1-based). The range is from lo up to, but not
4300 * including, hi. The list is returned as a Python list of string objects.
4301 */
4302 static PyObject *
4303GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4304{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004305 PyInt i;
4306 PyInt n = hi - lo;
4307 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004308
4309 if (list == NULL)
4310 return NULL;
4311
4312 for (i = 0; i < n; ++i)
4313 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004314 PyObject *string = LineToString(
4315 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004316
4317 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004318 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004319 {
4320 Py_DECREF(list);
4321 return NULL;
4322 }
4323
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004324 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004325 }
4326
4327 /* The ownership of the Python list is passed to the caller (ie,
4328 * the caller should Py_DECREF() the object when it is finished
4329 * with it).
4330 */
4331
4332 return list;
4333}
4334
4335/*
4336 * Check if deleting lines made the cursor position invalid.
4337 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4338 * deleted).
4339 */
4340 static void
4341py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4342{
4343 if (curwin->w_cursor.lnum >= lo)
4344 {
4345 /* Adjust the cursor position if it's in/after the changed
4346 * lines. */
4347 if (curwin->w_cursor.lnum >= hi)
4348 {
4349 curwin->w_cursor.lnum += extra;
4350 check_cursor_col();
4351 }
4352 else if (extra < 0)
4353 {
4354 curwin->w_cursor.lnum = lo;
4355 check_cursor();
4356 }
4357 else
4358 check_cursor_col();
4359 changed_cline_bef_curs();
4360 }
4361 invalidate_botline();
4362}
4363
Bram Moolenaar19e60942011-06-19 00:27:51 +02004364/*
4365 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004366 * in Vim format (1-based). The replacement line is given as
4367 * a Python string object. The object is checked for validity
4368 * and correct format. Errors are returned as a value of FAIL.
4369 * The return value is OK on success.
4370 * If OK is returned and len_change is not NULL, *len_change
4371 * is set to the change in the buffer length.
4372 */
4373 static int
4374SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4375{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004376 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004377 win_T *save_curwin = NULL;
4378 tabpage_T *save_curtab = NULL;
4379
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004380 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004381 * There are three cases:
4382 * 1. NULL, or None - this is a deletion.
4383 * 2. A string - this is a replacement.
4384 * 3. Anything else - this is an error.
4385 */
4386 if (line == Py_None || line == NULL)
4387 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004388 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004389 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004391 VimTryStart();
4392
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004394 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004395 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004396 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 else
4398 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004399 if (buf == curbuf && (save_curwin != NULL
4400 || save_curbuf.br_buf == NULL))
4401 // Using an existing window for the buffer, adjust the cursor
4402 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004403 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004404 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004405 /* Only adjust marks if we managed to switch to a window that
4406 * holds the buffer, otherwise line numbers will be invalid. */
4407 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004408 }
4409
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004410 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004411
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004412 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004413 return FAIL;
4414
4415 if (len_change)
4416 *len_change = -1;
4417
4418 return OK;
4419 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004420 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004422 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423
4424 if (save == NULL)
4425 return FAIL;
4426
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004427 VimTryStart();
4428
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 /* We do not need to free "save" if ml_replace() consumes it. */
4430 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004431 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432
4433 if (u_savesub((linenr_T)n) == FAIL)
4434 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004435 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004436 vim_free(save);
4437 }
4438 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4439 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004440 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441 vim_free(save);
4442 }
4443 else
4444 changed_bytes((linenr_T)n, 0);
4445
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004446 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004447
4448 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004449 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004450 check_cursor_col();
4451
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004452 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453 return FAIL;
4454
4455 if (len_change)
4456 *len_change = 0;
4457
4458 return OK;
4459 }
4460 else
4461 {
4462 PyErr_BadArgument();
4463 return FAIL;
4464 }
4465}
4466
Bram Moolenaar19e60942011-06-19 00:27:51 +02004467/* Replace a range of lines in the specified buffer. The line numbers are in
4468 * Vim format (1-based). The range is from lo up to, but not including, hi.
4469 * The replacement lines are given as a Python list of string objects. The
4470 * list is checked for validity and correct format. Errors are returned as a
4471 * value of FAIL. The return value is OK on success.
4472 * If OK is returned and len_change is not NULL, *len_change
4473 * is set to the change in the buffer length.
4474 */
4475 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004476SetBufferLineList(
4477 buf_T *buf,
4478 PyInt lo,
4479 PyInt hi,
4480 PyObject *list,
4481 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004482{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004483 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004484 win_T *save_curwin = NULL;
4485 tabpage_T *save_curtab = NULL;
4486
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004487 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004488 * There are three cases:
4489 * 1. NULL, or None - this is a deletion.
4490 * 2. A list - this is a replacement.
4491 * 3. Anything else - this is an error.
4492 */
4493 if (list == Py_None || list == NULL)
4494 {
4495 PyInt i;
4496 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004497
4498 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004499 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004500 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004501
4502 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004503 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504 else
4505 {
4506 for (i = 0; i < n; ++i)
4507 {
4508 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4509 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004510 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004511 break;
4512 }
4513 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004514 if (buf == curbuf && (save_curwin != NULL
4515 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004516 /* Using an existing window for the buffer, adjust the cursor
4517 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004518 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004519 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004520 /* Only adjust marks if we managed to switch to a window that
4521 * holds the buffer, otherwise line numbers will be invalid. */
4522 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004523 }
4524
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004525 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004526
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004527 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004528 return FAIL;
4529
4530 if (len_change)
4531 *len_change = -n;
4532
4533 return OK;
4534 }
4535 else if (PyList_Check(list))
4536 {
4537 PyInt i;
4538 PyInt new_len = PyList_Size(list);
4539 PyInt old_len = hi - lo;
4540 PyInt extra = 0; /* lines added to text, can be negative */
4541 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004542
4543 if (new_len == 0) /* avoid allocating zero bytes */
4544 array = NULL;
4545 else
4546 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004547 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004548 if (array == NULL)
4549 {
4550 PyErr_NoMemory();
4551 return FAIL;
4552 }
4553 }
4554
4555 for (i = 0; i < new_len; ++i)
4556 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004557 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004559 if (!(line = PyList_GetItem(list, i)) ||
4560 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004561 {
4562 while (i)
4563 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004564 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004565 return FAIL;
4566 }
4567 }
4568
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004569 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004570 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004571
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004572 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004573 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004574
4575 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004576 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004577
4578 /* If the size of the range is reducing (ie, new_len < old_len) we
4579 * need to delete some old_len. We do this at the start, by
4580 * repeatedly deleting line "lo".
4581 */
4582 if (!PyErr_Occurred())
4583 {
4584 for (i = 0; i < old_len - new_len; ++i)
4585 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4586 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004587 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004588 break;
4589 }
4590 extra -= i;
4591 }
4592
4593 /* For as long as possible, replace the existing old_len with the
4594 * new old_len. This is a more efficient operation, as it requires
4595 * less memory allocation and freeing.
4596 */
4597 if (!PyErr_Occurred())
4598 {
4599 for (i = 0; i < old_len && i < new_len; ++i)
4600 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4601 == FAIL)
4602 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004603 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004604 break;
4605 }
4606 }
4607 else
4608 i = 0;
4609
4610 /* Now we may need to insert the remaining new old_len. If we do, we
4611 * must free the strings as we finish with them (we can't pass the
4612 * responsibility to vim in this case).
4613 */
4614 if (!PyErr_Occurred())
4615 {
4616 while (i < new_len)
4617 {
4618 if (ml_append((linenr_T)(lo + i - 1),
4619 (char_u *)array[i], 0, FALSE) == FAIL)
4620 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004621 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 break;
4623 }
4624 vim_free(array[i]);
4625 ++i;
4626 ++extra;
4627 }
4628 }
4629
4630 /* Free any left-over old_len, as a result of an error */
4631 while (i < new_len)
4632 {
4633 vim_free(array[i]);
4634 ++i;
4635 }
4636
4637 /* Free the array of old_len. All of its contents have now
4638 * been dealt with (either freed, or the responsibility passed
4639 * to vim.
4640 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004641 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004642
4643 /* Adjust marks. Invalidate any which lie in the
4644 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004645 * Only adjust marks if we managed to switch to a window that holds
4646 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004647 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004648 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004649 (long)MAXLNUM, (long)extra);
4650 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4651
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004652 if (buf == curbuf && (save_curwin != NULL
4653 || save_curbuf.br_buf == NULL))
4654 // Using an existing window for the buffer, adjust the cursor
4655 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004656 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4657
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004658 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004659 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004660
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004661 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004662 return FAIL;
4663
4664 if (len_change)
4665 *len_change = new_len - old_len;
4666
4667 return OK;
4668 }
4669 else
4670 {
4671 PyErr_BadArgument();
4672 return FAIL;
4673 }
4674}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004676/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677 * The line number is in Vim format (1-based). The lines to be inserted are
4678 * given as a Python list of string objects or as a single string. The lines
4679 * to be added are checked for validity and correct format. Errors are
4680 * returned as a value of FAIL. The return value is OK on success.
4681 * If OK is returned and len_change is not NULL, *len_change
4682 * is set to the change in the buffer length.
4683 */
4684 static int
4685InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4686{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004687 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004688 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004689 tabpage_T *save_curtab = NULL;
4690
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691 /* First of all, we check the type of the supplied Python object.
4692 * It must be a string or a list, or the call is in error.
4693 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004694 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004695 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004696 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697
4698 if (str == NULL)
4699 return FAIL;
4700
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004701 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004702 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004703 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004704
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004705 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004706 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004707 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004708 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004709 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004710 /* Only adjust marks if we managed to switch to a window that
4711 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 appended_lines_mark((linenr_T)n, 1L);
4713
4714 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004715 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004716 update_screen(VALID);
4717
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004718 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004719 return FAIL;
4720
4721 if (len_change)
4722 *len_change = 1;
4723
4724 return OK;
4725 }
4726 else if (PyList_Check(lines))
4727 {
4728 PyInt i;
4729 PyInt size = PyList_Size(lines);
4730 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004732 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733 if (array == NULL)
4734 {
4735 PyErr_NoMemory();
4736 return FAIL;
4737 }
4738
4739 for (i = 0; i < size; ++i)
4740 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004741 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004743 if (!(line = PyList_GetItem(lines, i)) ||
4744 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 {
4746 while (i)
4747 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004748 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 return FAIL;
4750 }
4751 }
4752
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004753 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004754 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004755 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004756
4757 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004758 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004759 else
4760 {
4761 for (i = 0; i < size; ++i)
4762 {
4763 if (ml_append((linenr_T)(n + i),
4764 (char_u *)array[i], 0, FALSE) == FAIL)
4765 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004766 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004767
4768 /* Free the rest of the lines */
4769 while (i < size)
4770 vim_free(array[i++]);
4771
4772 break;
4773 }
4774 vim_free(array[i]);
4775 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004776 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004777 /* Only adjust marks if we managed to switch to a window that
4778 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779 appended_lines_mark((linenr_T)n, (long)i);
4780 }
4781
4782 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004783 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004784 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004785 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004787 update_screen(VALID);
4788
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004789 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004790 return FAIL;
4791
4792 if (len_change)
4793 *len_change = size;
4794
4795 return OK;
4796 }
4797 else
4798 {
4799 PyErr_BadArgument();
4800 return FAIL;
4801 }
4802}
4803
4804/*
4805 * Common routines for buffers and line ranges
4806 * -------------------------------------------
4807 */
4808
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004809typedef struct
4810{
4811 PyObject_HEAD
4812 buf_T *buf;
4813} BufferObject;
4814
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004815 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004816CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004818 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004819 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004820 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004821 return -1;
4822 }
4823
4824 return 0;
4825}
4826
4827 static PyObject *
4828RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4829{
4830 if (CheckBuffer(self))
4831 return NULL;
4832
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004833 if (end == -1)
4834 end = self->buf->b_ml.ml_line_count;
4835
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004836 if (n < 0)
4837 n += end - start + 1;
4838
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004839 if (n < 0 || n > end - start)
4840 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004841 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004842 return NULL;
4843 }
4844
4845 return GetBufferLine(self->buf, n+start);
4846}
4847
4848 static PyObject *
4849RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4850{
4851 PyInt size;
4852
4853 if (CheckBuffer(self))
4854 return NULL;
4855
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004856 if (end == -1)
4857 end = self->buf->b_ml.ml_line_count;
4858
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004859 size = end - start + 1;
4860
4861 if (lo < 0)
4862 lo = 0;
4863 else if (lo > size)
4864 lo = size;
4865 if (hi < 0)
4866 hi = 0;
4867 if (hi < lo)
4868 hi = lo;
4869 else if (hi > size)
4870 hi = size;
4871
4872 return GetBufferLineList(self->buf, lo+start, hi+start);
4873}
4874
4875 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004876RBAsItem(
4877 BufferObject *self,
4878 PyInt n,
4879 PyObject *valObject,
4880 PyInt start,
4881 PyInt end,
4882 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004883{
4884 PyInt len_change;
4885
4886 if (CheckBuffer(self))
4887 return -1;
4888
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004889 if (end == -1)
4890 end = self->buf->b_ml.ml_line_count;
4891
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004892 if (n < 0)
4893 n += end - start + 1;
4894
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004895 if (n < 0 || n > end - start)
4896 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004897 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004898 return -1;
4899 }
4900
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004901 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004902 return -1;
4903
4904 if (new_end)
4905 *new_end = end + len_change;
4906
4907 return 0;
4908}
4909
Bram Moolenaar19e60942011-06-19 00:27:51 +02004910 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004911RBAsSlice(
4912 BufferObject *self,
4913 PyInt lo,
4914 PyInt hi,
4915 PyObject *valObject,
4916 PyInt start,
4917 PyInt end,
4918 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004919{
4920 PyInt size;
4921 PyInt len_change;
4922
4923 /* Self must be a valid buffer */
4924 if (CheckBuffer(self))
4925 return -1;
4926
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004927 if (end == -1)
4928 end = self->buf->b_ml.ml_line_count;
4929
Bram Moolenaar19e60942011-06-19 00:27:51 +02004930 /* Sort out the slice range */
4931 size = end - start + 1;
4932
4933 if (lo < 0)
4934 lo = 0;
4935 else if (lo > size)
4936 lo = size;
4937 if (hi < 0)
4938 hi = 0;
4939 if (hi < lo)
4940 hi = lo;
4941 else if (hi > size)
4942 hi = size;
4943
4944 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004945 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004946 return -1;
4947
4948 if (new_end)
4949 *new_end = end + len_change;
4950
4951 return 0;
4952}
4953
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004954
4955 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004956RBAppend(
4957 BufferObject *self,
4958 PyObject *args,
4959 PyInt start,
4960 PyInt end,
4961 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004962{
4963 PyObject *lines;
4964 PyInt len_change;
4965 PyInt max;
4966 PyInt n;
4967
4968 if (CheckBuffer(self))
4969 return NULL;
4970
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004971 if (end == -1)
4972 end = self->buf->b_ml.ml_line_count;
4973
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004974 max = n = end - start + 1;
4975
4976 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4977 return NULL;
4978
4979 if (n < 0 || n > max)
4980 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004981 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004982 return NULL;
4983 }
4984
4985 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4986 return NULL;
4987
4988 if (new_end)
4989 *new_end = end + len_change;
4990
4991 Py_INCREF(Py_None);
4992 return Py_None;
4993}
4994
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004995/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004996 */
4997
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004998static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004999static PySequenceMethods RangeAsSeq;
5000static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005001
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005002typedef struct
5003{
5004 PyObject_HEAD
5005 BufferObject *buf;
5006 PyInt start;
5007 PyInt end;
5008} RangeObject;
5009
5010 static PyObject *
5011RangeNew(buf_T *buf, PyInt start, PyInt end)
5012{
5013 BufferObject *bufr;
5014 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005015 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005016 if (self == NULL)
5017 return NULL;
5018
5019 bufr = (BufferObject *)BufferNew(buf);
5020 if (bufr == NULL)
5021 {
5022 Py_DECREF(self);
5023 return NULL;
5024 }
5025 Py_INCREF(bufr);
5026
5027 self->buf = bufr;
5028 self->start = start;
5029 self->end = end;
5030
5031 return (PyObject *)(self);
5032}
5033
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005034 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005035RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005036{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005037 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005038 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005039 PyObject_GC_Del((void *)(self));
5040}
5041
5042 static int
5043RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5044{
5045 Py_VISIT(((PyObject *)(self->buf)));
5046 return 0;
5047}
5048
5049 static int
5050RangeClear(RangeObject *self)
5051{
5052 Py_CLEAR(self->buf);
5053 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005054}
5055
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005056 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005057RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005058{
5059 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005061 return -1; /* ??? */
5062
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064}
5065
5066 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005068{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005070}
5071
5072 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005073RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005074{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005075 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005076}
5077
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005078static char *RangeAttrs[] = {
5079 "start", "end",
5080 NULL
5081};
5082
5083 static PyObject *
5084RangeDir(PyObject *self)
5085{
5086 return ObjectDir(self, RangeAttrs);
5087}
5088
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093}
5094
5095 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005098 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005099 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5100 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101 else
5102 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005103 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104
5105 if (name == NULL)
5106 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005108 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005109 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005110 }
5111}
5112
5113static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005114 /* name, function, calling, documentation */
5115 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005116 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5117 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005118};
5119
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005121static PySequenceMethods BufferAsSeq;
5122static PyMappingMethods BufferAsMapping;
5123
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005125BufferNew(buf_T *buf)
5126{
5127 /* We need to handle deletion of buffers underneath us.
5128 * If we add a "b_python*_ref" field to the buf_T structure,
5129 * then we can get at it in buf_freeall() in vim. We then
5130 * need to create only ONE Python object per buffer - if
5131 * we try to create a second, just INCREF the existing one
5132 * and return it. The (single) Python object referring to
5133 * the buffer is stored in "b_python*_ref".
5134 * Question: what to do on a buf_freeall(). We'll probably
5135 * have to either delete the Python object (DECREF it to
5136 * zero - a bad idea, as it leaves dangling refs!) or
5137 * set the buf_T * value to an invalid value (-1?), which
5138 * means we need checks in all access functions... Bah.
5139 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005140 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005141 * b_python_ref and b_python3_ref fields respectively.
5142 */
5143
5144 BufferObject *self;
5145
5146 if (BUF_PYTHON_REF(buf) != NULL)
5147 {
5148 self = BUF_PYTHON_REF(buf);
5149 Py_INCREF(self);
5150 }
5151 else
5152 {
5153 self = PyObject_NEW(BufferObject, &BufferType);
5154 if (self == NULL)
5155 return NULL;
5156 self->buf = buf;
5157 BUF_PYTHON_REF(buf) = self;
5158 }
5159
5160 return (PyObject *)(self);
5161}
5162
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005163 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005164BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005165{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005166 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5167 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005168
5169 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005170}
5171
Bram Moolenaar971db462013-05-12 18:44:48 +02005172 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005173BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005174{
5175 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005176 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005177 return -1; /* ??? */
5178
Bram Moolenaard6e39182013-05-21 18:30:34 +02005179 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005180}
5181
5182 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005183BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005184{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005185 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005186}
5187
5188 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005189BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005190{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005191 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005192}
5193
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005194static char *BufferAttrs[] = {
5195 "name", "number", "vars", "options", "valid",
5196 NULL
5197};
5198
5199 static PyObject *
5200BufferDir(PyObject *self)
5201{
5202 return ObjectDir(self, BufferAttrs);
5203}
5204
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005205 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005206BufferAttrValid(BufferObject *self, char *name)
5207{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005208 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005209
5210 if (strcmp(name, "valid") != 0)
5211 return NULL;
5212
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005213 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5214 Py_INCREF(ret);
5215 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005216}
5217
5218 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005219BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005220{
5221 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005222 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005223 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005224 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005225 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005226 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005227 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005228 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005229 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5230 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005231 else if (strcmp(name, "__members__") == 0)
5232 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005233 else
5234 return NULL;
5235}
5236
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005237 static int
5238BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5239{
5240 if (CheckBuffer(self))
5241 return -1;
5242
5243 if (strcmp(name, "name") == 0)
5244 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005245 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005246 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005247 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005248 PyObject *todecref;
5249
5250 if (!(val = StringToChars(valObject, &todecref)))
5251 return -1;
5252
5253 VimTryStart();
5254 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5255 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005256 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005257 aucmd_restbuf(&aco);
5258 Py_XDECREF(todecref);
5259 if (VimTryEnd())
5260 return -1;
5261
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005262 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005263 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005264 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005265 return -1;
5266 }
5267 return 0;
5268 }
5269 else
5270 {
5271 PyErr_SetString(PyExc_AttributeError, name);
5272 return -1;
5273 }
5274}
5275
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005276 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005277BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005278{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005279 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005280}
5281
5282 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005283BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005284{
5285 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005286 char_u *pmark;
5287 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005288 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005289 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005290
Bram Moolenaard6e39182013-05-21 18:30:34 +02005291 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005292 return NULL;
5293
Bram Moolenaar389a1792013-06-23 13:00:44 +02005294 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005295 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005296
Bram Moolenaar389a1792013-06-23 13:00:44 +02005297 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005298 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005299 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005300 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005301 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005302 return NULL;
5303 }
5304
5305 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005306
5307 Py_XDECREF(todecref);
5308
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005309 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005312 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005313 if (VimTryEnd())
5314 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315
5316 if (posp == NULL)
5317 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005318 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005319 return NULL;
5320 }
5321
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005322 if (posp->lnum <= 0)
5323 {
5324 /* Or raise an error? */
5325 Py_INCREF(Py_None);
5326 return Py_None;
5327 }
5328
5329 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5330}
5331
5332 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005333BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005334{
5335 PyInt start;
5336 PyInt end;
5337
Bram Moolenaard6e39182013-05-21 18:30:34 +02005338 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005339 return NULL;
5340
5341 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5342 return NULL;
5343
Bram Moolenaard6e39182013-05-21 18:30:34 +02005344 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005345}
5346
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005347 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005348BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005349{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005350 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005351 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005352 else
5353 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005354 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005355
5356 if (name == NULL)
5357 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005358
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005359 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005360 }
5361}
5362
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005363static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005364 /* name, function, calling, documentation */
5365 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005366 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005367 {"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 +02005368 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5369 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005370};
5371
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005372/*
5373 * Buffer list object - Implementation
5374 */
5375
5376static PyTypeObject BufMapType;
5377
5378typedef struct
5379{
5380 PyObject_HEAD
5381} BufMapObject;
5382
5383 static PyInt
5384BufMapLength(PyObject *self UNUSED)
5385{
5386 buf_T *b = firstbuf;
5387 PyInt n = 0;
5388
5389 while (b)
5390 {
5391 ++n;
5392 b = b->b_next;
5393 }
5394
5395 return n;
5396}
5397
5398 static PyObject *
5399BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5400{
5401 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005402 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005403
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005404 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005405 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005406
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005407 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005408
5409 if (b)
5410 return BufferNew(b);
5411 else
5412 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005413 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005414 return NULL;
5415 }
5416}
5417
5418 static void
5419BufMapIterDestruct(PyObject *buffer)
5420{
5421 /* Iteration was stopped before all buffers were processed */
5422 if (buffer)
5423 {
5424 Py_DECREF(buffer);
5425 }
5426}
5427
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005428 static int
5429BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5430{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005431 if (buffer)
5432 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005433 return 0;
5434}
5435
5436 static int
5437BufMapIterClear(PyObject **buffer)
5438{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005439 if (*buffer)
5440 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005441 return 0;
5442}
5443
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005444 static PyObject *
5445BufMapIterNext(PyObject **buffer)
5446{
5447 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005448 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005449
5450 if (!*buffer)
5451 return NULL;
5452
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005453 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005454
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005455 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005456 {
5457 *buffer = NULL;
5458 return NULL;
5459 }
5460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005461 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005462 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005463 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005464 return NULL;
5465 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005466 /* Do not increment reference: we no longer hold it (decref), but whoever
5467 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005468 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005469}
5470
5471 static PyObject *
5472BufMapIter(PyObject *self UNUSED)
5473{
5474 PyObject *buffer;
5475
5476 buffer = BufferNew(firstbuf);
5477 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005478 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5479 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005480}
5481
5482static PyMappingMethods BufMapAsMapping = {
5483 (lenfunc) BufMapLength,
5484 (binaryfunc) BufMapItem,
5485 (objobjargproc) 0,
5486};
5487
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005488/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005489 */
5490
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005491static char *CurrentAttrs[] = {
5492 "buffer", "window", "line", "range", "tabpage",
5493 NULL
5494};
5495
5496 static PyObject *
5497CurrentDir(PyObject *self)
5498{
5499 return ObjectDir(self, CurrentAttrs);
5500}
5501
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005502 static PyObject *
5503CurrentGetattr(PyObject *self UNUSED, char *name)
5504{
5505 if (strcmp(name, "buffer") == 0)
5506 return (PyObject *)BufferNew(curbuf);
5507 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005508 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005509 else if (strcmp(name, "tabpage") == 0)
5510 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005511 else if (strcmp(name, "line") == 0)
5512 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5513 else if (strcmp(name, "range") == 0)
5514 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005515 else if (strcmp(name, "__members__") == 0)
5516 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005517 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005518#if PY_MAJOR_VERSION < 3
5519 return Py_FindMethod(WindowMethods, self, name);
5520#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005521 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005522#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005523}
5524
5525 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005526CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005527{
5528 if (strcmp(name, "line") == 0)
5529 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005530 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5531 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005532 return -1;
5533
5534 return 0;
5535 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005536 else if (strcmp(name, "buffer") == 0)
5537 {
5538 int count;
5539
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005540 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005541 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005542 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005543 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005544 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005545 return -1;
5546 }
5547
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005548 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005549 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005550 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005551
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005552 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005553 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5554 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005555 if (VimTryEnd())
5556 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005557 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005558 return -1;
5559 }
5560
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005561 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005562 }
5563 else if (strcmp(name, "window") == 0)
5564 {
5565 int count;
5566
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005567 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005568 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005569 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005570 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005572 return -1;
5573 }
5574
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005575 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005576 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005578
5579 if (!count)
5580 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005581 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005582 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005583 return -1;
5584 }
5585
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005586 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005587 win_goto(((WindowObject *)(valObject))->win);
5588 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005589 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005590 if (VimTryEnd())
5591 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005592 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005593 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005594 return -1;
5595 }
5596
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005597 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005598 }
5599 else if (strcmp(name, "tabpage") == 0)
5600 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005601 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005602 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005603 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005604 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005606 return -1;
5607 }
5608
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005609 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005610 return -1;
5611
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005612 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005613 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5614 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005615 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005616 if (VimTryEnd())
5617 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005618 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005619 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 return -1;
5621 }
5622
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005623 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005624 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005625 else
5626 {
5627 PyErr_SetString(PyExc_AttributeError, name);
5628 return -1;
5629 }
5630}
5631
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005632static struct PyMethodDef CurrentMethods[] = {
5633 /* name, function, calling, documentation */
5634 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5635 { NULL, NULL, 0, NULL}
5636};
5637
Bram Moolenaardb913952012-06-29 12:54:53 +02005638 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005639init_range_cmd(exarg_T *eap)
5640{
5641 RangeStart = eap->line1;
5642 RangeEnd = eap->line2;
5643}
5644
5645 static void
5646init_range_eval(typval_T *rettv UNUSED)
5647{
5648 RangeStart = (PyInt) curwin->w_cursor.lnum;
5649 RangeEnd = RangeStart;
5650}
5651
5652 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005653run_cmd(const char *cmd, void *arg UNUSED
5654#ifdef PY_CAN_RECURSE
5655 , PyGILState_STATE *pygilstate UNUSED
5656#endif
5657 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005658{
Bram Moolenaar41009372013-07-01 22:03:04 +02005659 PyObject *run_ret;
5660 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5661 if (run_ret != NULL)
5662 {
5663 Py_DECREF(run_ret);
5664 }
5665 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005667 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005668 PyErr_Clear();
5669 }
5670 else
5671 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005672}
5673
5674static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5675static int code_hdr_len = 30;
5676
5677 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005678run_do(const char *cmd, void *arg UNUSED
5679#ifdef PY_CAN_RECURSE
5680 , PyGILState_STATE *pygilstate
5681#endif
5682 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005683{
5684 PyInt lnum;
5685 size_t len;
5686 char *code;
5687 int status;
5688 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005689 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005690 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005692 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005694 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005695 return;
5696 }
5697
5698 len = code_hdr_len + STRLEN(cmd);
5699 code = PyMem_New(char, len + 1);
5700 memcpy(code, code_hdr, code_hdr_len);
5701 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005702 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5703 status = -1;
5704 if (run_ret != NULL)
5705 {
5706 status = 0;
5707 Py_DECREF(run_ret);
5708 }
5709 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5710 {
5711 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005712 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005713 PyErr_Clear();
5714 return;
5715 }
5716 else
5717 PyErr_PrintEx(1);
5718
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005719 PyMem_Free(code);
5720
5721 if (status)
5722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005723 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724 return;
5725 }
5726
5727 status = 0;
5728 pymain = PyImport_AddModule("__main__");
5729 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005730#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005731 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005732#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005733
5734 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5735 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005736 PyObject *line;
5737 PyObject *linenr;
5738 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005739
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005740#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005741 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005742#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005743 /* Check the line number, the command my have deleted lines. */
5744 if (lnum > curbuf->b_ml.ml_line_count
5745 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005746 goto err;
5747 if (!(linenr = PyInt_FromLong((long) lnum)))
5748 {
5749 Py_DECREF(line);
5750 goto err;
5751 }
5752 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5753 Py_DECREF(line);
5754 Py_DECREF(linenr);
5755 if (!ret)
5756 goto err;
5757
Bram Moolenaara58883b2017-01-29 21:31:09 +01005758 /* Check that the command didn't switch to another buffer. */
5759 if (curbuf != was_curbuf)
5760 {
5761 Py_XDECREF(ret);
5762 goto err;
5763 }
5764
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005765 if (ret != Py_None)
5766 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005767 {
5768 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005769 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005770 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771
5772 Py_XDECREF(ret);
5773 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005774#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005775 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005776#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005777 }
5778 goto out;
5779err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005780#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005782#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005783 PyErr_PrintEx(0);
5784 PythonIO_Flush();
5785 status = 1;
5786out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005787#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005788 if (!status)
5789 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005790#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005791 Py_DECREF(pyfunc);
5792 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5793 if (status)
5794 return;
5795 check_cursor();
5796 update_curbuf(NOT_VALID);
5797}
5798
5799 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005800run_eval(const char *cmd, typval_T *rettv
5801#ifdef PY_CAN_RECURSE
5802 , PyGILState_STATE *pygilstate UNUSED
5803#endif
5804 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005805{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005806 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005807
Bram Moolenaar41009372013-07-01 22:03:04 +02005808 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005809 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005810 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005811 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005813 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005814 PyErr_Clear();
5815 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005816 else
5817 {
5818 if (PyErr_Occurred() && !msg_silent)
5819 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005820 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005821 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005822 }
5823 else
5824 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005825 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005826 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005827 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005828 }
5829 PyErr_Clear();
5830}
5831
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005832 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005833set_ref_in_py(const int copyID)
5834{
5835 pylinkedlist_T *cur;
5836 dict_T *dd;
5837 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005838 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005839 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005840 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005841
5842 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005843 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005844 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005845 {
5846 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5847 if (dd->dv_copyID != copyID)
5848 {
5849 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005850 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005851 }
5852 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005853 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005854
5855 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005857 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005858 {
5859 ll = ((ListObject *) (cur->pll_obj))->list;
5860 if (ll->lv_copyID != copyID)
5861 {
5862 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005864 }
5865 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005866 }
5867
Bram Moolenaar8110a092016-04-14 15:56:09 +02005868 if (lastfunc != NULL)
5869 {
5870 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5871 {
5872 func = (FunctionObject *) cur->pll_obj;
5873 if (func->self != NULL && func->self->dv_copyID != copyID)
5874 {
5875 func->self->dv_copyID = copyID;
5876 abort = abort || set_ref_in_ht(
5877 &func->self->dv_hashtab, copyID, NULL);
5878 }
5879 if (func->argc)
5880 for (i = 0; !abort && i < func->argc; ++i)
5881 abort = abort
5882 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5883 }
5884 }
5885
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005886 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005887}
5888
5889 static int
5890set_string_copy(char_u *str, typval_T *tv)
5891{
5892 tv->vval.v_string = vim_strsave(str);
5893 if (tv->vval.v_string == NULL)
5894 {
5895 PyErr_NoMemory();
5896 return -1;
5897 }
5898 return 0;
5899}
5900
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005902pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005903{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005904 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005905 char_u *key;
5906 dictitem_T *di;
5907 PyObject *keyObject;
5908 PyObject *valObject;
5909 Py_ssize_t iter = 0;
5910
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005911 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005912 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005913
5914 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005915 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916
5917 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5918 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005919 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920
Bram Moolenaara03e6312013-05-29 22:49:26 +02005921 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005922 {
5923 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005924 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005925 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005927 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005928 {
5929 dict_unref(dict);
5930 return -1;
5931 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005932
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005933 if (*key == NUL)
5934 {
5935 dict_unref(dict);
5936 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005937 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005938 return -1;
5939 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005940
5941 di = dictitem_alloc(key);
5942
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005943 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005944
5945 if (di == NULL)
5946 {
5947 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005948 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005949 return -1;
5950 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005952 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 {
5954 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005955 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956 return -1;
5957 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005958
5959 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005960 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005961 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005962 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965 return -1;
5966 }
5967 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005968
5969 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 return 0;
5971}
5972
5973 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005974pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005976 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 char_u *key;
5978 dictitem_T *di;
5979 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005980 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 PyObject *keyObject;
5982 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005984 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005986
5987 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005988 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005990 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005991 {
5992 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005993 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005994 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005995
5996 if (!(iterator = PyObject_GetIter(list)))
5997 {
5998 dict_unref(dict);
5999 Py_DECREF(list);
6000 return -1;
6001 }
6002 Py_DECREF(list);
6003
6004 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006005 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006006 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006007
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006008 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006009 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006010 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006011 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006012 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006013 return -1;
6014 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006015
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006016 if (*key == NUL)
6017 {
6018 Py_DECREF(keyObject);
6019 Py_DECREF(iterator);
6020 Py_XDECREF(todecref);
6021 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006022 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 return -1;
6024 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006025
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006026 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006027 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006028 Py_DECREF(keyObject);
6029 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006030 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006031 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006032 return -1;
6033 }
6034
6035 di = dictitem_alloc(key);
6036
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006037 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006038 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006039
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006040 if (di == NULL)
6041 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006042 Py_DECREF(iterator);
6043 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006044 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006045 PyErr_NoMemory();
6046 return -1;
6047 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006048
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006049 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006050 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006051 Py_DECREF(iterator);
6052 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006053 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006054 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006055 return -1;
6056 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006057
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006058 Py_DECREF(valObject);
6059
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006060 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006062 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006063 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006064 dictitem_free(di);
6065 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006066 return -1;
6067 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006069 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071 return 0;
6072}
6073
6074 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006075pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076{
6077 list_T *l;
6078
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006080 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006081
6082 tv->v_type = VAR_LIST;
6083 tv->vval.v_list = l;
6084
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006085 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006086 {
6087 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006088 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006089 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006090
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006091 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006092 return 0;
6093}
6094
Bram Moolenaardb913952012-06-29 12:54:53 +02006095typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6096
6097 static int
6098convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006099 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006100{
6101 PyObject *capsule;
6102 char hexBuf[sizeof(void *) * 2 + 3];
6103
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006104 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006105
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006106#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006107 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006108#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006109 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006110#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006111 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006112 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006113#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006114 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006115#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006116 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006117#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006118 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6119 {
6120 Py_DECREF(capsule);
6121 tv->v_type = VAR_UNKNOWN;
6122 return -1;
6123 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006124
6125 Py_DECREF(capsule);
6126
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006127 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006128 {
6129 tv->v_type = VAR_UNKNOWN;
6130 return -1;
6131 }
6132 /* As we are not using copy_tv which increments reference count we must
6133 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006134 if (tv->v_type == VAR_DICT)
6135 ++tv->vval.v_dict->dv_refcount;
6136 else if (tv->v_type == VAR_LIST)
6137 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006138 }
6139 else
6140 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006141 typval_T *v;
6142
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006143#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006144 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006145#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006146 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006147#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006148 copy_tv(v, tv);
6149 }
6150 return 0;
6151}
6152
6153 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006154ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6155{
6156 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006157 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006158
6159 if (!(lookup_dict = PyDict_New()))
6160 return -1;
6161
6162 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6163 {
6164 tv->v_type = VAR_DICT;
6165 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6166 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006167 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006168 }
6169 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006170 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006171 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006172 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006173 else
6174 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006175 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006176 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006177 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006178 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006179 }
6180 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006181 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006182}
6183
6184 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006185ConvertFromPySequence(PyObject *obj, typval_T *tv)
6186{
6187 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006188 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006189
6190 if (!(lookup_dict = PyDict_New()))
6191 return -1;
6192
6193 if (PyType_IsSubtype(obj->ob_type, &ListType))
6194 {
6195 tv->v_type = VAR_LIST;
6196 tv->vval.v_list = (((ListObject *)(obj))->list);
6197 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006198 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006199 }
6200 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006201 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006202 else
6203 {
6204 PyErr_FORMAT(PyExc_TypeError,
6205 N_("unable to convert %s to vim list"),
6206 Py_TYPE_NAME(obj));
6207 ret = -1;
6208 }
6209 Py_DECREF(lookup_dict);
6210 return ret;
6211}
6212
6213 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006214ConvertFromPyObject(PyObject *obj, typval_T *tv)
6215{
6216 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006217 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006218
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006219 if (!(lookup_dict = PyDict_New()))
6220 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006221 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006222 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006223 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006224}
6225
6226 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006227_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006228{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006229 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006230 {
6231 tv->v_type = VAR_DICT;
6232 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6233 ++tv->vval.v_dict->dv_refcount;
6234 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006235 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006236 {
6237 tv->v_type = VAR_LIST;
6238 tv->vval.v_list = (((ListObject *)(obj))->list);
6239 ++tv->vval.v_list->lv_refcount;
6240 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006241 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006242 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006243 FunctionObject *func = (FunctionObject *) obj;
6244 if (func->self != NULL || func->argv != NULL)
6245 {
6246 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6247 set_partial(func, pt, TRUE);
6248 tv->vval.v_partial = pt;
6249 tv->v_type = VAR_PARTIAL;
6250 }
6251 else
6252 {
6253 if (set_string_copy(func->name, tv) == -1)
6254 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006255
Bram Moolenaar8110a092016-04-14 15:56:09 +02006256 tv->v_type = VAR_FUNC;
6257 }
6258 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 else if (PyBytes_Check(obj))
6261 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006262 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006263
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006264 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006265 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006266 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006267 return -1;
6268
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006269 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006270 return -1;
6271
6272 tv->v_type = VAR_STRING;
6273 }
6274 else if (PyUnicode_Check(obj))
6275 {
6276 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006277 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006278
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006279 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006280 if (bytes == NULL)
6281 return -1;
6282
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006283 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006284 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006285 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006286 return -1;
6287
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006288 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 {
6290 Py_XDECREF(bytes);
6291 return -1;
6292 }
6293 Py_XDECREF(bytes);
6294
6295 tv->v_type = VAR_STRING;
6296 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006297#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006298 else if (PyInt_Check(obj))
6299 {
6300 tv->v_type = VAR_NUMBER;
6301 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006302 if (PyErr_Occurred())
6303 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 }
6305#endif
6306 else if (PyLong_Check(obj))
6307 {
6308 tv->v_type = VAR_NUMBER;
6309 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006310 if (PyErr_Occurred())
6311 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006312 }
6313 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006314 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006315#ifdef FEAT_FLOAT
6316 else if (PyFloat_Check(obj))
6317 {
6318 tv->v_type = VAR_FLOAT;
6319 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6320 }
6321#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006322 else if (PyObject_HasAttrString(obj, "keys"))
6323 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006324 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006325 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006326 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006327 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006328 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006329 else if (PyNumber_Check(obj))
6330 {
6331 PyObject *num;
6332
6333 if (!(num = PyNumber_Long(obj)))
6334 return -1;
6335
6336 tv->v_type = VAR_NUMBER;
6337 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6338
6339 Py_DECREF(num);
6340 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006341 else if (obj == Py_None)
6342 {
6343 tv->v_type = VAR_SPECIAL;
6344 tv->vval.v_number = VVAL_NONE;
6345 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006346 else
6347 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006348 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006349 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006350 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006351 return -1;
6352 }
6353 return 0;
6354}
6355
6356 static PyObject *
6357ConvertToPyObject(typval_T *tv)
6358{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006359 typval_T *argv;
6360 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006361 if (tv == NULL)
6362 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006363 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006364 return NULL;
6365 }
6366 switch (tv->v_type)
6367 {
6368 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006369 return PyBytes_FromString(tv->vval.v_string == NULL
6370 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006371 case VAR_NUMBER:
6372 return PyLong_FromLong((long) tv->vval.v_number);
6373#ifdef FEAT_FLOAT
6374 case VAR_FLOAT:
6375 return PyFloat_FromDouble((double) tv->vval.v_float);
6376#endif
6377 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006378 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006379 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006380 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006381 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006382 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006383 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006384 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006385 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006386 if (tv->vval.v_partial->pt_argc)
6387 {
6388 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6389 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6390 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6391 }
6392 else
6393 argv = NULL;
6394 if (tv->vval.v_partial->pt_dict != NULL)
6395 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006396 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006397 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006398 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006399 tv->vval.v_partial->pt_dict,
6400 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006401 case VAR_BLOB:
6402 return PyBytes_FromStringAndSize(
6403 (char*) tv->vval.v_blob->bv_ga.ga_data,
6404 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006405 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006406 case VAR_CHANNEL:
6407 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006408 Py_INCREF(Py_None);
6409 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006410 case VAR_SPECIAL:
6411 switch (tv->vval.v_number)
6412 {
6413 case VVAL_FALSE: return AlwaysFalse(NULL);
6414 case VVAL_TRUE: return AlwaysTrue(NULL);
6415 case VVAL_NONE:
6416 case VVAL_NULL: return AlwaysNone(NULL);
6417 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006418 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006419 return NULL;
6420 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006421 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006422}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006423
6424typedef struct
6425{
6426 PyObject_HEAD
6427} CurrentObject;
6428static PyTypeObject CurrentType;
6429
6430 static void
6431init_structs(void)
6432{
6433 vim_memset(&OutputType, 0, sizeof(OutputType));
6434 OutputType.tp_name = "vim.message";
6435 OutputType.tp_basicsize = sizeof(OutputObject);
6436 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6437 OutputType.tp_doc = "vim message object";
6438 OutputType.tp_methods = OutputMethods;
6439#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006440 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6441 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006442 OutputType.tp_alloc = call_PyType_GenericAlloc;
6443 OutputType.tp_new = call_PyType_GenericNew;
6444 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006445 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006446#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006447 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6448 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006449 // Disabled, because this causes a crash in test86
6450 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006451#endif
6452
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006453 vim_memset(&IterType, 0, sizeof(IterType));
6454 IterType.tp_name = "vim.iter";
6455 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006456 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006457 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006458 IterType.tp_iter = (getiterfunc)IterIter;
6459 IterType.tp_iternext = (iternextfunc)IterNext;
6460 IterType.tp_dealloc = (destructor)IterDestructor;
6461 IterType.tp_traverse = (traverseproc)IterTraverse;
6462 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006463
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006464 vim_memset(&BufferType, 0, sizeof(BufferType));
6465 BufferType.tp_name = "vim.buffer";
6466 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006467 BufferType.tp_dealloc = (destructor)BufferDestructor;
6468 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006469 BufferType.tp_as_sequence = &BufferAsSeq;
6470 BufferType.tp_as_mapping = &BufferAsMapping;
6471 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6472 BufferType.tp_doc = "vim buffer object";
6473 BufferType.tp_methods = BufferMethods;
6474#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006475 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006476 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006477 BufferType.tp_alloc = call_PyType_GenericAlloc;
6478 BufferType.tp_new = call_PyType_GenericNew;
6479 BufferType.tp_free = call_PyObject_Free;
6480#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006481 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006482 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483#endif
6484
6485 vim_memset(&WindowType, 0, sizeof(WindowType));
6486 WindowType.tp_name = "vim.window";
6487 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006488 WindowType.tp_dealloc = (destructor)WindowDestructor;
6489 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006490 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491 WindowType.tp_doc = "vim Window object";
6492 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006493 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6494 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006495#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006496 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6497 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006498 WindowType.tp_alloc = call_PyType_GenericAlloc;
6499 WindowType.tp_new = call_PyType_GenericNew;
6500 WindowType.tp_free = call_PyObject_Free;
6501#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6503 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504#endif
6505
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006506 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6507 TabPageType.tp_name = "vim.tabpage";
6508 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006509 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6510 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006511 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6512 TabPageType.tp_doc = "vim tab page object";
6513 TabPageType.tp_methods = TabPageMethods;
6514#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006516 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6517 TabPageType.tp_new = call_PyType_GenericNew;
6518 TabPageType.tp_free = call_PyObject_Free;
6519#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006520 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006521#endif
6522
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006523 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6524 BufMapType.tp_name = "vim.bufferlist";
6525 BufMapType.tp_basicsize = sizeof(BufMapObject);
6526 BufMapType.tp_as_mapping = &BufMapAsMapping;
6527 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006528 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006529 BufferType.tp_doc = "vim buffer list";
6530
6531 vim_memset(&WinListType, 0, sizeof(WinListType));
6532 WinListType.tp_name = "vim.windowlist";
6533 WinListType.tp_basicsize = sizeof(WinListType);
6534 WinListType.tp_as_sequence = &WinListAsSeq;
6535 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6536 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006537 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006538
6539 vim_memset(&TabListType, 0, sizeof(TabListType));
6540 TabListType.tp_name = "vim.tabpagelist";
6541 TabListType.tp_basicsize = sizeof(TabListType);
6542 TabListType.tp_as_sequence = &TabListAsSeq;
6543 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6544 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006545
6546 vim_memset(&RangeType, 0, sizeof(RangeType));
6547 RangeType.tp_name = "vim.range";
6548 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006549 RangeType.tp_dealloc = (destructor)RangeDestructor;
6550 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551 RangeType.tp_as_sequence = &RangeAsSeq;
6552 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006553 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006554 RangeType.tp_doc = "vim Range object";
6555 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006556 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6557 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006558#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006559 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560 RangeType.tp_alloc = call_PyType_GenericAlloc;
6561 RangeType.tp_new = call_PyType_GenericNew;
6562 RangeType.tp_free = call_PyObject_Free;
6563#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006564 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565#endif
6566
6567 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6568 CurrentType.tp_name = "vim.currentdata";
6569 CurrentType.tp_basicsize = sizeof(CurrentObject);
6570 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6571 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006572 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006573#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006574 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6575 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006576#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006577 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6578 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579#endif
6580
6581 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6582 DictionaryType.tp_name = "vim.dictionary";
6583 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006584 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006585 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006586 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006587 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6589 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006590 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6591 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6592 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006593#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006594 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6595 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006596#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006597 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6598 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006599#endif
6600
6601 vim_memset(&ListType, 0, sizeof(ListType));
6602 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006604 ListType.tp_basicsize = sizeof(ListObject);
6605 ListType.tp_as_sequence = &ListAsSeq;
6606 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006607 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608 ListType.tp_doc = "list pushing modifications to vim structure";
6609 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006610 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006611 ListType.tp_new = (newfunc)ListConstructor;
6612 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006613#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006614 ListType.tp_getattro = (getattrofunc)ListGetattro;
6615 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 ListType.tp_getattr = (getattrfunc)ListGetattr;
6618 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619#endif
6620
6621 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006622 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006624 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6625 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006626 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006627 FunctionType.tp_doc = "object that calls vim function";
6628 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006629 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006630 FunctionType.tp_new = (newfunc)FunctionConstructor;
6631 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006632#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006633 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006634#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006635 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006636#endif
6637
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006638 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6639 OptionsType.tp_name = "vim.options";
6640 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006641 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006642 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006643 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006644 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006645 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006646 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6647 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6648 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006649
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006650#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006651 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6652 LoaderType.tp_name = "vim.Loader";
6653 LoaderType.tp_basicsize = sizeof(LoaderObject);
6654 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6655 LoaderType.tp_doc = "vim message object";
6656 LoaderType.tp_methods = LoaderMethods;
6657 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006658#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006659
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006660#if PY_MAJOR_VERSION >= 3
6661 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6662 vimmodule.m_name = "vim";
6663 vimmodule.m_doc = "Vim Python interface\n";
6664 vimmodule.m_size = -1;
6665 vimmodule.m_methods = VimMethods;
6666#endif
6667}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006668
6669#define PYTYPE_READY(type) \
6670 if (PyType_Ready(&type)) \
6671 return -1;
6672
6673 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006674init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006675{
6676 PYTYPE_READY(IterType);
6677 PYTYPE_READY(BufferType);
6678 PYTYPE_READY(RangeType);
6679 PYTYPE_READY(WindowType);
6680 PYTYPE_READY(TabPageType);
6681 PYTYPE_READY(BufMapType);
6682 PYTYPE_READY(WinListType);
6683 PYTYPE_READY(TabListType);
6684 PYTYPE_READY(CurrentType);
6685 PYTYPE_READY(DictionaryType);
6686 PYTYPE_READY(ListType);
6687 PYTYPE_READY(FunctionType);
6688 PYTYPE_READY(OptionsType);
6689 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006690#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006691 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006692#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006693 return 0;
6694}
6695
6696 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006697init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006698{
6699 PyObject *path;
6700 PyObject *path_hook;
6701 PyObject *path_hooks;
6702
6703 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6704 return -1;
6705
6706 if (!(path_hooks = PySys_GetObject("path_hooks")))
6707 {
6708 PyErr_Clear();
6709 path_hooks = PyList_New(1);
6710 PyList_SET_ITEM(path_hooks, 0, path_hook);
6711 if (PySys_SetObject("path_hooks", path_hooks))
6712 {
6713 Py_DECREF(path_hooks);
6714 return -1;
6715 }
6716 Py_DECREF(path_hooks);
6717 }
6718 else if (PyList_Check(path_hooks))
6719 {
6720 if (PyList_Append(path_hooks, path_hook))
6721 {
6722 Py_DECREF(path_hook);
6723 return -1;
6724 }
6725 Py_DECREF(path_hook);
6726 }
6727 else
6728 {
6729 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006730 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006731 "You should now do the following:\n"
6732 "- append vim.path_hook to sys.path_hooks\n"
6733 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6734 VimTryEnd(); /* Discard the error */
6735 Py_DECREF(path_hook);
6736 return 0;
6737 }
6738
6739 if (!(path = PySys_GetObject("path")))
6740 {
6741 PyErr_Clear();
6742 path = PyList_New(1);
6743 Py_INCREF(vim_special_path_object);
6744 PyList_SET_ITEM(path, 0, vim_special_path_object);
6745 if (PySys_SetObject("path", path))
6746 {
6747 Py_DECREF(path);
6748 return -1;
6749 }
6750 Py_DECREF(path);
6751 }
6752 else if (PyList_Check(path))
6753 {
6754 if (PyList_Append(path, vim_special_path_object))
6755 return -1;
6756 }
6757 else
6758 {
6759 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006760 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006761 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6762 VimTryEnd(); /* Discard the error */
6763 }
6764
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006765 return 0;
6766}
6767
6768static BufMapObject TheBufferMap =
6769{
6770 PyObject_HEAD_INIT(&BufMapType)
6771};
6772
6773static WinListObject TheWindowList =
6774{
6775 PyObject_HEAD_INIT(&WinListType)
6776 NULL
6777};
6778
6779static CurrentObject TheCurrent =
6780{
6781 PyObject_HEAD_INIT(&CurrentType)
6782};
6783
6784static TabListObject TheTabPageList =
6785{
6786 PyObject_HEAD_INIT(&TabListType)
6787};
6788
6789static struct numeric_constant {
6790 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006791 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006792} numeric_constants[] = {
6793 {"VAR_LOCKED", VAR_LOCKED},
6794 {"VAR_FIXED", VAR_FIXED},
6795 {"VAR_SCOPE", VAR_SCOPE},
6796 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6797};
6798
6799static struct object_constant {
6800 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006801 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006802} object_constants[] = {
6803 {"buffers", (PyObject *)(void *)&TheBufferMap},
6804 {"windows", (PyObject *)(void *)&TheWindowList},
6805 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6806 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006807
6808 {"Buffer", (PyObject *)&BufferType},
6809 {"Range", (PyObject *)&RangeType},
6810 {"Window", (PyObject *)&WindowType},
6811 {"TabPage", (PyObject *)&TabPageType},
6812 {"Dictionary", (PyObject *)&DictionaryType},
6813 {"List", (PyObject *)&ListType},
6814 {"Function", (PyObject *)&FunctionType},
6815 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006816#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006817 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006818#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006819};
6820
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006821#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006822 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006823 return -1;
6824
6825#define ADD_CHECKED_OBJECT(m, name, obj) \
6826 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006827 PyObject *valObject = obj; \
6828 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006829 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006830 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006831 }
6832
6833 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006834populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006835{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006836 int i;
6837 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006838 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006839 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006840#if PY_VERSION_HEX >= 0x030700f0
6841 PyObject *dict;
6842 PyObject *cls;
6843#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006844
6845 for (i = 0; i < (int)(sizeof(numeric_constants)
6846 / sizeof(struct numeric_constant));
6847 ++i)
6848 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006849 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850
6851 for (i = 0; i < (int)(sizeof(object_constants)
6852 / sizeof(struct object_constant));
6853 ++i)
6854 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006855 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006856
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006857 valObject = object_constants[i].valObject;
6858 Py_INCREF(valObject);
6859 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006860 }
6861
6862 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6863 return -1;
6864 ADD_OBJECT(m, "error", VimError);
6865
Bram Moolenaara9922d62013-05-30 13:01:18 +02006866 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6867 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006868 ADD_CHECKED_OBJECT(m, "options",
6869 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006870
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006871 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006872 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006873 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006874
Bram Moolenaar22081f42016-06-01 20:38:34 +02006875#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006876 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006877 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006878#else
6879 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6880 return -1;
6881#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006882 ADD_OBJECT(m, "_getcwd", py_getcwd)
6883
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006884 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006885 return -1;
6886 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006887 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006889 if (PyObject_SetAttrString(other_module, "chdir", attr))
6890 {
6891 Py_DECREF(attr);
6892 return -1;
6893 }
6894 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006895
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006896 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006897 {
6898 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006899 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006900 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006901 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6902 {
6903 Py_DECREF(attr);
6904 return -1;
6905 }
6906 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006907 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006908 else
6909 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006910
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006911 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6912 return -1;
6913
6914 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6915
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006916#if PY_VERSION_HEX >= 0x030700f0
6917 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6918 return -1;
6919
6920 dict = PyModule_GetDict(imp);
6921
6922 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6923 {
6924 Py_DECREF(imp);
6925 return -1;
6926 }
6927
6928 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6929 {
6930 Py_DECREF(imp);
6931 return -1;
6932 }
6933
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006934 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6935 {
6936 // find_module() is deprecated, this may stop working in some later
6937 // version.
6938 ADD_OBJECT(m, "_find_module", py_find_module);
6939 }
6940
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006941 Py_DECREF(imp);
6942
6943 ADD_OBJECT(m, "_find_spec", py_find_spec);
6944#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006945 if (!(imp = PyImport_ImportModule("imp")))
6946 return -1;
6947
6948 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6949 {
6950 Py_DECREF(imp);
6951 return -1;
6952 }
6953
6954 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6955 {
6956 Py_DECREF(py_find_module);
6957 Py_DECREF(imp);
6958 return -1;
6959 }
6960
6961 Py_DECREF(imp);
6962
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006963 ADD_OBJECT(m, "_find_module", py_find_module);
6964 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006965#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006966
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006967 return 0;
6968}