blob: 165c84728342824263489df7c99512d3eae08eb2 [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 Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020075static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020076static PyObject *WindowNew(win_T *, tabpage_T *);
77static PyObject *BufferNew (buf_T *);
78static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020079
80static PyInt RangeStart;
81static PyInt RangeEnd;
82
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020083static PyObject *globals;
84
Bram Moolenaarf4258302013-06-02 18:20:17 +020085static PyObject *py_chdir;
86static PyObject *py_fchdir;
87static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020088static PyObject *vim_module;
89static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020090
Bram Moolenaar79a494d2018-07-22 04:30:21 +020091#if PY_VERSION_HEX >= 0x030700f0
92static PyObject *py_find_spec;
93#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020094static PyObject *py_find_module;
95static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020096#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +020097
98static PyObject *VimError;
99
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200100/*
101 * obtain a lock on the Vim data structures
102 */
103 static void
104Python_Lock_Vim(void)
105{
106}
107
108/*
109 * release a lock on the Vim data structures
110 */
111 static void
112Python_Release_Vim(void)
113{
114}
115
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200116/*
117 * The "todecref" argument holds a pointer to PyObject * that must be
118 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
119 * was needed to generate returned value is object.
120 *
121 * Use Py_XDECREF to decrement reference count.
122 */
123 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200127
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200128 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 {
130
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200131 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
132 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 return NULL;
134
135 *todecref = NULL;
136 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200137 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200138 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200139 PyObject *bytes;
140
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200141 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200142 return NULL;
143
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200144 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
145 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200146 {
147 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200148 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200149 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200150
151 *todecref = bytes;
152 }
153 else
154 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200156 PyErr_FORMAT(PyExc_TypeError,
157 N_("expected str() or unicode() instance, but got %s"),
158 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200159#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200160 PyErr_FORMAT(PyExc_TypeError,
161 N_("expected bytes() or str() instance, but got %s"),
162 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200163#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200164 return NULL;
165 }
166
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200167 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200168}
169
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200170#define NUMBER_LONG 1
171#define NUMBER_INT 2
172#define NUMBER_NATURAL 4
173#define NUMBER_UNSIGNED 8
174
175 static int
176NumberToLong(PyObject *obj, long *result, int flags)
177{
178#if PY_MAJOR_VERSION < 3
179 if (PyInt_Check(obj))
180 {
181 *result = PyInt_AsLong(obj);
182 if (PyErr_Occurred())
183 return -1;
184 }
185 else
186#endif
187 if (PyLong_Check(obj))
188 {
189 *result = PyLong_AsLong(obj);
190 if (PyErr_Occurred())
191 return -1;
192 }
193 else if (PyNumber_Check(obj))
194 {
195 PyObject *num;
196
197 if (!(num = PyNumber_Long(obj)))
198 return -1;
199
200 *result = PyLong_AsLong(num);
201
202 Py_DECREF(num);
203
204 if (PyErr_Occurred())
205 return -1;
206 }
207 else
208 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200212 "coercing to long(), but got %s"),
213 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200215 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200216 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200217 "but got %s"),
218 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200219#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200220 return -1;
221 }
222
223 if (flags & NUMBER_INT)
224 {
225 if (*result > INT_MAX)
226 {
227 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200228 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200229 return -1;
230 }
231 else if (*result < INT_MIN)
232 {
233 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200234 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200235 return -1;
236 }
237 }
238
239 if (flags & NUMBER_NATURAL)
240 {
241 if (*result <= 0)
242 {
243 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100244 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200245 return -1;
246 }
247 }
248 else if (flags & NUMBER_UNSIGNED)
249 {
250 if (*result < 0)
251 {
252 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200253 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200254 return -1;
255 }
256 }
257
258 return 0;
259}
260
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200261 static int
262add_string(PyObject *list, char *s)
263{
264 PyObject *string;
265
266 if (!(string = PyString_FromString(s)))
267 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200268
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200269 if (PyList_Append(list, string))
270 {
271 Py_DECREF(string);
272 return -1;
273 }
274
275 Py_DECREF(string);
276 return 0;
277}
278
279 static PyObject *
280ObjectDir(PyObject *self, char **attributes)
281{
282 PyMethodDef *method;
283 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200284 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200286 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 return NULL;
288
289 if (self)
290 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200291 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200292 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 return NULL;
295 }
296
297 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200298 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200299 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 return NULL;
302 }
303
304#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200305 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200306 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308 return NULL;
309 }
310#endif
311
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200312 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200313}
314
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200316 */
317
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200318/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200319typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200320
321static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322
323typedef struct
324{
325 PyObject_HEAD
326 long softspace;
327 long error;
328} OutputObject;
329
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200330static char *OutputAttrs[] = {
331 "softspace",
332 NULL
333};
334
335 static PyObject *
336OutputDir(PyObject *self)
337{
338 return ObjectDir(self, OutputAttrs);
339}
340
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200342OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200343{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200344 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200345 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200346 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200347 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200348 return -1;
349 }
350
351 if (strcmp(name, "softspace") == 0)
352 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200353 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200355 return 0;
356 }
357
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200358 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200359 return -1;
360}
361
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200362/* Buffer IO, we write one whole line at a time. */
363static garray_T io_ga = {0, 0, 1, 80, NULL};
364static writefn old_fn = NULL;
365
366 static void
367PythonIO_Flush(void)
368{
369 if (old_fn != NULL && io_ga.ga_len > 0)
370 {
371 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
372 old_fn((char_u *)io_ga.ga_data);
373 }
374 io_ga.ga_len = 0;
375}
376
377 static void
378writer(writefn fn, char_u *str, PyInt n)
379{
380 char_u *ptr;
381
382 /* Flush when switching output function. */
383 if (fn != old_fn)
384 PythonIO_Flush();
385 old_fn = fn;
386
387 /* Write each NL separated line. Text after the last NL is kept for
388 * writing later. */
389 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
390 {
391 PyInt len = ptr - str;
392
393 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
394 break;
395
396 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
397 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
398 fn((char_u *)io_ga.ga_data);
399 str = ptr + 1;
400 n -= len + 1;
401 io_ga.ga_len = 0;
402 }
403
404 /* Put the remaining text into io_ga for later printing. */
405 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
406 {
407 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
408 io_ga.ga_len += (int)n;
409 }
410}
411
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200412 static int
413write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 Py_ssize_t len = 0;
416 char *str = NULL;
417 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200418
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200419 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
420 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200421
422 Py_BEGIN_ALLOW_THREADS
423 Python_Lock_Vim();
424 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
425 Python_Release_Vim();
426 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200427 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200428
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200429 return 0;
430}
431
432 static PyObject *
433OutputWrite(OutputObject *self, PyObject *string)
434{
435 if (write_output(self, string))
436 return NULL;
437
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200438 Py_INCREF(Py_None);
439 return Py_None;
440}
441
442 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200443OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200445 PyObject *iterator;
446 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200448 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200453 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200454 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(iterator);
456 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200457 return NULL;
458 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200459 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200460 }
461
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200462 Py_DECREF(iterator);
463
464 /* Iterator may have finished due to an exception */
465 if (PyErr_Occurred())
466 return NULL;
467
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200468 Py_INCREF(Py_None);
469 return Py_None;
470}
471
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100472 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100473AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100474{
475 /* do nothing */
476 Py_INCREF(Py_None);
477 return Py_None;
478}
479
Bram Moolenaard4247472015-11-02 13:28:59 +0100480 static PyObject *
481AlwaysFalse(PyObject *self UNUSED)
482{
483 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100484 PyObject *ret = Py_False;
485 Py_INCREF(ret);
486 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100487}
488
489 static PyObject *
490AlwaysTrue(PyObject *self UNUSED)
491{
492 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100493 PyObject *ret = Py_True;
494 Py_INCREF(ret);
495 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100496}
497
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200498/***************/
499
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200500static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200501 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200502 {"write", (PyCFunction)OutputWrite, METH_O, ""},
503 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100504 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
505 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
506 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
507 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
508 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
509 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200510 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200511 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200512 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200513};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200514
515static OutputObject Output =
516{
517 PyObject_HEAD_INIT(&OutputType)
518 0,
519 0
520};
521
522static OutputObject Error =
523{
524 PyObject_HEAD_INIT(&OutputType)
525 0,
526 1
527};
528
529 static int
530PythonIO_Init_io(void)
531{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200532 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
533 return -1;
534 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
535 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200536
537 if (PyErr_Occurred())
538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100539 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200540 return -1;
541 }
542
543 return 0;
544}
545
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200546#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200547static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
548
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200549typedef struct
550{
551 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200552 char *fullname;
553 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200554} LoaderObject;
555static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200557 static void
558LoaderDestructor(LoaderObject *self)
559{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200560 vim_free(self->fullname);
561 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200562 DESTRUCTOR_FINISH(self);
563}
564
565 static PyObject *
566LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
567{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200568 char *fullname = self->fullname;
569 PyObject *result = self->result;
570 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200571
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200572 if (!fullname)
573 {
574 module = result ? result : Py_None;
575 Py_INCREF(module);
576 return module;
577 }
578
579 module = call_load_module(fullname, (int)STRLEN(fullname), result);
580
581 self->fullname = NULL;
582 self->result = module;
583
584 vim_free(fullname);
585 Py_DECREF(result);
586
587 if (!module)
588 {
589 if (PyErr_Occurred())
590 return NULL;
591
592 Py_INCREF(Py_None);
593 return Py_None;
594 }
595
596 Py_INCREF(module);
597 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200598}
599
600static struct PyMethodDef LoaderMethods[] = {
601 /* name, function, calling, doc */
602 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
603 { NULL, NULL, 0, NULL}
604};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200605#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200606
607/* Check to see whether a Vim error has been reported, or a keyboard
608 * interrupt has been detected.
609 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200610
611 static void
612VimTryStart(void)
613{
614 ++trylevel;
615}
616
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200617 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200618VimTryEnd(void)
619{
620 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100621 /* Without this it stops processing all subsequent Vim script commands and
622 * generates strange error messages if I e.g. try calling Test() in a cycle
623 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200624 did_emsg = FALSE;
625 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200626 if (got_int)
627 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100628 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100629 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100630 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200631 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200632 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200633 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100634 else if (msg_list != NULL && *msg_list != NULL)
635 {
636 int should_free;
637 char_u *msg;
638
639 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
640
641 if (msg == NULL)
642 {
643 PyErr_NoMemory();
644 return -1;
645 }
646
647 PyErr_SetVim((char *) msg);
648
649 free_global_msglist();
650
651 if (should_free)
652 vim_free(msg);
653
654 return -1;
655 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200656 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200657 return (PyErr_Occurred() ? -1 : 0);
658 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200659 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200660 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100661 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200662 return -1;
663 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100664 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 else
666 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200667 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200668 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200669 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200670 }
671}
672
673 static int
674VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200675{
676 if (got_int)
677 {
678 PyErr_SetNone(PyExc_KeyboardInterrupt);
679 return 1;
680 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200681 return 0;
682}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683
684/* Vim module - Implementation
685 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200686
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200688VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200690 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200691 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200692 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693
Bram Moolenaar389a1792013-06-23 13:00:44 +0200694 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695 return NULL;
696
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 Py_BEGIN_ALLOW_THREADS
698 Python_Lock_Vim();
699
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200700 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200701 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 update_screen(VALID);
703
704 Python_Release_Vim();
705 Py_END_ALLOW_THREADS
706
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200707 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200713 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200714 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715}
716
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717/*
718 * Function to translate a typval_T into a PyObject; this will recursively
719 * translate lists/dictionaries into their Python equivalents.
720 *
721 * The depth parameter is to avoid infinite recursion, set it to 1 when
722 * you call VimToPython.
723 */
724 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200725VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200729 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730
731 /* Avoid infinite recursion */
732 if (depth > 100)
733 {
734 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200735 ret = Py_None;
736 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
738
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200739 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200740 * then and we can use it again. */
741 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
742 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
743 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200744 sprintf(ptrBuf, "%p",
745 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
746 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 Py_INCREF(ret);
751 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200752 }
753 }
754
755 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200756 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200757 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 else if (our_tv->v_type == VAR_NUMBER)
759 {
760 char buf[NUMBUFLEN];
761
762 /* For backwards compatibility numbers are stored as strings. */
763 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200764 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 }
766# ifdef FEAT_FLOAT
767 else if (our_tv->v_type == VAR_FLOAT)
768 {
769 char buf[NUMBUFLEN];
770
771 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200772 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 }
774# endif
775 else if (our_tv->v_type == VAR_LIST)
776 {
777 list_T *list = our_tv->vval.v_list;
778 listitem_T *curr;
779
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 if (list == NULL)
781 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200782
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200784 return NULL;
785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200786 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200788 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200789 return NULL;
790 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200792 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
793 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200794 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200796 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200797 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200799 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 {
801 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200802 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200803 return NULL;
804 }
805 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200806 }
807 }
808 else if (our_tv->v_type == VAR_DICT)
809 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100811 hashtab_T *ht;
812 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200813 hashitem_T *hi;
814 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100815
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200816 if (our_tv->vval.v_dict == NULL)
817 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100818 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200820 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200821 return NULL;
822
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200823 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200825 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200826 return NULL;
827 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200828
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100829 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200830 for (hi = ht->ht_array; todo > 0; ++hi)
831 {
832 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200833 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200834 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200835
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200836 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200837 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200838 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200840 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200842 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200843 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200844 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200845 Py_DECREF(newObj);
846 return NULL;
847 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848 }
849 }
850 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100851 else if (our_tv->v_type == VAR_SPECIAL)
852 {
853 if (our_tv->vval.v_number == VVAL_FALSE)
854 {
855 ret = Py_False;
856 Py_INCREF(ret);
857 }
858 else if (our_tv->vval.v_number == VVAL_TRUE)
859 {
860 ret = Py_True;
861 Py_INCREF(ret);
862 }
863 else
864 {
865 Py_INCREF(Py_None);
866 ret = Py_None;
867 }
868 return ret;
869 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100870 else if (our_tv->v_type == VAR_BLOB)
871 ret = PyBytes_FromStringAndSize(
872 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
873 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 else
875 {
876 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200877 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878 }
879
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200880 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200881}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200882
883 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200884VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200886 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200887 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200888 PyObject *string;
889 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200890 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200891 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200892
Bram Moolenaar389a1792013-06-23 13:00:44 +0200893 if (!PyArg_ParseTuple(args, "O", &string))
894 return NULL;
895
896 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200897 return NULL;
898
899 Py_BEGIN_ALLOW_THREADS
900 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200901 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200903 Python_Release_Vim();
904 Py_END_ALLOW_THREADS
905
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906 Py_XDECREF(todecref);
907
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200908 if (VimTryEnd())
909 return NULL;
910
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200911 if (our_tv == NULL)
912 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200913 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200914 return NULL;
915 }
916
917 /* Convert the Vim type into a Python type. Create a dictionary that's
918 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200919 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200920 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200921 else
922 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200923 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200924 Py_DECREF(lookup_dict);
925 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200926
927
928 Py_BEGIN_ALLOW_THREADS
929 Python_Lock_Vim();
930 free_tv(our_tv);
931 Python_Release_Vim();
932 Py_END_ALLOW_THREADS
933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200934 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200935}
936
Bram Moolenaardb913952012-06-29 12:54:53 +0200937static PyObject *ConvertToPyObject(typval_T *);
938
939 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200941{
Bram Moolenaardb913952012-06-29 12:54:53 +0200942 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200943 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200944 char_u *expr;
945 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200946
Bram Moolenaar389a1792013-06-23 13:00:44 +0200947 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200948 return NULL;
949
950 Py_BEGIN_ALLOW_THREADS
951 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200952 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200954 Python_Release_Vim();
955 Py_END_ALLOW_THREADS
956
Bram Moolenaar389a1792013-06-23 13:00:44 +0200957 Py_XDECREF(todecref);
958
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200959 if (VimTryEnd())
960 return NULL;
961
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 if (our_tv == NULL)
963 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200964 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200965 return NULL;
966 }
967
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200969 Py_BEGIN_ALLOW_THREADS
970 Python_Lock_Vim();
971 free_tv(our_tv);
972 Python_Release_Vim();
973 Py_END_ALLOW_THREADS
974
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200975 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200976}
977
978 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200979VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200980{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200981 char_u *str;
982 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200983 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200984
Bram Moolenaar389a1792013-06-23 13:00:44 +0200985 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200986 return NULL;
987
Bram Moolenaara54bf402012-12-05 16:30:07 +0100988#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200989 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100990#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200991 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100992#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200993
994 Py_XDECREF(todecref);
995
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200996 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200997}
998
Bram Moolenaarf4258302013-06-02 18:20:17 +0200999 static PyObject *
1000_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
1001{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001002 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001003 PyObject *newwd;
1004 PyObject *todecref;
1005 char_u *new_dir;
1006
Bram Moolenaard4209d22013-06-05 20:34:15 +02001007 if (_chdir == NULL)
1008 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001009 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001010 return NULL;
1011
1012 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1013 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001014 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001015 return NULL;
1016 }
1017
1018 if (!(new_dir = StringToChars(newwd, &todecref)))
1019 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001020 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001021 Py_DECREF(newwd);
1022 return NULL;
1023 }
1024
1025 VimTryStart();
1026
1027 if (vim_chdir(new_dir))
1028 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001029 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001030 Py_DECREF(newwd);
1031 Py_XDECREF(todecref);
1032
1033 if (VimTryEnd())
1034 return NULL;
1035
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001036 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001037 return NULL;
1038 }
1039
1040 Py_DECREF(newwd);
1041 Py_XDECREF(todecref);
1042
1043 post_chdir(FALSE);
1044
1045 if (VimTryEnd())
1046 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001047 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001048 return NULL;
1049 }
1050
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001051 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001052}
1053
1054 static PyObject *
1055VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1056{
1057 return _VimChdir(py_chdir, args, kwargs);
1058}
1059
1060 static PyObject *
1061VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1062{
1063 return _VimChdir(py_fchdir, args, kwargs);
1064}
1065
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001066typedef struct {
1067 PyObject *callable;
1068 PyObject *result;
1069} map_rtp_data;
1070
1071 static void
1072map_rtp_callback(char_u *path, void *_data)
1073{
1074 void **data = (void **) _data;
1075 PyObject *pathObject;
1076 map_rtp_data *mr_data = *((map_rtp_data **) data);
1077
Bram Moolenaar41009372013-07-01 22:03:04 +02001078 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001079 {
1080 *data = NULL;
1081 return;
1082 }
1083
1084 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1085 pathObject, NULL);
1086
1087 Py_DECREF(pathObject);
1088
1089 if (!mr_data->result || mr_data->result != Py_None)
1090 *data = NULL;
1091 else
1092 {
1093 Py_DECREF(mr_data->result);
1094 mr_data->result = NULL;
1095 }
1096}
1097
1098 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001099VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001100{
1101 map_rtp_data data;
1102
Bram Moolenaar389a1792013-06-23 13:00:44 +02001103 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001104 data.result = NULL;
1105
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001106 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001107
1108 if (data.result == NULL)
1109 {
1110 if (PyErr_Occurred())
1111 return NULL;
1112 else
1113 {
1114 Py_INCREF(Py_None);
1115 return Py_None;
1116 }
1117 }
1118 return data.result;
1119}
1120
1121/*
1122 * _vim_runtimepath_ special path implementation.
1123 */
1124
1125 static void
1126map_finder_callback(char_u *path, void *_data)
1127{
1128 void **data = (void **) _data;
1129 PyObject *list = *((PyObject **) data);
1130 PyObject *pathObject1, *pathObject2;
1131 char *pathbuf;
1132 size_t pathlen;
1133
1134 pathlen = STRLEN(path);
1135
1136#if PY_MAJOR_VERSION < 3
1137# define PY_MAIN_DIR_STRING "python2"
1138#else
1139# define PY_MAIN_DIR_STRING "python3"
1140#endif
1141#define PY_ALTERNATE_DIR_STRING "pythonx"
1142
1143#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1144 if (!(pathbuf = PyMem_New(char,
1145 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1146 {
1147 PyErr_NoMemory();
1148 *data = NULL;
1149 return;
1150 }
1151
1152 mch_memmove(pathbuf, path, pathlen + 1);
1153 add_pathsep((char_u *) pathbuf);
1154
1155 pathlen = STRLEN(pathbuf);
1156 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1157 PYTHONX_STRING_LENGTH + 1);
1158
1159 if (!(pathObject1 = PyString_FromString(pathbuf)))
1160 {
1161 *data = NULL;
1162 PyMem_Free(pathbuf);
1163 return;
1164 }
1165
1166 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1167 PYTHONX_STRING_LENGTH + 1);
1168
1169 if (!(pathObject2 = PyString_FromString(pathbuf)))
1170 {
1171 Py_DECREF(pathObject1);
1172 PyMem_Free(pathbuf);
1173 *data = NULL;
1174 return;
1175 }
1176
1177 PyMem_Free(pathbuf);
1178
1179 if (PyList_Append(list, pathObject1)
1180 || PyList_Append(list, pathObject2))
1181 *data = NULL;
1182
1183 Py_DECREF(pathObject1);
1184 Py_DECREF(pathObject2);
1185}
1186
1187 static PyObject *
1188Vim_GetPaths(PyObject *self UNUSED)
1189{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001190 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001191
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001192 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001193 return NULL;
1194
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001195 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001196
1197 if (PyErr_Occurred())
1198 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001199 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001200 return NULL;
1201 }
1202
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001203 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001204}
1205
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001206#if PY_VERSION_HEX >= 0x030700f0
1207 static PyObject *
1208FinderFindSpec(PyObject *self, PyObject *args)
1209{
1210 char *fullname;
1211 PyObject *paths;
1212 PyObject *target = Py_None;
1213 PyObject *spec;
1214
1215 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1216 return NULL;
1217
1218 if (!(paths = Vim_GetPaths(self)))
1219 return NULL;
1220
1221 spec = PyObject_CallFunction(py_find_spec, "sNN", fullname, paths, target);
1222
1223 Py_DECREF(paths);
1224
1225 if (!spec)
1226 {
1227 if (PyErr_Occurred())
1228 return NULL;
1229
1230 Py_INCREF(Py_None);
1231 return Py_None;
1232 }
1233
1234 return spec;
1235}
1236#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001237 static PyObject *
1238call_load_module(char *name, int len, PyObject *find_module_result)
1239{
1240 PyObject *fd, *pathname, *description;
1241
Bram Moolenaarc476e522013-06-23 13:46:40 +02001242 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001243 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001244 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001245 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001246 Py_TYPE_NAME(find_module_result));
1247 return NULL;
1248 }
1249 if (PyTuple_GET_SIZE(find_module_result) != 3)
1250 {
1251 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001252 N_("expected 3-tuple as imp.find_module() result, but got "
1253 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001254 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001255 return NULL;
1256 }
1257
1258 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1259 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1260 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1261 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001262 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001263 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001264 return NULL;
1265 }
1266
1267 return PyObject_CallFunction(py_load_module,
1268 "s#OOO", name, len, fd, pathname, description);
1269}
1270
1271 static PyObject *
1272find_module(char *fullname, char *tail, PyObject *new_path)
1273{
1274 PyObject *find_module_result;
1275 PyObject *module;
1276 char *dot;
1277
Bram Moolenaar41009372013-07-01 22:03:04 +02001278 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001279 {
1280 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001281 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001282 * first component
1283 */
1284 PyObject *newest_path;
1285 int partlen = (int) (dot - 1 - tail);
1286
1287 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1288 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001289 {
1290 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1291 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001292 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001293 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001294
1295 if (!(module = call_load_module(
1296 fullname,
1297 ((int) (tail - fullname)) + partlen,
1298 find_module_result)))
1299 {
1300 Py_DECREF(find_module_result);
1301 return NULL;
1302 }
1303
1304 Py_DECREF(find_module_result);
1305
1306 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1307 {
1308 Py_DECREF(module);
1309 return NULL;
1310 }
1311
1312 Py_DECREF(module);
1313
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001314 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001315
1316 Py_DECREF(newest_path);
1317
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001318 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001319 }
1320 else
1321 {
1322 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1323 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001324 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001325 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1326 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001327 return NULL;
1328 }
1329
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001330 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001331 }
1332}
1333
1334 static PyObject *
1335FinderFindModule(PyObject *self, PyObject *args)
1336{
1337 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001338 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001339 PyObject *new_path;
1340 LoaderObject *loader;
1341
1342 if (!PyArg_ParseTuple(args, "s", &fullname))
1343 return NULL;
1344
1345 if (!(new_path = Vim_GetPaths(self)))
1346 return NULL;
1347
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001348 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001349
1350 Py_DECREF(new_path);
1351
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001352 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001353 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001354 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001355 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001356
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001357 Py_INCREF(Py_None);
1358 return Py_None;
1359 }
1360
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001361 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001362 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001363 Py_DECREF(result);
1364 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001365 return NULL;
1366 }
1367
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001368 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1369 {
1370 vim_free(fullname);
1371 Py_DECREF(result);
1372 return NULL;
1373 }
1374
1375 loader->fullname = fullname;
1376 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001377
1378 return (PyObject *) loader;
1379}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001380#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001381
1382 static PyObject *
1383VimPathHook(PyObject *self UNUSED, PyObject *args)
1384{
1385 char *path;
1386
1387 if (PyArg_ParseTuple(args, "s", &path)
1388 && STRCMP(path, vim_special_path) == 0)
1389 {
1390 Py_INCREF(vim_module);
1391 return vim_module;
1392 }
1393
1394 PyErr_Clear();
1395 PyErr_SetNone(PyExc_ImportError);
1396 return NULL;
1397}
1398
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001399/*
1400 * Vim module - Definitions
1401 */
1402
1403static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001404 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001405 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001406 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001407 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1408 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001409 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1410 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001411 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001412#if PY_VERSION_HEX >= 0x030700f0
1413 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
1414#else
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001415 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001416#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001417 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1418 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1419 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001420};
1421
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001422/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001423 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001424 */
1425
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001426static PyTypeObject IterType;
1427
1428typedef PyObject *(*nextfun)(void **);
1429typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001430typedef int (*traversefun)(void *, visitproc, void *);
1431typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001432
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001433/* Main purpose of this object is removing the need for do python
1434 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1435 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001436
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001437typedef struct
1438{
1439 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001440 void *cur;
1441 nextfun next;
1442 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001443 traversefun traverse;
1444 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001445} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001446
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001447 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001448IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1449 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001450{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001451 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001452
Bram Moolenaar774267b2013-05-21 20:51:59 +02001453 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001454 self->cur = start;
1455 self->next = next;
1456 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001457 self->traverse = traverse;
1458 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001459
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001460 return (PyObject *)(self);
1461}
1462
1463 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001464IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001465{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001466 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001467 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001468 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001469}
1470
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001471 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001472IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001473{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001474 if (self->traverse != NULL)
1475 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001476 else
1477 return 0;
1478}
1479
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001480/* Mac OSX defines clear() somewhere. */
1481#ifdef clear
1482# undef clear
1483#endif
1484
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001485 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001486IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001487{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001488 if (self->clear != NULL)
1489 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001490 else
1491 return 0;
1492}
1493
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001494 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001495IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001496{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001497 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001498}
1499
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001500 static PyObject *
1501IterIter(PyObject *self)
1502{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001503 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001504 return self;
1505}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001506
Bram Moolenaardb913952012-06-29 12:54:53 +02001507typedef struct pylinkedlist_S {
1508 struct pylinkedlist_S *pll_next;
1509 struct pylinkedlist_S *pll_prev;
1510 PyObject *pll_obj;
1511} pylinkedlist_T;
1512
1513static pylinkedlist_T *lastdict = NULL;
1514static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001515static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001516
1517 static void
1518pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1519{
1520 if (ref->pll_prev == NULL)
1521 {
1522 if (ref->pll_next == NULL)
1523 {
1524 *last = NULL;
1525 return;
1526 }
1527 }
1528 else
1529 ref->pll_prev->pll_next = ref->pll_next;
1530
1531 if (ref->pll_next == NULL)
1532 *last = ref->pll_prev;
1533 else
1534 ref->pll_next->pll_prev = ref->pll_prev;
1535}
1536
1537 static void
1538pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1539{
1540 if (*last == NULL)
1541 ref->pll_prev = NULL;
1542 else
1543 {
1544 (*last)->pll_next = ref;
1545 ref->pll_prev = *last;
1546 }
1547 ref->pll_next = NULL;
1548 ref->pll_obj = self;
1549 *last = ref;
1550}
1551
1552static PyTypeObject DictionaryType;
1553
1554typedef struct
1555{
1556 PyObject_HEAD
1557 dict_T *dict;
1558 pylinkedlist_T ref;
1559} DictionaryObject;
1560
Bram Moolenaara9922d62013-05-30 13:01:18 +02001561static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1562
1563#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1564
Bram Moolenaardb913952012-06-29 12:54:53 +02001565 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001566DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001567{
1568 DictionaryObject *self;
1569
Bram Moolenaara9922d62013-05-30 13:01:18 +02001570 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001571 if (self == NULL)
1572 return NULL;
1573 self->dict = dict;
1574 ++dict->dv_refcount;
1575
1576 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1577
1578 return (PyObject *)(self);
1579}
1580
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001582py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001583{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001584 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001585
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001586 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001587 {
1588 PyErr_NoMemory();
1589 return NULL;
1590 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001591 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594}
1595
1596 static PyObject *
1597DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1598{
1599 DictionaryObject *self;
1600 dict_T *dict;
1601
1602 if (!(dict = py_dict_alloc()))
1603 return NULL;
1604
1605 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1606
1607 --dict->dv_refcount;
1608
1609 if (kwargs || PyTuple_Size(args))
1610 {
1611 PyObject *tmp;
1612 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1613 {
1614 Py_DECREF(self);
1615 return NULL;
1616 }
1617
1618 Py_DECREF(tmp);
1619 }
1620
1621 return (PyObject *)(self);
1622}
1623
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001624 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001625DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001626{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001627 pyll_remove(&self->ref, &lastdict);
1628 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001629
1630 DESTRUCTOR_FINISH(self);
1631}
1632
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001633static char *DictionaryAttrs[] = {
1634 "locked", "scope",
1635 NULL
1636};
1637
1638 static PyObject *
1639DictionaryDir(PyObject *self)
1640{
1641 return ObjectDir(self, DictionaryAttrs);
1642}
1643
Bram Moolenaardb913952012-06-29 12:54:53 +02001644 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001645DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001646{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001647 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001648 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001649 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001650 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001651 return -1;
1652 }
1653
1654 if (strcmp(name, "locked") == 0)
1655 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001656 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001657 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001658 PyErr_SET_STRING(PyExc_TypeError,
1659 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001660 return -1;
1661 }
1662 else
1663 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001665 if (istrue == -1)
1666 return -1;
1667 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001668 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001669 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001670 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001671 }
1672 return 0;
1673 }
1674 else
1675 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001676 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001677 return -1;
1678 }
1679}
1680
1681 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001682DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001683{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001684 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001685}
1686
Bram Moolenaara9922d62013-05-30 13:01:18 +02001687#define DICT_FLAG_HAS_DEFAULT 0x01
1688#define DICT_FLAG_POP 0x02
1689#define DICT_FLAG_NONE_DEFAULT 0x04
1690#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1691#define DICT_FLAG_RETURN_PAIR 0x10
1692
Bram Moolenaardb913952012-06-29 12:54:53 +02001693 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001694_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001695{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001696 PyObject *keyObject;
1697 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001698 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001699 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001700 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701 dict_T *dict = self->dict;
1702 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001703 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001704
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705 if (flags & DICT_FLAG_HAS_DEFAULT)
1706 {
1707 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1708 return NULL;
1709 }
1710 else
1711 keyObject = args;
1712
1713 if (flags & DICT_FLAG_RETURN_BOOL)
1714 defObject = Py_False;
1715
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001716 if (!(key = StringToChars(keyObject, &todecref)))
1717 return NULL;
1718
1719 if (*key == NUL)
1720 {
1721 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001722 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001723 return NULL;
1724 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001725
Bram Moolenaara9922d62013-05-30 13:01:18 +02001726 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001727
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001728 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001729
Bram Moolenaara9922d62013-05-30 13:01:18 +02001730 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001731 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001732 if (defObject)
1733 {
1734 Py_INCREF(defObject);
1735 return defObject;
1736 }
1737 else
1738 {
1739 PyErr_SetObject(PyExc_KeyError, keyObject);
1740 return NULL;
1741 }
1742 }
1743 else if (flags & DICT_FLAG_RETURN_BOOL)
1744 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001745 ret = Py_True;
1746 Py_INCREF(ret);
1747 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001748 }
1749
1750 di = dict_lookup(hi);
1751
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001752 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001753 return NULL;
1754
1755 if (flags & DICT_FLAG_POP)
1756 {
1757 if (dict->dv_lock)
1758 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001759 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001760 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001761 return NULL;
1762 }
1763
1764 hash_remove(&dict->dv_hashtab, hi);
1765 dictitem_free(di);
1766 }
1767
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001768 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001769}
1770
1771 static PyObject *
1772DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1773{
1774 return _DictionaryItem(self, keyObject, 0);
1775}
1776
1777 static int
1778DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1779{
1780 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001781 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001782
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001783 if (rObj == NULL)
1784 return -1;
1785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001786 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001787
Bram Moolenaardee2e312013-06-23 16:35:47 +02001788 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001790 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001791}
1792
1793typedef struct
1794{
1795 hashitem_T *ht_array;
1796 long_u ht_used;
1797 hashtab_T *ht;
1798 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001799 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001800} dictiterinfo_T;
1801
1802 static PyObject *
1803DictionaryIterNext(dictiterinfo_T **dii)
1804{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001805 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001806
1807 if (!(*dii)->todo)
1808 return NULL;
1809
1810 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1811 (*dii)->ht->ht_used != (*dii)->ht_used)
1812 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001813 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001814 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001815 return NULL;
1816 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001817
Bram Moolenaara9922d62013-05-30 13:01:18 +02001818 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1819 ++((*dii)->hi);
1820
1821 --((*dii)->todo);
1822
Bram Moolenaar41009372013-07-01 22:03:04 +02001823 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001824 return NULL;
1825
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001826 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001827}
1828
1829 static PyObject *
1830DictionaryIter(DictionaryObject *self)
1831{
1832 dictiterinfo_T *dii;
1833 hashtab_T *ht;
1834
1835 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1836 {
1837 PyErr_NoMemory();
1838 return NULL;
1839 }
1840
1841 ht = &self->dict->dv_hashtab;
1842 dii->ht_array = ht->ht_array;
1843 dii->ht_used = ht->ht_used;
1844 dii->ht = ht;
1845 dii->hi = dii->ht_array;
1846 dii->todo = dii->ht_used;
1847
1848 return IterNew(dii,
1849 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1850 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001851}
1852
1853 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001854DictionaryAssItem(
1855 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001856{
1857 char_u *key;
1858 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001859 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001860 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001861 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001862
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001863 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001864 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001865 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866 return -1;
1867 }
1868
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001869 if (!(key = StringToChars(keyObject, &todecref)))
1870 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001871
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001872 if (*key == NUL)
1873 {
1874 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001875 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001876 return -1;
1877 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001878
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001879 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001880
1881 if (valObject == NULL)
1882 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001883 hashitem_T *hi;
1884
Bram Moolenaardb913952012-06-29 12:54:53 +02001885 if (di == NULL)
1886 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001887 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001888 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001889 return -1;
1890 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001891 hi = hash_find(&dict->dv_hashtab, di->di_key);
1892 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001894 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 return 0;
1896 }
1897
1898 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001899 {
1900 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001902 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001903
1904 if (di == NULL)
1905 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001906 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001908 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001909 PyErr_NoMemory();
1910 return -1;
1911 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001912 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001913
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001914 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001915 {
1916 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001917 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001918 RAISE_KEY_ADD_FAIL(key);
1919 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001920 return -1;
1921 }
1922 }
1923 else
1924 clear_tv(&di->di_tv);
1925
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001926 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001927
1928 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001929 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001930 return 0;
1931}
1932
Bram Moolenaara9922d62013-05-30 13:01:18 +02001933typedef PyObject *(*hi_to_py)(hashitem_T *);
1934
Bram Moolenaardb913952012-06-29 12:54:53 +02001935 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001936DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001937{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001938 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001939 long_u todo = dict->dv_hashtab.ht_used;
1940 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001941 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001943 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001944
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001945 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001946 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1947 {
1948 if (!HASHITEM_EMPTY(hi))
1949 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001950 if (!(newObj = hiconvert(hi)))
1951 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001952 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001953 return NULL;
1954 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001955 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001956 --todo;
1957 ++i;
1958 }
1959 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001960 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001961}
1962
Bram Moolenaara9922d62013-05-30 13:01:18 +02001963 static PyObject *
1964dict_key(hashitem_T *hi)
1965{
1966 return PyBytes_FromString((char *)(hi->hi_key));
1967}
1968
1969 static PyObject *
1970DictionaryListKeys(DictionaryObject *self)
1971{
1972 return DictionaryListObjects(self, dict_key);
1973}
1974
1975 static PyObject *
1976dict_val(hashitem_T *hi)
1977{
1978 dictitem_T *di;
1979
1980 di = dict_lookup(hi);
1981 return ConvertToPyObject(&di->di_tv);
1982}
1983
1984 static PyObject *
1985DictionaryListValues(DictionaryObject *self)
1986{
1987 return DictionaryListObjects(self, dict_val);
1988}
1989
1990 static PyObject *
1991dict_item(hashitem_T *hi)
1992{
1993 PyObject *keyObject;
1994 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001995 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001996
1997 if (!(keyObject = dict_key(hi)))
1998 return NULL;
1999
2000 if (!(valObject = dict_val(hi)))
2001 {
2002 Py_DECREF(keyObject);
2003 return NULL;
2004 }
2005
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002006 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002007
2008 Py_DECREF(keyObject);
2009 Py_DECREF(valObject);
2010
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002011 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012}
2013
2014 static PyObject *
2015DictionaryListItems(DictionaryObject *self)
2016{
2017 return DictionaryListObjects(self, dict_item);
2018}
2019
2020 static PyObject *
2021DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2022{
2023 dict_T *dict = self->dict;
2024
2025 if (dict->dv_lock)
2026 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002027 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002028 return NULL;
2029 }
2030
2031 if (kwargs)
2032 {
2033 typval_T tv;
2034
2035 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2036 return NULL;
2037
2038 VimTryStart();
2039 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2040 clear_tv(&tv);
2041 if (VimTryEnd())
2042 return NULL;
2043 }
2044 else
2045 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002046 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002047
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002048 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002049 return NULL;
2050
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002051 if (obj == NULL)
2052 {
2053 Py_INCREF(Py_None);
2054 return Py_None;
2055 }
2056
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002057 if (PyObject_HasAttrString(obj, "keys"))
2058 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002059 else
2060 {
2061 PyObject *iterator;
2062 PyObject *item;
2063
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002064 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002065 return NULL;
2066
2067 while ((item = PyIter_Next(iterator)))
2068 {
2069 PyObject *fast;
2070 PyObject *keyObject;
2071 PyObject *valObject;
2072 PyObject *todecref;
2073 char_u *key;
2074 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002075 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002076
2077 if (!(fast = PySequence_Fast(item, "")))
2078 {
2079 Py_DECREF(iterator);
2080 Py_DECREF(item);
2081 return NULL;
2082 }
2083
2084 Py_DECREF(item);
2085
2086 if (PySequence_Fast_GET_SIZE(fast) != 2)
2087 {
2088 Py_DECREF(iterator);
2089 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002090 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002091 N_("expected sequence element of size 2, "
2092 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002093 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002094 return NULL;
2095 }
2096
2097 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2098
2099 if (!(key = StringToChars(keyObject, &todecref)))
2100 {
2101 Py_DECREF(iterator);
2102 Py_DECREF(fast);
2103 return NULL;
2104 }
2105
2106 di = dictitem_alloc(key);
2107
2108 Py_XDECREF(todecref);
2109
2110 if (di == NULL)
2111 {
2112 Py_DECREF(fast);
2113 Py_DECREF(iterator);
2114 PyErr_NoMemory();
2115 return NULL;
2116 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002117 di->di_tv.v_type = VAR_UNKNOWN;
2118
2119 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2120
2121 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2122 {
2123 Py_DECREF(iterator);
2124 Py_DECREF(fast);
2125 dictitem_free(di);
2126 return NULL;
2127 }
2128
2129 Py_DECREF(fast);
2130
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002131 hi = hash_find(&dict->dv_hashtab, di->di_key);
2132 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002133 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002134 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002135 Py_DECREF(iterator);
2136 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002137 return NULL;
2138 }
2139 }
2140
2141 Py_DECREF(iterator);
2142
2143 /* Iterator may have finished due to an exception */
2144 if (PyErr_Occurred())
2145 return NULL;
2146 }
2147 }
2148 Py_INCREF(Py_None);
2149 return Py_None;
2150}
2151
2152 static PyObject *
2153DictionaryGet(DictionaryObject *self, PyObject *args)
2154{
2155 return _DictionaryItem(self, args,
2156 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2157}
2158
2159 static PyObject *
2160DictionaryPop(DictionaryObject *self, PyObject *args)
2161{
2162 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2163}
2164
2165 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002166DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002167{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002168 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002169 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002170 PyObject *valObject;
2171 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002172
Bram Moolenaarde71b562013-06-02 17:41:54 +02002173 if (self->dict->dv_hashtab.ht_used == 0)
2174 {
2175 PyErr_SetNone(PyExc_KeyError);
2176 return NULL;
2177 }
2178
2179 hi = self->dict->dv_hashtab.ht_array;
2180 while (HASHITEM_EMPTY(hi))
2181 ++hi;
2182
2183 di = dict_lookup(hi);
2184
2185 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002186 return NULL;
2187
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002188 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002189 {
2190 Py_DECREF(valObject);
2191 return NULL;
2192 }
2193
2194 hash_remove(&self->dict->dv_hashtab, hi);
2195 dictitem_free(di);
2196
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002197 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002198}
2199
2200 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002201DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002202{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002203 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2204}
2205
2206static PySequenceMethods DictionaryAsSeq = {
2207 0, /* sq_length */
2208 0, /* sq_concat */
2209 0, /* sq_repeat */
2210 0, /* sq_item */
2211 0, /* sq_slice */
2212 0, /* sq_ass_item */
2213 0, /* sq_ass_slice */
2214 (objobjproc) DictionaryContains, /* sq_contains */
2215 0, /* sq_inplace_concat */
2216 0, /* sq_inplace_repeat */
2217};
2218
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002219static PyMappingMethods DictionaryAsMapping = {
2220 (lenfunc) DictionaryLength,
2221 (binaryfunc) DictionaryItem,
2222 (objobjargproc) DictionaryAssItem,
2223};
2224
Bram Moolenaardb913952012-06-29 12:54:53 +02002225static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002226 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002227 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2228 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2229 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2230 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2231 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002232 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002233 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002234 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2235 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002236};
2237
2238static PyTypeObject ListType;
2239
2240typedef struct
2241{
2242 PyObject_HEAD
2243 list_T *list;
2244 pylinkedlist_T ref;
2245} ListObject;
2246
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002247#define NEW_LIST(list) ListNew(&ListType, list)
2248
Bram Moolenaardb913952012-06-29 12:54:53 +02002249 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002250ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002251{
2252 ListObject *self;
2253
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002254 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002255 if (self == NULL)
2256 return NULL;
2257 self->list = list;
2258 ++list->lv_refcount;
2259
2260 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2261
2262 return (PyObject *)(self);
2263}
2264
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002265 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002266py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002267{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002268 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002269
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002270 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002271 {
2272 PyErr_NoMemory();
2273 return NULL;
2274 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002275 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002277 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002278}
2279
2280 static int
2281list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2282{
2283 PyObject *iterator;
2284 PyObject *item;
2285 listitem_T *li;
2286
2287 if (!(iterator = PyObject_GetIter(obj)))
2288 return -1;
2289
2290 while ((item = PyIter_Next(iterator)))
2291 {
2292 if (!(li = listitem_alloc()))
2293 {
2294 PyErr_NoMemory();
2295 Py_DECREF(item);
2296 Py_DECREF(iterator);
2297 return -1;
2298 }
2299 li->li_tv.v_lock = 0;
2300 li->li_tv.v_type = VAR_UNKNOWN;
2301
2302 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2303 {
2304 Py_DECREF(item);
2305 Py_DECREF(iterator);
2306 listitem_free(li);
2307 return -1;
2308 }
2309
2310 Py_DECREF(item);
2311
2312 list_append(l, li);
2313 }
2314
2315 Py_DECREF(iterator);
2316
2317 /* Iterator may have finished due to an exception */
2318 if (PyErr_Occurred())
2319 return -1;
2320
2321 return 0;
2322}
2323
2324 static PyObject *
2325ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2326{
2327 list_T *list;
2328 PyObject *obj = NULL;
2329
2330 if (kwargs)
2331 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002332 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002333 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002334 return NULL;
2335 }
2336
2337 if (!PyArg_ParseTuple(args, "|O", &obj))
2338 return NULL;
2339
2340 if (!(list = py_list_alloc()))
2341 return NULL;
2342
2343 if (obj)
2344 {
2345 PyObject *lookup_dict;
2346
2347 if (!(lookup_dict = PyDict_New()))
2348 {
2349 list_unref(list);
2350 return NULL;
2351 }
2352
2353 if (list_py_concat(list, obj, lookup_dict) == -1)
2354 {
2355 Py_DECREF(lookup_dict);
2356 list_unref(list);
2357 return NULL;
2358 }
2359
2360 Py_DECREF(lookup_dict);
2361 }
2362
2363 return ListNew(subtype, list);
2364}
2365
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002366 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002367ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002368{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002369 pyll_remove(&self->ref, &lastlist);
2370 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002371
2372 DESTRUCTOR_FINISH(self);
2373}
2374
Bram Moolenaardb913952012-06-29 12:54:53 +02002375 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002377{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002378 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002379}
2380
2381 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002382ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002383{
2384 listitem_T *li;
2385
Bram Moolenaard6e39182013-05-21 18:30:34 +02002386 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002387 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002388 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002389 return NULL;
2390 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002391 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002392 if (li == NULL)
2393 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002394 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002395 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002396 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002397 return NULL;
2398 }
2399 return ConvertToPyObject(&li->li_tv);
2400}
2401
Bram Moolenaardb913952012-06-29 12:54:53 +02002402 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002403ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2404 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002405{
2406 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002407 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002408
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002409 if (step == 0)
2410 {
2411 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2412 return NULL;
2413 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002414
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002415 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 if (list == NULL)
2417 return NULL;
2418
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002419 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002420 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002421 PyObject *item;
2422
2423 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002424 if (item == NULL)
2425 {
2426 Py_DECREF(list);
2427 return NULL;
2428 }
2429
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002430 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002431 }
2432
2433 return list;
2434}
2435
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002436 static PyObject *
2437ListItem(ListObject *self, PyObject* idx)
2438{
2439#if PY_MAJOR_VERSION < 3
2440 if (PyInt_Check(idx))
2441 {
2442 long _idx = PyInt_AsLong(idx);
2443 return ListIndex(self, _idx);
2444 }
2445 else
2446#endif
2447 if (PyLong_Check(idx))
2448 {
2449 long _idx = PyLong_AsLong(idx);
2450 return ListIndex(self, _idx);
2451 }
2452 else if (PySlice_Check(idx))
2453 {
2454 Py_ssize_t start, stop, step, slicelen;
2455
Bram Moolenaar922a4662014-03-30 16:11:43 +02002456 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002457 &start, &stop, &step, &slicelen) < 0)
2458 return NULL;
2459 return ListSlice(self, start, step, slicelen);
2460 }
2461 else
2462 {
2463 RAISE_INVALID_INDEX_TYPE(idx);
2464 return NULL;
2465 }
2466}
2467
2468 static void
2469list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2470 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2471{
2472 while (numreplaced--)
2473 {
2474 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2475 listitem_remove(l, lis[slicelen + numreplaced]);
2476 }
2477 while (numadded--)
2478 {
2479 listitem_T *next;
2480
2481 next = lastaddedli->li_prev;
2482 listitem_remove(l, lastaddedli);
2483 lastaddedli = next;
2484 }
2485}
2486
2487 static int
2488ListAssSlice(ListObject *self, Py_ssize_t first,
2489 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2490{
2491 PyObject *iterator;
2492 PyObject *item;
2493 listitem_T *li;
2494 listitem_T *lastaddedli = NULL;
2495 listitem_T *next;
2496 typval_T v;
2497 list_T *l = self->list;
2498 PyInt i;
2499 PyInt j;
2500 PyInt numreplaced = 0;
2501 PyInt numadded = 0;
2502 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002503 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002504
2505 size = ListLength(self);
2506
2507 if (l->lv_lock)
2508 {
2509 RAISE_LOCKED_LIST;
2510 return -1;
2511 }
2512
2513 if (step == 0)
2514 {
2515 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2516 return -1;
2517 }
2518
2519 if (step != 1 && slicelen == 0)
2520 {
2521 /* Nothing to do. Only error out if obj has some items. */
2522 int ret = 0;
2523
2524 if (obj == NULL)
2525 return 0;
2526
2527 if (!(iterator = PyObject_GetIter(obj)))
2528 return -1;
2529
2530 if ((item = PyIter_Next(iterator)))
2531 {
2532 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002533 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002534 "to extended slice"), 0);
2535 Py_DECREF(item);
2536 ret = -1;
2537 }
2538 Py_DECREF(iterator);
2539 return ret;
2540 }
2541
2542 if (obj != NULL)
2543 /* XXX May allocate zero bytes. */
2544 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2545 {
2546 PyErr_NoMemory();
2547 return -1;
2548 }
2549
2550 if (first == size)
2551 li = NULL;
2552 else
2553 {
2554 li = list_find(l, (long) first);
2555 if (li == NULL)
2556 {
2557 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2558 (int)first);
2559 if (obj != NULL)
2560 PyMem_Free(lis);
2561 return -1;
2562 }
2563 i = slicelen;
2564 while (i-- && li != NULL)
2565 {
2566 j = step;
2567 next = li;
2568 if (step > 0)
2569 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2570 else
2571 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2572
2573 if (obj == NULL)
2574 listitem_remove(l, li);
2575 else
2576 lis[slicelen - i - 1] = li;
2577
2578 li = next;
2579 }
2580 if (li == NULL && i != -1)
2581 {
2582 PyErr_SET_VIM(N_("internal error: not enough list items"));
2583 if (obj != NULL)
2584 PyMem_Free(lis);
2585 return -1;
2586 }
2587 }
2588
2589 if (obj == NULL)
2590 return 0;
2591
2592 if (!(iterator = PyObject_GetIter(obj)))
2593 {
2594 PyMem_Free(lis);
2595 return -1;
2596 }
2597
2598 i = 0;
2599 while ((item = PyIter_Next(iterator)))
2600 {
2601 if (ConvertFromPyObject(item, &v) == -1)
2602 {
2603 Py_DECREF(iterator);
2604 Py_DECREF(item);
2605 PyMem_Free(lis);
2606 return -1;
2607 }
2608 Py_DECREF(item);
2609 if (list_insert_tv(l, &v, numreplaced < slicelen
2610 ? lis[numreplaced]
2611 : li) == FAIL)
2612 {
2613 clear_tv(&v);
2614 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2615 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2616 PyMem_Free(lis);
2617 return -1;
2618 }
2619 if (numreplaced < slicelen)
2620 {
2621 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002622 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002623 numreplaced++;
2624 }
2625 else
2626 {
2627 if (li)
2628 lastaddedli = li->li_prev;
2629 else
2630 lastaddedli = l->lv_last;
2631 numadded++;
2632 }
2633 clear_tv(&v);
2634 if (step != 1 && i >= slicelen)
2635 {
2636 Py_DECREF(iterator);
2637 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002638 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002639 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002640 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2641 PyMem_Free(lis);
2642 return -1;
2643 }
2644 ++i;
2645 }
2646 Py_DECREF(iterator);
2647
2648 if (step != 1 && i != slicelen)
2649 {
2650 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002651 N_("attempt to assign sequence of size %d to extended slice "
2652 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002653 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2654 PyMem_Free(lis);
2655 return -1;
2656 }
2657
2658 if (PyErr_Occurred())
2659 {
2660 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2661 PyMem_Free(lis);
2662 return -1;
2663 }
2664
2665 for (i = 0; i < numreplaced; i++)
2666 listitem_free(lis[i]);
2667 if (step == 1)
2668 for (i = numreplaced; i < slicelen; i++)
2669 listitem_remove(l, lis[i]);
2670
2671 PyMem_Free(lis);
2672
2673 return 0;
2674}
2675
2676 static int
2677ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2678{
2679 typval_T tv;
2680 list_T *l = self->list;
2681 listitem_T *li;
2682 Py_ssize_t length = ListLength(self);
2683
2684 if (l->lv_lock)
2685 {
2686 RAISE_LOCKED_LIST;
2687 return -1;
2688 }
2689 if (index > length || (index == length && obj == NULL))
2690 {
2691 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2692 return -1;
2693 }
2694
2695 if (obj == NULL)
2696 {
2697 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002698 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002699 clear_tv(&li->li_tv);
2700 vim_free(li);
2701 return 0;
2702 }
2703
2704 if (ConvertFromPyObject(obj, &tv) == -1)
2705 return -1;
2706
2707 if (index == length)
2708 {
2709 if (list_append_tv(l, &tv) == FAIL)
2710 {
2711 clear_tv(&tv);
2712 PyErr_SET_VIM(N_("failed to add item to list"));
2713 return -1;
2714 }
2715 }
2716 else
2717 {
2718 li = list_find(l, (long) index);
2719 clear_tv(&li->li_tv);
2720 copy_tv(&tv, &li->li_tv);
2721 clear_tv(&tv);
2722 }
2723 return 0;
2724}
2725
2726 static Py_ssize_t
2727ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2728{
2729#if PY_MAJOR_VERSION < 3
2730 if (PyInt_Check(idx))
2731 {
2732 long _idx = PyInt_AsLong(idx);
2733 return ListAssIndex(self, _idx, obj);
2734 }
2735 else
2736#endif
2737 if (PyLong_Check(idx))
2738 {
2739 long _idx = PyLong_AsLong(idx);
2740 return ListAssIndex(self, _idx, obj);
2741 }
2742 else if (PySlice_Check(idx))
2743 {
2744 Py_ssize_t start, stop, step, slicelen;
2745
Bram Moolenaar922a4662014-03-30 16:11:43 +02002746 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002747 &start, &stop, &step, &slicelen) < 0)
2748 return -1;
2749 return ListAssSlice(self, start, step, slicelen,
2750 obj);
2751 }
2752 else
2753 {
2754 RAISE_INVALID_INDEX_TYPE(idx);
2755 return -1;
2756 }
2757}
2758
2759 static PyObject *
2760ListConcatInPlace(ListObject *self, PyObject *obj)
2761{
2762 list_T *l = self->list;
2763 PyObject *lookup_dict;
2764
2765 if (l->lv_lock)
2766 {
2767 RAISE_LOCKED_LIST;
2768 return NULL;
2769 }
2770
2771 if (!(lookup_dict = PyDict_New()))
2772 return NULL;
2773
2774 if (list_py_concat(l, obj, lookup_dict) == -1)
2775 {
2776 Py_DECREF(lookup_dict);
2777 return NULL;
2778 }
2779 Py_DECREF(lookup_dict);
2780
2781 Py_INCREF(self);
2782 return (PyObject *)(self);
2783}
2784
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002785typedef struct
2786{
2787 listwatch_T lw;
2788 list_T *list;
2789} listiterinfo_T;
2790
2791 static void
2792ListIterDestruct(listiterinfo_T *lii)
2793{
2794 list_rem_watch(lii->list, &lii->lw);
2795 PyMem_Free(lii);
2796}
2797
2798 static PyObject *
2799ListIterNext(listiterinfo_T **lii)
2800{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002801 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002802
2803 if (!((*lii)->lw.lw_item))
2804 return NULL;
2805
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002806 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002807 return NULL;
2808
2809 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2810
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002811 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002812}
2813
2814 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002815ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002816{
2817 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002818 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002819
2820 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2821 {
2822 PyErr_NoMemory();
2823 return NULL;
2824 }
2825
2826 list_add_watch(l, &lii->lw);
2827 lii->lw.lw_item = l->lv_first;
2828 lii->list = l;
2829
2830 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002831 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2832 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002833}
2834
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002835static char *ListAttrs[] = {
2836 "locked",
2837 NULL
2838};
2839
2840 static PyObject *
2841ListDir(PyObject *self)
2842{
2843 return ObjectDir(self, ListAttrs);
2844}
2845
Bram Moolenaar66b79852012-09-21 14:00:35 +02002846 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002847ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002848{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002849 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002850 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002851 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002852 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002853 return -1;
2854 }
2855
2856 if (strcmp(name, "locked") == 0)
2857 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002858 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002859 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002860 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002861 return -1;
2862 }
2863 else
2864 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002865 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002866 if (istrue == -1)
2867 return -1;
2868 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002869 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002871 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002872 }
2873 return 0;
2874 }
2875 else
2876 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002877 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002878 return -1;
2879 }
2880}
2881
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002882static PySequenceMethods ListAsSeq = {
2883 (lenfunc) ListLength, /* sq_length, len(x) */
2884 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2885 0, /* RangeRepeat, sq_repeat, x*n */
2886 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2887 0, /* was_sq_slice, x[i:j] */
2888 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2889 0, /* was_sq_ass_slice, x[i:j]=v */
2890 0, /* sq_contains */
2891 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2892 0, /* sq_inplace_repeat */
2893};
2894
2895static PyMappingMethods ListAsMapping = {
2896 /* mp_length */ (lenfunc) ListLength,
2897 /* mp_subscript */ (binaryfunc) ListItem,
2898 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2899};
2900
Bram Moolenaardb913952012-06-29 12:54:53 +02002901static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002902 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2903 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2904 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002905};
2906
2907typedef struct
2908{
2909 PyObject_HEAD
2910 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002911 int argc;
2912 typval_T *argv;
2913 dict_T *self;
2914 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002915 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002916} FunctionObject;
2917
2918static PyTypeObject FunctionType;
2919
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002920#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2921 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002922
Bram Moolenaardb913952012-06-29 12:54:53 +02002923 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002924FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002925 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002926{
2927 FunctionObject *self;
2928
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002929 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002930 if (self == NULL)
2931 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002932
2933 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002934 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002935 if (!translated_function_exists(name))
2936 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002937 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002938 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002939 return NULL;
2940 }
2941 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002942 }
2943 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002944 {
2945 char_u *p;
2946
2947 if ((p = get_expanded_name(name,
2948 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002949 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002950 PyErr_FORMAT(PyExc_ValueError,
2951 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002952 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002953 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002954
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002955 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2956 {
2957 char_u *np;
2958 size_t len = STRLEN(p) + 1;
2959
Bram Moolenaar80dae042018-12-23 13:36:40 +01002960 if ((np = alloc((int)len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002961 {
2962 vim_free(p);
2963 return NULL;
2964 }
2965 mch_memmove(np, "<SNR>", 5);
2966 mch_memmove(np + 5, p + 3, len - 3);
2967 vim_free(p);
2968 self->name = np;
2969 }
2970 else
2971 self->name = p;
2972 }
2973
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002974 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002975 self->argc = argc;
2976 self->argv = argv;
2977 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002978 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002979
2980 if (self->argv || self->self)
2981 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2982
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002983 return (PyObject *)(self);
2984}
2985
2986 static PyObject *
2987FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2988{
2989 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002990 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002991 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002992 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002993 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002994 typval_T selfdicttv;
2995 typval_T argstv;
2996 list_T *argslist = NULL;
2997 dict_T *selfdict = NULL;
2998 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002999 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003000 typval_T *argv = NULL;
3001 typval_T *curtv;
3002 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003003
Bram Moolenaar8110a092016-04-14 15:56:09 +02003004 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003005 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003006 selfdictObject = PyDict_GetItemString(kwargs, "self");
3007 if (selfdictObject != NULL)
3008 {
3009 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3010 return NULL;
3011 selfdict = selfdicttv.vval.v_dict;
3012 }
3013 argsObject = PyDict_GetItemString(kwargs, "args");
3014 if (argsObject != NULL)
3015 {
3016 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3017 {
3018 dict_unref(selfdict);
3019 return NULL;
3020 }
3021 argslist = argstv.vval.v_list;
3022
3023 argc = argslist->lv_len;
3024 if (argc != 0)
3025 {
3026 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003027 if (argv == NULL)
3028 {
3029 PyErr_NoMemory();
3030 dict_unref(selfdict);
3031 list_unref(argslist);
3032 return NULL;
3033 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003034 curtv = argv;
3035 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3036 copy_tv(&li->li_tv, curtv++);
3037 }
3038 list_unref(argslist);
3039 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003040 if (selfdict != NULL)
3041 {
3042 auto_rebind = FALSE;
3043 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3044 if (autoRebindObject != NULL)
3045 {
3046 auto_rebind = PyObject_IsTrue(autoRebindObject);
3047 if (auto_rebind == -1)
3048 {
3049 dict_unref(selfdict);
3050 list_unref(argslist);
3051 return NULL;
3052 }
3053 }
3054 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003055 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003056
Bram Moolenaar389a1792013-06-23 13:00:44 +02003057 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003058 {
3059 dict_unref(selfdict);
3060 while (argc--)
3061 clear_tv(&argv[argc]);
3062 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003063 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003064 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003065
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003066 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003067
Bram Moolenaar389a1792013-06-23 13:00:44 +02003068 PyMem_Free(name);
3069
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003070 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003071}
3072
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003073 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003074FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003075{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003076 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003077 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003078 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003079 for (i = 0; i < self->argc; ++i)
3080 clear_tv(&self->argv[i]);
3081 PyMem_Free(self->argv);
3082 dict_unref(self->self);
3083 if (self->argv || self->self)
3084 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003085
3086 DESTRUCTOR_FINISH(self);
3087}
3088
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003089static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003090 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003091 NULL
3092};
3093
3094 static PyObject *
3095FunctionDir(PyObject *self)
3096{
3097 return ObjectDir(self, FunctionAttrs);
3098}
3099
Bram Moolenaardb913952012-06-29 12:54:53 +02003100 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003101FunctionAttr(FunctionObject *self, char *name)
3102{
3103 list_T *list;
3104 int i;
3105 if (strcmp(name, "name") == 0)
3106 return PyString_FromString((char *)(self->name));
3107 else if (strcmp(name, "args") == 0)
3108 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003109 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003110 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003111
Bram Moolenaar8110a092016-04-14 15:56:09 +02003112 for (i = 0; i < self->argc; ++i)
3113 list_append_tv(list, &self->argv[i]);
3114 return NEW_LIST(list);
3115 }
3116 else if (strcmp(name, "self") == 0)
3117 return self->self == NULL
3118 ? AlwaysNone(NULL)
3119 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003120 else if (strcmp(name, "auto_rebind") == 0)
3121 return self->auto_rebind
3122 ? AlwaysTrue(NULL)
3123 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003124 else if (strcmp(name, "__members__") == 0)
3125 return ObjectDir(NULL, FunctionAttrs);
3126 return NULL;
3127}
3128
3129/* Populate partial_T given function object.
3130 *
3131 * "exported" should be set to true when it is needed to construct a partial
3132 * that may be stored in a variable (i.e. may be freed by Vim).
3133 */
3134 static void
3135set_partial(FunctionObject *self, partial_T *pt, int exported)
3136{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003137 int i;
3138
3139 pt->pt_name = self->name;
3140 if (self->argv)
3141 {
3142 pt->pt_argc = self->argc;
3143 if (exported)
3144 {
3145 pt->pt_argv = (typval_T *)alloc_clear(
3146 sizeof(typval_T) * self->argc);
3147 for (i = 0; i < pt->pt_argc; ++i)
3148 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3149 }
3150 else
3151 pt->pt_argv = self->argv;
3152 }
3153 else
3154 {
3155 pt->pt_argc = 0;
3156 pt->pt_argv = NULL;
3157 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003158 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003159 pt->pt_dict = self->self;
3160 if (exported && self->self)
3161 ++pt->pt_dict->dv_refcount;
3162 if (exported)
3163 pt->pt_name = vim_strsave(pt->pt_name);
3164 pt->pt_refcount = 1;
3165}
3166
3167 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003168FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003169{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003170 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003171 typval_T args;
3172 typval_T selfdicttv;
3173 typval_T rettv;
3174 dict_T *selfdict = NULL;
3175 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003176 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003177 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003178 partial_T pt;
3179 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003180
Bram Moolenaar8110a092016-04-14 15:56:09 +02003181 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003182 return NULL;
3183
3184 if (kwargs != NULL)
3185 {
3186 selfdictObject = PyDict_GetItemString(kwargs, "self");
3187 if (selfdictObject != NULL)
3188 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003189 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003190 {
3191 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003192 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003193 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003194 selfdict = selfdicttv.vval.v_dict;
3195 }
3196 }
3197
Bram Moolenaar8110a092016-04-14 15:56:09 +02003198 if (self->argv || self->self)
3199 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003200 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003201 set_partial(self, &pt, FALSE);
3202 pt_ptr = &pt;
3203 }
3204
Bram Moolenaar71700b82013-05-15 17:49:05 +02003205 Py_BEGIN_ALLOW_THREADS
3206 Python_Lock_Vim();
3207
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003208 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003209 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003210
3211 Python_Release_Vim();
3212 Py_END_ALLOW_THREADS
3213
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003214 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003215 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003216 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003217 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003218 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003219 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003220 }
3221 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003222 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003223
Bram Moolenaardb913952012-06-29 12:54:53 +02003224 clear_tv(&args);
3225 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003226 if (selfdict != NULL)
3227 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003228
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003229 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003230}
3231
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003232 static PyObject *
3233FunctionRepr(FunctionObject *self)
3234{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003235 PyObject *ret;
3236 garray_T repr_ga;
3237 int i;
3238 char_u *tofree = NULL;
3239 typval_T tv;
3240 char_u numbuf[NUMBUFLEN];
3241
3242 ga_init2(&repr_ga, (int)sizeof(char), 70);
3243 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3244 if (self->name)
3245 ga_concat(&repr_ga, self->name);
3246 else
3247 ga_concat(&repr_ga, (char_u *)"<NULL>");
3248 ga_append(&repr_ga, '\'');
3249 if (self->argv)
3250 {
3251 ga_concat(&repr_ga, (char_u *)", args=[");
3252 ++emsg_silent;
3253 for (i = 0; i < self->argc; i++)
3254 {
3255 if (i != 0)
3256 ga_concat(&repr_ga, (char_u *)", ");
3257 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3258 get_copyID()));
3259 vim_free(tofree);
3260 }
3261 --emsg_silent;
3262 ga_append(&repr_ga, ']');
3263 }
3264 if (self->self)
3265 {
3266 ga_concat(&repr_ga, (char_u *)", self=");
3267 tv.v_type = VAR_DICT;
3268 tv.vval.v_dict = self->self;
3269 ++emsg_silent;
3270 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3271 --emsg_silent;
3272 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003273 if (self->auto_rebind)
3274 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003275 }
3276 ga_append(&repr_ga, '>');
3277 ret = PyString_FromString((char *)repr_ga.ga_data);
3278 ga_clear(&repr_ga);
3279 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003280}
3281
Bram Moolenaardb913952012-06-29 12:54:53 +02003282static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003283 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3284 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003285};
3286
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003287/*
3288 * Options object
3289 */
3290
3291static PyTypeObject OptionsType;
3292
3293typedef int (*checkfun)(void *);
3294
3295typedef struct
3296{
3297 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003298 int opt_type;
3299 void *from;
3300 checkfun Check;
3301 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003302} OptionsObject;
3303
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003304 static int
3305dummy_check(void *arg UNUSED)
3306{
3307 return 0;
3308}
3309
3310 static PyObject *
3311OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3312{
3313 OptionsObject *self;
3314
Bram Moolenaar774267b2013-05-21 20:51:59 +02003315 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003316 if (self == NULL)
3317 return NULL;
3318
3319 self->opt_type = opt_type;
3320 self->from = from;
3321 self->Check = Check;
3322 self->fromObj = fromObj;
3323 if (fromObj)
3324 Py_INCREF(fromObj);
3325
3326 return (PyObject *)(self);
3327}
3328
3329 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003330OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003331{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003332 PyObject_GC_UnTrack((void *)(self));
3333 Py_XDECREF(self->fromObj);
3334 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003335}
3336
3337 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003338OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003339{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003340 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341 return 0;
3342}
3343
3344 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003345OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003346{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003347 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003348 return 0;
3349}
3350
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003351 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003352OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003353{
3354 char_u *key;
3355 int flags;
3356 long numval;
3357 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003358 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003359
Bram Moolenaard6e39182013-05-21 18:30:34 +02003360 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003361 return NULL;
3362
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003363 if (!(key = StringToChars(keyObject, &todecref)))
3364 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003365
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003366 if (*key == NUL)
3367 {
3368 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003369 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003370 return NULL;
3371 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003372
3373 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003374 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003375
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003376 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003377
3378 if (flags == 0)
3379 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003380 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003381 return NULL;
3382 }
3383
3384 if (flags & SOPT_UNSET)
3385 {
3386 Py_INCREF(Py_None);
3387 return Py_None;
3388 }
3389 else if (flags & SOPT_BOOL)
3390 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003391 PyObject *ret;
3392 ret = numval ? Py_True : Py_False;
3393 Py_INCREF(ret);
3394 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003395 }
3396 else if (flags & SOPT_NUM)
3397 return PyInt_FromLong(numval);
3398 else if (flags & SOPT_STRING)
3399 {
3400 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003401 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003402 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003403 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003404 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003405 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003406 else
3407 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003408 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003409 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003410 return NULL;
3411 }
3412 }
3413 else
3414 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003415 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003416 return NULL;
3417 }
3418}
3419
3420 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003421OptionsContains(OptionsObject *self, PyObject *keyObject)
3422{
3423 char_u *key;
3424 PyObject *todecref;
3425
3426 if (!(key = StringToChars(keyObject, &todecref)))
3427 return -1;
3428
3429 if (*key == NUL)
3430 {
3431 Py_XDECREF(todecref);
3432 return 0;
3433 }
3434
3435 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3436 {
3437 Py_XDECREF(todecref);
3438 return 1;
3439 }
3440 else
3441 {
3442 Py_XDECREF(todecref);
3443 return 0;
3444 }
3445}
3446
3447typedef struct
3448{
3449 void *lastoption;
3450 int opt_type;
3451} optiterinfo_T;
3452
3453 static PyObject *
3454OptionsIterNext(optiterinfo_T **oii)
3455{
3456 char_u *name;
3457
3458 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3459 return PyString_FromString((char *)name);
3460
3461 return NULL;
3462}
3463
3464 static PyObject *
3465OptionsIter(OptionsObject *self)
3466{
3467 optiterinfo_T *oii;
3468
3469 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3470 {
3471 PyErr_NoMemory();
3472 return NULL;
3473 }
3474
3475 oii->opt_type = self->opt_type;
3476 oii->lastoption = NULL;
3477
3478 return IterNew(oii,
3479 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3480 NULL, NULL);
3481}
3482
3483 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003484set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003485{
3486 char_u *errmsg;
3487
3488 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3489 {
3490 if (VimTryEnd())
3491 return FAIL;
3492 PyErr_SetVim((char *)errmsg);
3493 return FAIL;
3494 }
3495 return OK;
3496}
3497
3498 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003499set_option_value_for(
3500 char_u *key,
3501 int numval,
3502 char_u *stringval,
3503 int opt_flags,
3504 int opt_type,
3505 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003506{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003507 win_T *save_curwin = NULL;
3508 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003509 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003510 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003511
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003512 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003513 switch (opt_type)
3514 {
3515 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003516 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003517 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003518 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003519 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003520 if (VimTryEnd())
3521 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003522 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003523 return -1;
3524 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003525 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3526 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003527 break;
3528 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003529 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003530 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003531 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003532 break;
3533 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003534 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003535 break;
3536 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003537 if (set_ret == FAIL)
3538 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003539 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540}
3541
3542 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003543OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003544{
3545 char_u *key;
3546 int flags;
3547 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003548 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003549 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003550
Bram Moolenaard6e39182013-05-21 18:30:34 +02003551 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003552 return -1;
3553
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003554 if (!(key = StringToChars(keyObject, &todecref)))
3555 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003556
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003557 if (*key == NUL)
3558 {
3559 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003560 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003561 return -1;
3562 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003563
3564 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003565 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003566
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003567 if (flags == 0)
3568 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003569 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003570 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003571 return -1;
3572 }
3573
3574 if (valObject == NULL)
3575 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003576 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003578 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003579 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003580 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003581 return -1;
3582 }
3583 else if (!(flags & SOPT_GLOBAL))
3584 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003585 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003586 N_("unable to unset option %s "
3587 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003588 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003589 return -1;
3590 }
3591 else
3592 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003593 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003594 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003595 return 0;
3596 }
3597 }
3598
Bram Moolenaard6e39182013-05-21 18:30:34 +02003599 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003600
3601 if (flags & SOPT_BOOL)
3602 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003603 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003604
Bram Moolenaarb983f752013-05-15 16:11:50 +02003605 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003606 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003607 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003608 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003609 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003610 }
3611 else if (flags & SOPT_NUM)
3612 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003613 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003615 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003616 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003617 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003618 return -1;
3619 }
3620
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003621 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003622 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003623 }
3624 else
3625 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003626 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003627 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003628
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003629 if ((val = StringToChars(valObject, &todecref2)))
3630 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003632 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003633 Py_XDECREF(todecref2);
3634 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003635 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003636 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003637 }
3638
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003639 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003640
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003641 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003642}
3643
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003644static PySequenceMethods OptionsAsSeq = {
3645 0, /* sq_length */
3646 0, /* sq_concat */
3647 0, /* sq_repeat */
3648 0, /* sq_item */
3649 0, /* sq_slice */
3650 0, /* sq_ass_item */
3651 0, /* sq_ass_slice */
3652 (objobjproc) OptionsContains, /* sq_contains */
3653 0, /* sq_inplace_concat */
3654 0, /* sq_inplace_repeat */
3655};
3656
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003657static PyMappingMethods OptionsAsMapping = {
3658 (lenfunc) NULL,
3659 (binaryfunc) OptionsItem,
3660 (objobjargproc) OptionsAssItem,
3661};
3662
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003663/* Tabpage object
3664 */
3665
3666typedef struct
3667{
3668 PyObject_HEAD
3669 tabpage_T *tab;
3670} TabPageObject;
3671
3672static PyObject *WinListNew(TabPageObject *tabObject);
3673
3674static PyTypeObject TabPageType;
3675
3676 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003677CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003678{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003679 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003680 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003681 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003682 return -1;
3683 }
3684
3685 return 0;
3686}
3687
3688 static PyObject *
3689TabPageNew(tabpage_T *tab)
3690{
3691 TabPageObject *self;
3692
3693 if (TAB_PYTHON_REF(tab))
3694 {
3695 self = TAB_PYTHON_REF(tab);
3696 Py_INCREF(self);
3697 }
3698 else
3699 {
3700 self = PyObject_NEW(TabPageObject, &TabPageType);
3701 if (self == NULL)
3702 return NULL;
3703 self->tab = tab;
3704 TAB_PYTHON_REF(tab) = self;
3705 }
3706
3707 return (PyObject *)(self);
3708}
3709
3710 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003711TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003712{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003713 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3714 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003715
3716 DESTRUCTOR_FINISH(self);
3717}
3718
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003719static char *TabPageAttrs[] = {
3720 "windows", "number", "vars", "window", "valid",
3721 NULL
3722};
3723
3724 static PyObject *
3725TabPageDir(PyObject *self)
3726{
3727 return ObjectDir(self, TabPageAttrs);
3728}
3729
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003730 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003731TabPageAttrValid(TabPageObject *self, char *name)
3732{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003733 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003734
3735 if (strcmp(name, "valid") != 0)
3736 return NULL;
3737
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003738 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3739 Py_INCREF(ret);
3740 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003741}
3742
3743 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003744TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003745{
3746 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003747 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003748 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003749 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003750 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003751 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003752 else if (strcmp(name, "window") == 0)
3753 {
3754 /* For current tab window.c does not bother to set or update tp_curwin
3755 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003756 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003757 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003758 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003759 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003760 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003761 else if (strcmp(name, "__members__") == 0)
3762 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003763 return NULL;
3764}
3765
3766 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003767TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003768{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003769 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003770 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771 else
3772 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003773 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774
3775 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003776 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3777 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003778 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003779 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003780 }
3781}
3782
3783static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003784 /* name, function, calling, documentation */
3785 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3786 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003787};
3788
3789/*
3790 * Window list object
3791 */
3792
3793static PyTypeObject TabListType;
3794static PySequenceMethods TabListAsSeq;
3795
3796typedef struct
3797{
3798 PyObject_HEAD
3799} TabListObject;
3800
3801 static PyInt
3802TabListLength(PyObject *self UNUSED)
3803{
3804 tabpage_T *tp = first_tabpage;
3805 PyInt n = 0;
3806
3807 while (tp != NULL)
3808 {
3809 ++n;
3810 tp = tp->tp_next;
3811 }
3812
3813 return n;
3814}
3815
3816 static PyObject *
3817TabListItem(PyObject *self UNUSED, PyInt n)
3818{
3819 tabpage_T *tp;
3820
3821 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3822 if (n == 0)
3823 return TabPageNew(tp);
3824
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003825 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003826 return NULL;
3827}
3828
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003829/*
3830 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003831 */
3832
3833typedef struct
3834{
3835 PyObject_HEAD
3836 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003837 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003838} WindowObject;
3839
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003840static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841
3842 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003843CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003845 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003846 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003847 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003848 return -1;
3849 }
3850
3851 return 0;
3852}
3853
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003854 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003855WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003856{
3857 /* We need to handle deletion of windows underneath us.
3858 * If we add a "w_python*_ref" field to the win_T structure,
3859 * then we can get at it in win_free() in vim. We then
3860 * need to create only ONE Python object per window - if
3861 * we try to create a second, just INCREF the existing one
3862 * and return it. The (single) Python object referring to
3863 * the window is stored in "w_python*_ref".
3864 * On a win_free() we set the Python object's win_T* field
3865 * to an invalid value. We trap all uses of a window
3866 * object, and reject them if the win_T* field is invalid.
3867 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003868 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003869 * w_python_ref and w_python3_ref fields respectively.
3870 */
3871
3872 WindowObject *self;
3873
3874 if (WIN_PYTHON_REF(win))
3875 {
3876 self = WIN_PYTHON_REF(win);
3877 Py_INCREF(self);
3878 }
3879 else
3880 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003881 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003882 if (self == NULL)
3883 return NULL;
3884 self->win = win;
3885 WIN_PYTHON_REF(win) = self;
3886 }
3887
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003888 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3889
Bram Moolenaar971db462013-05-12 18:44:48 +02003890 return (PyObject *)(self);
3891}
3892
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003893 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003894WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003895{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003896 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897 if (self->win && self->win != INVALID_WINDOW_VALUE)
3898 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003899 Py_XDECREF(((PyObject *)(self->tabObject)));
3900 PyObject_GC_Del((void *)(self));
3901}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003902
Bram Moolenaar774267b2013-05-21 20:51:59 +02003903 static int
3904WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3905{
3906 Py_VISIT(((PyObject *)(self->tabObject)));
3907 return 0;
3908}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003909
Bram Moolenaar774267b2013-05-21 20:51:59 +02003910 static int
3911WindowClear(WindowObject *self)
3912{
3913 Py_CLEAR(self->tabObject);
3914 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003915}
3916
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003917 static win_T *
3918get_firstwin(TabPageObject *tabObject)
3919{
3920 if (tabObject)
3921 {
3922 if (CheckTabPage(tabObject))
3923 return NULL;
3924 /* For current tab window.c does not bother to set or update tp_firstwin
3925 */
3926 else if (tabObject->tab == curtab)
3927 return firstwin;
3928 else
3929 return tabObject->tab->tp_firstwin;
3930 }
3931 else
3932 return firstwin;
3933}
Bram Moolenaare950f992018-06-10 13:55:55 +02003934
3935// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003936static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003937 "buffer",
3938 "cursor",
3939 "height",
3940 "row",
3941 "width",
3942 "col",
3943 "vars",
3944 "options",
3945 "number",
3946 "tabpage",
3947 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003948 NULL
3949};
3950
3951 static PyObject *
3952WindowDir(PyObject *self)
3953{
3954 return ObjectDir(self, WindowAttrs);
3955}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003956
Bram Moolenaar971db462013-05-12 18:44:48 +02003957 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003958WindowAttrValid(WindowObject *self, char *name)
3959{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003960 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003961
3962 if (strcmp(name, "valid") != 0)
3963 return NULL;
3964
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003965 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3966 Py_INCREF(ret);
3967 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003968}
3969
3970 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003971WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003972{
3973 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003974 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003975 else if (strcmp(name, "cursor") == 0)
3976 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003977 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003978
3979 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3980 }
3981 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003982 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003983 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003984 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003985 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003986 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003987 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003988 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003989 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003990 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003991 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3993 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003994 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003995 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003997 return NULL;
3998 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004000 }
4001 else if (strcmp(name, "tabpage") == 0)
4002 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004003 Py_INCREF(self->tabObject);
4004 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004005 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004006 else if (strcmp(name, "__members__") == 0)
4007 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004008 else
4009 return NULL;
4010}
4011
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004012 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004013WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004014{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004016 return -1;
4017
4018 if (strcmp(name, "buffer") == 0)
4019 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004020 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004021 return -1;
4022 }
4023 else if (strcmp(name, "cursor") == 0)
4024 {
4025 long lnum;
4026 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004027
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004028 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004029 return -1;
4030
Bram Moolenaard6e39182013-05-21 18:30:34 +02004031 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004032 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004033 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 return -1;
4035 }
4036
4037 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004038 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039 return -1;
4040
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 self->win->w_cursor.lnum = lnum;
4042 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004043 self->win->w_set_curswant = TRUE;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02004045 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004047 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004048 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004050 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 return 0;
4052 }
4053 else if (strcmp(name, "height") == 0)
4054 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004055 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056 win_T *savewin;
4057
Bram Moolenaardee2e312013-06-23 16:35:47 +02004058 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 return -1;
4060
4061#ifdef FEAT_GUI
4062 need_mouse_correct = TRUE;
4063#endif
4064 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004065 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004066
4067 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004068 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004070 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071 return -1;
4072
4073 return 0;
4074 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004075 else if (strcmp(name, "width") == 0)
4076 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004077 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078 win_T *savewin;
4079
Bram Moolenaardee2e312013-06-23 16:35:47 +02004080 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 return -1;
4082
4083#ifdef FEAT_GUI
4084 need_mouse_correct = TRUE;
4085#endif
4086 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004087 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004088
4089 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004090 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004092 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 return -1;
4094
4095 return 0;
4096 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004097 else
4098 {
4099 PyErr_SetString(PyExc_AttributeError, name);
4100 return -1;
4101 }
4102}
4103
4104 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004105WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004107 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004108 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 else
4110 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004111 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112
Bram Moolenaar6d216452013-05-12 19:00:41 +02004113 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004114 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115 (self));
4116 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004117 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118 }
4119}
4120
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004121static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004122 /* name, function, calling, documentation */
4123 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4124 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004125};
4126
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004128 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004130
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004131static PyTypeObject WinListType;
4132static PySequenceMethods WinListAsSeq;
4133
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004134typedef struct
4135{
4136 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004137 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004138} WinListObject;
4139
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004140 static PyObject *
4141WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004142{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004143 WinListObject *self;
4144
4145 self = PyObject_NEW(WinListObject, &WinListType);
4146 self->tabObject = tabObject;
4147 Py_INCREF(tabObject);
4148
4149 return (PyObject *)(self);
4150}
4151
4152 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004153WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004154{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004155 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004156
4157 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004158 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004159 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004160 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004161
4162 DESTRUCTOR_FINISH(self);
4163}
4164
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004165 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004166WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004167{
4168 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004169 PyInt n = 0;
4170
Bram Moolenaard6e39182013-05-21 18:30:34 +02004171 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004172 return -1;
4173
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004174 while (w != NULL)
4175 {
4176 ++n;
4177 w = W_NEXT(w);
4178 }
4179
4180 return n;
4181}
4182
4183 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004184WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004185{
4186 win_T *w;
4187
Bram Moolenaard6e39182013-05-21 18:30:34 +02004188 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004189 return NULL;
4190
4191 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004193 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004194
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004195 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004196 return NULL;
4197}
4198
4199/* Convert a Python string into a Vim line.
4200 *
4201 * The result is in allocated memory. All internal nulls are replaced by
4202 * newline characters. It is an error for the string to contain newline
4203 * characters.
4204 *
4205 * On errors, the Python exception data is set, and NULL is returned.
4206 */
4207 static char *
4208StringToLine(PyObject *obj)
4209{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004210 char *str;
4211 char *save;
4212 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004213 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004214 PyInt i;
4215 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004217 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004219 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4220 || str == NULL)
4221 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004222 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004223 else if (PyUnicode_Check(obj))
4224 {
4225 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4226 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004227
Bram Moolenaardaa27022013-06-24 22:33:30 +02004228 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004229 || str == NULL)
4230 {
4231 Py_DECREF(bytes);
4232 return NULL;
4233 }
4234 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004235 else
4236 {
4237#if PY_MAJOR_VERSION < 3
4238 PyErr_FORMAT(PyExc_TypeError,
4239 N_("expected str() or unicode() instance, but got %s"),
4240 Py_TYPE_NAME(obj));
4241#else
4242 PyErr_FORMAT(PyExc_TypeError,
4243 N_("expected bytes() or str() instance, but got %s"),
4244 Py_TYPE_NAME(obj));
4245#endif
4246 return NULL;
4247 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004248
4249 /*
4250 * Error checking: String must not contain newlines, as we
4251 * are replacing a single line, and we must replace it with
4252 * a single line.
4253 * A trailing newline is removed, so that append(f.readlines()) works.
4254 */
4255 p = memchr(str, '\n', len);
4256 if (p != NULL)
4257 {
4258 if (p == str + len - 1)
4259 --len;
4260 else
4261 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004262 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004263 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004264 return NULL;
4265 }
4266 }
4267
4268 /* Create a copy of the string, with internal nulls replaced by
4269 * newline characters, as is the vim convention.
4270 */
4271 save = (char *)alloc((unsigned)(len+1));
4272 if (save == NULL)
4273 {
4274 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004275 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004276 return NULL;
4277 }
4278
4279 for (i = 0; i < len; ++i)
4280 {
4281 if (str[i] == '\0')
4282 save[i] = '\n';
4283 else
4284 save[i] = str[i];
4285 }
4286
4287 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004288 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004289
4290 return save;
4291}
4292
4293/* Get a line from the specified buffer. The line number is
4294 * in Vim format (1-based). The line is returned as a Python
4295 * string object.
4296 */
4297 static PyObject *
4298GetBufferLine(buf_T *buf, PyInt n)
4299{
4300 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4301}
4302
4303
4304/* Get a list of lines from the specified buffer. The line numbers
4305 * are in Vim format (1-based). The range is from lo up to, but not
4306 * including, hi. The list is returned as a Python list of string objects.
4307 */
4308 static PyObject *
4309GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4310{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004311 PyInt i;
4312 PyInt n = hi - lo;
4313 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004314
4315 if (list == NULL)
4316 return NULL;
4317
4318 for (i = 0; i < n; ++i)
4319 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004320 PyObject *string = LineToString(
4321 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004322
4323 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004324 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004325 {
4326 Py_DECREF(list);
4327 return NULL;
4328 }
4329
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004330 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004331 }
4332
4333 /* The ownership of the Python list is passed to the caller (ie,
4334 * the caller should Py_DECREF() the object when it is finished
4335 * with it).
4336 */
4337
4338 return list;
4339}
4340
4341/*
4342 * Check if deleting lines made the cursor position invalid.
4343 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4344 * deleted).
4345 */
4346 static void
4347py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4348{
4349 if (curwin->w_cursor.lnum >= lo)
4350 {
4351 /* Adjust the cursor position if it's in/after the changed
4352 * lines. */
4353 if (curwin->w_cursor.lnum >= hi)
4354 {
4355 curwin->w_cursor.lnum += extra;
4356 check_cursor_col();
4357 }
4358 else if (extra < 0)
4359 {
4360 curwin->w_cursor.lnum = lo;
4361 check_cursor();
4362 }
4363 else
4364 check_cursor_col();
4365 changed_cline_bef_curs();
4366 }
4367 invalidate_botline();
4368}
4369
Bram Moolenaar19e60942011-06-19 00:27:51 +02004370/*
4371 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004372 * in Vim format (1-based). The replacement line is given as
4373 * a Python string object. The object is checked for validity
4374 * and correct format. Errors are returned as a value of FAIL.
4375 * The return value is OK on success.
4376 * If OK is returned and len_change is not NULL, *len_change
4377 * is set to the change in the buffer length.
4378 */
4379 static int
4380SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4381{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004382 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004383 win_T *save_curwin = NULL;
4384 tabpage_T *save_curtab = NULL;
4385
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004386 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004387 * There are three cases:
4388 * 1. NULL, or None - this is a deletion.
4389 * 2. A string - this is a replacement.
4390 * 3. Anything else - this is an error.
4391 */
4392 if (line == Py_None || line == NULL)
4393 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004394 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004395 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004397 VimTryStart();
4398
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004399 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004400 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004402 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004403 else
4404 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004405 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004407 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004408 /* Only adjust marks if we managed to switch to a window that
4409 * holds the buffer, otherwise line numbers will be invalid. */
4410 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004411 }
4412
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004413 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004414
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004415 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416 return FAIL;
4417
4418 if (len_change)
4419 *len_change = -1;
4420
4421 return OK;
4422 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004423 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004424 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004425 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426
4427 if (save == NULL)
4428 return FAIL;
4429
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004430 VimTryStart();
4431
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 /* We do not need to free "save" if ml_replace() consumes it. */
4433 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004434 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435
4436 if (u_savesub((linenr_T)n) == FAIL)
4437 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004438 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004439 vim_free(save);
4440 }
4441 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4442 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004443 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004444 vim_free(save);
4445 }
4446 else
4447 changed_bytes((linenr_T)n, 0);
4448
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004449 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004450
4451 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004452 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453 check_cursor_col();
4454
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004455 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456 return FAIL;
4457
4458 if (len_change)
4459 *len_change = 0;
4460
4461 return OK;
4462 }
4463 else
4464 {
4465 PyErr_BadArgument();
4466 return FAIL;
4467 }
4468}
4469
Bram Moolenaar19e60942011-06-19 00:27:51 +02004470/* Replace a range of lines in the specified buffer. The line numbers are in
4471 * Vim format (1-based). The range is from lo up to, but not including, hi.
4472 * The replacement lines are given as a Python list of string objects. The
4473 * list is checked for validity and correct format. Errors are returned as a
4474 * value of FAIL. The return value is OK on success.
4475 * If OK is returned and len_change is not NULL, *len_change
4476 * is set to the change in the buffer length.
4477 */
4478 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004479SetBufferLineList(
4480 buf_T *buf,
4481 PyInt lo,
4482 PyInt hi,
4483 PyObject *list,
4484 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004485{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004486 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004487 win_T *save_curwin = NULL;
4488 tabpage_T *save_curtab = NULL;
4489
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004490 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004491 * There are three cases:
4492 * 1. NULL, or None - this is a deletion.
4493 * 2. A list - this is a replacement.
4494 * 3. Anything else - this is an error.
4495 */
4496 if (list == Py_None || list == NULL)
4497 {
4498 PyInt i;
4499 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004500
4501 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004502 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004503 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504
4505 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004506 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004507 else
4508 {
4509 for (i = 0; i < n; ++i)
4510 {
4511 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4512 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004513 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004514 break;
4515 }
4516 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004517 if (buf == curbuf && (save_curwin != NULL
4518 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004519 /* Using an existing window for the buffer, adjust the cursor
4520 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004521 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004522 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004523 /* Only adjust marks if we managed to switch to a window that
4524 * holds the buffer, otherwise line numbers will be invalid. */
4525 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004526 }
4527
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004528 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004529
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004530 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004531 return FAIL;
4532
4533 if (len_change)
4534 *len_change = -n;
4535
4536 return OK;
4537 }
4538 else if (PyList_Check(list))
4539 {
4540 PyInt i;
4541 PyInt new_len = PyList_Size(list);
4542 PyInt old_len = hi - lo;
4543 PyInt extra = 0; /* lines added to text, can be negative */
4544 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004545
4546 if (new_len == 0) /* avoid allocating zero bytes */
4547 array = NULL;
4548 else
4549 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004550 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551 if (array == NULL)
4552 {
4553 PyErr_NoMemory();
4554 return FAIL;
4555 }
4556 }
4557
4558 for (i = 0; i < new_len; ++i)
4559 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004560 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004561
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004562 if (!(line = PyList_GetItem(list, i)) ||
4563 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004564 {
4565 while (i)
4566 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004567 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004568 return FAIL;
4569 }
4570 }
4571
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004572 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004573 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004574
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004575 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004576 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004577
4578 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004579 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004580
4581 /* If the size of the range is reducing (ie, new_len < old_len) we
4582 * need to delete some old_len. We do this at the start, by
4583 * repeatedly deleting line "lo".
4584 */
4585 if (!PyErr_Occurred())
4586 {
4587 for (i = 0; i < old_len - new_len; ++i)
4588 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4589 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004590 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004591 break;
4592 }
4593 extra -= i;
4594 }
4595
4596 /* For as long as possible, replace the existing old_len with the
4597 * new old_len. This is a more efficient operation, as it requires
4598 * less memory allocation and freeing.
4599 */
4600 if (!PyErr_Occurred())
4601 {
4602 for (i = 0; i < old_len && i < new_len; ++i)
4603 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4604 == FAIL)
4605 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004606 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004607 break;
4608 }
4609 }
4610 else
4611 i = 0;
4612
4613 /* Now we may need to insert the remaining new old_len. If we do, we
4614 * must free the strings as we finish with them (we can't pass the
4615 * responsibility to vim in this case).
4616 */
4617 if (!PyErr_Occurred())
4618 {
4619 while (i < new_len)
4620 {
4621 if (ml_append((linenr_T)(lo + i - 1),
4622 (char_u *)array[i], 0, FALSE) == FAIL)
4623 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004624 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004625 break;
4626 }
4627 vim_free(array[i]);
4628 ++i;
4629 ++extra;
4630 }
4631 }
4632
4633 /* Free any left-over old_len, as a result of an error */
4634 while (i < new_len)
4635 {
4636 vim_free(array[i]);
4637 ++i;
4638 }
4639
4640 /* Free the array of old_len. All of its contents have now
4641 * been dealt with (either freed, or the responsibility passed
4642 * to vim.
4643 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004644 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004645
4646 /* Adjust marks. Invalidate any which lie in the
4647 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004648 * Only adjust marks if we managed to switch to a window that holds
4649 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004650 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004651 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004652 (long)MAXLNUM, (long)extra);
4653 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4654
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004655 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004656 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4657
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004658 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004659 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004660
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004661 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004662 return FAIL;
4663
4664 if (len_change)
4665 *len_change = new_len - old_len;
4666
4667 return OK;
4668 }
4669 else
4670 {
4671 PyErr_BadArgument();
4672 return FAIL;
4673 }
4674}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004676/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677 * The line number is in Vim format (1-based). The lines to be inserted are
4678 * given as a Python list of string objects or as a single string. The lines
4679 * to be added are checked for validity and correct format. Errors are
4680 * returned as a value of FAIL. The return value is OK on success.
4681 * If OK is returned and len_change is not NULL, *len_change
4682 * is set to the change in the buffer length.
4683 */
4684 static int
4685InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4686{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004687 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004688 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004689 tabpage_T *save_curtab = NULL;
4690
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691 /* First of all, we check the type of the supplied Python object.
4692 * It must be a string or a list, or the call is in error.
4693 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004694 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004695 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004696 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697
4698 if (str == NULL)
4699 return FAIL;
4700
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004701 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004702 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004703 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004704
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004705 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004706 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004707 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004708 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004709 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004710 /* Only adjust marks if we managed to switch to a window that
4711 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 appended_lines_mark((linenr_T)n, 1L);
4713
4714 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004715 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004716 update_screen(VALID);
4717
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004718 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004719 return FAIL;
4720
4721 if (len_change)
4722 *len_change = 1;
4723
4724 return OK;
4725 }
4726 else if (PyList_Check(lines))
4727 {
4728 PyInt i;
4729 PyInt size = PyList_Size(lines);
4730 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004732 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733 if (array == NULL)
4734 {
4735 PyErr_NoMemory();
4736 return FAIL;
4737 }
4738
4739 for (i = 0; i < size; ++i)
4740 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004741 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004743 if (!(line = PyList_GetItem(lines, i)) ||
4744 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 {
4746 while (i)
4747 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004748 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 return FAIL;
4750 }
4751 }
4752
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004753 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004754 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004755 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004756
4757 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004758 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004759 else
4760 {
4761 for (i = 0; i < size; ++i)
4762 {
4763 if (ml_append((linenr_T)(n + i),
4764 (char_u *)array[i], 0, FALSE) == FAIL)
4765 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004766 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004767
4768 /* Free the rest of the lines */
4769 while (i < size)
4770 vim_free(array[i++]);
4771
4772 break;
4773 }
4774 vim_free(array[i]);
4775 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004776 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004777 /* Only adjust marks if we managed to switch to a window that
4778 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779 appended_lines_mark((linenr_T)n, (long)i);
4780 }
4781
4782 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004783 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004784 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004785 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004787 update_screen(VALID);
4788
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004789 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004790 return FAIL;
4791
4792 if (len_change)
4793 *len_change = size;
4794
4795 return OK;
4796 }
4797 else
4798 {
4799 PyErr_BadArgument();
4800 return FAIL;
4801 }
4802}
4803
4804/*
4805 * Common routines for buffers and line ranges
4806 * -------------------------------------------
4807 */
4808
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004809typedef struct
4810{
4811 PyObject_HEAD
4812 buf_T *buf;
4813} BufferObject;
4814
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004815 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004816CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004818 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004819 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004820 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004821 return -1;
4822 }
4823
4824 return 0;
4825}
4826
4827 static PyObject *
4828RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4829{
4830 if (CheckBuffer(self))
4831 return NULL;
4832
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004833 if (end == -1)
4834 end = self->buf->b_ml.ml_line_count;
4835
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004836 if (n < 0)
4837 n += end - start + 1;
4838
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004839 if (n < 0 || n > end - start)
4840 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004841 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004842 return NULL;
4843 }
4844
4845 return GetBufferLine(self->buf, n+start);
4846}
4847
4848 static PyObject *
4849RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4850{
4851 PyInt size;
4852
4853 if (CheckBuffer(self))
4854 return NULL;
4855
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004856 if (end == -1)
4857 end = self->buf->b_ml.ml_line_count;
4858
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004859 size = end - start + 1;
4860
4861 if (lo < 0)
4862 lo = 0;
4863 else if (lo > size)
4864 lo = size;
4865 if (hi < 0)
4866 hi = 0;
4867 if (hi < lo)
4868 hi = lo;
4869 else if (hi > size)
4870 hi = size;
4871
4872 return GetBufferLineList(self->buf, lo+start, hi+start);
4873}
4874
4875 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004876RBAsItem(
4877 BufferObject *self,
4878 PyInt n,
4879 PyObject *valObject,
4880 PyInt start,
4881 PyInt end,
4882 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004883{
4884 PyInt len_change;
4885
4886 if (CheckBuffer(self))
4887 return -1;
4888
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004889 if (end == -1)
4890 end = self->buf->b_ml.ml_line_count;
4891
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004892 if (n < 0)
4893 n += end - start + 1;
4894
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004895 if (n < 0 || n > end - start)
4896 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004897 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004898 return -1;
4899 }
4900
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004901 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004902 return -1;
4903
4904 if (new_end)
4905 *new_end = end + len_change;
4906
4907 return 0;
4908}
4909
Bram Moolenaar19e60942011-06-19 00:27:51 +02004910 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004911RBAsSlice(
4912 BufferObject *self,
4913 PyInt lo,
4914 PyInt hi,
4915 PyObject *valObject,
4916 PyInt start,
4917 PyInt end,
4918 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004919{
4920 PyInt size;
4921 PyInt len_change;
4922
4923 /* Self must be a valid buffer */
4924 if (CheckBuffer(self))
4925 return -1;
4926
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004927 if (end == -1)
4928 end = self->buf->b_ml.ml_line_count;
4929
Bram Moolenaar19e60942011-06-19 00:27:51 +02004930 /* Sort out the slice range */
4931 size = end - start + 1;
4932
4933 if (lo < 0)
4934 lo = 0;
4935 else if (lo > size)
4936 lo = size;
4937 if (hi < 0)
4938 hi = 0;
4939 if (hi < lo)
4940 hi = lo;
4941 else if (hi > size)
4942 hi = size;
4943
4944 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004945 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004946 return -1;
4947
4948 if (new_end)
4949 *new_end = end + len_change;
4950
4951 return 0;
4952}
4953
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004954
4955 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004956RBAppend(
4957 BufferObject *self,
4958 PyObject *args,
4959 PyInt start,
4960 PyInt end,
4961 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004962{
4963 PyObject *lines;
4964 PyInt len_change;
4965 PyInt max;
4966 PyInt n;
4967
4968 if (CheckBuffer(self))
4969 return NULL;
4970
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004971 if (end == -1)
4972 end = self->buf->b_ml.ml_line_count;
4973
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004974 max = n = end - start + 1;
4975
4976 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4977 return NULL;
4978
4979 if (n < 0 || n > max)
4980 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004981 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004982 return NULL;
4983 }
4984
4985 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4986 return NULL;
4987
4988 if (new_end)
4989 *new_end = end + len_change;
4990
4991 Py_INCREF(Py_None);
4992 return Py_None;
4993}
4994
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004995/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004996 */
4997
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004998static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004999static PySequenceMethods RangeAsSeq;
5000static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005001
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005002typedef struct
5003{
5004 PyObject_HEAD
5005 BufferObject *buf;
5006 PyInt start;
5007 PyInt end;
5008} RangeObject;
5009
5010 static PyObject *
5011RangeNew(buf_T *buf, PyInt start, PyInt end)
5012{
5013 BufferObject *bufr;
5014 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005015 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005016 if (self == NULL)
5017 return NULL;
5018
5019 bufr = (BufferObject *)BufferNew(buf);
5020 if (bufr == NULL)
5021 {
5022 Py_DECREF(self);
5023 return NULL;
5024 }
5025 Py_INCREF(bufr);
5026
5027 self->buf = bufr;
5028 self->start = start;
5029 self->end = end;
5030
5031 return (PyObject *)(self);
5032}
5033
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005034 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005035RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005036{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005037 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005038 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005039 PyObject_GC_Del((void *)(self));
5040}
5041
5042 static int
5043RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5044{
5045 Py_VISIT(((PyObject *)(self->buf)));
5046 return 0;
5047}
5048
5049 static int
5050RangeClear(RangeObject *self)
5051{
5052 Py_CLEAR(self->buf);
5053 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005054}
5055
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005056 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005057RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005058{
5059 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005061 return -1; /* ??? */
5062
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064}
5065
5066 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005068{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005070}
5071
5072 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005073RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005074{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005075 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005076}
5077
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005078static char *RangeAttrs[] = {
5079 "start", "end",
5080 NULL
5081};
5082
5083 static PyObject *
5084RangeDir(PyObject *self)
5085{
5086 return ObjectDir(self, RangeAttrs);
5087}
5088
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093}
5094
5095 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005098 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005099 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5100 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101 else
5102 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005103 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104
5105 if (name == NULL)
5106 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005108 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005109 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005110 }
5111}
5112
5113static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005114 /* name, function, calling, documentation */
5115 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005116 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5117 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005118};
5119
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005121static PySequenceMethods BufferAsSeq;
5122static PyMappingMethods BufferAsMapping;
5123
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005125BufferNew(buf_T *buf)
5126{
5127 /* We need to handle deletion of buffers underneath us.
5128 * If we add a "b_python*_ref" field to the buf_T structure,
5129 * then we can get at it in buf_freeall() in vim. We then
5130 * need to create only ONE Python object per buffer - if
5131 * we try to create a second, just INCREF the existing one
5132 * and return it. The (single) Python object referring to
5133 * the buffer is stored in "b_python*_ref".
5134 * Question: what to do on a buf_freeall(). We'll probably
5135 * have to either delete the Python object (DECREF it to
5136 * zero - a bad idea, as it leaves dangling refs!) or
5137 * set the buf_T * value to an invalid value (-1?), which
5138 * means we need checks in all access functions... Bah.
5139 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005140 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005141 * b_python_ref and b_python3_ref fields respectively.
5142 */
5143
5144 BufferObject *self;
5145
5146 if (BUF_PYTHON_REF(buf) != NULL)
5147 {
5148 self = BUF_PYTHON_REF(buf);
5149 Py_INCREF(self);
5150 }
5151 else
5152 {
5153 self = PyObject_NEW(BufferObject, &BufferType);
5154 if (self == NULL)
5155 return NULL;
5156 self->buf = buf;
5157 BUF_PYTHON_REF(buf) = self;
5158 }
5159
5160 return (PyObject *)(self);
5161}
5162
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005163 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005164BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005165{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005166 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5167 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005168
5169 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005170}
5171
Bram Moolenaar971db462013-05-12 18:44:48 +02005172 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005173BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005174{
5175 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005176 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005177 return -1; /* ??? */
5178
Bram Moolenaard6e39182013-05-21 18:30:34 +02005179 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005180}
5181
5182 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005183BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005184{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005185 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005186}
5187
5188 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005189BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005190{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005191 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005192}
5193
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005194static char *BufferAttrs[] = {
5195 "name", "number", "vars", "options", "valid",
5196 NULL
5197};
5198
5199 static PyObject *
5200BufferDir(PyObject *self)
5201{
5202 return ObjectDir(self, BufferAttrs);
5203}
5204
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005205 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005206BufferAttrValid(BufferObject *self, char *name)
5207{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005208 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005209
5210 if (strcmp(name, "valid") != 0)
5211 return NULL;
5212
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005213 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5214 Py_INCREF(ret);
5215 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005216}
5217
5218 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005219BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005220{
5221 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005222 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005223 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005224 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005225 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005226 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005227 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005228 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005229 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5230 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005231 else if (strcmp(name, "__members__") == 0)
5232 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005233 else
5234 return NULL;
5235}
5236
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005237 static int
5238BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5239{
5240 if (CheckBuffer(self))
5241 return -1;
5242
5243 if (strcmp(name, "name") == 0)
5244 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005245 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005246 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005247 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005248 PyObject *todecref;
5249
5250 if (!(val = StringToChars(valObject, &todecref)))
5251 return -1;
5252
5253 VimTryStart();
5254 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5255 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005256 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005257 aucmd_restbuf(&aco);
5258 Py_XDECREF(todecref);
5259 if (VimTryEnd())
5260 return -1;
5261
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005262 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005263 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005264 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005265 return -1;
5266 }
5267 return 0;
5268 }
5269 else
5270 {
5271 PyErr_SetString(PyExc_AttributeError, name);
5272 return -1;
5273 }
5274}
5275
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005276 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005277BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005278{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005279 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005280}
5281
5282 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005283BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005284{
5285 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005286 char_u *pmark;
5287 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005288 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005289 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005290
Bram Moolenaard6e39182013-05-21 18:30:34 +02005291 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005292 return NULL;
5293
Bram Moolenaar389a1792013-06-23 13:00:44 +02005294 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005295 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005296
Bram Moolenaar389a1792013-06-23 13:00:44 +02005297 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005298 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005299 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005300 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005301 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005302 return NULL;
5303 }
5304
5305 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005306
5307 Py_XDECREF(todecref);
5308
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005309 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005312 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005313 if (VimTryEnd())
5314 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315
5316 if (posp == NULL)
5317 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005318 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005319 return NULL;
5320 }
5321
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005322 if (posp->lnum <= 0)
5323 {
5324 /* Or raise an error? */
5325 Py_INCREF(Py_None);
5326 return Py_None;
5327 }
5328
5329 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5330}
5331
5332 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005333BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005334{
5335 PyInt start;
5336 PyInt end;
5337
Bram Moolenaard6e39182013-05-21 18:30:34 +02005338 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005339 return NULL;
5340
5341 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5342 return NULL;
5343
Bram Moolenaard6e39182013-05-21 18:30:34 +02005344 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005345}
5346
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005347 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005348BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005349{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005350 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005351 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005352 else
5353 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005354 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005355
5356 if (name == NULL)
5357 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005358
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005359 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005360 }
5361}
5362
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005363static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005364 /* name, function, calling, documentation */
5365 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005366 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005367 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005368 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5369 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005370};
5371
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005372/*
5373 * Buffer list object - Implementation
5374 */
5375
5376static PyTypeObject BufMapType;
5377
5378typedef struct
5379{
5380 PyObject_HEAD
5381} BufMapObject;
5382
5383 static PyInt
5384BufMapLength(PyObject *self UNUSED)
5385{
5386 buf_T *b = firstbuf;
5387 PyInt n = 0;
5388
5389 while (b)
5390 {
5391 ++n;
5392 b = b->b_next;
5393 }
5394
5395 return n;
5396}
5397
5398 static PyObject *
5399BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5400{
5401 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005402 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005403
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005404 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005405 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005406
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005407 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005408
5409 if (b)
5410 return BufferNew(b);
5411 else
5412 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005413 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005414 return NULL;
5415 }
5416}
5417
5418 static void
5419BufMapIterDestruct(PyObject *buffer)
5420{
5421 /* Iteration was stopped before all buffers were processed */
5422 if (buffer)
5423 {
5424 Py_DECREF(buffer);
5425 }
5426}
5427
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005428 static int
5429BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5430{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005431 if (buffer)
5432 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005433 return 0;
5434}
5435
5436 static int
5437BufMapIterClear(PyObject **buffer)
5438{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005439 if (*buffer)
5440 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005441 return 0;
5442}
5443
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005444 static PyObject *
5445BufMapIterNext(PyObject **buffer)
5446{
5447 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005448 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005449
5450 if (!*buffer)
5451 return NULL;
5452
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005453 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005454
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005455 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005456 {
5457 *buffer = NULL;
5458 return NULL;
5459 }
5460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005461 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005462 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005463 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005464 return NULL;
5465 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005466 /* Do not increment reference: we no longer hold it (decref), but whoever
5467 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005468 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005469}
5470
5471 static PyObject *
5472BufMapIter(PyObject *self UNUSED)
5473{
5474 PyObject *buffer;
5475
5476 buffer = BufferNew(firstbuf);
5477 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005478 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5479 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005480}
5481
5482static PyMappingMethods BufMapAsMapping = {
5483 (lenfunc) BufMapLength,
5484 (binaryfunc) BufMapItem,
5485 (objobjargproc) 0,
5486};
5487
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005488/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005489 */
5490
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005491static char *CurrentAttrs[] = {
5492 "buffer", "window", "line", "range", "tabpage",
5493 NULL
5494};
5495
5496 static PyObject *
5497CurrentDir(PyObject *self)
5498{
5499 return ObjectDir(self, CurrentAttrs);
5500}
5501
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005502 static PyObject *
5503CurrentGetattr(PyObject *self UNUSED, char *name)
5504{
5505 if (strcmp(name, "buffer") == 0)
5506 return (PyObject *)BufferNew(curbuf);
5507 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005508 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005509 else if (strcmp(name, "tabpage") == 0)
5510 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005511 else if (strcmp(name, "line") == 0)
5512 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5513 else if (strcmp(name, "range") == 0)
5514 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005515 else if (strcmp(name, "__members__") == 0)
5516 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005517 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005518#if PY_MAJOR_VERSION < 3
5519 return Py_FindMethod(WindowMethods, self, name);
5520#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005521 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005522#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005523}
5524
5525 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005526CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005527{
5528 if (strcmp(name, "line") == 0)
5529 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005530 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5531 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005532 return -1;
5533
5534 return 0;
5535 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005536 else if (strcmp(name, "buffer") == 0)
5537 {
5538 int count;
5539
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005540 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005541 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005542 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005543 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005544 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005545 return -1;
5546 }
5547
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005548 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005549 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005550 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005551
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005552 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005553 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5554 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005555 if (VimTryEnd())
5556 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005557 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005558 return -1;
5559 }
5560
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005561 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005562 }
5563 else if (strcmp(name, "window") == 0)
5564 {
5565 int count;
5566
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005567 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005568 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005569 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005570 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005572 return -1;
5573 }
5574
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005575 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005576 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005578
5579 if (!count)
5580 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005581 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005582 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005583 return -1;
5584 }
5585
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005586 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005587 win_goto(((WindowObject *)(valObject))->win);
5588 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005589 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005590 if (VimTryEnd())
5591 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005592 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005593 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005594 return -1;
5595 }
5596
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005597 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005598 }
5599 else if (strcmp(name, "tabpage") == 0)
5600 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005601 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005602 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005603 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005604 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005606 return -1;
5607 }
5608
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005609 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005610 return -1;
5611
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005612 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005613 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5614 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005615 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005616 if (VimTryEnd())
5617 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005618 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005619 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 return -1;
5621 }
5622
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005623 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005624 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005625 else
5626 {
5627 PyErr_SetString(PyExc_AttributeError, name);
5628 return -1;
5629 }
5630}
5631
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005632static struct PyMethodDef CurrentMethods[] = {
5633 /* name, function, calling, documentation */
5634 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5635 { NULL, NULL, 0, NULL}
5636};
5637
Bram Moolenaardb913952012-06-29 12:54:53 +02005638 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005639init_range_cmd(exarg_T *eap)
5640{
5641 RangeStart = eap->line1;
5642 RangeEnd = eap->line2;
5643}
5644
5645 static void
5646init_range_eval(typval_T *rettv UNUSED)
5647{
5648 RangeStart = (PyInt) curwin->w_cursor.lnum;
5649 RangeEnd = RangeStart;
5650}
5651
5652 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005653run_cmd(const char *cmd, void *arg UNUSED
5654#ifdef PY_CAN_RECURSE
5655 , PyGILState_STATE *pygilstate UNUSED
5656#endif
5657 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005658{
Bram Moolenaar41009372013-07-01 22:03:04 +02005659 PyObject *run_ret;
5660 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5661 if (run_ret != NULL)
5662 {
5663 Py_DECREF(run_ret);
5664 }
5665 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005667 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005668 PyErr_Clear();
5669 }
5670 else
5671 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005672}
5673
5674static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5675static int code_hdr_len = 30;
5676
5677 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005678run_do(const char *cmd, void *arg UNUSED
5679#ifdef PY_CAN_RECURSE
5680 , PyGILState_STATE *pygilstate
5681#endif
5682 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005683{
5684 PyInt lnum;
5685 size_t len;
5686 char *code;
5687 int status;
5688 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005689 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005690 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005692 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005694 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005695 return;
5696 }
5697
5698 len = code_hdr_len + STRLEN(cmd);
5699 code = PyMem_New(char, len + 1);
5700 memcpy(code, code_hdr, code_hdr_len);
5701 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005702 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5703 status = -1;
5704 if (run_ret != NULL)
5705 {
5706 status = 0;
5707 Py_DECREF(run_ret);
5708 }
5709 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5710 {
5711 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005712 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005713 PyErr_Clear();
5714 return;
5715 }
5716 else
5717 PyErr_PrintEx(1);
5718
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005719 PyMem_Free(code);
5720
5721 if (status)
5722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005723 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724 return;
5725 }
5726
5727 status = 0;
5728 pymain = PyImport_AddModule("__main__");
5729 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005730#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005731 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005732#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005733
5734 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5735 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005736 PyObject *line;
5737 PyObject *linenr;
5738 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005739
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005740#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005741 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005742#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005743 /* Check the line number, the command my have deleted lines. */
5744 if (lnum > curbuf->b_ml.ml_line_count
5745 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005746 goto err;
5747 if (!(linenr = PyInt_FromLong((long) lnum)))
5748 {
5749 Py_DECREF(line);
5750 goto err;
5751 }
5752 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5753 Py_DECREF(line);
5754 Py_DECREF(linenr);
5755 if (!ret)
5756 goto err;
5757
Bram Moolenaara58883b2017-01-29 21:31:09 +01005758 /* Check that the command didn't switch to another buffer. */
5759 if (curbuf != was_curbuf)
5760 {
5761 Py_XDECREF(ret);
5762 goto err;
5763 }
5764
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005765 if (ret != Py_None)
5766 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005767 {
5768 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005769 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005770 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771
5772 Py_XDECREF(ret);
5773 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005774#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005775 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005776#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005777 }
5778 goto out;
5779err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005780#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005782#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005783 PyErr_PrintEx(0);
5784 PythonIO_Flush();
5785 status = 1;
5786out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005787#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005788 if (!status)
5789 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005790#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005791 Py_DECREF(pyfunc);
5792 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5793 if (status)
5794 return;
5795 check_cursor();
5796 update_curbuf(NOT_VALID);
5797}
5798
5799 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005800run_eval(const char *cmd, typval_T *rettv
5801#ifdef PY_CAN_RECURSE
5802 , PyGILState_STATE *pygilstate UNUSED
5803#endif
5804 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005805{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005806 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005807
Bram Moolenaar41009372013-07-01 22:03:04 +02005808 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005809 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005810 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005811 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005813 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005814 PyErr_Clear();
5815 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005816 else
5817 {
5818 if (PyErr_Occurred() && !msg_silent)
5819 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005820 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005821 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005822 }
5823 else
5824 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005825 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005826 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005827 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005828 }
5829 PyErr_Clear();
5830}
5831
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005832 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005833set_ref_in_py(const int copyID)
5834{
5835 pylinkedlist_T *cur;
5836 dict_T *dd;
5837 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005838 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005839 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005840 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005841
5842 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005843 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005844 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005845 {
5846 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5847 if (dd->dv_copyID != copyID)
5848 {
5849 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005850 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005851 }
5852 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005853 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005854
5855 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005857 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005858 {
5859 ll = ((ListObject *) (cur->pll_obj))->list;
5860 if (ll->lv_copyID != copyID)
5861 {
5862 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005864 }
5865 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005866 }
5867
Bram Moolenaar8110a092016-04-14 15:56:09 +02005868 if (lastfunc != NULL)
5869 {
5870 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5871 {
5872 func = (FunctionObject *) cur->pll_obj;
5873 if (func->self != NULL && func->self->dv_copyID != copyID)
5874 {
5875 func->self->dv_copyID = copyID;
5876 abort = abort || set_ref_in_ht(
5877 &func->self->dv_hashtab, copyID, NULL);
5878 }
5879 if (func->argc)
5880 for (i = 0; !abort && i < func->argc; ++i)
5881 abort = abort
5882 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5883 }
5884 }
5885
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005886 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005887}
5888
5889 static int
5890set_string_copy(char_u *str, typval_T *tv)
5891{
5892 tv->vval.v_string = vim_strsave(str);
5893 if (tv->vval.v_string == NULL)
5894 {
5895 PyErr_NoMemory();
5896 return -1;
5897 }
5898 return 0;
5899}
5900
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005902pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005903{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005904 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005905 char_u *key;
5906 dictitem_T *di;
5907 PyObject *keyObject;
5908 PyObject *valObject;
5909 Py_ssize_t iter = 0;
5910
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005911 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005912 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005913
5914 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005915 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916
5917 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5918 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005919 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920
Bram Moolenaara03e6312013-05-29 22:49:26 +02005921 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005922 {
5923 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005924 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005925 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005927 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005928 {
5929 dict_unref(dict);
5930 return -1;
5931 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005932
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005933 if (*key == NUL)
5934 {
5935 dict_unref(dict);
5936 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005937 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005938 return -1;
5939 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005940
5941 di = dictitem_alloc(key);
5942
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005943 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005944
5945 if (di == NULL)
5946 {
5947 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005948 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005949 return -1;
5950 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005952 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 {
5954 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005955 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956 return -1;
5957 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005958
5959 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005960 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005961 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005962 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965 return -1;
5966 }
5967 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005968
5969 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 return 0;
5971}
5972
5973 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005974pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005976 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 char_u *key;
5978 dictitem_T *di;
5979 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005980 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 PyObject *keyObject;
5982 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005984 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005986
5987 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005988 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005990 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005991 {
5992 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005993 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005994 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005995
5996 if (!(iterator = PyObject_GetIter(list)))
5997 {
5998 dict_unref(dict);
5999 Py_DECREF(list);
6000 return -1;
6001 }
6002 Py_DECREF(list);
6003
6004 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006005 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006006 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006007
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006008 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006009 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006010 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006011 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006012 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006013 return -1;
6014 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006015
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006016 if (*key == NUL)
6017 {
6018 Py_DECREF(keyObject);
6019 Py_DECREF(iterator);
6020 Py_XDECREF(todecref);
6021 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006022 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 return -1;
6024 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006025
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006026 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006027 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006028 Py_DECREF(keyObject);
6029 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006030 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006031 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006032 return -1;
6033 }
6034
6035 di = dictitem_alloc(key);
6036
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006037 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006038 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006039
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006040 if (di == NULL)
6041 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006042 Py_DECREF(iterator);
6043 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006044 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006045 PyErr_NoMemory();
6046 return -1;
6047 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006048
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006049 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006050 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006051 Py_DECREF(iterator);
6052 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006053 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006054 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006055 return -1;
6056 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006057
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006058 Py_DECREF(valObject);
6059
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006060 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006062 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006063 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006064 dictitem_free(di);
6065 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006066 return -1;
6067 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006069 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071 return 0;
6072}
6073
6074 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006075pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076{
6077 list_T *l;
6078
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006080 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006081
6082 tv->v_type = VAR_LIST;
6083 tv->vval.v_list = l;
6084
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006085 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006086 {
6087 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006088 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006089 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006090
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006091 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006092 return 0;
6093}
6094
Bram Moolenaardb913952012-06-29 12:54:53 +02006095typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6096
6097 static int
6098convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006099 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006100{
6101 PyObject *capsule;
6102 char hexBuf[sizeof(void *) * 2 + 3];
6103
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006104 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006105
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006106# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006107 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006108# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006109 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006110# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006111 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006112 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006113# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006114 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006115# else
6116 capsule = PyCObject_FromVoidPtr(tv, NULL);
6117# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006118 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6119 {
6120 Py_DECREF(capsule);
6121 tv->v_type = VAR_UNKNOWN;
6122 return -1;
6123 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006124
6125 Py_DECREF(capsule);
6126
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006127 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006128 {
6129 tv->v_type = VAR_UNKNOWN;
6130 return -1;
6131 }
6132 /* As we are not using copy_tv which increments reference count we must
6133 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006134 if (tv->v_type == VAR_DICT)
6135 ++tv->vval.v_dict->dv_refcount;
6136 else if (tv->v_type == VAR_LIST)
6137 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006138 }
6139 else
6140 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006141 typval_T *v;
6142
6143# ifdef PY_USE_CAPSULE
6144 v = PyCapsule_GetPointer(capsule, NULL);
6145# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006146 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006147# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006148 copy_tv(v, tv);
6149 }
6150 return 0;
6151}
6152
6153 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006154ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6155{
6156 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006157 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006158
6159 if (!(lookup_dict = PyDict_New()))
6160 return -1;
6161
6162 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6163 {
6164 tv->v_type = VAR_DICT;
6165 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6166 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006167 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006168 }
6169 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006170 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006171 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006172 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006173 else
6174 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006175 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006176 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006177 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006178 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006179 }
6180 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006181 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006182}
6183
6184 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006185ConvertFromPySequence(PyObject *obj, typval_T *tv)
6186{
6187 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006188 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006189
6190 if (!(lookup_dict = PyDict_New()))
6191 return -1;
6192
6193 if (PyType_IsSubtype(obj->ob_type, &ListType))
6194 {
6195 tv->v_type = VAR_LIST;
6196 tv->vval.v_list = (((ListObject *)(obj))->list);
6197 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006198 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006199 }
6200 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006201 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006202 else
6203 {
6204 PyErr_FORMAT(PyExc_TypeError,
6205 N_("unable to convert %s to vim list"),
6206 Py_TYPE_NAME(obj));
6207 ret = -1;
6208 }
6209 Py_DECREF(lookup_dict);
6210 return ret;
6211}
6212
6213 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006214ConvertFromPyObject(PyObject *obj, typval_T *tv)
6215{
6216 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006217 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006218
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006219 if (!(lookup_dict = PyDict_New()))
6220 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006221 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006222 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006223 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006224}
6225
6226 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006227_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006228{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006229 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006230 {
6231 tv->v_type = VAR_DICT;
6232 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6233 ++tv->vval.v_dict->dv_refcount;
6234 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006235 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006236 {
6237 tv->v_type = VAR_LIST;
6238 tv->vval.v_list = (((ListObject *)(obj))->list);
6239 ++tv->vval.v_list->lv_refcount;
6240 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006241 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006242 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006243 FunctionObject *func = (FunctionObject *) obj;
6244 if (func->self != NULL || func->argv != NULL)
6245 {
6246 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6247 set_partial(func, pt, TRUE);
6248 tv->vval.v_partial = pt;
6249 tv->v_type = VAR_PARTIAL;
6250 }
6251 else
6252 {
6253 if (set_string_copy(func->name, tv) == -1)
6254 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006255
Bram Moolenaar8110a092016-04-14 15:56:09 +02006256 tv->v_type = VAR_FUNC;
6257 }
6258 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 else if (PyBytes_Check(obj))
6261 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006262 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006263
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006264 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006265 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006266 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006267 return -1;
6268
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006269 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006270 return -1;
6271
6272 tv->v_type = VAR_STRING;
6273 }
6274 else if (PyUnicode_Check(obj))
6275 {
6276 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006277 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006278
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006279 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006280 if (bytes == NULL)
6281 return -1;
6282
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006283 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006284 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006285 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006286 return -1;
6287
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006288 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 {
6290 Py_XDECREF(bytes);
6291 return -1;
6292 }
6293 Py_XDECREF(bytes);
6294
6295 tv->v_type = VAR_STRING;
6296 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006297#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006298 else if (PyInt_Check(obj))
6299 {
6300 tv->v_type = VAR_NUMBER;
6301 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006302 if (PyErr_Occurred())
6303 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 }
6305#endif
6306 else if (PyLong_Check(obj))
6307 {
6308 tv->v_type = VAR_NUMBER;
6309 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006310 if (PyErr_Occurred())
6311 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006312 }
6313 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006314 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006315#ifdef FEAT_FLOAT
6316 else if (PyFloat_Check(obj))
6317 {
6318 tv->v_type = VAR_FLOAT;
6319 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6320 }
6321#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006322 else if (PyObject_HasAttrString(obj, "keys"))
6323 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006324 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006325 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006326 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006327 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006328 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006329 else if (PyNumber_Check(obj))
6330 {
6331 PyObject *num;
6332
6333 if (!(num = PyNumber_Long(obj)))
6334 return -1;
6335
6336 tv->v_type = VAR_NUMBER;
6337 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6338
6339 Py_DECREF(num);
6340 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006341 else if (obj == Py_None)
6342 {
6343 tv->v_type = VAR_SPECIAL;
6344 tv->vval.v_number = VVAL_NONE;
6345 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006346 else
6347 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006348 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006349 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006350 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006351 return -1;
6352 }
6353 return 0;
6354}
6355
6356 static PyObject *
6357ConvertToPyObject(typval_T *tv)
6358{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006359 typval_T *argv;
6360 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006361 if (tv == NULL)
6362 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006363 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006364 return NULL;
6365 }
6366 switch (tv->v_type)
6367 {
6368 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006369 return PyBytes_FromString(tv->vval.v_string == NULL
6370 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006371 case VAR_NUMBER:
6372 return PyLong_FromLong((long) tv->vval.v_number);
6373#ifdef FEAT_FLOAT
6374 case VAR_FLOAT:
6375 return PyFloat_FromDouble((double) tv->vval.v_float);
6376#endif
6377 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006378 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006379 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006380 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006381 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006382 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006383 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006384 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006385 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006386 if (tv->vval.v_partial->pt_argc)
6387 {
6388 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6389 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6390 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6391 }
6392 else
6393 argv = NULL;
6394 if (tv->vval.v_partial->pt_dict != NULL)
6395 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006396 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006397 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006398 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006399 tv->vval.v_partial->pt_dict,
6400 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006401 case VAR_BLOB:
6402 return PyBytes_FromStringAndSize(
6403 (char*) tv->vval.v_blob->bv_ga.ga_data,
6404 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006405 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006406 case VAR_CHANNEL:
6407 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006408 Py_INCREF(Py_None);
6409 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006410 case VAR_SPECIAL:
6411 switch (tv->vval.v_number)
6412 {
6413 case VVAL_FALSE: return AlwaysFalse(NULL);
6414 case VVAL_TRUE: return AlwaysTrue(NULL);
6415 case VVAL_NONE:
6416 case VVAL_NULL: return AlwaysNone(NULL);
6417 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006418 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006419 return NULL;
6420 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006421 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006422}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006423
6424typedef struct
6425{
6426 PyObject_HEAD
6427} CurrentObject;
6428static PyTypeObject CurrentType;
6429
6430 static void
6431init_structs(void)
6432{
6433 vim_memset(&OutputType, 0, sizeof(OutputType));
6434 OutputType.tp_name = "vim.message";
6435 OutputType.tp_basicsize = sizeof(OutputObject);
6436 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6437 OutputType.tp_doc = "vim message object";
6438 OutputType.tp_methods = OutputMethods;
6439#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006440 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6441 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006442 OutputType.tp_alloc = call_PyType_GenericAlloc;
6443 OutputType.tp_new = call_PyType_GenericNew;
6444 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006445 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006446#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006447 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6448 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006449 // Disabled, because this causes a crash in test86
6450 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006451#endif
6452
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006453 vim_memset(&IterType, 0, sizeof(IterType));
6454 IterType.tp_name = "vim.iter";
6455 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006456 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006457 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006458 IterType.tp_iter = (getiterfunc)IterIter;
6459 IterType.tp_iternext = (iternextfunc)IterNext;
6460 IterType.tp_dealloc = (destructor)IterDestructor;
6461 IterType.tp_traverse = (traverseproc)IterTraverse;
6462 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006463
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006464 vim_memset(&BufferType, 0, sizeof(BufferType));
6465 BufferType.tp_name = "vim.buffer";
6466 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006467 BufferType.tp_dealloc = (destructor)BufferDestructor;
6468 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006469 BufferType.tp_as_sequence = &BufferAsSeq;
6470 BufferType.tp_as_mapping = &BufferAsMapping;
6471 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6472 BufferType.tp_doc = "vim buffer object";
6473 BufferType.tp_methods = BufferMethods;
6474#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006475 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006476 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006477 BufferType.tp_alloc = call_PyType_GenericAlloc;
6478 BufferType.tp_new = call_PyType_GenericNew;
6479 BufferType.tp_free = call_PyObject_Free;
6480#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006481 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006482 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483#endif
6484
6485 vim_memset(&WindowType, 0, sizeof(WindowType));
6486 WindowType.tp_name = "vim.window";
6487 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006488 WindowType.tp_dealloc = (destructor)WindowDestructor;
6489 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006490 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491 WindowType.tp_doc = "vim Window object";
6492 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006493 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6494 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006495#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006496 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6497 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006498 WindowType.tp_alloc = call_PyType_GenericAlloc;
6499 WindowType.tp_new = call_PyType_GenericNew;
6500 WindowType.tp_free = call_PyObject_Free;
6501#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6503 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504#endif
6505
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006506 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6507 TabPageType.tp_name = "vim.tabpage";
6508 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006509 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6510 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006511 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6512 TabPageType.tp_doc = "vim tab page object";
6513 TabPageType.tp_methods = TabPageMethods;
6514#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006516 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6517 TabPageType.tp_new = call_PyType_GenericNew;
6518 TabPageType.tp_free = call_PyObject_Free;
6519#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006520 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006521#endif
6522
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006523 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6524 BufMapType.tp_name = "vim.bufferlist";
6525 BufMapType.tp_basicsize = sizeof(BufMapObject);
6526 BufMapType.tp_as_mapping = &BufMapAsMapping;
6527 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006528 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006529 BufferType.tp_doc = "vim buffer list";
6530
6531 vim_memset(&WinListType, 0, sizeof(WinListType));
6532 WinListType.tp_name = "vim.windowlist";
6533 WinListType.tp_basicsize = sizeof(WinListType);
6534 WinListType.tp_as_sequence = &WinListAsSeq;
6535 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6536 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006537 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006538
6539 vim_memset(&TabListType, 0, sizeof(TabListType));
6540 TabListType.tp_name = "vim.tabpagelist";
6541 TabListType.tp_basicsize = sizeof(TabListType);
6542 TabListType.tp_as_sequence = &TabListAsSeq;
6543 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6544 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006545
6546 vim_memset(&RangeType, 0, sizeof(RangeType));
6547 RangeType.tp_name = "vim.range";
6548 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006549 RangeType.tp_dealloc = (destructor)RangeDestructor;
6550 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551 RangeType.tp_as_sequence = &RangeAsSeq;
6552 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006553 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006554 RangeType.tp_doc = "vim Range object";
6555 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006556 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6557 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006558#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006559 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560 RangeType.tp_alloc = call_PyType_GenericAlloc;
6561 RangeType.tp_new = call_PyType_GenericNew;
6562 RangeType.tp_free = call_PyObject_Free;
6563#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006564 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565#endif
6566
6567 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6568 CurrentType.tp_name = "vim.currentdata";
6569 CurrentType.tp_basicsize = sizeof(CurrentObject);
6570 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6571 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006572 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006573#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006574 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6575 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006576#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006577 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6578 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579#endif
6580
6581 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6582 DictionaryType.tp_name = "vim.dictionary";
6583 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006584 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006585 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006586 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006587 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6589 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006590 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6591 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6592 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006593#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006594 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6595 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006596#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006597 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6598 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006599#endif
6600
6601 vim_memset(&ListType, 0, sizeof(ListType));
6602 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006604 ListType.tp_basicsize = sizeof(ListObject);
6605 ListType.tp_as_sequence = &ListAsSeq;
6606 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006607 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608 ListType.tp_doc = "list pushing modifications to vim structure";
6609 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006610 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006611 ListType.tp_new = (newfunc)ListConstructor;
6612 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006613#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006614 ListType.tp_getattro = (getattrofunc)ListGetattro;
6615 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 ListType.tp_getattr = (getattrfunc)ListGetattr;
6618 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619#endif
6620
6621 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006622 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006624 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6625 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006626 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006627 FunctionType.tp_doc = "object that calls vim function";
6628 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006629 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006630 FunctionType.tp_new = (newfunc)FunctionConstructor;
6631 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006632#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006633 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006634#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006635 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006636#endif
6637
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006638 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6639 OptionsType.tp_name = "vim.options";
6640 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006641 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006642 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006643 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006644 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006645 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006646 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6647 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6648 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006649
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006650#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006651 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6652 LoaderType.tp_name = "vim.Loader";
6653 LoaderType.tp_basicsize = sizeof(LoaderObject);
6654 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6655 LoaderType.tp_doc = "vim message object";
6656 LoaderType.tp_methods = LoaderMethods;
6657 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006658#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006659
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006660#if PY_MAJOR_VERSION >= 3
6661 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6662 vimmodule.m_name = "vim";
6663 vimmodule.m_doc = "Vim Python interface\n";
6664 vimmodule.m_size = -1;
6665 vimmodule.m_methods = VimMethods;
6666#endif
6667}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006668
6669#define PYTYPE_READY(type) \
6670 if (PyType_Ready(&type)) \
6671 return -1;
6672
6673 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006674init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006675{
6676 PYTYPE_READY(IterType);
6677 PYTYPE_READY(BufferType);
6678 PYTYPE_READY(RangeType);
6679 PYTYPE_READY(WindowType);
6680 PYTYPE_READY(TabPageType);
6681 PYTYPE_READY(BufMapType);
6682 PYTYPE_READY(WinListType);
6683 PYTYPE_READY(TabListType);
6684 PYTYPE_READY(CurrentType);
6685 PYTYPE_READY(DictionaryType);
6686 PYTYPE_READY(ListType);
6687 PYTYPE_READY(FunctionType);
6688 PYTYPE_READY(OptionsType);
6689 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006690#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006691 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006692#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006693 return 0;
6694}
6695
6696 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006697init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006698{
6699 PyObject *path;
6700 PyObject *path_hook;
6701 PyObject *path_hooks;
6702
6703 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6704 return -1;
6705
6706 if (!(path_hooks = PySys_GetObject("path_hooks")))
6707 {
6708 PyErr_Clear();
6709 path_hooks = PyList_New(1);
6710 PyList_SET_ITEM(path_hooks, 0, path_hook);
6711 if (PySys_SetObject("path_hooks", path_hooks))
6712 {
6713 Py_DECREF(path_hooks);
6714 return -1;
6715 }
6716 Py_DECREF(path_hooks);
6717 }
6718 else if (PyList_Check(path_hooks))
6719 {
6720 if (PyList_Append(path_hooks, path_hook))
6721 {
6722 Py_DECREF(path_hook);
6723 return -1;
6724 }
6725 Py_DECREF(path_hook);
6726 }
6727 else
6728 {
6729 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006730 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006731 "You should now do the following:\n"
6732 "- append vim.path_hook to sys.path_hooks\n"
6733 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6734 VimTryEnd(); /* Discard the error */
6735 Py_DECREF(path_hook);
6736 return 0;
6737 }
6738
6739 if (!(path = PySys_GetObject("path")))
6740 {
6741 PyErr_Clear();
6742 path = PyList_New(1);
6743 Py_INCREF(vim_special_path_object);
6744 PyList_SET_ITEM(path, 0, vim_special_path_object);
6745 if (PySys_SetObject("path", path))
6746 {
6747 Py_DECREF(path);
6748 return -1;
6749 }
6750 Py_DECREF(path);
6751 }
6752 else if (PyList_Check(path))
6753 {
6754 if (PyList_Append(path, vim_special_path_object))
6755 return -1;
6756 }
6757 else
6758 {
6759 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006760 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006761 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6762 VimTryEnd(); /* Discard the error */
6763 }
6764
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006765 return 0;
6766}
6767
6768static BufMapObject TheBufferMap =
6769{
6770 PyObject_HEAD_INIT(&BufMapType)
6771};
6772
6773static WinListObject TheWindowList =
6774{
6775 PyObject_HEAD_INIT(&WinListType)
6776 NULL
6777};
6778
6779static CurrentObject TheCurrent =
6780{
6781 PyObject_HEAD_INIT(&CurrentType)
6782};
6783
6784static TabListObject TheTabPageList =
6785{
6786 PyObject_HEAD_INIT(&TabListType)
6787};
6788
6789static struct numeric_constant {
6790 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006791 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006792} numeric_constants[] = {
6793 {"VAR_LOCKED", VAR_LOCKED},
6794 {"VAR_FIXED", VAR_FIXED},
6795 {"VAR_SCOPE", VAR_SCOPE},
6796 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6797};
6798
6799static struct object_constant {
6800 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006801 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006802} object_constants[] = {
6803 {"buffers", (PyObject *)(void *)&TheBufferMap},
6804 {"windows", (PyObject *)(void *)&TheWindowList},
6805 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6806 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006807
6808 {"Buffer", (PyObject *)&BufferType},
6809 {"Range", (PyObject *)&RangeType},
6810 {"Window", (PyObject *)&WindowType},
6811 {"TabPage", (PyObject *)&TabPageType},
6812 {"Dictionary", (PyObject *)&DictionaryType},
6813 {"List", (PyObject *)&ListType},
6814 {"Function", (PyObject *)&FunctionType},
6815 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006816#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006817 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006818#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006819};
6820
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006821#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006822 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006823 return -1;
6824
6825#define ADD_CHECKED_OBJECT(m, name, obj) \
6826 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006827 PyObject *valObject = obj; \
6828 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006829 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006830 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006831 }
6832
6833 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006834populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006835{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006836 int i;
6837 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006838 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006839 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006840#if PY_VERSION_HEX >= 0x030700f0
6841 PyObject *dict;
6842 PyObject *cls;
6843#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006844
6845 for (i = 0; i < (int)(sizeof(numeric_constants)
6846 / sizeof(struct numeric_constant));
6847 ++i)
6848 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006849 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850
6851 for (i = 0; i < (int)(sizeof(object_constants)
6852 / sizeof(struct object_constant));
6853 ++i)
6854 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006855 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006856
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006857 valObject = object_constants[i].valObject;
6858 Py_INCREF(valObject);
6859 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006860 }
6861
6862 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6863 return -1;
6864 ADD_OBJECT(m, "error", VimError);
6865
Bram Moolenaara9922d62013-05-30 13:01:18 +02006866 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6867 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006868 ADD_CHECKED_OBJECT(m, "options",
6869 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006870
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006871 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006872 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006873 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006874
Bram Moolenaar22081f42016-06-01 20:38:34 +02006875#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006876 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006877 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006878#else
6879 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6880 return -1;
6881#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006882 ADD_OBJECT(m, "_getcwd", py_getcwd)
6883
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006884 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006885 return -1;
6886 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006887 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006889 if (PyObject_SetAttrString(other_module, "chdir", attr))
6890 {
6891 Py_DECREF(attr);
6892 return -1;
6893 }
6894 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006895
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006896 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006897 {
6898 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006899 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006900 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006901 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6902 {
6903 Py_DECREF(attr);
6904 return -1;
6905 }
6906 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006907 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006908 else
6909 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006910
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006911 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6912 return -1;
6913
6914 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6915
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006916#if PY_VERSION_HEX >= 0x030700f0
6917 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6918 return -1;
6919
6920 dict = PyModule_GetDict(imp);
6921
6922 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6923 {
6924 Py_DECREF(imp);
6925 return -1;
6926 }
6927
6928 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6929 {
6930 Py_DECREF(imp);
6931 return -1;
6932 }
6933
6934 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}