blob: cc0450c13b599798ccce569d42296fb41ada5ea7 [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}
1225#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001226 static PyObject *
1227call_load_module(char *name, int len, PyObject *find_module_result)
1228{
1229 PyObject *fd, *pathname, *description;
1230
Bram Moolenaarc476e522013-06-23 13:46:40 +02001231 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001232 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001233 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001234 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001235 Py_TYPE_NAME(find_module_result));
1236 return NULL;
1237 }
1238 if (PyTuple_GET_SIZE(find_module_result) != 3)
1239 {
1240 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001241 N_("expected 3-tuple as imp.find_module() result, but got "
1242 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001243 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001244 return NULL;
1245 }
1246
1247 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1248 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1249 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1250 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001251 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001252 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001253 return NULL;
1254 }
1255
1256 return PyObject_CallFunction(py_load_module,
1257 "s#OOO", name, len, fd, pathname, description);
1258}
1259
1260 static PyObject *
1261find_module(char *fullname, char *tail, PyObject *new_path)
1262{
1263 PyObject *find_module_result;
1264 PyObject *module;
1265 char *dot;
1266
Bram Moolenaar41009372013-07-01 22:03:04 +02001267 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001268 {
1269 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001270 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001271 * first component
1272 */
1273 PyObject *newest_path;
1274 int partlen = (int) (dot - 1 - tail);
1275
1276 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1277 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001278 {
1279 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1280 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001281 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001282 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001283
1284 if (!(module = call_load_module(
1285 fullname,
1286 ((int) (tail - fullname)) + partlen,
1287 find_module_result)))
1288 {
1289 Py_DECREF(find_module_result);
1290 return NULL;
1291 }
1292
1293 Py_DECREF(find_module_result);
1294
1295 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1296 {
1297 Py_DECREF(module);
1298 return NULL;
1299 }
1300
1301 Py_DECREF(module);
1302
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001303 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001304
1305 Py_DECREF(newest_path);
1306
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001307 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001308 }
1309 else
1310 {
1311 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1312 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001313 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001314 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1315 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001316 return NULL;
1317 }
1318
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001319 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001320 }
1321}
1322
1323 static PyObject *
1324FinderFindModule(PyObject *self, PyObject *args)
1325{
1326 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001327 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001328 PyObject *new_path;
1329 LoaderObject *loader;
1330
1331 if (!PyArg_ParseTuple(args, "s", &fullname))
1332 return NULL;
1333
1334 if (!(new_path = Vim_GetPaths(self)))
1335 return NULL;
1336
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001337 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001338
1339 Py_DECREF(new_path);
1340
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001341 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001342 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001343 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001344 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001345
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001346 Py_INCREF(Py_None);
1347 return Py_None;
1348 }
1349
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001350 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001351 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001352 Py_DECREF(result);
1353 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001354 return NULL;
1355 }
1356
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001357 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1358 {
1359 vim_free(fullname);
1360 Py_DECREF(result);
1361 return NULL;
1362 }
1363
1364 loader->fullname = fullname;
1365 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001366
1367 return (PyObject *) loader;
1368}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001369#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001370
1371 static PyObject *
1372VimPathHook(PyObject *self UNUSED, PyObject *args)
1373{
1374 char *path;
1375
1376 if (PyArg_ParseTuple(args, "s", &path)
1377 && STRCMP(path, vim_special_path) == 0)
1378 {
1379 Py_INCREF(vim_module);
1380 return vim_module;
1381 }
1382
1383 PyErr_Clear();
1384 PyErr_SetNone(PyExc_ImportError);
1385 return NULL;
1386}
1387
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001388/*
1389 * Vim module - Definitions
1390 */
1391
1392static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001393 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001394 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001395 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001396 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1397 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001398 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1399 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001400 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001401#if PY_VERSION_HEX >= 0x030700f0
1402 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
1403#else
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001404 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001405#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001406 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1407 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1408 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001409};
1410
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001411/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001412 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001413 */
1414
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001415static PyTypeObject IterType;
1416
1417typedef PyObject *(*nextfun)(void **);
1418typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001419typedef int (*traversefun)(void *, visitproc, void *);
1420typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001421
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001422/* Main purpose of this object is removing the need for do python
1423 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1424 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001425
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001426typedef struct
1427{
1428 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001429 void *cur;
1430 nextfun next;
1431 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001432 traversefun traverse;
1433 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001434} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001435
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001436 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001437IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1438 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001439{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001440 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001441
Bram Moolenaar774267b2013-05-21 20:51:59 +02001442 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001443 self->cur = start;
1444 self->next = next;
1445 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001446 self->traverse = traverse;
1447 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001448
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001449 return (PyObject *)(self);
1450}
1451
1452 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001453IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001454{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001455 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001456 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001457 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001458}
1459
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001460 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001461IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001462{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001463 if (self->traverse != NULL)
1464 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001465 else
1466 return 0;
1467}
1468
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001469/* Mac OSX defines clear() somewhere. */
1470#ifdef clear
1471# undef clear
1472#endif
1473
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001474 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001475IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001476{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001477 if (self->clear != NULL)
1478 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001479 else
1480 return 0;
1481}
1482
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001483 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001484IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001485{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001486 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001487}
1488
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001489 static PyObject *
1490IterIter(PyObject *self)
1491{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001492 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001493 return self;
1494}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001495
Bram Moolenaardb913952012-06-29 12:54:53 +02001496typedef struct pylinkedlist_S {
1497 struct pylinkedlist_S *pll_next;
1498 struct pylinkedlist_S *pll_prev;
1499 PyObject *pll_obj;
1500} pylinkedlist_T;
1501
1502static pylinkedlist_T *lastdict = NULL;
1503static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001504static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001505
1506 static void
1507pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1508{
1509 if (ref->pll_prev == NULL)
1510 {
1511 if (ref->pll_next == NULL)
1512 {
1513 *last = NULL;
1514 return;
1515 }
1516 }
1517 else
1518 ref->pll_prev->pll_next = ref->pll_next;
1519
1520 if (ref->pll_next == NULL)
1521 *last = ref->pll_prev;
1522 else
1523 ref->pll_next->pll_prev = ref->pll_prev;
1524}
1525
1526 static void
1527pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1528{
1529 if (*last == NULL)
1530 ref->pll_prev = NULL;
1531 else
1532 {
1533 (*last)->pll_next = ref;
1534 ref->pll_prev = *last;
1535 }
1536 ref->pll_next = NULL;
1537 ref->pll_obj = self;
1538 *last = ref;
1539}
1540
1541static PyTypeObject DictionaryType;
1542
1543typedef struct
1544{
1545 PyObject_HEAD
1546 dict_T *dict;
1547 pylinkedlist_T ref;
1548} DictionaryObject;
1549
Bram Moolenaara9922d62013-05-30 13:01:18 +02001550static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1551
1552#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1553
Bram Moolenaardb913952012-06-29 12:54:53 +02001554 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001555DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001556{
1557 DictionaryObject *self;
1558
Bram Moolenaara9922d62013-05-30 13:01:18 +02001559 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001560 if (self == NULL)
1561 return NULL;
1562 self->dict = dict;
1563 ++dict->dv_refcount;
1564
1565 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1566
1567 return (PyObject *)(self);
1568}
1569
Bram Moolenaara9922d62013-05-30 13:01:18 +02001570 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001571py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001572{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001573 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001574
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001575 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 {
1577 PyErr_NoMemory();
1578 return NULL;
1579 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001580 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001582 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001583}
1584
1585 static PyObject *
1586DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1587{
1588 DictionaryObject *self;
1589 dict_T *dict;
1590
1591 if (!(dict = py_dict_alloc()))
1592 return NULL;
1593
1594 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1595
1596 --dict->dv_refcount;
1597
1598 if (kwargs || PyTuple_Size(args))
1599 {
1600 PyObject *tmp;
1601 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1602 {
1603 Py_DECREF(self);
1604 return NULL;
1605 }
1606
1607 Py_DECREF(tmp);
1608 }
1609
1610 return (PyObject *)(self);
1611}
1612
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001613 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001614DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001615{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001616 pyll_remove(&self->ref, &lastdict);
1617 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001618
1619 DESTRUCTOR_FINISH(self);
1620}
1621
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001622static char *DictionaryAttrs[] = {
1623 "locked", "scope",
1624 NULL
1625};
1626
1627 static PyObject *
1628DictionaryDir(PyObject *self)
1629{
1630 return ObjectDir(self, DictionaryAttrs);
1631}
1632
Bram Moolenaardb913952012-06-29 12:54:53 +02001633 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001634DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001635{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001636 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001637 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001638 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001639 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001640 return -1;
1641 }
1642
1643 if (strcmp(name, "locked") == 0)
1644 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001645 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001646 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001647 PyErr_SET_STRING(PyExc_TypeError,
1648 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001649 return -1;
1650 }
1651 else
1652 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001653 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001654 if (istrue == -1)
1655 return -1;
1656 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001657 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001658 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001659 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001660 }
1661 return 0;
1662 }
1663 else
1664 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001665 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001666 return -1;
1667 }
1668}
1669
1670 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001671DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001672{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001673 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001674}
1675
Bram Moolenaara9922d62013-05-30 13:01:18 +02001676#define DICT_FLAG_HAS_DEFAULT 0x01
1677#define DICT_FLAG_POP 0x02
1678#define DICT_FLAG_NONE_DEFAULT 0x04
1679#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1680#define DICT_FLAG_RETURN_PAIR 0x10
1681
Bram Moolenaardb913952012-06-29 12:54:53 +02001682 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001683_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001684{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001685 PyObject *keyObject;
1686 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001687 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001688 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001689 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001690 dict_T *dict = self->dict;
1691 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001692 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001693
Bram Moolenaara9922d62013-05-30 13:01:18 +02001694 if (flags & DICT_FLAG_HAS_DEFAULT)
1695 {
1696 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1697 return NULL;
1698 }
1699 else
1700 keyObject = args;
1701
1702 if (flags & DICT_FLAG_RETURN_BOOL)
1703 defObject = Py_False;
1704
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001705 if (!(key = StringToChars(keyObject, &todecref)))
1706 return NULL;
1707
1708 if (*key == NUL)
1709 {
1710 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001711 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001712 return NULL;
1713 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001714
Bram Moolenaara9922d62013-05-30 13:01:18 +02001715 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001716
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001717 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001718
Bram Moolenaara9922d62013-05-30 13:01:18 +02001719 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001720 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001721 if (defObject)
1722 {
1723 Py_INCREF(defObject);
1724 return defObject;
1725 }
1726 else
1727 {
1728 PyErr_SetObject(PyExc_KeyError, keyObject);
1729 return NULL;
1730 }
1731 }
1732 else if (flags & DICT_FLAG_RETURN_BOOL)
1733 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001734 ret = Py_True;
1735 Py_INCREF(ret);
1736 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001737 }
1738
1739 di = dict_lookup(hi);
1740
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001741 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001742 return NULL;
1743
1744 if (flags & DICT_FLAG_POP)
1745 {
1746 if (dict->dv_lock)
1747 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001748 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001749 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001750 return NULL;
1751 }
1752
1753 hash_remove(&dict->dv_hashtab, hi);
1754 dictitem_free(di);
1755 }
1756
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001757 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001758}
1759
1760 static PyObject *
1761DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1762{
1763 return _DictionaryItem(self, keyObject, 0);
1764}
1765
1766 static int
1767DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1768{
1769 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001770 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001771
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001772 if (rObj == NULL)
1773 return -1;
1774
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001775 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001776
Bram Moolenaardee2e312013-06-23 16:35:47 +02001777 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001779 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001780}
1781
1782typedef struct
1783{
1784 hashitem_T *ht_array;
1785 long_u ht_used;
1786 hashtab_T *ht;
1787 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001788 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789} dictiterinfo_T;
1790
1791 static PyObject *
1792DictionaryIterNext(dictiterinfo_T **dii)
1793{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001794 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001795
1796 if (!(*dii)->todo)
1797 return NULL;
1798
1799 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1800 (*dii)->ht->ht_used != (*dii)->ht_used)
1801 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001802 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001803 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001804 return NULL;
1805 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001806
Bram Moolenaara9922d62013-05-30 13:01:18 +02001807 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1808 ++((*dii)->hi);
1809
1810 --((*dii)->todo);
1811
Bram Moolenaar41009372013-07-01 22:03:04 +02001812 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001813 return NULL;
1814
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001815 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816}
1817
1818 static PyObject *
1819DictionaryIter(DictionaryObject *self)
1820{
1821 dictiterinfo_T *dii;
1822 hashtab_T *ht;
1823
1824 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1825 {
1826 PyErr_NoMemory();
1827 return NULL;
1828 }
1829
1830 ht = &self->dict->dv_hashtab;
1831 dii->ht_array = ht->ht_array;
1832 dii->ht_used = ht->ht_used;
1833 dii->ht = ht;
1834 dii->hi = dii->ht_array;
1835 dii->todo = dii->ht_used;
1836
1837 return IterNew(dii,
1838 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1839 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001840}
1841
1842 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001843DictionaryAssItem(
1844 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001845{
1846 char_u *key;
1847 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001848 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001849 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001850 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001851
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001852 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001853 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001854 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001855 return -1;
1856 }
1857
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001858 if (!(key = StringToChars(keyObject, &todecref)))
1859 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001860
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001861 if (*key == NUL)
1862 {
1863 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001864 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001865 return -1;
1866 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001867
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001868 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001869
1870 if (valObject == NULL)
1871 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001872 hashitem_T *hi;
1873
Bram Moolenaardb913952012-06-29 12:54:53 +02001874 if (di == NULL)
1875 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001876 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001877 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001878 return -1;
1879 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001880 hi = hash_find(&dict->dv_hashtab, di->di_key);
1881 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001882 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001883 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001884 return 0;
1885 }
1886
1887 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001888 {
1889 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001890 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001891 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001892
1893 if (di == NULL)
1894 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001895 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001896 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001897 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001898 PyErr_NoMemory();
1899 return -1;
1900 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001901 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001902
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001903 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001904 {
1905 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001906 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001907 RAISE_KEY_ADD_FAIL(key);
1908 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001909 return -1;
1910 }
1911 }
1912 else
1913 clear_tv(&di->di_tv);
1914
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001915 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001916
1917 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001918 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001919 return 0;
1920}
1921
Bram Moolenaara9922d62013-05-30 13:01:18 +02001922typedef PyObject *(*hi_to_py)(hashitem_T *);
1923
Bram Moolenaardb913952012-06-29 12:54:53 +02001924 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001925DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001926{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001927 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001928 long_u todo = dict->dv_hashtab.ht_used;
1929 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001930 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001932 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001934 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001935 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1936 {
1937 if (!HASHITEM_EMPTY(hi))
1938 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939 if (!(newObj = hiconvert(hi)))
1940 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001941 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001942 return NULL;
1943 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001944 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001945 --todo;
1946 ++i;
1947 }
1948 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001949 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001950}
1951
Bram Moolenaara9922d62013-05-30 13:01:18 +02001952 static PyObject *
1953dict_key(hashitem_T *hi)
1954{
1955 return PyBytes_FromString((char *)(hi->hi_key));
1956}
1957
1958 static PyObject *
1959DictionaryListKeys(DictionaryObject *self)
1960{
1961 return DictionaryListObjects(self, dict_key);
1962}
1963
1964 static PyObject *
1965dict_val(hashitem_T *hi)
1966{
1967 dictitem_T *di;
1968
1969 di = dict_lookup(hi);
1970 return ConvertToPyObject(&di->di_tv);
1971}
1972
1973 static PyObject *
1974DictionaryListValues(DictionaryObject *self)
1975{
1976 return DictionaryListObjects(self, dict_val);
1977}
1978
1979 static PyObject *
1980dict_item(hashitem_T *hi)
1981{
1982 PyObject *keyObject;
1983 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001984 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001985
1986 if (!(keyObject = dict_key(hi)))
1987 return NULL;
1988
1989 if (!(valObject = dict_val(hi)))
1990 {
1991 Py_DECREF(keyObject);
1992 return NULL;
1993 }
1994
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001995 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001996
1997 Py_DECREF(keyObject);
1998 Py_DECREF(valObject);
1999
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002000 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002001}
2002
2003 static PyObject *
2004DictionaryListItems(DictionaryObject *self)
2005{
2006 return DictionaryListObjects(self, dict_item);
2007}
2008
2009 static PyObject *
2010DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2011{
2012 dict_T *dict = self->dict;
2013
2014 if (dict->dv_lock)
2015 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002016 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002017 return NULL;
2018 }
2019
2020 if (kwargs)
2021 {
2022 typval_T tv;
2023
2024 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2025 return NULL;
2026
2027 VimTryStart();
2028 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2029 clear_tv(&tv);
2030 if (VimTryEnd())
2031 return NULL;
2032 }
2033 else
2034 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002035 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002036
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002037 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002038 return NULL;
2039
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002040 if (obj == NULL)
2041 {
2042 Py_INCREF(Py_None);
2043 return Py_None;
2044 }
2045
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002046 if (PyObject_HasAttrString(obj, "keys"))
2047 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002048 else
2049 {
2050 PyObject *iterator;
2051 PyObject *item;
2052
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002053 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002054 return NULL;
2055
2056 while ((item = PyIter_Next(iterator)))
2057 {
2058 PyObject *fast;
2059 PyObject *keyObject;
2060 PyObject *valObject;
2061 PyObject *todecref;
2062 char_u *key;
2063 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002064 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002065
2066 if (!(fast = PySequence_Fast(item, "")))
2067 {
2068 Py_DECREF(iterator);
2069 Py_DECREF(item);
2070 return NULL;
2071 }
2072
2073 Py_DECREF(item);
2074
2075 if (PySequence_Fast_GET_SIZE(fast) != 2)
2076 {
2077 Py_DECREF(iterator);
2078 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002079 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002080 N_("expected sequence element of size 2, "
2081 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002082 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002083 return NULL;
2084 }
2085
2086 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2087
2088 if (!(key = StringToChars(keyObject, &todecref)))
2089 {
2090 Py_DECREF(iterator);
2091 Py_DECREF(fast);
2092 return NULL;
2093 }
2094
2095 di = dictitem_alloc(key);
2096
2097 Py_XDECREF(todecref);
2098
2099 if (di == NULL)
2100 {
2101 Py_DECREF(fast);
2102 Py_DECREF(iterator);
2103 PyErr_NoMemory();
2104 return NULL;
2105 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002106 di->di_tv.v_type = VAR_UNKNOWN;
2107
2108 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2109
2110 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2111 {
2112 Py_DECREF(iterator);
2113 Py_DECREF(fast);
2114 dictitem_free(di);
2115 return NULL;
2116 }
2117
2118 Py_DECREF(fast);
2119
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002120 hi = hash_find(&dict->dv_hashtab, di->di_key);
2121 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002122 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002123 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002124 Py_DECREF(iterator);
2125 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002126 return NULL;
2127 }
2128 }
2129
2130 Py_DECREF(iterator);
2131
2132 /* Iterator may have finished due to an exception */
2133 if (PyErr_Occurred())
2134 return NULL;
2135 }
2136 }
2137 Py_INCREF(Py_None);
2138 return Py_None;
2139}
2140
2141 static PyObject *
2142DictionaryGet(DictionaryObject *self, PyObject *args)
2143{
2144 return _DictionaryItem(self, args,
2145 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2146}
2147
2148 static PyObject *
2149DictionaryPop(DictionaryObject *self, PyObject *args)
2150{
2151 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2152}
2153
2154 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002155DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002156{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002157 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002158 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002159 PyObject *valObject;
2160 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002161
Bram Moolenaarde71b562013-06-02 17:41:54 +02002162 if (self->dict->dv_hashtab.ht_used == 0)
2163 {
2164 PyErr_SetNone(PyExc_KeyError);
2165 return NULL;
2166 }
2167
2168 hi = self->dict->dv_hashtab.ht_array;
2169 while (HASHITEM_EMPTY(hi))
2170 ++hi;
2171
2172 di = dict_lookup(hi);
2173
2174 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002175 return NULL;
2176
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002177 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002178 {
2179 Py_DECREF(valObject);
2180 return NULL;
2181 }
2182
2183 hash_remove(&self->dict->dv_hashtab, hi);
2184 dictitem_free(di);
2185
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002186 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002187}
2188
2189 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002190DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002191{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002192 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2193}
2194
2195static PySequenceMethods DictionaryAsSeq = {
2196 0, /* sq_length */
2197 0, /* sq_concat */
2198 0, /* sq_repeat */
2199 0, /* sq_item */
2200 0, /* sq_slice */
2201 0, /* sq_ass_item */
2202 0, /* sq_ass_slice */
2203 (objobjproc) DictionaryContains, /* sq_contains */
2204 0, /* sq_inplace_concat */
2205 0, /* sq_inplace_repeat */
2206};
2207
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002208static PyMappingMethods DictionaryAsMapping = {
2209 (lenfunc) DictionaryLength,
2210 (binaryfunc) DictionaryItem,
2211 (objobjargproc) DictionaryAssItem,
2212};
2213
Bram Moolenaardb913952012-06-29 12:54:53 +02002214static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002215 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002216 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2217 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2218 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2219 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2220 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002221 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002222 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002223 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2224 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002225};
2226
2227static PyTypeObject ListType;
2228
2229typedef struct
2230{
2231 PyObject_HEAD
2232 list_T *list;
2233 pylinkedlist_T ref;
2234} ListObject;
2235
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002236#define NEW_LIST(list) ListNew(&ListType, list)
2237
Bram Moolenaardb913952012-06-29 12:54:53 +02002238 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002239ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002240{
2241 ListObject *self;
2242
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002243 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002244 if (self == NULL)
2245 return NULL;
2246 self->list = list;
2247 ++list->lv_refcount;
2248
2249 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2250
2251 return (PyObject *)(self);
2252}
2253
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002254 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002255py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002256{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002257 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002258
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002259 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002260 {
2261 PyErr_NoMemory();
2262 return NULL;
2263 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002264 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002265
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002266 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002267}
2268
2269 static int
2270list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2271{
2272 PyObject *iterator;
2273 PyObject *item;
2274 listitem_T *li;
2275
2276 if (!(iterator = PyObject_GetIter(obj)))
2277 return -1;
2278
2279 while ((item = PyIter_Next(iterator)))
2280 {
2281 if (!(li = listitem_alloc()))
2282 {
2283 PyErr_NoMemory();
2284 Py_DECREF(item);
2285 Py_DECREF(iterator);
2286 return -1;
2287 }
2288 li->li_tv.v_lock = 0;
2289 li->li_tv.v_type = VAR_UNKNOWN;
2290
2291 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2292 {
2293 Py_DECREF(item);
2294 Py_DECREF(iterator);
2295 listitem_free(li);
2296 return -1;
2297 }
2298
2299 Py_DECREF(item);
2300
2301 list_append(l, li);
2302 }
2303
2304 Py_DECREF(iterator);
2305
2306 /* Iterator may have finished due to an exception */
2307 if (PyErr_Occurred())
2308 return -1;
2309
2310 return 0;
2311}
2312
2313 static PyObject *
2314ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2315{
2316 list_T *list;
2317 PyObject *obj = NULL;
2318
2319 if (kwargs)
2320 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002321 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002322 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002323 return NULL;
2324 }
2325
2326 if (!PyArg_ParseTuple(args, "|O", &obj))
2327 return NULL;
2328
2329 if (!(list = py_list_alloc()))
2330 return NULL;
2331
2332 if (obj)
2333 {
2334 PyObject *lookup_dict;
2335
2336 if (!(lookup_dict = PyDict_New()))
2337 {
2338 list_unref(list);
2339 return NULL;
2340 }
2341
2342 if (list_py_concat(list, obj, lookup_dict) == -1)
2343 {
2344 Py_DECREF(lookup_dict);
2345 list_unref(list);
2346 return NULL;
2347 }
2348
2349 Py_DECREF(lookup_dict);
2350 }
2351
2352 return ListNew(subtype, list);
2353}
2354
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002355 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002356ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002357{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002358 pyll_remove(&self->ref, &lastlist);
2359 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002360
2361 DESTRUCTOR_FINISH(self);
2362}
2363
Bram Moolenaardb913952012-06-29 12:54:53 +02002364 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002365ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002366{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002367 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002368}
2369
2370 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002371ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002372{
2373 listitem_T *li;
2374
Bram Moolenaard6e39182013-05-21 18:30:34 +02002375 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002376 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002377 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002378 return NULL;
2379 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002380 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002381 if (li == NULL)
2382 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002383 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002384 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002385 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002386 return NULL;
2387 }
2388 return ConvertToPyObject(&li->li_tv);
2389}
2390
Bram Moolenaardb913952012-06-29 12:54:53 +02002391 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002392ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2393 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002394{
2395 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002396 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002397
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002398 if (step == 0)
2399 {
2400 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2401 return NULL;
2402 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002403
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002404 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002405 if (list == NULL)
2406 return NULL;
2407
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002408 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002409 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002410 PyObject *item;
2411
2412 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002413 if (item == NULL)
2414 {
2415 Py_DECREF(list);
2416 return NULL;
2417 }
2418
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002419 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002420 }
2421
2422 return list;
2423}
2424
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002425 static PyObject *
2426ListItem(ListObject *self, PyObject* idx)
2427{
2428#if PY_MAJOR_VERSION < 3
2429 if (PyInt_Check(idx))
2430 {
2431 long _idx = PyInt_AsLong(idx);
2432 return ListIndex(self, _idx);
2433 }
2434 else
2435#endif
2436 if (PyLong_Check(idx))
2437 {
2438 long _idx = PyLong_AsLong(idx);
2439 return ListIndex(self, _idx);
2440 }
2441 else if (PySlice_Check(idx))
2442 {
2443 Py_ssize_t start, stop, step, slicelen;
2444
Bram Moolenaar922a4662014-03-30 16:11:43 +02002445 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002446 &start, &stop, &step, &slicelen) < 0)
2447 return NULL;
2448 return ListSlice(self, start, step, slicelen);
2449 }
2450 else
2451 {
2452 RAISE_INVALID_INDEX_TYPE(idx);
2453 return NULL;
2454 }
2455}
2456
2457 static void
2458list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2459 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2460{
2461 while (numreplaced--)
2462 {
2463 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2464 listitem_remove(l, lis[slicelen + numreplaced]);
2465 }
2466 while (numadded--)
2467 {
2468 listitem_T *next;
2469
2470 next = lastaddedli->li_prev;
2471 listitem_remove(l, lastaddedli);
2472 lastaddedli = next;
2473 }
2474}
2475
2476 static int
2477ListAssSlice(ListObject *self, Py_ssize_t first,
2478 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2479{
2480 PyObject *iterator;
2481 PyObject *item;
2482 listitem_T *li;
2483 listitem_T *lastaddedli = NULL;
2484 listitem_T *next;
2485 typval_T v;
2486 list_T *l = self->list;
2487 PyInt i;
2488 PyInt j;
2489 PyInt numreplaced = 0;
2490 PyInt numadded = 0;
2491 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002492 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002493
2494 size = ListLength(self);
2495
2496 if (l->lv_lock)
2497 {
2498 RAISE_LOCKED_LIST;
2499 return -1;
2500 }
2501
2502 if (step == 0)
2503 {
2504 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2505 return -1;
2506 }
2507
2508 if (step != 1 && slicelen == 0)
2509 {
2510 /* Nothing to do. Only error out if obj has some items. */
2511 int ret = 0;
2512
2513 if (obj == NULL)
2514 return 0;
2515
2516 if (!(iterator = PyObject_GetIter(obj)))
2517 return -1;
2518
2519 if ((item = PyIter_Next(iterator)))
2520 {
2521 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002522 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002523 "to extended slice"), 0);
2524 Py_DECREF(item);
2525 ret = -1;
2526 }
2527 Py_DECREF(iterator);
2528 return ret;
2529 }
2530
2531 if (obj != NULL)
2532 /* XXX May allocate zero bytes. */
2533 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2534 {
2535 PyErr_NoMemory();
2536 return -1;
2537 }
2538
2539 if (first == size)
2540 li = NULL;
2541 else
2542 {
2543 li = list_find(l, (long) first);
2544 if (li == NULL)
2545 {
2546 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2547 (int)first);
2548 if (obj != NULL)
2549 PyMem_Free(lis);
2550 return -1;
2551 }
2552 i = slicelen;
2553 while (i-- && li != NULL)
2554 {
2555 j = step;
2556 next = li;
2557 if (step > 0)
2558 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2559 else
2560 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2561
2562 if (obj == NULL)
2563 listitem_remove(l, li);
2564 else
2565 lis[slicelen - i - 1] = li;
2566
2567 li = next;
2568 }
2569 if (li == NULL && i != -1)
2570 {
2571 PyErr_SET_VIM(N_("internal error: not enough list items"));
2572 if (obj != NULL)
2573 PyMem_Free(lis);
2574 return -1;
2575 }
2576 }
2577
2578 if (obj == NULL)
2579 return 0;
2580
2581 if (!(iterator = PyObject_GetIter(obj)))
2582 {
2583 PyMem_Free(lis);
2584 return -1;
2585 }
2586
2587 i = 0;
2588 while ((item = PyIter_Next(iterator)))
2589 {
2590 if (ConvertFromPyObject(item, &v) == -1)
2591 {
2592 Py_DECREF(iterator);
2593 Py_DECREF(item);
2594 PyMem_Free(lis);
2595 return -1;
2596 }
2597 Py_DECREF(item);
2598 if (list_insert_tv(l, &v, numreplaced < slicelen
2599 ? lis[numreplaced]
2600 : li) == FAIL)
2601 {
2602 clear_tv(&v);
2603 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2604 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2605 PyMem_Free(lis);
2606 return -1;
2607 }
2608 if (numreplaced < slicelen)
2609 {
2610 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002611 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002612 numreplaced++;
2613 }
2614 else
2615 {
2616 if (li)
2617 lastaddedli = li->li_prev;
2618 else
2619 lastaddedli = l->lv_last;
2620 numadded++;
2621 }
2622 clear_tv(&v);
2623 if (step != 1 && i >= slicelen)
2624 {
2625 Py_DECREF(iterator);
2626 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002627 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002628 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002629 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2630 PyMem_Free(lis);
2631 return -1;
2632 }
2633 ++i;
2634 }
2635 Py_DECREF(iterator);
2636
2637 if (step != 1 && i != slicelen)
2638 {
2639 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002640 N_("attempt to assign sequence of size %d to extended slice "
2641 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002642 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2643 PyMem_Free(lis);
2644 return -1;
2645 }
2646
2647 if (PyErr_Occurred())
2648 {
2649 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2650 PyMem_Free(lis);
2651 return -1;
2652 }
2653
2654 for (i = 0; i < numreplaced; i++)
2655 listitem_free(lis[i]);
2656 if (step == 1)
2657 for (i = numreplaced; i < slicelen; i++)
2658 listitem_remove(l, lis[i]);
2659
2660 PyMem_Free(lis);
2661
2662 return 0;
2663}
2664
2665 static int
2666ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2667{
2668 typval_T tv;
2669 list_T *l = self->list;
2670 listitem_T *li;
2671 Py_ssize_t length = ListLength(self);
2672
2673 if (l->lv_lock)
2674 {
2675 RAISE_LOCKED_LIST;
2676 return -1;
2677 }
2678 if (index > length || (index == length && obj == NULL))
2679 {
2680 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2681 return -1;
2682 }
2683
2684 if (obj == NULL)
2685 {
2686 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002687 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002688 clear_tv(&li->li_tv);
2689 vim_free(li);
2690 return 0;
2691 }
2692
2693 if (ConvertFromPyObject(obj, &tv) == -1)
2694 return -1;
2695
2696 if (index == length)
2697 {
2698 if (list_append_tv(l, &tv) == FAIL)
2699 {
2700 clear_tv(&tv);
2701 PyErr_SET_VIM(N_("failed to add item to list"));
2702 return -1;
2703 }
2704 }
2705 else
2706 {
2707 li = list_find(l, (long) index);
2708 clear_tv(&li->li_tv);
2709 copy_tv(&tv, &li->li_tv);
2710 clear_tv(&tv);
2711 }
2712 return 0;
2713}
2714
2715 static Py_ssize_t
2716ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2717{
2718#if PY_MAJOR_VERSION < 3
2719 if (PyInt_Check(idx))
2720 {
2721 long _idx = PyInt_AsLong(idx);
2722 return ListAssIndex(self, _idx, obj);
2723 }
2724 else
2725#endif
2726 if (PyLong_Check(idx))
2727 {
2728 long _idx = PyLong_AsLong(idx);
2729 return ListAssIndex(self, _idx, obj);
2730 }
2731 else if (PySlice_Check(idx))
2732 {
2733 Py_ssize_t start, stop, step, slicelen;
2734
Bram Moolenaar922a4662014-03-30 16:11:43 +02002735 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002736 &start, &stop, &step, &slicelen) < 0)
2737 return -1;
2738 return ListAssSlice(self, start, step, slicelen,
2739 obj);
2740 }
2741 else
2742 {
2743 RAISE_INVALID_INDEX_TYPE(idx);
2744 return -1;
2745 }
2746}
2747
2748 static PyObject *
2749ListConcatInPlace(ListObject *self, PyObject *obj)
2750{
2751 list_T *l = self->list;
2752 PyObject *lookup_dict;
2753
2754 if (l->lv_lock)
2755 {
2756 RAISE_LOCKED_LIST;
2757 return NULL;
2758 }
2759
2760 if (!(lookup_dict = PyDict_New()))
2761 return NULL;
2762
2763 if (list_py_concat(l, obj, lookup_dict) == -1)
2764 {
2765 Py_DECREF(lookup_dict);
2766 return NULL;
2767 }
2768 Py_DECREF(lookup_dict);
2769
2770 Py_INCREF(self);
2771 return (PyObject *)(self);
2772}
2773
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002774typedef struct
2775{
2776 listwatch_T lw;
2777 list_T *list;
2778} listiterinfo_T;
2779
2780 static void
2781ListIterDestruct(listiterinfo_T *lii)
2782{
2783 list_rem_watch(lii->list, &lii->lw);
2784 PyMem_Free(lii);
2785}
2786
2787 static PyObject *
2788ListIterNext(listiterinfo_T **lii)
2789{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002790 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002791
2792 if (!((*lii)->lw.lw_item))
2793 return NULL;
2794
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002795 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002796 return NULL;
2797
2798 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2799
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002800 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002801}
2802
2803 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002804ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002805{
2806 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002807 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002808
2809 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2810 {
2811 PyErr_NoMemory();
2812 return NULL;
2813 }
2814
2815 list_add_watch(l, &lii->lw);
2816 lii->lw.lw_item = l->lv_first;
2817 lii->list = l;
2818
2819 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002820 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2821 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002822}
2823
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002824static char *ListAttrs[] = {
2825 "locked",
2826 NULL
2827};
2828
2829 static PyObject *
2830ListDir(PyObject *self)
2831{
2832 return ObjectDir(self, ListAttrs);
2833}
2834
Bram Moolenaar66b79852012-09-21 14:00:35 +02002835 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002836ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002837{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002838 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002839 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002840 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002841 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002842 return -1;
2843 }
2844
2845 if (strcmp(name, "locked") == 0)
2846 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002847 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002848 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002849 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002850 return -1;
2851 }
2852 else
2853 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002854 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002855 if (istrue == -1)
2856 return -1;
2857 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002858 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002859 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002860 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002861 }
2862 return 0;
2863 }
2864 else
2865 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002866 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002867 return -1;
2868 }
2869}
2870
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002871static PySequenceMethods ListAsSeq = {
2872 (lenfunc) ListLength, /* sq_length, len(x) */
2873 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2874 0, /* RangeRepeat, sq_repeat, x*n */
2875 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2876 0, /* was_sq_slice, x[i:j] */
2877 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2878 0, /* was_sq_ass_slice, x[i:j]=v */
2879 0, /* sq_contains */
2880 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2881 0, /* sq_inplace_repeat */
2882};
2883
2884static PyMappingMethods ListAsMapping = {
2885 /* mp_length */ (lenfunc) ListLength,
2886 /* mp_subscript */ (binaryfunc) ListItem,
2887 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2888};
2889
Bram Moolenaardb913952012-06-29 12:54:53 +02002890static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002891 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2892 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2893 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002894};
2895
2896typedef struct
2897{
2898 PyObject_HEAD
2899 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002900 int argc;
2901 typval_T *argv;
2902 dict_T *self;
2903 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002904 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002905} FunctionObject;
2906
2907static PyTypeObject FunctionType;
2908
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002909#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2910 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002911
Bram Moolenaardb913952012-06-29 12:54:53 +02002912 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002913FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002914 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002915{
2916 FunctionObject *self;
2917
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002918 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002919 if (self == NULL)
2920 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002921
2922 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002923 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002924 if (!translated_function_exists(name))
2925 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002926 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002927 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002928 return NULL;
2929 }
2930 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002931 }
2932 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002933 {
2934 char_u *p;
2935
2936 if ((p = get_expanded_name(name,
2937 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002938 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002939 PyErr_FORMAT(PyExc_ValueError,
2940 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002941 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002942 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002943
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002944 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2945 {
2946 char_u *np;
2947 size_t len = STRLEN(p) + 1;
2948
Bram Moolenaar80dae042018-12-23 13:36:40 +01002949 if ((np = alloc((int)len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002950 {
2951 vim_free(p);
2952 return NULL;
2953 }
2954 mch_memmove(np, "<SNR>", 5);
2955 mch_memmove(np + 5, p + 3, len - 3);
2956 vim_free(p);
2957 self->name = np;
2958 }
2959 else
2960 self->name = p;
2961 }
2962
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002963 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002964 self->argc = argc;
2965 self->argv = argv;
2966 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002967 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002968
2969 if (self->argv || self->self)
2970 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2971
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002972 return (PyObject *)(self);
2973}
2974
2975 static PyObject *
2976FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2977{
2978 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002979 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002980 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002981 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002982 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002983 typval_T selfdicttv;
2984 typval_T argstv;
2985 list_T *argslist = NULL;
2986 dict_T *selfdict = NULL;
2987 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002988 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002989 typval_T *argv = NULL;
2990 typval_T *curtv;
2991 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002992
Bram Moolenaar8110a092016-04-14 15:56:09 +02002993 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002994 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002995 selfdictObject = PyDict_GetItemString(kwargs, "self");
2996 if (selfdictObject != NULL)
2997 {
2998 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2999 return NULL;
3000 selfdict = selfdicttv.vval.v_dict;
3001 }
3002 argsObject = PyDict_GetItemString(kwargs, "args");
3003 if (argsObject != NULL)
3004 {
3005 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3006 {
3007 dict_unref(selfdict);
3008 return NULL;
3009 }
3010 argslist = argstv.vval.v_list;
3011
3012 argc = argslist->lv_len;
3013 if (argc != 0)
3014 {
3015 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003016 if (argv == NULL)
3017 {
3018 PyErr_NoMemory();
3019 dict_unref(selfdict);
3020 list_unref(argslist);
3021 return NULL;
3022 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003023 curtv = argv;
3024 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3025 copy_tv(&li->li_tv, curtv++);
3026 }
3027 list_unref(argslist);
3028 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003029 if (selfdict != NULL)
3030 {
3031 auto_rebind = FALSE;
3032 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3033 if (autoRebindObject != NULL)
3034 {
3035 auto_rebind = PyObject_IsTrue(autoRebindObject);
3036 if (auto_rebind == -1)
3037 {
3038 dict_unref(selfdict);
3039 list_unref(argslist);
3040 return NULL;
3041 }
3042 }
3043 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003044 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003045
Bram Moolenaar389a1792013-06-23 13:00:44 +02003046 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003047 {
3048 dict_unref(selfdict);
3049 while (argc--)
3050 clear_tv(&argv[argc]);
3051 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003052 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003053 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003054
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003055 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003056
Bram Moolenaar389a1792013-06-23 13:00:44 +02003057 PyMem_Free(name);
3058
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003059 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003060}
3061
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003062 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003063FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003064{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003065 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003066 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003067 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003068 for (i = 0; i < self->argc; ++i)
3069 clear_tv(&self->argv[i]);
3070 PyMem_Free(self->argv);
3071 dict_unref(self->self);
3072 if (self->argv || self->self)
3073 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003074
3075 DESTRUCTOR_FINISH(self);
3076}
3077
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003078static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003079 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003080 NULL
3081};
3082
3083 static PyObject *
3084FunctionDir(PyObject *self)
3085{
3086 return ObjectDir(self, FunctionAttrs);
3087}
3088
Bram Moolenaardb913952012-06-29 12:54:53 +02003089 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003090FunctionAttr(FunctionObject *self, char *name)
3091{
3092 list_T *list;
3093 int i;
3094 if (strcmp(name, "name") == 0)
3095 return PyString_FromString((char *)(self->name));
3096 else if (strcmp(name, "args") == 0)
3097 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003098 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003099 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003100
Bram Moolenaar8110a092016-04-14 15:56:09 +02003101 for (i = 0; i < self->argc; ++i)
3102 list_append_tv(list, &self->argv[i]);
3103 return NEW_LIST(list);
3104 }
3105 else if (strcmp(name, "self") == 0)
3106 return self->self == NULL
3107 ? AlwaysNone(NULL)
3108 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003109 else if (strcmp(name, "auto_rebind") == 0)
3110 return self->auto_rebind
3111 ? AlwaysTrue(NULL)
3112 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003113 else if (strcmp(name, "__members__") == 0)
3114 return ObjectDir(NULL, FunctionAttrs);
3115 return NULL;
3116}
3117
3118/* Populate partial_T given function object.
3119 *
3120 * "exported" should be set to true when it is needed to construct a partial
3121 * that may be stored in a variable (i.e. may be freed by Vim).
3122 */
3123 static void
3124set_partial(FunctionObject *self, partial_T *pt, int exported)
3125{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003126 int i;
3127
3128 pt->pt_name = self->name;
3129 if (self->argv)
3130 {
3131 pt->pt_argc = self->argc;
3132 if (exported)
3133 {
3134 pt->pt_argv = (typval_T *)alloc_clear(
3135 sizeof(typval_T) * self->argc);
3136 for (i = 0; i < pt->pt_argc; ++i)
3137 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3138 }
3139 else
3140 pt->pt_argv = self->argv;
3141 }
3142 else
3143 {
3144 pt->pt_argc = 0;
3145 pt->pt_argv = NULL;
3146 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003147 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003148 pt->pt_dict = self->self;
3149 if (exported && self->self)
3150 ++pt->pt_dict->dv_refcount;
3151 if (exported)
3152 pt->pt_name = vim_strsave(pt->pt_name);
3153 pt->pt_refcount = 1;
3154}
3155
3156 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003157FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003158{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003159 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003160 typval_T args;
3161 typval_T selfdicttv;
3162 typval_T rettv;
3163 dict_T *selfdict = NULL;
3164 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003165 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003166 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003167 partial_T pt;
3168 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003169
Bram Moolenaar8110a092016-04-14 15:56:09 +02003170 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003171 return NULL;
3172
3173 if (kwargs != NULL)
3174 {
3175 selfdictObject = PyDict_GetItemString(kwargs, "self");
3176 if (selfdictObject != NULL)
3177 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003178 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003179 {
3180 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003181 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003182 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003183 selfdict = selfdicttv.vval.v_dict;
3184 }
3185 }
3186
Bram Moolenaar8110a092016-04-14 15:56:09 +02003187 if (self->argv || self->self)
3188 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003189 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003190 set_partial(self, &pt, FALSE);
3191 pt_ptr = &pt;
3192 }
3193
Bram Moolenaar71700b82013-05-15 17:49:05 +02003194 Py_BEGIN_ALLOW_THREADS
3195 Python_Lock_Vim();
3196
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003197 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003198 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003199
3200 Python_Release_Vim();
3201 Py_END_ALLOW_THREADS
3202
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003203 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003204 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003205 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003206 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003207 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003208 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003209 }
3210 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003211 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003212
Bram Moolenaardb913952012-06-29 12:54:53 +02003213 clear_tv(&args);
3214 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003215 if (selfdict != NULL)
3216 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003217
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003218 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003219}
3220
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003221 static PyObject *
3222FunctionRepr(FunctionObject *self)
3223{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003224 PyObject *ret;
3225 garray_T repr_ga;
3226 int i;
3227 char_u *tofree = NULL;
3228 typval_T tv;
3229 char_u numbuf[NUMBUFLEN];
3230
3231 ga_init2(&repr_ga, (int)sizeof(char), 70);
3232 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3233 if (self->name)
3234 ga_concat(&repr_ga, self->name);
3235 else
3236 ga_concat(&repr_ga, (char_u *)"<NULL>");
3237 ga_append(&repr_ga, '\'');
3238 if (self->argv)
3239 {
3240 ga_concat(&repr_ga, (char_u *)", args=[");
3241 ++emsg_silent;
3242 for (i = 0; i < self->argc; i++)
3243 {
3244 if (i != 0)
3245 ga_concat(&repr_ga, (char_u *)", ");
3246 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3247 get_copyID()));
3248 vim_free(tofree);
3249 }
3250 --emsg_silent;
3251 ga_append(&repr_ga, ']');
3252 }
3253 if (self->self)
3254 {
3255 ga_concat(&repr_ga, (char_u *)", self=");
3256 tv.v_type = VAR_DICT;
3257 tv.vval.v_dict = self->self;
3258 ++emsg_silent;
3259 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3260 --emsg_silent;
3261 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003262 if (self->auto_rebind)
3263 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003264 }
3265 ga_append(&repr_ga, '>');
3266 ret = PyString_FromString((char *)repr_ga.ga_data);
3267 ga_clear(&repr_ga);
3268 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003269}
3270
Bram Moolenaardb913952012-06-29 12:54:53 +02003271static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003272 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3273 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003274};
3275
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003276/*
3277 * Options object
3278 */
3279
3280static PyTypeObject OptionsType;
3281
3282typedef int (*checkfun)(void *);
3283
3284typedef struct
3285{
3286 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003287 int opt_type;
3288 void *from;
3289 checkfun Check;
3290 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003291} OptionsObject;
3292
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003293 static int
3294dummy_check(void *arg UNUSED)
3295{
3296 return 0;
3297}
3298
3299 static PyObject *
3300OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3301{
3302 OptionsObject *self;
3303
Bram Moolenaar774267b2013-05-21 20:51:59 +02003304 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003305 if (self == NULL)
3306 return NULL;
3307
3308 self->opt_type = opt_type;
3309 self->from = from;
3310 self->Check = Check;
3311 self->fromObj = fromObj;
3312 if (fromObj)
3313 Py_INCREF(fromObj);
3314
3315 return (PyObject *)(self);
3316}
3317
3318 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003319OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003320{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003321 PyObject_GC_UnTrack((void *)(self));
3322 Py_XDECREF(self->fromObj);
3323 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003324}
3325
3326 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003327OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003328{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003329 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003330 return 0;
3331}
3332
3333 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003335{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003337 return 0;
3338}
3339
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003340 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003341OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003342{
3343 char_u *key;
3344 int flags;
3345 long numval;
3346 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003347 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003348
Bram Moolenaard6e39182013-05-21 18:30:34 +02003349 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003350 return NULL;
3351
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003352 if (!(key = StringToChars(keyObject, &todecref)))
3353 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003354
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003355 if (*key == NUL)
3356 {
3357 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003358 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003359 return NULL;
3360 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003361
3362 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003364
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003365 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003366
3367 if (flags == 0)
3368 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003369 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003370 return NULL;
3371 }
3372
3373 if (flags & SOPT_UNSET)
3374 {
3375 Py_INCREF(Py_None);
3376 return Py_None;
3377 }
3378 else if (flags & SOPT_BOOL)
3379 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003380 PyObject *ret;
3381 ret = numval ? Py_True : Py_False;
3382 Py_INCREF(ret);
3383 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003384 }
3385 else if (flags & SOPT_NUM)
3386 return PyInt_FromLong(numval);
3387 else if (flags & SOPT_STRING)
3388 {
3389 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003390 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003391 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003392 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003393 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003394 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003395 else
3396 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003397 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003398 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003399 return NULL;
3400 }
3401 }
3402 else
3403 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003404 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003405 return NULL;
3406 }
3407}
3408
3409 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003410OptionsContains(OptionsObject *self, PyObject *keyObject)
3411{
3412 char_u *key;
3413 PyObject *todecref;
3414
3415 if (!(key = StringToChars(keyObject, &todecref)))
3416 return -1;
3417
3418 if (*key == NUL)
3419 {
3420 Py_XDECREF(todecref);
3421 return 0;
3422 }
3423
3424 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3425 {
3426 Py_XDECREF(todecref);
3427 return 1;
3428 }
3429 else
3430 {
3431 Py_XDECREF(todecref);
3432 return 0;
3433 }
3434}
3435
3436typedef struct
3437{
3438 void *lastoption;
3439 int opt_type;
3440} optiterinfo_T;
3441
3442 static PyObject *
3443OptionsIterNext(optiterinfo_T **oii)
3444{
3445 char_u *name;
3446
3447 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3448 return PyString_FromString((char *)name);
3449
3450 return NULL;
3451}
3452
3453 static PyObject *
3454OptionsIter(OptionsObject *self)
3455{
3456 optiterinfo_T *oii;
3457
3458 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3459 {
3460 PyErr_NoMemory();
3461 return NULL;
3462 }
3463
3464 oii->opt_type = self->opt_type;
3465 oii->lastoption = NULL;
3466
3467 return IterNew(oii,
3468 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3469 NULL, NULL);
3470}
3471
3472 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003473set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003474{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003475 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003476
3477 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3478 {
3479 if (VimTryEnd())
3480 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003481 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003482 return FAIL;
3483 }
3484 return OK;
3485}
3486
3487 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003488set_option_value_for(
3489 char_u *key,
3490 int numval,
3491 char_u *stringval,
3492 int opt_flags,
3493 int opt_type,
3494 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003495{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003496 win_T *save_curwin = NULL;
3497 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003498 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003499 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003500
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003501 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003502 switch (opt_type)
3503 {
3504 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003505 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003506 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003507 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003508 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003509 if (VimTryEnd())
3510 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003511 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003512 return -1;
3513 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003514 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3515 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003516 break;
3517 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003518 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003519 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003520 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003521 break;
3522 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003523 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003524 break;
3525 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003526 if (set_ret == FAIL)
3527 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003528 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003529}
3530
3531 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003532OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003533{
3534 char_u *key;
3535 int flags;
3536 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003537 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003538 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003539
Bram Moolenaard6e39182013-05-21 18:30:34 +02003540 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003541 return -1;
3542
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003543 if (!(key = StringToChars(keyObject, &todecref)))
3544 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003545
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003546 if (*key == NUL)
3547 {
3548 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003549 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003550 return -1;
3551 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003552
3553 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003554 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003555
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003556 if (flags == 0)
3557 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003558 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003559 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003560 return -1;
3561 }
3562
3563 if (valObject == NULL)
3564 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003565 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003566 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003567 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003568 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003569 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003570 return -1;
3571 }
3572 else if (!(flags & SOPT_GLOBAL))
3573 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003574 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003575 N_("unable to unset option %s "
3576 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003577 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003578 return -1;
3579 }
3580 else
3581 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003582 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003583 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003584 return 0;
3585 }
3586 }
3587
Bram Moolenaard6e39182013-05-21 18:30:34 +02003588 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003589
3590 if (flags & SOPT_BOOL)
3591 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003592 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003593
Bram Moolenaarb983f752013-05-15 16:11:50 +02003594 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003595 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003596 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003597 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003598 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003599 }
3600 else if (flags & SOPT_NUM)
3601 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003602 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003603
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003604 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003605 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003606 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003607 return -1;
3608 }
3609
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003610 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003611 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003612 }
3613 else
3614 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003615 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003616 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003617
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003618 if ((val = StringToChars(valObject, &todecref2)))
3619 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003620 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003621 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003622 Py_XDECREF(todecref2);
3623 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003624 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003625 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003626 }
3627
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003628 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003629
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003630 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003631}
3632
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003633static PySequenceMethods OptionsAsSeq = {
3634 0, /* sq_length */
3635 0, /* sq_concat */
3636 0, /* sq_repeat */
3637 0, /* sq_item */
3638 0, /* sq_slice */
3639 0, /* sq_ass_item */
3640 0, /* sq_ass_slice */
3641 (objobjproc) OptionsContains, /* sq_contains */
3642 0, /* sq_inplace_concat */
3643 0, /* sq_inplace_repeat */
3644};
3645
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003646static PyMappingMethods OptionsAsMapping = {
3647 (lenfunc) NULL,
3648 (binaryfunc) OptionsItem,
3649 (objobjargproc) OptionsAssItem,
3650};
3651
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003652/* Tabpage object
3653 */
3654
3655typedef struct
3656{
3657 PyObject_HEAD
3658 tabpage_T *tab;
3659} TabPageObject;
3660
3661static PyObject *WinListNew(TabPageObject *tabObject);
3662
3663static PyTypeObject TabPageType;
3664
3665 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003666CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003667{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003668 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003669 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003670 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003671 return -1;
3672 }
3673
3674 return 0;
3675}
3676
3677 static PyObject *
3678TabPageNew(tabpage_T *tab)
3679{
3680 TabPageObject *self;
3681
3682 if (TAB_PYTHON_REF(tab))
3683 {
3684 self = TAB_PYTHON_REF(tab);
3685 Py_INCREF(self);
3686 }
3687 else
3688 {
3689 self = PyObject_NEW(TabPageObject, &TabPageType);
3690 if (self == NULL)
3691 return NULL;
3692 self->tab = tab;
3693 TAB_PYTHON_REF(tab) = self;
3694 }
3695
3696 return (PyObject *)(self);
3697}
3698
3699 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003700TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003701{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003702 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3703 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003704
3705 DESTRUCTOR_FINISH(self);
3706}
3707
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003708static char *TabPageAttrs[] = {
3709 "windows", "number", "vars", "window", "valid",
3710 NULL
3711};
3712
3713 static PyObject *
3714TabPageDir(PyObject *self)
3715{
3716 return ObjectDir(self, TabPageAttrs);
3717}
3718
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003719 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003720TabPageAttrValid(TabPageObject *self, char *name)
3721{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003722 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003723
3724 if (strcmp(name, "valid") != 0)
3725 return NULL;
3726
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003727 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3728 Py_INCREF(ret);
3729 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003730}
3731
3732 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003733TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003734{
3735 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003736 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003737 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003738 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003739 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003740 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003741 else if (strcmp(name, "window") == 0)
3742 {
3743 /* For current tab window.c does not bother to set or update tp_curwin
3744 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003745 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003746 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003747 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003748 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003749 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003750 else if (strcmp(name, "__members__") == 0)
3751 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003752 return NULL;
3753}
3754
3755 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003756TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003757{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003758 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003759 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003760 else
3761 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003762 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003763
3764 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003765 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3766 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003767 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003768 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003769 }
3770}
3771
3772static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003773 /* name, function, calling, documentation */
3774 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3775 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003776};
3777
3778/*
3779 * Window list object
3780 */
3781
3782static PyTypeObject TabListType;
3783static PySequenceMethods TabListAsSeq;
3784
3785typedef struct
3786{
3787 PyObject_HEAD
3788} TabListObject;
3789
3790 static PyInt
3791TabListLength(PyObject *self UNUSED)
3792{
3793 tabpage_T *tp = first_tabpage;
3794 PyInt n = 0;
3795
3796 while (tp != NULL)
3797 {
3798 ++n;
3799 tp = tp->tp_next;
3800 }
3801
3802 return n;
3803}
3804
3805 static PyObject *
3806TabListItem(PyObject *self UNUSED, PyInt n)
3807{
3808 tabpage_T *tp;
3809
3810 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3811 if (n == 0)
3812 return TabPageNew(tp);
3813
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003814 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003815 return NULL;
3816}
3817
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003818/*
3819 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003820 */
3821
3822typedef struct
3823{
3824 PyObject_HEAD
3825 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003826 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003827} WindowObject;
3828
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003829static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003830
3831 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003832CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003833{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003834 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003835 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003836 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003837 return -1;
3838 }
3839
3840 return 0;
3841}
3842
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003843 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003844WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003845{
3846 /* We need to handle deletion of windows underneath us.
3847 * If we add a "w_python*_ref" field to the win_T structure,
3848 * then we can get at it in win_free() in vim. We then
3849 * need to create only ONE Python object per window - if
3850 * we try to create a second, just INCREF the existing one
3851 * and return it. The (single) Python object referring to
3852 * the window is stored in "w_python*_ref".
3853 * On a win_free() we set the Python object's win_T* field
3854 * to an invalid value. We trap all uses of a window
3855 * object, and reject them if the win_T* field is invalid.
3856 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003857 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003858 * w_python_ref and w_python3_ref fields respectively.
3859 */
3860
3861 WindowObject *self;
3862
3863 if (WIN_PYTHON_REF(win))
3864 {
3865 self = WIN_PYTHON_REF(win);
3866 Py_INCREF(self);
3867 }
3868 else
3869 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003870 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003871 if (self == NULL)
3872 return NULL;
3873 self->win = win;
3874 WIN_PYTHON_REF(win) = self;
3875 }
3876
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003877 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3878
Bram Moolenaar971db462013-05-12 18:44:48 +02003879 return (PyObject *)(self);
3880}
3881
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003882 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003883WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003884{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003885 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003886 if (self->win && self->win != INVALID_WINDOW_VALUE)
3887 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003888 Py_XDECREF(((PyObject *)(self->tabObject)));
3889 PyObject_GC_Del((void *)(self));
3890}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003891
Bram Moolenaar774267b2013-05-21 20:51:59 +02003892 static int
3893WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3894{
3895 Py_VISIT(((PyObject *)(self->tabObject)));
3896 return 0;
3897}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003898
Bram Moolenaar774267b2013-05-21 20:51:59 +02003899 static int
3900WindowClear(WindowObject *self)
3901{
3902 Py_CLEAR(self->tabObject);
3903 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003904}
3905
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003906 static win_T *
3907get_firstwin(TabPageObject *tabObject)
3908{
3909 if (tabObject)
3910 {
3911 if (CheckTabPage(tabObject))
3912 return NULL;
3913 /* For current tab window.c does not bother to set or update tp_firstwin
3914 */
3915 else if (tabObject->tab == curtab)
3916 return firstwin;
3917 else
3918 return tabObject->tab->tp_firstwin;
3919 }
3920 else
3921 return firstwin;
3922}
Bram Moolenaare950f992018-06-10 13:55:55 +02003923
3924// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003925static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003926 "buffer",
3927 "cursor",
3928 "height",
3929 "row",
3930 "width",
3931 "col",
3932 "vars",
3933 "options",
3934 "number",
3935 "tabpage",
3936 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003937 NULL
3938};
3939
3940 static PyObject *
3941WindowDir(PyObject *self)
3942{
3943 return ObjectDir(self, WindowAttrs);
3944}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003945
Bram Moolenaar971db462013-05-12 18:44:48 +02003946 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003947WindowAttrValid(WindowObject *self, char *name)
3948{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003949 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003950
3951 if (strcmp(name, "valid") != 0)
3952 return NULL;
3953
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003954 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3955 Py_INCREF(ret);
3956 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003957}
3958
3959 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003960WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003961{
3962 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003963 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003964 else if (strcmp(name, "cursor") == 0)
3965 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003966 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003967
3968 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3969 }
3970 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003971 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003972 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003973 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003974 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003975 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003976 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003977 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003978 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003979 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003980 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003981 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3982 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003983 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003984 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003985 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003986 return NULL;
3987 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003989 }
3990 else if (strcmp(name, "tabpage") == 0)
3991 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 Py_INCREF(self->tabObject);
3993 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003994 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003995 else if (strcmp(name, "__members__") == 0)
3996 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003997 else
3998 return NULL;
3999}
4000
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004001 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004002WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004004 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004005 return -1;
4006
4007 if (strcmp(name, "buffer") == 0)
4008 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004009 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010 return -1;
4011 }
4012 else if (strcmp(name, "cursor") == 0)
4013 {
4014 long lnum;
4015 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004016
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004017 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018 return -1;
4019
Bram Moolenaard6e39182013-05-21 18:30:34 +02004020 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004021 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004022 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004023 return -1;
4024 }
4025
4026 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004027 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004028 return -1;
4029
Bram Moolenaard6e39182013-05-21 18:30:34 +02004030 self->win->w_cursor.lnum = lnum;
4031 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004032 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004033 self->win->w_cursor.coladd = 0;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004034 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004035 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004037 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 return 0;
4039 }
4040 else if (strcmp(name, "height") == 0)
4041 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004042 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043 win_T *savewin;
4044
Bram Moolenaardee2e312013-06-23 16:35:47 +02004045 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046 return -1;
4047
4048#ifdef FEAT_GUI
4049 need_mouse_correct = TRUE;
4050#endif
4051 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004052 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004053
4054 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004055 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004057 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004058 return -1;
4059
4060 return 0;
4061 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 else if (strcmp(name, "width") == 0)
4063 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004064 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 win_T *savewin;
4066
Bram Moolenaardee2e312013-06-23 16:35:47 +02004067 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 return -1;
4069
4070#ifdef FEAT_GUI
4071 need_mouse_correct = TRUE;
4072#endif
4073 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004074 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004075
4076 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004077 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004079 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004080 return -1;
4081
4082 return 0;
4083 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004084 else
4085 {
4086 PyErr_SetString(PyExc_AttributeError, name);
4087 return -1;
4088 }
4089}
4090
4091 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004092WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004094 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004095 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096 else
4097 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004098 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099
Bram Moolenaar6d216452013-05-12 19:00:41 +02004100 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004101 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102 (self));
4103 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004104 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105 }
4106}
4107
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004108static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004109 /* name, function, calling, documentation */
4110 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4111 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004112};
4113
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004115 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004117
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004118static PyTypeObject WinListType;
4119static PySequenceMethods WinListAsSeq;
4120
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004121typedef struct
4122{
4123 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004124 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004125} WinListObject;
4126
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004127 static PyObject *
4128WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004130 WinListObject *self;
4131
4132 self = PyObject_NEW(WinListObject, &WinListType);
4133 self->tabObject = tabObject;
4134 Py_INCREF(tabObject);
4135
4136 return (PyObject *)(self);
4137}
4138
4139 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004140WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004141{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004142 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004143
4144 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004145 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004146 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004147 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004148
4149 DESTRUCTOR_FINISH(self);
4150}
4151
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004152 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004153WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004154{
4155 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004156 PyInt n = 0;
4157
Bram Moolenaard6e39182013-05-21 18:30:34 +02004158 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004159 return -1;
4160
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004161 while (w != NULL)
4162 {
4163 ++n;
4164 w = W_NEXT(w);
4165 }
4166
4167 return n;
4168}
4169
4170 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004171WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004172{
4173 win_T *w;
4174
Bram Moolenaard6e39182013-05-21 18:30:34 +02004175 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004176 return NULL;
4177
4178 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004179 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004180 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004181
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004182 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004183 return NULL;
4184}
4185
4186/* Convert a Python string into a Vim line.
4187 *
4188 * The result is in allocated memory. All internal nulls are replaced by
4189 * newline characters. It is an error for the string to contain newline
4190 * characters.
4191 *
4192 * On errors, the Python exception data is set, and NULL is returned.
4193 */
4194 static char *
4195StringToLine(PyObject *obj)
4196{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004197 char *str;
4198 char *save;
4199 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004200 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004201 PyInt i;
4202 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004203
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004204 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004205 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004206 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4207 || str == NULL)
4208 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004209 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004210 else if (PyUnicode_Check(obj))
4211 {
4212 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4213 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214
Bram Moolenaardaa27022013-06-24 22:33:30 +02004215 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004216 || str == NULL)
4217 {
4218 Py_DECREF(bytes);
4219 return NULL;
4220 }
4221 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004222 else
4223 {
4224#if PY_MAJOR_VERSION < 3
4225 PyErr_FORMAT(PyExc_TypeError,
4226 N_("expected str() or unicode() instance, but got %s"),
4227 Py_TYPE_NAME(obj));
4228#else
4229 PyErr_FORMAT(PyExc_TypeError,
4230 N_("expected bytes() or str() instance, but got %s"),
4231 Py_TYPE_NAME(obj));
4232#endif
4233 return NULL;
4234 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004235
4236 /*
4237 * Error checking: String must not contain newlines, as we
4238 * are replacing a single line, and we must replace it with
4239 * a single line.
4240 * A trailing newline is removed, so that append(f.readlines()) works.
4241 */
4242 p = memchr(str, '\n', len);
4243 if (p != NULL)
4244 {
4245 if (p == str + len - 1)
4246 --len;
4247 else
4248 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004249 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004250 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004251 return NULL;
4252 }
4253 }
4254
4255 /* Create a copy of the string, with internal nulls replaced by
4256 * newline characters, as is the vim convention.
4257 */
4258 save = (char *)alloc((unsigned)(len+1));
4259 if (save == NULL)
4260 {
4261 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004262 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004263 return NULL;
4264 }
4265
4266 for (i = 0; i < len; ++i)
4267 {
4268 if (str[i] == '\0')
4269 save[i] = '\n';
4270 else
4271 save[i] = str[i];
4272 }
4273
4274 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004275 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004276
4277 return save;
4278}
4279
4280/* Get a line from the specified buffer. The line number is
4281 * in Vim format (1-based). The line is returned as a Python
4282 * string object.
4283 */
4284 static PyObject *
4285GetBufferLine(buf_T *buf, PyInt n)
4286{
4287 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4288}
4289
4290
4291/* Get a list of lines from the specified buffer. The line numbers
4292 * are in Vim format (1-based). The range is from lo up to, but not
4293 * including, hi. The list is returned as a Python list of string objects.
4294 */
4295 static PyObject *
4296GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4297{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004298 PyInt i;
4299 PyInt n = hi - lo;
4300 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004301
4302 if (list == NULL)
4303 return NULL;
4304
4305 for (i = 0; i < n; ++i)
4306 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004307 PyObject *string = LineToString(
4308 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004309
4310 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004311 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004312 {
4313 Py_DECREF(list);
4314 return NULL;
4315 }
4316
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004317 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004318 }
4319
4320 /* The ownership of the Python list is passed to the caller (ie,
4321 * the caller should Py_DECREF() the object when it is finished
4322 * with it).
4323 */
4324
4325 return list;
4326}
4327
4328/*
4329 * Check if deleting lines made the cursor position invalid.
4330 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4331 * deleted).
4332 */
4333 static void
4334py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4335{
4336 if (curwin->w_cursor.lnum >= lo)
4337 {
4338 /* Adjust the cursor position if it's in/after the changed
4339 * lines. */
4340 if (curwin->w_cursor.lnum >= hi)
4341 {
4342 curwin->w_cursor.lnum += extra;
4343 check_cursor_col();
4344 }
4345 else if (extra < 0)
4346 {
4347 curwin->w_cursor.lnum = lo;
4348 check_cursor();
4349 }
4350 else
4351 check_cursor_col();
4352 changed_cline_bef_curs();
4353 }
4354 invalidate_botline();
4355}
4356
Bram Moolenaar19e60942011-06-19 00:27:51 +02004357/*
4358 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004359 * in Vim format (1-based). The replacement line is given as
4360 * a Python string object. The object is checked for validity
4361 * and correct format. Errors are returned as a value of FAIL.
4362 * The return value is OK on success.
4363 * If OK is returned and len_change is not NULL, *len_change
4364 * is set to the change in the buffer length.
4365 */
4366 static int
4367SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4368{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004369 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004370 win_T *save_curwin = NULL;
4371 tabpage_T *save_curtab = NULL;
4372
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004373 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004374 * There are three cases:
4375 * 1. NULL, or None - this is a deletion.
4376 * 2. A string - this is a replacement.
4377 * 3. Anything else - this is an error.
4378 */
4379 if (line == Py_None || line == NULL)
4380 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004381 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004382 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004384 VimTryStart();
4385
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004386 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004387 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004388 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004389 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 else
4391 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004392 if (buf == curbuf && (save_curwin != NULL
4393 || save_curbuf.br_buf == NULL))
4394 // Using an existing window for the buffer, adjust the cursor
4395 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004397 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004398 /* Only adjust marks if we managed to switch to a window that
4399 * holds the buffer, otherwise line numbers will be invalid. */
4400 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 }
4402
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004403 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004404
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004405 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406 return FAIL;
4407
4408 if (len_change)
4409 *len_change = -1;
4410
4411 return OK;
4412 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004413 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004414 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004415 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416
4417 if (save == NULL)
4418 return FAIL;
4419
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004420 VimTryStart();
4421
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422 /* We do not need to free "save" if ml_replace() consumes it. */
4423 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004424 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425
4426 if (u_savesub((linenr_T)n) == FAIL)
4427 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004428 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 vim_free(save);
4430 }
4431 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4432 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004433 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434 vim_free(save);
4435 }
4436 else
4437 changed_bytes((linenr_T)n, 0);
4438
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004439 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440
4441 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004442 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 check_cursor_col();
4444
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004445 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446 return FAIL;
4447
4448 if (len_change)
4449 *len_change = 0;
4450
4451 return OK;
4452 }
4453 else
4454 {
4455 PyErr_BadArgument();
4456 return FAIL;
4457 }
4458}
4459
Bram Moolenaar19e60942011-06-19 00:27:51 +02004460/* Replace a range of lines in the specified buffer. The line numbers are in
4461 * Vim format (1-based). The range is from lo up to, but not including, hi.
4462 * The replacement lines are given as a Python list of string objects. The
4463 * list is checked for validity and correct format. Errors are returned as a
4464 * value of FAIL. The return value is OK on success.
4465 * If OK is returned and len_change is not NULL, *len_change
4466 * is set to the change in the buffer length.
4467 */
4468 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004469SetBufferLineList(
4470 buf_T *buf,
4471 PyInt lo,
4472 PyInt hi,
4473 PyObject *list,
4474 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004475{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004476 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004477 win_T *save_curwin = NULL;
4478 tabpage_T *save_curtab = NULL;
4479
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004480 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004481 * There are three cases:
4482 * 1. NULL, or None - this is a deletion.
4483 * 2. A list - this is a replacement.
4484 * 3. Anything else - this is an error.
4485 */
4486 if (list == Py_None || list == NULL)
4487 {
4488 PyInt i;
4489 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004490
4491 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004492 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004493 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004494
4495 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004496 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004497 else
4498 {
4499 for (i = 0; i < n; ++i)
4500 {
4501 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4502 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004503 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504 break;
4505 }
4506 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004507 if (buf == curbuf && (save_curwin != NULL
4508 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004509 /* Using an existing window for the buffer, adjust the cursor
4510 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004511 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004512 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004513 /* Only adjust marks if we managed to switch to a window that
4514 * holds the buffer, otherwise line numbers will be invalid. */
4515 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004516 }
4517
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004518 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004519
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004520 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004521 return FAIL;
4522
4523 if (len_change)
4524 *len_change = -n;
4525
4526 return OK;
4527 }
4528 else if (PyList_Check(list))
4529 {
4530 PyInt i;
4531 PyInt new_len = PyList_Size(list);
4532 PyInt old_len = hi - lo;
4533 PyInt extra = 0; /* lines added to text, can be negative */
4534 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004535
4536 if (new_len == 0) /* avoid allocating zero bytes */
4537 array = NULL;
4538 else
4539 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004540 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541 if (array == NULL)
4542 {
4543 PyErr_NoMemory();
4544 return FAIL;
4545 }
4546 }
4547
4548 for (i = 0; i < new_len; ++i)
4549 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004550 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004552 if (!(line = PyList_GetItem(list, i)) ||
4553 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004554 {
4555 while (i)
4556 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004557 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558 return FAIL;
4559 }
4560 }
4561
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004562 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004563 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004564
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004565 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004566 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004567
4568 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004569 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004570
4571 /* If the size of the range is reducing (ie, new_len < old_len) we
4572 * need to delete some old_len. We do this at the start, by
4573 * repeatedly deleting line "lo".
4574 */
4575 if (!PyErr_Occurred())
4576 {
4577 for (i = 0; i < old_len - new_len; ++i)
4578 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4579 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004580 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004581 break;
4582 }
4583 extra -= i;
4584 }
4585
4586 /* For as long as possible, replace the existing old_len with the
4587 * new old_len. This is a more efficient operation, as it requires
4588 * less memory allocation and freeing.
4589 */
4590 if (!PyErr_Occurred())
4591 {
4592 for (i = 0; i < old_len && i < new_len; ++i)
4593 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4594 == FAIL)
4595 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004596 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597 break;
4598 }
4599 }
4600 else
4601 i = 0;
4602
4603 /* Now we may need to insert the remaining new old_len. If we do, we
4604 * must free the strings as we finish with them (we can't pass the
4605 * responsibility to vim in this case).
4606 */
4607 if (!PyErr_Occurred())
4608 {
4609 while (i < new_len)
4610 {
4611 if (ml_append((linenr_T)(lo + i - 1),
4612 (char_u *)array[i], 0, FALSE) == FAIL)
4613 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004614 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004615 break;
4616 }
4617 vim_free(array[i]);
4618 ++i;
4619 ++extra;
4620 }
4621 }
4622
4623 /* Free any left-over old_len, as a result of an error */
4624 while (i < new_len)
4625 {
4626 vim_free(array[i]);
4627 ++i;
4628 }
4629
4630 /* Free the array of old_len. All of its contents have now
4631 * been dealt with (either freed, or the responsibility passed
4632 * to vim.
4633 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004634 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004635
4636 /* Adjust marks. Invalidate any which lie in the
4637 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004638 * Only adjust marks if we managed to switch to a window that holds
4639 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004640 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004641 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004642 (long)MAXLNUM, (long)extra);
4643 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4644
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004645 if (buf == curbuf && (save_curwin != NULL
4646 || save_curbuf.br_buf == NULL))
4647 // Using an existing window for the buffer, adjust the cursor
4648 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004649 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4650
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004651 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004652 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004653
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004654 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004655 return FAIL;
4656
4657 if (len_change)
4658 *len_change = new_len - old_len;
4659
4660 return OK;
4661 }
4662 else
4663 {
4664 PyErr_BadArgument();
4665 return FAIL;
4666 }
4667}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004668
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004669/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004670 * The line number is in Vim format (1-based). The lines to be inserted are
4671 * given as a Python list of string objects or as a single string. The lines
4672 * to be added are checked for validity and correct format. Errors are
4673 * returned as a value of FAIL. The return value is OK on success.
4674 * If OK is returned and len_change is not NULL, *len_change
4675 * is set to the change in the buffer length.
4676 */
4677 static int
4678InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4679{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004680 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004681 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004682 tabpage_T *save_curtab = NULL;
4683
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004684 /* First of all, we check the type of the supplied Python object.
4685 * It must be a string or a list, or the call is in error.
4686 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004687 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004688 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004689 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004690
4691 if (str == NULL)
4692 return FAIL;
4693
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004695 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004696 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004698 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004699 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004700 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004701 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004702 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004703 /* Only adjust marks if we managed to switch to a window that
4704 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004705 appended_lines_mark((linenr_T)n, 1L);
4706
4707 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004708 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004709 update_screen(VALID);
4710
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004711 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 return FAIL;
4713
4714 if (len_change)
4715 *len_change = 1;
4716
4717 return OK;
4718 }
4719 else if (PyList_Check(lines))
4720 {
4721 PyInt i;
4722 PyInt size = PyList_Size(lines);
4723 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004724
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004725 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004726 if (array == NULL)
4727 {
4728 PyErr_NoMemory();
4729 return FAIL;
4730 }
4731
4732 for (i = 0; i < size; ++i)
4733 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004734 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004736 if (!(line = PyList_GetItem(lines, i)) ||
4737 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738 {
4739 while (i)
4740 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004741 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742 return FAIL;
4743 }
4744 }
4745
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004746 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004747 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004748 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749
4750 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004751 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752 else
4753 {
4754 for (i = 0; i < size; ++i)
4755 {
4756 if (ml_append((linenr_T)(n + i),
4757 (char_u *)array[i], 0, FALSE) == FAIL)
4758 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004759 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004760
4761 /* Free the rest of the lines */
4762 while (i < size)
4763 vim_free(array[i++]);
4764
4765 break;
4766 }
4767 vim_free(array[i]);
4768 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004769 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004770 /* Only adjust marks if we managed to switch to a window that
4771 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004772 appended_lines_mark((linenr_T)n, (long)i);
4773 }
4774
4775 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004776 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004777 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004778 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004780 update_screen(VALID);
4781
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004782 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 return FAIL;
4784
4785 if (len_change)
4786 *len_change = size;
4787
4788 return OK;
4789 }
4790 else
4791 {
4792 PyErr_BadArgument();
4793 return FAIL;
4794 }
4795}
4796
4797/*
4798 * Common routines for buffers and line ranges
4799 * -------------------------------------------
4800 */
4801
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004802typedef struct
4803{
4804 PyObject_HEAD
4805 buf_T *buf;
4806} BufferObject;
4807
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004808 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004809CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004810{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004811 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004812 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004813 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004814 return -1;
4815 }
4816
4817 return 0;
4818}
4819
4820 static PyObject *
4821RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4822{
4823 if (CheckBuffer(self))
4824 return NULL;
4825
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004826 if (end == -1)
4827 end = self->buf->b_ml.ml_line_count;
4828
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004829 if (n < 0)
4830 n += end - start + 1;
4831
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004832 if (n < 0 || n > end - start)
4833 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004834 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004835 return NULL;
4836 }
4837
4838 return GetBufferLine(self->buf, n+start);
4839}
4840
4841 static PyObject *
4842RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4843{
4844 PyInt size;
4845
4846 if (CheckBuffer(self))
4847 return NULL;
4848
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004849 if (end == -1)
4850 end = self->buf->b_ml.ml_line_count;
4851
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004852 size = end - start + 1;
4853
4854 if (lo < 0)
4855 lo = 0;
4856 else if (lo > size)
4857 lo = size;
4858 if (hi < 0)
4859 hi = 0;
4860 if (hi < lo)
4861 hi = lo;
4862 else if (hi > size)
4863 hi = size;
4864
4865 return GetBufferLineList(self->buf, lo+start, hi+start);
4866}
4867
4868 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004869RBAsItem(
4870 BufferObject *self,
4871 PyInt n,
4872 PyObject *valObject,
4873 PyInt start,
4874 PyInt end,
4875 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004876{
4877 PyInt len_change;
4878
4879 if (CheckBuffer(self))
4880 return -1;
4881
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004882 if (end == -1)
4883 end = self->buf->b_ml.ml_line_count;
4884
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004885 if (n < 0)
4886 n += end - start + 1;
4887
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004888 if (n < 0 || n > end - start)
4889 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004890 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004891 return -1;
4892 }
4893
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004894 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004895 return -1;
4896
4897 if (new_end)
4898 *new_end = end + len_change;
4899
4900 return 0;
4901}
4902
Bram Moolenaar19e60942011-06-19 00:27:51 +02004903 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004904RBAsSlice(
4905 BufferObject *self,
4906 PyInt lo,
4907 PyInt hi,
4908 PyObject *valObject,
4909 PyInt start,
4910 PyInt end,
4911 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004912{
4913 PyInt size;
4914 PyInt len_change;
4915
4916 /* Self must be a valid buffer */
4917 if (CheckBuffer(self))
4918 return -1;
4919
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004920 if (end == -1)
4921 end = self->buf->b_ml.ml_line_count;
4922
Bram Moolenaar19e60942011-06-19 00:27:51 +02004923 /* Sort out the slice range */
4924 size = end - start + 1;
4925
4926 if (lo < 0)
4927 lo = 0;
4928 else if (lo > size)
4929 lo = size;
4930 if (hi < 0)
4931 hi = 0;
4932 if (hi < lo)
4933 hi = lo;
4934 else if (hi > size)
4935 hi = size;
4936
4937 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004938 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004939 return -1;
4940
4941 if (new_end)
4942 *new_end = end + len_change;
4943
4944 return 0;
4945}
4946
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004947
4948 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004949RBAppend(
4950 BufferObject *self,
4951 PyObject *args,
4952 PyInt start,
4953 PyInt end,
4954 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004955{
4956 PyObject *lines;
4957 PyInt len_change;
4958 PyInt max;
4959 PyInt n;
4960
4961 if (CheckBuffer(self))
4962 return NULL;
4963
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004964 if (end == -1)
4965 end = self->buf->b_ml.ml_line_count;
4966
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004967 max = n = end - start + 1;
4968
4969 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4970 return NULL;
4971
4972 if (n < 0 || n > max)
4973 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004974 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004975 return NULL;
4976 }
4977
4978 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4979 return NULL;
4980
4981 if (new_end)
4982 *new_end = end + len_change;
4983
4984 Py_INCREF(Py_None);
4985 return Py_None;
4986}
4987
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004988/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004989 */
4990
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004991static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004992static PySequenceMethods RangeAsSeq;
4993static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004994
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004995typedef struct
4996{
4997 PyObject_HEAD
4998 BufferObject *buf;
4999 PyInt start;
5000 PyInt end;
5001} RangeObject;
5002
5003 static PyObject *
5004RangeNew(buf_T *buf, PyInt start, PyInt end)
5005{
5006 BufferObject *bufr;
5007 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005008 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005009 if (self == NULL)
5010 return NULL;
5011
5012 bufr = (BufferObject *)BufferNew(buf);
5013 if (bufr == NULL)
5014 {
5015 Py_DECREF(self);
5016 return NULL;
5017 }
5018 Py_INCREF(bufr);
5019
5020 self->buf = bufr;
5021 self->start = start;
5022 self->end = end;
5023
5024 return (PyObject *)(self);
5025}
5026
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005027 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005028RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005029{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005030 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005031 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005032 PyObject_GC_Del((void *)(self));
5033}
5034
5035 static int
5036RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5037{
5038 Py_VISIT(((PyObject *)(self->buf)));
5039 return 0;
5040}
5041
5042 static int
5043RangeClear(RangeObject *self)
5044{
5045 Py_CLEAR(self->buf);
5046 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005047}
5048
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005049 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005050RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005051{
5052 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005053 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005054 return -1; /* ??? */
5055
Bram Moolenaard6e39182013-05-21 18:30:34 +02005056 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005057}
5058
5059 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005061{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005062 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005063}
5064
5065 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005067{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005068 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005069}
5070
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005071static char *RangeAttrs[] = {
5072 "start", "end",
5073 NULL
5074};
5075
5076 static PyObject *
5077RangeDir(PyObject *self)
5078{
5079 return ObjectDir(self, RangeAttrs);
5080}
5081
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005082 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005083RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005084{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005085 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086}
5087
5088 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005090{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005091 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005092 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5093 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094 else
5095 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097
5098 if (name == NULL)
5099 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005101 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005102 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103 }
5104}
5105
5106static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005107 /* name, function, calling, documentation */
5108 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005109 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5110 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005111};
5112
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005113static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005114static PySequenceMethods BufferAsSeq;
5115static PyMappingMethods BufferAsMapping;
5116
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005117 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005118BufferNew(buf_T *buf)
5119{
5120 /* We need to handle deletion of buffers underneath us.
5121 * If we add a "b_python*_ref" field to the buf_T structure,
5122 * then we can get at it in buf_freeall() in vim. We then
5123 * need to create only ONE Python object per buffer - if
5124 * we try to create a second, just INCREF the existing one
5125 * and return it. The (single) Python object referring to
5126 * the buffer is stored in "b_python*_ref".
5127 * Question: what to do on a buf_freeall(). We'll probably
5128 * have to either delete the Python object (DECREF it to
5129 * zero - a bad idea, as it leaves dangling refs!) or
5130 * set the buf_T * value to an invalid value (-1?), which
5131 * means we need checks in all access functions... Bah.
5132 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005133 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005134 * b_python_ref and b_python3_ref fields respectively.
5135 */
5136
5137 BufferObject *self;
5138
5139 if (BUF_PYTHON_REF(buf) != NULL)
5140 {
5141 self = BUF_PYTHON_REF(buf);
5142 Py_INCREF(self);
5143 }
5144 else
5145 {
5146 self = PyObject_NEW(BufferObject, &BufferType);
5147 if (self == NULL)
5148 return NULL;
5149 self->buf = buf;
5150 BUF_PYTHON_REF(buf) = self;
5151 }
5152
5153 return (PyObject *)(self);
5154}
5155
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005156 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005157BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005158{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005159 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5160 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005161
5162 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005163}
5164
Bram Moolenaar971db462013-05-12 18:44:48 +02005165 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005166BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005167{
5168 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005170 return -1; /* ??? */
5171
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005173}
5174
5175 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005176BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005177{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005178 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005179}
5180
5181 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005182BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005183{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005184 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005185}
5186
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005187static char *BufferAttrs[] = {
5188 "name", "number", "vars", "options", "valid",
5189 NULL
5190};
5191
5192 static PyObject *
5193BufferDir(PyObject *self)
5194{
5195 return ObjectDir(self, BufferAttrs);
5196}
5197
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005198 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005199BufferAttrValid(BufferObject *self, char *name)
5200{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005201 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005202
5203 if (strcmp(name, "valid") != 0)
5204 return NULL;
5205
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005206 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5207 Py_INCREF(ret);
5208 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005209}
5210
5211 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005212BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005213{
5214 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005215 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005216 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005217 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005218 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005219 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005220 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005221 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005222 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5223 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005224 else if (strcmp(name, "__members__") == 0)
5225 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005226 else
5227 return NULL;
5228}
5229
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005230 static int
5231BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5232{
5233 if (CheckBuffer(self))
5234 return -1;
5235
5236 if (strcmp(name, "name") == 0)
5237 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005238 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005239 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005240 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005241 PyObject *todecref;
5242
5243 if (!(val = StringToChars(valObject, &todecref)))
5244 return -1;
5245
5246 VimTryStart();
5247 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5248 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005249 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005250 aucmd_restbuf(&aco);
5251 Py_XDECREF(todecref);
5252 if (VimTryEnd())
5253 return -1;
5254
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005255 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005256 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005257 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005258 return -1;
5259 }
5260 return 0;
5261 }
5262 else
5263 {
5264 PyErr_SetString(PyExc_AttributeError, name);
5265 return -1;
5266 }
5267}
5268
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005269 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005270BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005271{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005272 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005273}
5274
5275 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005276BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005277{
5278 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005279 char_u *pmark;
5280 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005281 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005282 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005283
Bram Moolenaard6e39182013-05-21 18:30:34 +02005284 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005285 return NULL;
5286
Bram Moolenaar389a1792013-06-23 13:00:44 +02005287 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005288 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005289
Bram Moolenaar389a1792013-06-23 13:00:44 +02005290 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005291 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005292 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005293 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005294 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005295 return NULL;
5296 }
5297
5298 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005299
5300 Py_XDECREF(todecref);
5301
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005302 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005303 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005304 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005305 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005306 if (VimTryEnd())
5307 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005308
5309 if (posp == NULL)
5310 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005311 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005312 return NULL;
5313 }
5314
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315 if (posp->lnum <= 0)
5316 {
5317 /* Or raise an error? */
5318 Py_INCREF(Py_None);
5319 return Py_None;
5320 }
5321
5322 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5323}
5324
5325 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005326BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005327{
5328 PyInt start;
5329 PyInt end;
5330
Bram Moolenaard6e39182013-05-21 18:30:34 +02005331 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005332 return NULL;
5333
5334 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5335 return NULL;
5336
Bram Moolenaard6e39182013-05-21 18:30:34 +02005337 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005338}
5339
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005340 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005341BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005342{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005343 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005344 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005345 else
5346 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005347 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005348
5349 if (name == NULL)
5350 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005351
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005352 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005353 }
5354}
5355
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005356static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005357 /* name, function, calling, documentation */
5358 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005359 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005360 {"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 +02005361 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5362 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005363};
5364
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005365/*
5366 * Buffer list object - Implementation
5367 */
5368
5369static PyTypeObject BufMapType;
5370
5371typedef struct
5372{
5373 PyObject_HEAD
5374} BufMapObject;
5375
5376 static PyInt
5377BufMapLength(PyObject *self UNUSED)
5378{
5379 buf_T *b = firstbuf;
5380 PyInt n = 0;
5381
5382 while (b)
5383 {
5384 ++n;
5385 b = b->b_next;
5386 }
5387
5388 return n;
5389}
5390
5391 static PyObject *
5392BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5393{
5394 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005395 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005396
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005397 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005398 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005399
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005400 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005401
5402 if (b)
5403 return BufferNew(b);
5404 else
5405 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005406 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005407 return NULL;
5408 }
5409}
5410
5411 static void
5412BufMapIterDestruct(PyObject *buffer)
5413{
5414 /* Iteration was stopped before all buffers were processed */
5415 if (buffer)
5416 {
5417 Py_DECREF(buffer);
5418 }
5419}
5420
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005421 static int
5422BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5423{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005424 if (buffer)
5425 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005426 return 0;
5427}
5428
5429 static int
5430BufMapIterClear(PyObject **buffer)
5431{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005432 if (*buffer)
5433 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005434 return 0;
5435}
5436
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005437 static PyObject *
5438BufMapIterNext(PyObject **buffer)
5439{
5440 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005441 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005442
5443 if (!*buffer)
5444 return NULL;
5445
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005446 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005447
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005448 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005449 {
5450 *buffer = NULL;
5451 return NULL;
5452 }
5453
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005454 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005455 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005456 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005457 return NULL;
5458 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005459 /* Do not increment reference: we no longer hold it (decref), but whoever
5460 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005461 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005462}
5463
5464 static PyObject *
5465BufMapIter(PyObject *self UNUSED)
5466{
5467 PyObject *buffer;
5468
5469 buffer = BufferNew(firstbuf);
5470 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005471 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5472 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005473}
5474
5475static PyMappingMethods BufMapAsMapping = {
5476 (lenfunc) BufMapLength,
5477 (binaryfunc) BufMapItem,
5478 (objobjargproc) 0,
5479};
5480
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005481/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005482 */
5483
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005484static char *CurrentAttrs[] = {
5485 "buffer", "window", "line", "range", "tabpage",
5486 NULL
5487};
5488
5489 static PyObject *
5490CurrentDir(PyObject *self)
5491{
5492 return ObjectDir(self, CurrentAttrs);
5493}
5494
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005495 static PyObject *
5496CurrentGetattr(PyObject *self UNUSED, char *name)
5497{
5498 if (strcmp(name, "buffer") == 0)
5499 return (PyObject *)BufferNew(curbuf);
5500 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005501 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005502 else if (strcmp(name, "tabpage") == 0)
5503 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005504 else if (strcmp(name, "line") == 0)
5505 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5506 else if (strcmp(name, "range") == 0)
5507 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005508 else if (strcmp(name, "__members__") == 0)
5509 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005510 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005511#if PY_MAJOR_VERSION < 3
5512 return Py_FindMethod(WindowMethods, self, name);
5513#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005514 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005515#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005516}
5517
5518 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005519CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005520{
5521 if (strcmp(name, "line") == 0)
5522 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005523 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5524 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005525 return -1;
5526
5527 return 0;
5528 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005529 else if (strcmp(name, "buffer") == 0)
5530 {
5531 int count;
5532
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005533 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005534 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005535 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005536 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005537 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005538 return -1;
5539 }
5540
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005541 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005542 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005543 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005544
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005545 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005546 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5547 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005548 if (VimTryEnd())
5549 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005550 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005551 return -1;
5552 }
5553
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005554 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005555 }
5556 else if (strcmp(name, "window") == 0)
5557 {
5558 int count;
5559
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005560 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005561 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005562 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005563 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005564 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005565 return -1;
5566 }
5567
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005568 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005569 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005570 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005571
5572 if (!count)
5573 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005574 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005575 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005576 return -1;
5577 }
5578
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005579 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005580 win_goto(((WindowObject *)(valObject))->win);
5581 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005583 if (VimTryEnd())
5584 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005585 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005586 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005587 return -1;
5588 }
5589
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005590 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005591 }
5592 else if (strcmp(name, "tabpage") == 0)
5593 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005594 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005595 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005596 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005597 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005598 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005599 return -1;
5600 }
5601
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005602 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005603 return -1;
5604
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005605 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005606 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5607 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005608 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005609 if (VimTryEnd())
5610 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005611 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005612 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005613 return -1;
5614 }
5615
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005616 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005617 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005618 else
5619 {
5620 PyErr_SetString(PyExc_AttributeError, name);
5621 return -1;
5622 }
5623}
5624
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005625static struct PyMethodDef CurrentMethods[] = {
5626 /* name, function, calling, documentation */
5627 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5628 { NULL, NULL, 0, NULL}
5629};
5630
Bram Moolenaardb913952012-06-29 12:54:53 +02005631 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005632init_range_cmd(exarg_T *eap)
5633{
5634 RangeStart = eap->line1;
5635 RangeEnd = eap->line2;
5636}
5637
5638 static void
5639init_range_eval(typval_T *rettv UNUSED)
5640{
5641 RangeStart = (PyInt) curwin->w_cursor.lnum;
5642 RangeEnd = RangeStart;
5643}
5644
5645 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005646run_cmd(const char *cmd, void *arg UNUSED
5647#ifdef PY_CAN_RECURSE
5648 , PyGILState_STATE *pygilstate UNUSED
5649#endif
5650 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005651{
Bram Moolenaar41009372013-07-01 22:03:04 +02005652 PyObject *run_ret;
5653 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5654 if (run_ret != NULL)
5655 {
5656 Py_DECREF(run_ret);
5657 }
5658 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005660 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005661 PyErr_Clear();
5662 }
5663 else
5664 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005665}
5666
5667static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5668static int code_hdr_len = 30;
5669
5670 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005671run_do(const char *cmd, void *arg UNUSED
5672#ifdef PY_CAN_RECURSE
5673 , PyGILState_STATE *pygilstate
5674#endif
5675 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005676{
5677 PyInt lnum;
5678 size_t len;
5679 char *code;
5680 int status;
5681 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005682 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005683 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005684
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005685 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005687 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005688 return;
5689 }
5690
5691 len = code_hdr_len + STRLEN(cmd);
5692 code = PyMem_New(char, len + 1);
5693 memcpy(code, code_hdr, code_hdr_len);
5694 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005695 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5696 status = -1;
5697 if (run_ret != NULL)
5698 {
5699 status = 0;
5700 Py_DECREF(run_ret);
5701 }
5702 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5703 {
5704 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005705 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005706 PyErr_Clear();
5707 return;
5708 }
5709 else
5710 PyErr_PrintEx(1);
5711
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005712 PyMem_Free(code);
5713
5714 if (status)
5715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005716 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005717 return;
5718 }
5719
5720 status = 0;
5721 pymain = PyImport_AddModule("__main__");
5722 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005723#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005725#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005726
5727 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5728 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005729 PyObject *line;
5730 PyObject *linenr;
5731 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005732
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005733#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005734 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005735#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005736 /* Check the line number, the command my have deleted lines. */
5737 if (lnum > curbuf->b_ml.ml_line_count
5738 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005739 goto err;
5740 if (!(linenr = PyInt_FromLong((long) lnum)))
5741 {
5742 Py_DECREF(line);
5743 goto err;
5744 }
5745 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5746 Py_DECREF(line);
5747 Py_DECREF(linenr);
5748 if (!ret)
5749 goto err;
5750
Bram Moolenaara58883b2017-01-29 21:31:09 +01005751 /* Check that the command didn't switch to another buffer. */
5752 if (curbuf != was_curbuf)
5753 {
5754 Py_XDECREF(ret);
5755 goto err;
5756 }
5757
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005758 if (ret != Py_None)
5759 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005760 {
5761 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005762 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005763 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764
5765 Py_XDECREF(ret);
5766 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005767#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005768 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005769#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005770 }
5771 goto out;
5772err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005773#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005774 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005775#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005776 PyErr_PrintEx(0);
5777 PythonIO_Flush();
5778 status = 1;
5779out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005780#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781 if (!status)
5782 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005783#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005784 Py_DECREF(pyfunc);
5785 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5786 if (status)
5787 return;
5788 check_cursor();
5789 update_curbuf(NOT_VALID);
5790}
5791
5792 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005793run_eval(const char *cmd, typval_T *rettv
5794#ifdef PY_CAN_RECURSE
5795 , PyGILState_STATE *pygilstate UNUSED
5796#endif
5797 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005798{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005799 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005800
Bram Moolenaar41009372013-07-01 22:03:04 +02005801 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005802 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005803 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005804 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005805 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005806 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005807 PyErr_Clear();
5808 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005809 else
5810 {
5811 if (PyErr_Occurred() && !msg_silent)
5812 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005813 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005814 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005815 }
5816 else
5817 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005818 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005819 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005820 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005821 }
5822 PyErr_Clear();
5823}
5824
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005825 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005826set_ref_in_py(const int copyID)
5827{
5828 pylinkedlist_T *cur;
5829 dict_T *dd;
5830 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005831 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005832 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005833 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005834
5835 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005836 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005837 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005838 {
5839 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5840 if (dd->dv_copyID != copyID)
5841 {
5842 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005843 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005844 }
5845 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005847
5848 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005849 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005850 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005851 {
5852 ll = ((ListObject *) (cur->pll_obj))->list;
5853 if (ll->lv_copyID != copyID)
5854 {
5855 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005857 }
5858 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005859 }
5860
Bram Moolenaar8110a092016-04-14 15:56:09 +02005861 if (lastfunc != NULL)
5862 {
5863 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5864 {
5865 func = (FunctionObject *) cur->pll_obj;
5866 if (func->self != NULL && func->self->dv_copyID != copyID)
5867 {
5868 func->self->dv_copyID = copyID;
5869 abort = abort || set_ref_in_ht(
5870 &func->self->dv_hashtab, copyID, NULL);
5871 }
5872 if (func->argc)
5873 for (i = 0; !abort && i < func->argc; ++i)
5874 abort = abort
5875 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5876 }
5877 }
5878
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005879 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005880}
5881
5882 static int
5883set_string_copy(char_u *str, typval_T *tv)
5884{
5885 tv->vval.v_string = vim_strsave(str);
5886 if (tv->vval.v_string == NULL)
5887 {
5888 PyErr_NoMemory();
5889 return -1;
5890 }
5891 return 0;
5892}
5893
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005894 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005895pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005896{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005897 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005898 char_u *key;
5899 dictitem_T *di;
5900 PyObject *keyObject;
5901 PyObject *valObject;
5902 Py_ssize_t iter = 0;
5903
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005904 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005905 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005906
5907 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005908 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005909
5910 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5911 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005912 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005913
Bram Moolenaara03e6312013-05-29 22:49:26 +02005914 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005915 {
5916 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005917 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005918 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005919
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005920 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005921 {
5922 dict_unref(dict);
5923 return -1;
5924 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005925
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005926 if (*key == NUL)
5927 {
5928 dict_unref(dict);
5929 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005930 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005931 return -1;
5932 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005933
5934 di = dictitem_alloc(key);
5935
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005936 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937
5938 if (di == NULL)
5939 {
5940 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005941 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005942 return -1;
5943 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005944
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005945 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946 {
5947 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005948 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005949 return -1;
5950 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005951
5952 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005954 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005955 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005957 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005958 return -1;
5959 }
5960 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005961
5962 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 return 0;
5964}
5965
5966 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005967pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005969 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 char_u *key;
5971 dictitem_T *di;
5972 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005973 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005974 PyObject *keyObject;
5975 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005977 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005978 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005979
5980 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005981 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005982
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005983 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005984 {
5985 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005986 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005987 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005988
5989 if (!(iterator = PyObject_GetIter(list)))
5990 {
5991 dict_unref(dict);
5992 Py_DECREF(list);
5993 return -1;
5994 }
5995 Py_DECREF(list);
5996
5997 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005999 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006000
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006001 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006002 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006003 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006004 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006005 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006006 return -1;
6007 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006008
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006009 if (*key == NUL)
6010 {
6011 Py_DECREF(keyObject);
6012 Py_DECREF(iterator);
6013 Py_XDECREF(todecref);
6014 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006015 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006016 return -1;
6017 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006018
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006019 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006020 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006021 Py_DECREF(keyObject);
6022 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006024 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006025 return -1;
6026 }
6027
6028 di = dictitem_alloc(key);
6029
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006030 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006031 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006032
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006033 if (di == NULL)
6034 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006035 Py_DECREF(iterator);
6036 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006037 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006038 PyErr_NoMemory();
6039 return -1;
6040 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006041
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006042 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006043 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006044 Py_DECREF(iterator);
6045 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006046 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006047 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006048 return -1;
6049 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006050
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006051 Py_DECREF(valObject);
6052
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006053 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006054 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006055 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006056 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006057 dictitem_free(di);
6058 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006059 return -1;
6060 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006062 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006063 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064 return 0;
6065}
6066
6067 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006068pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006069{
6070 list_T *l;
6071
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006072 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006073 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006074
6075 tv->v_type = VAR_LIST;
6076 tv->vval.v_list = l;
6077
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006078 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 {
6080 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006081 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006082 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006083
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006084 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006085 return 0;
6086}
6087
Bram Moolenaardb913952012-06-29 12:54:53 +02006088typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6089
6090 static int
6091convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006092 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006093{
6094 PyObject *capsule;
6095 char hexBuf[sizeof(void *) * 2 + 3];
6096
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006097 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006098
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006099#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006100 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006101#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006102 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006103#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006104 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006105 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006106#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006107 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006108#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006109 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006110#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006111 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6112 {
6113 Py_DECREF(capsule);
6114 tv->v_type = VAR_UNKNOWN;
6115 return -1;
6116 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006117
6118 Py_DECREF(capsule);
6119
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006120 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006121 {
6122 tv->v_type = VAR_UNKNOWN;
6123 return -1;
6124 }
6125 /* As we are not using copy_tv which increments reference count we must
6126 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006127 if (tv->v_type == VAR_DICT)
6128 ++tv->vval.v_dict->dv_refcount;
6129 else if (tv->v_type == VAR_LIST)
6130 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006131 }
6132 else
6133 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006134 typval_T *v;
6135
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006136#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006137 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006138#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006139 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006140#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006141 copy_tv(v, tv);
6142 }
6143 return 0;
6144}
6145
6146 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006147ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6148{
6149 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006150 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006151
6152 if (!(lookup_dict = PyDict_New()))
6153 return -1;
6154
6155 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6156 {
6157 tv->v_type = VAR_DICT;
6158 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6159 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006160 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006161 }
6162 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006163 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006164 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006165 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006166 else
6167 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006168 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006169 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006170 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006171 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006172 }
6173 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006174 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006175}
6176
6177 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006178ConvertFromPySequence(PyObject *obj, typval_T *tv)
6179{
6180 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006181 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006182
6183 if (!(lookup_dict = PyDict_New()))
6184 return -1;
6185
6186 if (PyType_IsSubtype(obj->ob_type, &ListType))
6187 {
6188 tv->v_type = VAR_LIST;
6189 tv->vval.v_list = (((ListObject *)(obj))->list);
6190 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006191 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006192 }
6193 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006194 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006195 else
6196 {
6197 PyErr_FORMAT(PyExc_TypeError,
6198 N_("unable to convert %s to vim list"),
6199 Py_TYPE_NAME(obj));
6200 ret = -1;
6201 }
6202 Py_DECREF(lookup_dict);
6203 return ret;
6204}
6205
6206 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006207ConvertFromPyObject(PyObject *obj, typval_T *tv)
6208{
6209 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006210 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006211
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006212 if (!(lookup_dict = PyDict_New()))
6213 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006214 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006215 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006216 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006217}
6218
6219 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006220_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006221{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006222 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006223 {
6224 tv->v_type = VAR_DICT;
6225 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6226 ++tv->vval.v_dict->dv_refcount;
6227 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006228 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006229 {
6230 tv->v_type = VAR_LIST;
6231 tv->vval.v_list = (((ListObject *)(obj))->list);
6232 ++tv->vval.v_list->lv_refcount;
6233 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006234 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006235 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006236 FunctionObject *func = (FunctionObject *) obj;
6237 if (func->self != NULL || func->argv != NULL)
6238 {
6239 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6240 set_partial(func, pt, TRUE);
6241 tv->vval.v_partial = pt;
6242 tv->v_type = VAR_PARTIAL;
6243 }
6244 else
6245 {
6246 if (set_string_copy(func->name, tv) == -1)
6247 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006248
Bram Moolenaar8110a092016-04-14 15:56:09 +02006249 tv->v_type = VAR_FUNC;
6250 }
6251 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006253 else if (PyBytes_Check(obj))
6254 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006255 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006256
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006257 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006258 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006259 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 return -1;
6261
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006262 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006263 return -1;
6264
6265 tv->v_type = VAR_STRING;
6266 }
6267 else if (PyUnicode_Check(obj))
6268 {
6269 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006270 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006271
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006272 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006273 if (bytes == NULL)
6274 return -1;
6275
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006276 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006277 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006278 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006279 return -1;
6280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006281 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006282 {
6283 Py_XDECREF(bytes);
6284 return -1;
6285 }
6286 Py_XDECREF(bytes);
6287
6288 tv->v_type = VAR_STRING;
6289 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006290#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006291 else if (PyInt_Check(obj))
6292 {
6293 tv->v_type = VAR_NUMBER;
6294 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006295 if (PyErr_Occurred())
6296 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006297 }
6298#endif
6299 else if (PyLong_Check(obj))
6300 {
6301 tv->v_type = VAR_NUMBER;
6302 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006303 if (PyErr_Occurred())
6304 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006305 }
6306 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006307 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006308#ifdef FEAT_FLOAT
6309 else if (PyFloat_Check(obj))
6310 {
6311 tv->v_type = VAR_FLOAT;
6312 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6313 }
6314#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006315 else if (PyObject_HasAttrString(obj, "keys"))
6316 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006317 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006318 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006319 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006320 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006321 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006322 else if (PyNumber_Check(obj))
6323 {
6324 PyObject *num;
6325
6326 if (!(num = PyNumber_Long(obj)))
6327 return -1;
6328
6329 tv->v_type = VAR_NUMBER;
6330 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6331
6332 Py_DECREF(num);
6333 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006334 else if (obj == Py_None)
6335 {
6336 tv->v_type = VAR_SPECIAL;
6337 tv->vval.v_number = VVAL_NONE;
6338 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006339 else
6340 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006341 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006342 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006343 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006344 return -1;
6345 }
6346 return 0;
6347}
6348
6349 static PyObject *
6350ConvertToPyObject(typval_T *tv)
6351{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006352 typval_T *argv;
6353 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006354 if (tv == NULL)
6355 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006356 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006357 return NULL;
6358 }
6359 switch (tv->v_type)
6360 {
6361 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006362 return PyBytes_FromString(tv->vval.v_string == NULL
6363 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006364 case VAR_NUMBER:
6365 return PyLong_FromLong((long) tv->vval.v_number);
6366#ifdef FEAT_FLOAT
6367 case VAR_FLOAT:
6368 return PyFloat_FromDouble((double) tv->vval.v_float);
6369#endif
6370 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006371 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006372 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006373 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006374 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006375 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006376 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006377 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006378 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006379 if (tv->vval.v_partial->pt_argc)
6380 {
6381 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6382 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6383 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6384 }
6385 else
6386 argv = NULL;
6387 if (tv->vval.v_partial->pt_dict != NULL)
6388 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006389 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006390 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006391 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006392 tv->vval.v_partial->pt_dict,
6393 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006394 case VAR_BLOB:
6395 return PyBytes_FromStringAndSize(
6396 (char*) tv->vval.v_blob->bv_ga.ga_data,
6397 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006399 case VAR_CHANNEL:
6400 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006401 Py_INCREF(Py_None);
6402 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006403 case VAR_SPECIAL:
6404 switch (tv->vval.v_number)
6405 {
6406 case VVAL_FALSE: return AlwaysFalse(NULL);
6407 case VVAL_TRUE: return AlwaysTrue(NULL);
6408 case VVAL_NONE:
6409 case VVAL_NULL: return AlwaysNone(NULL);
6410 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006411 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006412 return NULL;
6413 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006414 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006415}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006416
6417typedef struct
6418{
6419 PyObject_HEAD
6420} CurrentObject;
6421static PyTypeObject CurrentType;
6422
6423 static void
6424init_structs(void)
6425{
6426 vim_memset(&OutputType, 0, sizeof(OutputType));
6427 OutputType.tp_name = "vim.message";
6428 OutputType.tp_basicsize = sizeof(OutputObject);
6429 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6430 OutputType.tp_doc = "vim message object";
6431 OutputType.tp_methods = OutputMethods;
6432#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006433 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6434 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006435 OutputType.tp_alloc = call_PyType_GenericAlloc;
6436 OutputType.tp_new = call_PyType_GenericNew;
6437 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006438 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006439#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006440 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6441 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006442 // Disabled, because this causes a crash in test86
6443 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006444#endif
6445
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006446 vim_memset(&IterType, 0, sizeof(IterType));
6447 IterType.tp_name = "vim.iter";
6448 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006449 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006450 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006451 IterType.tp_iter = (getiterfunc)IterIter;
6452 IterType.tp_iternext = (iternextfunc)IterNext;
6453 IterType.tp_dealloc = (destructor)IterDestructor;
6454 IterType.tp_traverse = (traverseproc)IterTraverse;
6455 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006456
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006457 vim_memset(&BufferType, 0, sizeof(BufferType));
6458 BufferType.tp_name = "vim.buffer";
6459 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006460 BufferType.tp_dealloc = (destructor)BufferDestructor;
6461 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006462 BufferType.tp_as_sequence = &BufferAsSeq;
6463 BufferType.tp_as_mapping = &BufferAsMapping;
6464 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6465 BufferType.tp_doc = "vim buffer object";
6466 BufferType.tp_methods = BufferMethods;
6467#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006468 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006469 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006470 BufferType.tp_alloc = call_PyType_GenericAlloc;
6471 BufferType.tp_new = call_PyType_GenericNew;
6472 BufferType.tp_free = call_PyObject_Free;
6473#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006474 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006475 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006476#endif
6477
6478 vim_memset(&WindowType, 0, sizeof(WindowType));
6479 WindowType.tp_name = "vim.window";
6480 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006481 WindowType.tp_dealloc = (destructor)WindowDestructor;
6482 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006483 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006484 WindowType.tp_doc = "vim Window object";
6485 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6487 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6490 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491 WindowType.tp_alloc = call_PyType_GenericAlloc;
6492 WindowType.tp_new = call_PyType_GenericNew;
6493 WindowType.tp_free = call_PyObject_Free;
6494#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006495 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6496 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006497#endif
6498
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006499 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6500 TabPageType.tp_name = "vim.tabpage";
6501 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6503 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006504 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6505 TabPageType.tp_doc = "vim tab page object";
6506 TabPageType.tp_methods = TabPageMethods;
6507#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006508 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006509 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6510 TabPageType.tp_new = call_PyType_GenericNew;
6511 TabPageType.tp_free = call_PyObject_Free;
6512#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006513 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006514#endif
6515
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006516 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6517 BufMapType.tp_name = "vim.bufferlist";
6518 BufMapType.tp_basicsize = sizeof(BufMapObject);
6519 BufMapType.tp_as_mapping = &BufMapAsMapping;
6520 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006521 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006522 BufferType.tp_doc = "vim buffer list";
6523
6524 vim_memset(&WinListType, 0, sizeof(WinListType));
6525 WinListType.tp_name = "vim.windowlist";
6526 WinListType.tp_basicsize = sizeof(WinListType);
6527 WinListType.tp_as_sequence = &WinListAsSeq;
6528 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6529 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006530 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006531
6532 vim_memset(&TabListType, 0, sizeof(TabListType));
6533 TabListType.tp_name = "vim.tabpagelist";
6534 TabListType.tp_basicsize = sizeof(TabListType);
6535 TabListType.tp_as_sequence = &TabListAsSeq;
6536 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6537 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006538
6539 vim_memset(&RangeType, 0, sizeof(RangeType));
6540 RangeType.tp_name = "vim.range";
6541 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006542 RangeType.tp_dealloc = (destructor)RangeDestructor;
6543 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006544 RangeType.tp_as_sequence = &RangeAsSeq;
6545 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006546 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006547 RangeType.tp_doc = "vim Range object";
6548 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006549 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6550 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006552 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006553 RangeType.tp_alloc = call_PyType_GenericAlloc;
6554 RangeType.tp_new = call_PyType_GenericNew;
6555 RangeType.tp_free = call_PyObject_Free;
6556#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006557 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006558#endif
6559
6560 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6561 CurrentType.tp_name = "vim.currentdata";
6562 CurrentType.tp_basicsize = sizeof(CurrentObject);
6563 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6564 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006565 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006566#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006567 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6568 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006569#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006570 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6571 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572#endif
6573
6574 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6575 DictionaryType.tp_name = "vim.dictionary";
6576 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006577 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006578 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006580 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006581 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6582 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006583 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6584 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6585 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006586#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006587 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6588 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006589#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006590 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6591 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592#endif
6593
6594 vim_memset(&ListType, 0, sizeof(ListType));
6595 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006596 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006597 ListType.tp_basicsize = sizeof(ListObject);
6598 ListType.tp_as_sequence = &ListAsSeq;
6599 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006600 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006601 ListType.tp_doc = "list pushing modifications to vim structure";
6602 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006604 ListType.tp_new = (newfunc)ListConstructor;
6605 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006606#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006607 ListType.tp_getattro = (getattrofunc)ListGetattro;
6608 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006609#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006610 ListType.tp_getattr = (getattrfunc)ListGetattr;
6611 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006612#endif
6613
6614 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006615 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6618 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006619 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006620 FunctionType.tp_doc = "object that calls vim function";
6621 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006622 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006623 FunctionType.tp_new = (newfunc)FunctionConstructor;
6624 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006625#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006626 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006627#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006628 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006629#endif
6630
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006631 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6632 OptionsType.tp_name = "vim.options";
6633 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006634 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006635 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006636 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006637 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006638 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006639 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6640 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6641 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006642
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006643#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006644 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6645 LoaderType.tp_name = "vim.Loader";
6646 LoaderType.tp_basicsize = sizeof(LoaderObject);
6647 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6648 LoaderType.tp_doc = "vim message object";
6649 LoaderType.tp_methods = LoaderMethods;
6650 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006651#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006652
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006653#if PY_MAJOR_VERSION >= 3
6654 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6655 vimmodule.m_name = "vim";
6656 vimmodule.m_doc = "Vim Python interface\n";
6657 vimmodule.m_size = -1;
6658 vimmodule.m_methods = VimMethods;
6659#endif
6660}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006661
6662#define PYTYPE_READY(type) \
6663 if (PyType_Ready(&type)) \
6664 return -1;
6665
6666 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006667init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006668{
6669 PYTYPE_READY(IterType);
6670 PYTYPE_READY(BufferType);
6671 PYTYPE_READY(RangeType);
6672 PYTYPE_READY(WindowType);
6673 PYTYPE_READY(TabPageType);
6674 PYTYPE_READY(BufMapType);
6675 PYTYPE_READY(WinListType);
6676 PYTYPE_READY(TabListType);
6677 PYTYPE_READY(CurrentType);
6678 PYTYPE_READY(DictionaryType);
6679 PYTYPE_READY(ListType);
6680 PYTYPE_READY(FunctionType);
6681 PYTYPE_READY(OptionsType);
6682 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006683#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006684 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006685#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006686 return 0;
6687}
6688
6689 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006690init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006691{
6692 PyObject *path;
6693 PyObject *path_hook;
6694 PyObject *path_hooks;
6695
6696 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6697 return -1;
6698
6699 if (!(path_hooks = PySys_GetObject("path_hooks")))
6700 {
6701 PyErr_Clear();
6702 path_hooks = PyList_New(1);
6703 PyList_SET_ITEM(path_hooks, 0, path_hook);
6704 if (PySys_SetObject("path_hooks", path_hooks))
6705 {
6706 Py_DECREF(path_hooks);
6707 return -1;
6708 }
6709 Py_DECREF(path_hooks);
6710 }
6711 else if (PyList_Check(path_hooks))
6712 {
6713 if (PyList_Append(path_hooks, path_hook))
6714 {
6715 Py_DECREF(path_hook);
6716 return -1;
6717 }
6718 Py_DECREF(path_hook);
6719 }
6720 else
6721 {
6722 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006723 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006724 "You should now do the following:\n"
6725 "- append vim.path_hook to sys.path_hooks\n"
6726 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6727 VimTryEnd(); /* Discard the error */
6728 Py_DECREF(path_hook);
6729 return 0;
6730 }
6731
6732 if (!(path = PySys_GetObject("path")))
6733 {
6734 PyErr_Clear();
6735 path = PyList_New(1);
6736 Py_INCREF(vim_special_path_object);
6737 PyList_SET_ITEM(path, 0, vim_special_path_object);
6738 if (PySys_SetObject("path", path))
6739 {
6740 Py_DECREF(path);
6741 return -1;
6742 }
6743 Py_DECREF(path);
6744 }
6745 else if (PyList_Check(path))
6746 {
6747 if (PyList_Append(path, vim_special_path_object))
6748 return -1;
6749 }
6750 else
6751 {
6752 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006753 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006754 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6755 VimTryEnd(); /* Discard the error */
6756 }
6757
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006758 return 0;
6759}
6760
6761static BufMapObject TheBufferMap =
6762{
6763 PyObject_HEAD_INIT(&BufMapType)
6764};
6765
6766static WinListObject TheWindowList =
6767{
6768 PyObject_HEAD_INIT(&WinListType)
6769 NULL
6770};
6771
6772static CurrentObject TheCurrent =
6773{
6774 PyObject_HEAD_INIT(&CurrentType)
6775};
6776
6777static TabListObject TheTabPageList =
6778{
6779 PyObject_HEAD_INIT(&TabListType)
6780};
6781
6782static struct numeric_constant {
6783 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006784 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006785} numeric_constants[] = {
6786 {"VAR_LOCKED", VAR_LOCKED},
6787 {"VAR_FIXED", VAR_FIXED},
6788 {"VAR_SCOPE", VAR_SCOPE},
6789 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6790};
6791
6792static struct object_constant {
6793 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006794 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006795} object_constants[] = {
6796 {"buffers", (PyObject *)(void *)&TheBufferMap},
6797 {"windows", (PyObject *)(void *)&TheWindowList},
6798 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6799 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006800
6801 {"Buffer", (PyObject *)&BufferType},
6802 {"Range", (PyObject *)&RangeType},
6803 {"Window", (PyObject *)&WindowType},
6804 {"TabPage", (PyObject *)&TabPageType},
6805 {"Dictionary", (PyObject *)&DictionaryType},
6806 {"List", (PyObject *)&ListType},
6807 {"Function", (PyObject *)&FunctionType},
6808 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006809#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006810 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006811#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006812};
6813
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006814#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006815 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006816 return -1;
6817
6818#define ADD_CHECKED_OBJECT(m, name, obj) \
6819 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006820 PyObject *valObject = obj; \
6821 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006822 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006823 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006824 }
6825
6826 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006827populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006828{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006829 int i;
6830 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006831 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006832 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006833#if PY_VERSION_HEX >= 0x030700f0
6834 PyObject *dict;
6835 PyObject *cls;
6836#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006837
6838 for (i = 0; i < (int)(sizeof(numeric_constants)
6839 / sizeof(struct numeric_constant));
6840 ++i)
6841 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006842 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006843
6844 for (i = 0; i < (int)(sizeof(object_constants)
6845 / sizeof(struct object_constant));
6846 ++i)
6847 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006848 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006849
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006850 valObject = object_constants[i].valObject;
6851 Py_INCREF(valObject);
6852 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006853 }
6854
6855 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6856 return -1;
6857 ADD_OBJECT(m, "error", VimError);
6858
Bram Moolenaara9922d62013-05-30 13:01:18 +02006859 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6860 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006861 ADD_CHECKED_OBJECT(m, "options",
6862 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006863
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006864 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006865 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006866 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006867
Bram Moolenaar22081f42016-06-01 20:38:34 +02006868#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006869 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006870 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006871#else
6872 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6873 return -1;
6874#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006875 ADD_OBJECT(m, "_getcwd", py_getcwd)
6876
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006877 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006878 return -1;
6879 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006880 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006881 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006882 if (PyObject_SetAttrString(other_module, "chdir", attr))
6883 {
6884 Py_DECREF(attr);
6885 return -1;
6886 }
6887 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006889 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006890 {
6891 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006892 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006893 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006894 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6895 {
6896 Py_DECREF(attr);
6897 return -1;
6898 }
6899 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006900 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006901 else
6902 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006903
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006904 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6905 return -1;
6906
6907 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6908
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006909#if PY_VERSION_HEX >= 0x030700f0
6910 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6911 return -1;
6912
6913 dict = PyModule_GetDict(imp);
6914
6915 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6916 {
6917 Py_DECREF(imp);
6918 return -1;
6919 }
6920
6921 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6922 {
6923 Py_DECREF(imp);
6924 return -1;
6925 }
6926
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006927 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6928 {
6929 // find_module() is deprecated, this may stop working in some later
6930 // version.
6931 ADD_OBJECT(m, "_find_module", py_find_module);
6932 }
6933
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006934 Py_DECREF(imp);
6935
6936 ADD_OBJECT(m, "_find_spec", py_find_spec);
6937#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006938 if (!(imp = PyImport_ImportModule("imp")))
6939 return -1;
6940
6941 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6942 {
6943 Py_DECREF(imp);
6944 return -1;
6945 }
6946
6947 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6948 {
6949 Py_DECREF(py_find_module);
6950 Py_DECREF(imp);
6951 return -1;
6952 }
6953
6954 Py_DECREF(imp);
6955
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006956 ADD_OBJECT(m, "_find_module", py_find_module);
6957 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006958#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006959
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006960 return 0;
6961}