blob: 58db254dcc3c019e4ae1f702a5a91d8038c4be4d [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 {
539 EMSG(_("E264: Python: Error initialising I/O objects"));
540 return -1;
541 }
542
543 return 0;
544}
545
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200546#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200547typedef struct
548{
549 PyObject_HEAD
550 PyObject *module;
551} LoaderObject;
552static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200553
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200554 static void
555LoaderDestructor(LoaderObject *self)
556{
557 Py_DECREF(self->module);
558 DESTRUCTOR_FINISH(self);
559}
560
561 static PyObject *
562LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
563{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200564 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200565
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200566 Py_INCREF(ret);
567 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200568}
569
570static struct PyMethodDef LoaderMethods[] = {
571 /* name, function, calling, doc */
572 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
573 { NULL, NULL, 0, NULL}
574};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200575#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200576
577/* Check to see whether a Vim error has been reported, or a keyboard
578 * interrupt has been detected.
579 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200580
581 static void
582VimTryStart(void)
583{
584 ++trylevel;
585}
586
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200587 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200588VimTryEnd(void)
589{
590 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100591 /* Without this it stops processing all subsequent Vim script commands and
592 * generates strange error messages if I e.g. try calling Test() in a cycle
593 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200594 did_emsg = FALSE;
595 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200596 if (got_int)
597 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100598 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100599 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100600 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200601 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200602 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200603 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100604 else if (msg_list != NULL && *msg_list != NULL)
605 {
606 int should_free;
607 char_u *msg;
608
609 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
610
611 if (msg == NULL)
612 {
613 PyErr_NoMemory();
614 return -1;
615 }
616
617 PyErr_SetVim((char *) msg);
618
619 free_global_msglist();
620
621 if (should_free)
622 vim_free(msg);
623
624 return -1;
625 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200626 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200627 return (PyErr_Occurred() ? -1 : 0);
628 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200629 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200630 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100631 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200632 return -1;
633 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100634 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200635 else
636 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200637 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200638 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200639 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200640 }
641}
642
643 static int
644VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200645{
646 if (got_int)
647 {
648 PyErr_SetNone(PyExc_KeyboardInterrupt);
649 return 1;
650 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200651 return 0;
652}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200653
654/* Vim module - Implementation
655 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200656
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200657 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200658VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200660 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200661 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200662 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200663
Bram Moolenaar389a1792013-06-23 13:00:44 +0200664 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665 return NULL;
666
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200667 Py_BEGIN_ALLOW_THREADS
668 Python_Lock_Vim();
669
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200670 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200671 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200672 update_screen(VALID);
673
674 Python_Release_Vim();
675 Py_END_ALLOW_THREADS
676
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200677 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200678 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200680 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200681
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200682 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200683 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200684 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200685}
686
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687/*
688 * Function to translate a typval_T into a PyObject; this will recursively
689 * translate lists/dictionaries into their Python equivalents.
690 *
691 * The depth parameter is to avoid infinite recursion, set it to 1 when
692 * you call VimToPython.
693 */
694 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200695VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200696{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200697 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200698 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200699 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700
701 /* Avoid infinite recursion */
702 if (depth > 100)
703 {
704 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200705 ret = Py_None;
706 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707 }
708
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200709 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200710 * then and we can use it again. */
711 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
712 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
713 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200714 sprintf(ptrBuf, "%p",
715 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
716 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200717
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200718 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200719 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200720 Py_INCREF(ret);
721 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200722 }
723 }
724
725 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200726 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200727 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 else if (our_tv->v_type == VAR_NUMBER)
729 {
730 char buf[NUMBUFLEN];
731
732 /* For backwards compatibility numbers are stored as strings. */
733 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200734 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735 }
736# ifdef FEAT_FLOAT
737 else if (our_tv->v_type == VAR_FLOAT)
738 {
739 char buf[NUMBUFLEN];
740
741 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200742 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200743 }
744# endif
745 else if (our_tv->v_type == VAR_LIST)
746 {
747 list_T *list = our_tv->vval.v_list;
748 listitem_T *curr;
749
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200750 if (list == NULL)
751 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200752
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200753 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200754 return NULL;
755
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200756 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200758 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200759 return NULL;
760 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200762 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
763 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200764 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200766 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200767 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200769 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200770 {
771 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200772 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200773 return NULL;
774 }
775 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200776 }
777 }
778 else if (our_tv->v_type == VAR_DICT)
779 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100781 hashtab_T *ht;
782 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200783 hashitem_T *hi;
784 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100785
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200786 if (our_tv->vval.v_dict == NULL)
787 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100788 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200789
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200790 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200791 return NULL;
792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 return NULL;
797 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100799 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 for (hi = ht->ht_array; todo > 0; ++hi)
801 {
802 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200804 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200806 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200807 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200808 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200809 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200810 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200811 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200812 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200813 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200814 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200815 Py_DECREF(newObj);
816 return NULL;
817 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200818 }
819 }
820 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100821 else if (our_tv->v_type == VAR_SPECIAL)
822 {
823 if (our_tv->vval.v_number == VVAL_FALSE)
824 {
825 ret = Py_False;
826 Py_INCREF(ret);
827 }
828 else if (our_tv->vval.v_number == VVAL_TRUE)
829 {
830 ret = Py_True;
831 Py_INCREF(ret);
832 }
833 else
834 {
835 Py_INCREF(Py_None);
836 ret = Py_None;
837 }
838 return ret;
839 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840 else
841 {
842 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200843 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200844 }
845
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200846 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200847}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848
849 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200850VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200851{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200852 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200853 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200854 PyObject *string;
855 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200856 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200857 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200858
Bram Moolenaar389a1792013-06-23 13:00:44 +0200859 if (!PyArg_ParseTuple(args, "O", &string))
860 return NULL;
861
862 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200863 return NULL;
864
865 Py_BEGIN_ALLOW_THREADS
866 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200867 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200868 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200869 Python_Release_Vim();
870 Py_END_ALLOW_THREADS
871
Bram Moolenaar389a1792013-06-23 13:00:44 +0200872 Py_XDECREF(todecref);
873
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200874 if (VimTryEnd())
875 return NULL;
876
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200877 if (our_tv == NULL)
878 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200879 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200880 return NULL;
881 }
882
883 /* Convert the Vim type into a Python type. Create a dictionary that's
884 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200885 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200887 else
888 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200889 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200890 Py_DECREF(lookup_dict);
891 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200892
893
894 Py_BEGIN_ALLOW_THREADS
895 Python_Lock_Vim();
896 free_tv(our_tv);
897 Python_Release_Vim();
898 Py_END_ALLOW_THREADS
899
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200900 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200901}
902
Bram Moolenaardb913952012-06-29 12:54:53 +0200903static PyObject *ConvertToPyObject(typval_T *);
904
905 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200907{
Bram Moolenaardb913952012-06-29 12:54:53 +0200908 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200910 char_u *expr;
911 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200912
Bram Moolenaar389a1792013-06-23 13:00:44 +0200913 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200914 return NULL;
915
916 Py_BEGIN_ALLOW_THREADS
917 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200918 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200919 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200920 Python_Release_Vim();
921 Py_END_ALLOW_THREADS
922
Bram Moolenaar389a1792013-06-23 13:00:44 +0200923 Py_XDECREF(todecref);
924
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200925 if (VimTryEnd())
926 return NULL;
927
Bram Moolenaardb913952012-06-29 12:54:53 +0200928 if (our_tv == NULL)
929 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200930 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200931 return NULL;
932 }
933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200934 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200935 Py_BEGIN_ALLOW_THREADS
936 Python_Lock_Vim();
937 free_tv(our_tv);
938 Python_Release_Vim();
939 Py_END_ALLOW_THREADS
940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200941 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200942}
943
944 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200945VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200946{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200947 char_u *str;
948 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200949 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200950
Bram Moolenaar389a1792013-06-23 13:00:44 +0200951 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200952 return NULL;
953
Bram Moolenaara54bf402012-12-05 16:30:07 +0100954#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200955 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100956#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200957 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100958#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200959
960 Py_XDECREF(todecref);
961
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200962 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200963}
964
Bram Moolenaarf4258302013-06-02 18:20:17 +0200965 static PyObject *
966_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
967{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200969 PyObject *newwd;
970 PyObject *todecref;
971 char_u *new_dir;
972
Bram Moolenaard4209d22013-06-05 20:34:15 +0200973 if (_chdir == NULL)
974 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200975 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200976 return NULL;
977
978 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
979 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200980 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200981 return NULL;
982 }
983
984 if (!(new_dir = StringToChars(newwd, &todecref)))
985 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200986 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200987 Py_DECREF(newwd);
988 return NULL;
989 }
990
991 VimTryStart();
992
993 if (vim_chdir(new_dir))
994 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200995 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200996 Py_DECREF(newwd);
997 Py_XDECREF(todecref);
998
999 if (VimTryEnd())
1000 return NULL;
1001
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001002 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001003 return NULL;
1004 }
1005
1006 Py_DECREF(newwd);
1007 Py_XDECREF(todecref);
1008
1009 post_chdir(FALSE);
1010
1011 if (VimTryEnd())
1012 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001013 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001014 return NULL;
1015 }
1016
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001017 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001018}
1019
1020 static PyObject *
1021VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1022{
1023 return _VimChdir(py_chdir, args, kwargs);
1024}
1025
1026 static PyObject *
1027VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1028{
1029 return _VimChdir(py_fchdir, args, kwargs);
1030}
1031
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001032typedef struct {
1033 PyObject *callable;
1034 PyObject *result;
1035} map_rtp_data;
1036
1037 static void
1038map_rtp_callback(char_u *path, void *_data)
1039{
1040 void **data = (void **) _data;
1041 PyObject *pathObject;
1042 map_rtp_data *mr_data = *((map_rtp_data **) data);
1043
Bram Moolenaar41009372013-07-01 22:03:04 +02001044 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001045 {
1046 *data = NULL;
1047 return;
1048 }
1049
1050 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1051 pathObject, NULL);
1052
1053 Py_DECREF(pathObject);
1054
1055 if (!mr_data->result || mr_data->result != Py_None)
1056 *data = NULL;
1057 else
1058 {
1059 Py_DECREF(mr_data->result);
1060 mr_data->result = NULL;
1061 }
1062}
1063
1064 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001065VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001066{
1067 map_rtp_data data;
1068
Bram Moolenaar389a1792013-06-23 13:00:44 +02001069 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001070 data.result = NULL;
1071
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001072 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001073
1074 if (data.result == NULL)
1075 {
1076 if (PyErr_Occurred())
1077 return NULL;
1078 else
1079 {
1080 Py_INCREF(Py_None);
1081 return Py_None;
1082 }
1083 }
1084 return data.result;
1085}
1086
1087/*
1088 * _vim_runtimepath_ special path implementation.
1089 */
1090
1091 static void
1092map_finder_callback(char_u *path, void *_data)
1093{
1094 void **data = (void **) _data;
1095 PyObject *list = *((PyObject **) data);
1096 PyObject *pathObject1, *pathObject2;
1097 char *pathbuf;
1098 size_t pathlen;
1099
1100 pathlen = STRLEN(path);
1101
1102#if PY_MAJOR_VERSION < 3
1103# define PY_MAIN_DIR_STRING "python2"
1104#else
1105# define PY_MAIN_DIR_STRING "python3"
1106#endif
1107#define PY_ALTERNATE_DIR_STRING "pythonx"
1108
1109#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1110 if (!(pathbuf = PyMem_New(char,
1111 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1112 {
1113 PyErr_NoMemory();
1114 *data = NULL;
1115 return;
1116 }
1117
1118 mch_memmove(pathbuf, path, pathlen + 1);
1119 add_pathsep((char_u *) pathbuf);
1120
1121 pathlen = STRLEN(pathbuf);
1122 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1123 PYTHONX_STRING_LENGTH + 1);
1124
1125 if (!(pathObject1 = PyString_FromString(pathbuf)))
1126 {
1127 *data = NULL;
1128 PyMem_Free(pathbuf);
1129 return;
1130 }
1131
1132 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1133 PYTHONX_STRING_LENGTH + 1);
1134
1135 if (!(pathObject2 = PyString_FromString(pathbuf)))
1136 {
1137 Py_DECREF(pathObject1);
1138 PyMem_Free(pathbuf);
1139 *data = NULL;
1140 return;
1141 }
1142
1143 PyMem_Free(pathbuf);
1144
1145 if (PyList_Append(list, pathObject1)
1146 || PyList_Append(list, pathObject2))
1147 *data = NULL;
1148
1149 Py_DECREF(pathObject1);
1150 Py_DECREF(pathObject2);
1151}
1152
1153 static PyObject *
1154Vim_GetPaths(PyObject *self UNUSED)
1155{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001156 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001157
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001158 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001159 return NULL;
1160
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001161 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001162
1163 if (PyErr_Occurred())
1164 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001165 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001166 return NULL;
1167 }
1168
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001169 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001170}
1171
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001172#if PY_VERSION_HEX >= 0x030700f0
1173 static PyObject *
1174FinderFindSpec(PyObject *self, PyObject *args)
1175{
1176 char *fullname;
1177 PyObject *paths;
1178 PyObject *target = Py_None;
1179 PyObject *spec;
1180
1181 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1182 return NULL;
1183
1184 if (!(paths = Vim_GetPaths(self)))
1185 return NULL;
1186
1187 spec = PyObject_CallFunction(py_find_spec, "sNN", fullname, paths, target);
1188
1189 Py_DECREF(paths);
1190
1191 if (!spec)
1192 {
1193 if (PyErr_Occurred())
1194 return NULL;
1195
1196 Py_INCREF(Py_None);
1197 return Py_None;
1198 }
1199
1200 return spec;
1201}
1202#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001203 static PyObject *
1204call_load_module(char *name, int len, PyObject *find_module_result)
1205{
1206 PyObject *fd, *pathname, *description;
1207
Bram Moolenaarc476e522013-06-23 13:46:40 +02001208 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001209 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001211 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001212 Py_TYPE_NAME(find_module_result));
1213 return NULL;
1214 }
1215 if (PyTuple_GET_SIZE(find_module_result) != 3)
1216 {
1217 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001218 N_("expected 3-tuple as imp.find_module() result, but got "
1219 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001220 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001221 return NULL;
1222 }
1223
1224 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1225 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1226 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1227 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001228 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001229 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001230 return NULL;
1231 }
1232
1233 return PyObject_CallFunction(py_load_module,
1234 "s#OOO", name, len, fd, pathname, description);
1235}
1236
1237 static PyObject *
1238find_module(char *fullname, char *tail, PyObject *new_path)
1239{
1240 PyObject *find_module_result;
1241 PyObject *module;
1242 char *dot;
1243
Bram Moolenaar41009372013-07-01 22:03:04 +02001244 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001245 {
1246 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001247 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001248 * first component
1249 */
1250 PyObject *newest_path;
1251 int partlen = (int) (dot - 1 - tail);
1252
1253 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1254 "s#O", tail, partlen, new_path)))
1255 return NULL;
1256
1257 if (!(module = call_load_module(
1258 fullname,
1259 ((int) (tail - fullname)) + partlen,
1260 find_module_result)))
1261 {
1262 Py_DECREF(find_module_result);
1263 return NULL;
1264 }
1265
1266 Py_DECREF(find_module_result);
1267
1268 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1269 {
1270 Py_DECREF(module);
1271 return NULL;
1272 }
1273
1274 Py_DECREF(module);
1275
1276 module = find_module(fullname, dot + 1, newest_path);
1277
1278 Py_DECREF(newest_path);
1279
1280 return module;
1281 }
1282 else
1283 {
1284 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1285 "sO", tail, new_path)))
1286 return NULL;
1287
1288 if (!(module = call_load_module(
1289 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001290 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001291 find_module_result)))
1292 {
1293 Py_DECREF(find_module_result);
1294 return NULL;
1295 }
1296
1297 Py_DECREF(find_module_result);
1298
1299 return module;
1300 }
1301}
1302
1303 static PyObject *
1304FinderFindModule(PyObject *self, PyObject *args)
1305{
1306 char *fullname;
1307 PyObject *module;
1308 PyObject *new_path;
1309 LoaderObject *loader;
1310
1311 if (!PyArg_ParseTuple(args, "s", &fullname))
1312 return NULL;
1313
1314 if (!(new_path = Vim_GetPaths(self)))
1315 return NULL;
1316
1317 module = find_module(fullname, fullname, new_path);
1318
1319 Py_DECREF(new_path);
1320
1321 if (!module)
1322 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001323 if (PyErr_Occurred())
1324 {
1325 if (PyErr_ExceptionMatches(PyExc_ImportError))
1326 PyErr_Clear();
1327 else
1328 return NULL;
1329 }
1330
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001331 Py_INCREF(Py_None);
1332 return Py_None;
1333 }
1334
1335 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1336 {
1337 Py_DECREF(module);
1338 return NULL;
1339 }
1340
1341 loader->module = module;
1342
1343 return (PyObject *) loader;
1344}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001345#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001346
1347 static PyObject *
1348VimPathHook(PyObject *self UNUSED, PyObject *args)
1349{
1350 char *path;
1351
1352 if (PyArg_ParseTuple(args, "s", &path)
1353 && STRCMP(path, vim_special_path) == 0)
1354 {
1355 Py_INCREF(vim_module);
1356 return vim_module;
1357 }
1358
1359 PyErr_Clear();
1360 PyErr_SetNone(PyExc_ImportError);
1361 return NULL;
1362}
1363
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001364/*
1365 * Vim module - Definitions
1366 */
1367
1368static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001369 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001370 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001371 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001372 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1373 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001374 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1375 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001376 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001377#if PY_VERSION_HEX >= 0x030700f0
1378 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
1379#else
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001380 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001381#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001382 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1383 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1384 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001385};
1386
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001387/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001388 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001389 */
1390
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001391static PyTypeObject IterType;
1392
1393typedef PyObject *(*nextfun)(void **);
1394typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001395typedef int (*traversefun)(void *, visitproc, void *);
1396typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001397
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001398/* Main purpose of this object is removing the need for do python
1399 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1400 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001401
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001402typedef struct
1403{
1404 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001405 void *cur;
1406 nextfun next;
1407 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001408 traversefun traverse;
1409 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001410} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001411
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001412 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001413IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1414 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001415{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001416 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001417
Bram Moolenaar774267b2013-05-21 20:51:59 +02001418 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001419 self->cur = start;
1420 self->next = next;
1421 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001422 self->traverse = traverse;
1423 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001424
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001425 return (PyObject *)(self);
1426}
1427
1428 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001429IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001430{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001431 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001432 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001433 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001434}
1435
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001436 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001437IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001438{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001439 if (self->traverse != NULL)
1440 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001441 else
1442 return 0;
1443}
1444
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001445/* Mac OSX defines clear() somewhere. */
1446#ifdef clear
1447# undef clear
1448#endif
1449
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001450 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001451IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001452{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001453 if (self->clear != NULL)
1454 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001455 else
1456 return 0;
1457}
1458
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001459 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001460IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001461{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001462 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001463}
1464
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001465 static PyObject *
1466IterIter(PyObject *self)
1467{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001468 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001469 return self;
1470}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001471
Bram Moolenaardb913952012-06-29 12:54:53 +02001472typedef struct pylinkedlist_S {
1473 struct pylinkedlist_S *pll_next;
1474 struct pylinkedlist_S *pll_prev;
1475 PyObject *pll_obj;
1476} pylinkedlist_T;
1477
1478static pylinkedlist_T *lastdict = NULL;
1479static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001480static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001481
1482 static void
1483pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1484{
1485 if (ref->pll_prev == NULL)
1486 {
1487 if (ref->pll_next == NULL)
1488 {
1489 *last = NULL;
1490 return;
1491 }
1492 }
1493 else
1494 ref->pll_prev->pll_next = ref->pll_next;
1495
1496 if (ref->pll_next == NULL)
1497 *last = ref->pll_prev;
1498 else
1499 ref->pll_next->pll_prev = ref->pll_prev;
1500}
1501
1502 static void
1503pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1504{
1505 if (*last == NULL)
1506 ref->pll_prev = NULL;
1507 else
1508 {
1509 (*last)->pll_next = ref;
1510 ref->pll_prev = *last;
1511 }
1512 ref->pll_next = NULL;
1513 ref->pll_obj = self;
1514 *last = ref;
1515}
1516
1517static PyTypeObject DictionaryType;
1518
1519typedef struct
1520{
1521 PyObject_HEAD
1522 dict_T *dict;
1523 pylinkedlist_T ref;
1524} DictionaryObject;
1525
Bram Moolenaara9922d62013-05-30 13:01:18 +02001526static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1527
1528#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1529
Bram Moolenaardb913952012-06-29 12:54:53 +02001530 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001531DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001532{
1533 DictionaryObject *self;
1534
Bram Moolenaara9922d62013-05-30 13:01:18 +02001535 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001536 if (self == NULL)
1537 return NULL;
1538 self->dict = dict;
1539 ++dict->dv_refcount;
1540
1541 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1542
1543 return (PyObject *)(self);
1544}
1545
Bram Moolenaara9922d62013-05-30 13:01:18 +02001546 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001547py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001548{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001549 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001550
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001551 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001552 {
1553 PyErr_NoMemory();
1554 return NULL;
1555 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001556 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001557
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001558 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001559}
1560
1561 static PyObject *
1562DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1563{
1564 DictionaryObject *self;
1565 dict_T *dict;
1566
1567 if (!(dict = py_dict_alloc()))
1568 return NULL;
1569
1570 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1571
1572 --dict->dv_refcount;
1573
1574 if (kwargs || PyTuple_Size(args))
1575 {
1576 PyObject *tmp;
1577 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1578 {
1579 Py_DECREF(self);
1580 return NULL;
1581 }
1582
1583 Py_DECREF(tmp);
1584 }
1585
1586 return (PyObject *)(self);
1587}
1588
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001589 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001591{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001592 pyll_remove(&self->ref, &lastdict);
1593 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001594
1595 DESTRUCTOR_FINISH(self);
1596}
1597
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001598static char *DictionaryAttrs[] = {
1599 "locked", "scope",
1600 NULL
1601};
1602
1603 static PyObject *
1604DictionaryDir(PyObject *self)
1605{
1606 return ObjectDir(self, DictionaryAttrs);
1607}
1608
Bram Moolenaardb913952012-06-29 12:54:53 +02001609 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001610DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001611{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001612 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001613 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001614 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001615 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001616 return -1;
1617 }
1618
1619 if (strcmp(name, "locked") == 0)
1620 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001621 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001622 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001623 PyErr_SET_STRING(PyExc_TypeError,
1624 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001625 return -1;
1626 }
1627 else
1628 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001629 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001630 if (istrue == -1)
1631 return -1;
1632 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001633 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001634 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001636 }
1637 return 0;
1638 }
1639 else
1640 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001641 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001642 return -1;
1643 }
1644}
1645
1646 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001647DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001648{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001649 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001650}
1651
Bram Moolenaara9922d62013-05-30 13:01:18 +02001652#define DICT_FLAG_HAS_DEFAULT 0x01
1653#define DICT_FLAG_POP 0x02
1654#define DICT_FLAG_NONE_DEFAULT 0x04
1655#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1656#define DICT_FLAG_RETURN_PAIR 0x10
1657
Bram Moolenaardb913952012-06-29 12:54:53 +02001658 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001659_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001660{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001661 PyObject *keyObject;
1662 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001663 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001664 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001665 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001666 dict_T *dict = self->dict;
1667 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001668 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001669
Bram Moolenaara9922d62013-05-30 13:01:18 +02001670 if (flags & DICT_FLAG_HAS_DEFAULT)
1671 {
1672 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1673 return NULL;
1674 }
1675 else
1676 keyObject = args;
1677
1678 if (flags & DICT_FLAG_RETURN_BOOL)
1679 defObject = Py_False;
1680
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001681 if (!(key = StringToChars(keyObject, &todecref)))
1682 return NULL;
1683
1684 if (*key == NUL)
1685 {
1686 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001687 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001688 return NULL;
1689 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001690
Bram Moolenaara9922d62013-05-30 13:01:18 +02001691 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001692
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001693 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001694
Bram Moolenaara9922d62013-05-30 13:01:18 +02001695 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001696 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697 if (defObject)
1698 {
1699 Py_INCREF(defObject);
1700 return defObject;
1701 }
1702 else
1703 {
1704 PyErr_SetObject(PyExc_KeyError, keyObject);
1705 return NULL;
1706 }
1707 }
1708 else if (flags & DICT_FLAG_RETURN_BOOL)
1709 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001710 ret = Py_True;
1711 Py_INCREF(ret);
1712 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001713 }
1714
1715 di = dict_lookup(hi);
1716
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001717 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001718 return NULL;
1719
1720 if (flags & DICT_FLAG_POP)
1721 {
1722 if (dict->dv_lock)
1723 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001724 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001725 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001726 return NULL;
1727 }
1728
1729 hash_remove(&dict->dv_hashtab, hi);
1730 dictitem_free(di);
1731 }
1732
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001733 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001734}
1735
1736 static PyObject *
1737DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1738{
1739 return _DictionaryItem(self, keyObject, 0);
1740}
1741
1742 static int
1743DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1744{
1745 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001746 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001748 if (rObj == NULL)
1749 return -1;
1750
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001751 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001752
Bram Moolenaardee2e312013-06-23 16:35:47 +02001753 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001754
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001755 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001756}
1757
1758typedef struct
1759{
1760 hashitem_T *ht_array;
1761 long_u ht_used;
1762 hashtab_T *ht;
1763 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001764 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001765} dictiterinfo_T;
1766
1767 static PyObject *
1768DictionaryIterNext(dictiterinfo_T **dii)
1769{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001770 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001771
1772 if (!(*dii)->todo)
1773 return NULL;
1774
1775 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1776 (*dii)->ht->ht_used != (*dii)->ht_used)
1777 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001778 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001779 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001780 return NULL;
1781 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001782
Bram Moolenaara9922d62013-05-30 13:01:18 +02001783 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1784 ++((*dii)->hi);
1785
1786 --((*dii)->todo);
1787
Bram Moolenaar41009372013-07-01 22:03:04 +02001788 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789 return NULL;
1790
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001791 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792}
1793
1794 static PyObject *
1795DictionaryIter(DictionaryObject *self)
1796{
1797 dictiterinfo_T *dii;
1798 hashtab_T *ht;
1799
1800 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1801 {
1802 PyErr_NoMemory();
1803 return NULL;
1804 }
1805
1806 ht = &self->dict->dv_hashtab;
1807 dii->ht_array = ht->ht_array;
1808 dii->ht_used = ht->ht_used;
1809 dii->ht = ht;
1810 dii->hi = dii->ht_array;
1811 dii->todo = dii->ht_used;
1812
1813 return IterNew(dii,
1814 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1815 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001816}
1817
1818 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001819DictionaryAssItem(
1820 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001821{
1822 char_u *key;
1823 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001824 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001825 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001826 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001827
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001828 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001829 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001830 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 return -1;
1832 }
1833
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001834 if (!(key = StringToChars(keyObject, &todecref)))
1835 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001836
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001837 if (*key == NUL)
1838 {
1839 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001840 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001841 return -1;
1842 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001843
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001844 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001845
1846 if (valObject == NULL)
1847 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001848 hashitem_T *hi;
1849
Bram Moolenaardb913952012-06-29 12:54:53 +02001850 if (di == NULL)
1851 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001852 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001853 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001854 return -1;
1855 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001856 hi = hash_find(&dict->dv_hashtab, di->di_key);
1857 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001858 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001859 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001860 return 0;
1861 }
1862
1863 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001864 {
1865 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001866 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001867 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001868
1869 if (di == NULL)
1870 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001871 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001872 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001873 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001874 PyErr_NoMemory();
1875 return -1;
1876 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001877 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001878
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001879 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001880 {
1881 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001882 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001883 RAISE_KEY_ADD_FAIL(key);
1884 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001885 return -1;
1886 }
1887 }
1888 else
1889 clear_tv(&di->di_tv);
1890
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001891 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001892
1893 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001894 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 return 0;
1896}
1897
Bram Moolenaara9922d62013-05-30 13:01:18 +02001898typedef PyObject *(*hi_to_py)(hashitem_T *);
1899
Bram Moolenaardb913952012-06-29 12:54:53 +02001900 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001901DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001902{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001903 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001904 long_u todo = dict->dv_hashtab.ht_used;
1905 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001906 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001908 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001909
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001910 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1912 {
1913 if (!HASHITEM_EMPTY(hi))
1914 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001915 if (!(newObj = hiconvert(hi)))
1916 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001917 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001918 return NULL;
1919 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001920 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001921 --todo;
1922 ++i;
1923 }
1924 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001925 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001926}
1927
Bram Moolenaara9922d62013-05-30 13:01:18 +02001928 static PyObject *
1929dict_key(hashitem_T *hi)
1930{
1931 return PyBytes_FromString((char *)(hi->hi_key));
1932}
1933
1934 static PyObject *
1935DictionaryListKeys(DictionaryObject *self)
1936{
1937 return DictionaryListObjects(self, dict_key);
1938}
1939
1940 static PyObject *
1941dict_val(hashitem_T *hi)
1942{
1943 dictitem_T *di;
1944
1945 di = dict_lookup(hi);
1946 return ConvertToPyObject(&di->di_tv);
1947}
1948
1949 static PyObject *
1950DictionaryListValues(DictionaryObject *self)
1951{
1952 return DictionaryListObjects(self, dict_val);
1953}
1954
1955 static PyObject *
1956dict_item(hashitem_T *hi)
1957{
1958 PyObject *keyObject;
1959 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001960 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001961
1962 if (!(keyObject = dict_key(hi)))
1963 return NULL;
1964
1965 if (!(valObject = dict_val(hi)))
1966 {
1967 Py_DECREF(keyObject);
1968 return NULL;
1969 }
1970
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001971 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001972
1973 Py_DECREF(keyObject);
1974 Py_DECREF(valObject);
1975
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001976 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001977}
1978
1979 static PyObject *
1980DictionaryListItems(DictionaryObject *self)
1981{
1982 return DictionaryListObjects(self, dict_item);
1983}
1984
1985 static PyObject *
1986DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1987{
1988 dict_T *dict = self->dict;
1989
1990 if (dict->dv_lock)
1991 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001992 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001993 return NULL;
1994 }
1995
1996 if (kwargs)
1997 {
1998 typval_T tv;
1999
2000 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2001 return NULL;
2002
2003 VimTryStart();
2004 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2005 clear_tv(&tv);
2006 if (VimTryEnd())
2007 return NULL;
2008 }
2009 else
2010 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002011 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002013 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002014 return NULL;
2015
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002016 if (obj == NULL)
2017 {
2018 Py_INCREF(Py_None);
2019 return Py_None;
2020 }
2021
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002022 if (PyObject_HasAttrString(obj, "keys"))
2023 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002024 else
2025 {
2026 PyObject *iterator;
2027 PyObject *item;
2028
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002029 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002030 return NULL;
2031
2032 while ((item = PyIter_Next(iterator)))
2033 {
2034 PyObject *fast;
2035 PyObject *keyObject;
2036 PyObject *valObject;
2037 PyObject *todecref;
2038 char_u *key;
2039 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002040 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002041
2042 if (!(fast = PySequence_Fast(item, "")))
2043 {
2044 Py_DECREF(iterator);
2045 Py_DECREF(item);
2046 return NULL;
2047 }
2048
2049 Py_DECREF(item);
2050
2051 if (PySequence_Fast_GET_SIZE(fast) != 2)
2052 {
2053 Py_DECREF(iterator);
2054 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002055 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002056 N_("expected sequence element of size 2, "
2057 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002058 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002059 return NULL;
2060 }
2061
2062 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2063
2064 if (!(key = StringToChars(keyObject, &todecref)))
2065 {
2066 Py_DECREF(iterator);
2067 Py_DECREF(fast);
2068 return NULL;
2069 }
2070
2071 di = dictitem_alloc(key);
2072
2073 Py_XDECREF(todecref);
2074
2075 if (di == NULL)
2076 {
2077 Py_DECREF(fast);
2078 Py_DECREF(iterator);
2079 PyErr_NoMemory();
2080 return NULL;
2081 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002082 di->di_tv.v_type = VAR_UNKNOWN;
2083
2084 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2085
2086 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2087 {
2088 Py_DECREF(iterator);
2089 Py_DECREF(fast);
2090 dictitem_free(di);
2091 return NULL;
2092 }
2093
2094 Py_DECREF(fast);
2095
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002096 hi = hash_find(&dict->dv_hashtab, di->di_key);
2097 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002098 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002099 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002100 Py_DECREF(iterator);
2101 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002102 return NULL;
2103 }
2104 }
2105
2106 Py_DECREF(iterator);
2107
2108 /* Iterator may have finished due to an exception */
2109 if (PyErr_Occurred())
2110 return NULL;
2111 }
2112 }
2113 Py_INCREF(Py_None);
2114 return Py_None;
2115}
2116
2117 static PyObject *
2118DictionaryGet(DictionaryObject *self, PyObject *args)
2119{
2120 return _DictionaryItem(self, args,
2121 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2122}
2123
2124 static PyObject *
2125DictionaryPop(DictionaryObject *self, PyObject *args)
2126{
2127 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2128}
2129
2130 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002131DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002132{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002133 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002134 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002135 PyObject *valObject;
2136 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002137
Bram Moolenaarde71b562013-06-02 17:41:54 +02002138 if (self->dict->dv_hashtab.ht_used == 0)
2139 {
2140 PyErr_SetNone(PyExc_KeyError);
2141 return NULL;
2142 }
2143
2144 hi = self->dict->dv_hashtab.ht_array;
2145 while (HASHITEM_EMPTY(hi))
2146 ++hi;
2147
2148 di = dict_lookup(hi);
2149
2150 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002151 return NULL;
2152
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002153 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002154 {
2155 Py_DECREF(valObject);
2156 return NULL;
2157 }
2158
2159 hash_remove(&self->dict->dv_hashtab, hi);
2160 dictitem_free(di);
2161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002162 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002163}
2164
2165 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002166DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002167{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002168 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2169}
2170
2171static PySequenceMethods DictionaryAsSeq = {
2172 0, /* sq_length */
2173 0, /* sq_concat */
2174 0, /* sq_repeat */
2175 0, /* sq_item */
2176 0, /* sq_slice */
2177 0, /* sq_ass_item */
2178 0, /* sq_ass_slice */
2179 (objobjproc) DictionaryContains, /* sq_contains */
2180 0, /* sq_inplace_concat */
2181 0, /* sq_inplace_repeat */
2182};
2183
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002184static PyMappingMethods DictionaryAsMapping = {
2185 (lenfunc) DictionaryLength,
2186 (binaryfunc) DictionaryItem,
2187 (objobjargproc) DictionaryAssItem,
2188};
2189
Bram Moolenaardb913952012-06-29 12:54:53 +02002190static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002191 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002192 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2193 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2194 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2195 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2196 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002197 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002198 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002199 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2200 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002201};
2202
2203static PyTypeObject ListType;
2204
2205typedef struct
2206{
2207 PyObject_HEAD
2208 list_T *list;
2209 pylinkedlist_T ref;
2210} ListObject;
2211
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002212#define NEW_LIST(list) ListNew(&ListType, list)
2213
Bram Moolenaardb913952012-06-29 12:54:53 +02002214 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002215ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002216{
2217 ListObject *self;
2218
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002219 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002220 if (self == NULL)
2221 return NULL;
2222 self->list = list;
2223 ++list->lv_refcount;
2224
2225 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2226
2227 return (PyObject *)(self);
2228}
2229
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002230 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002231py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002232{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002233 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002234
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002235 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002236 {
2237 PyErr_NoMemory();
2238 return NULL;
2239 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002240 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002241
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002242 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002243}
2244
2245 static int
2246list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2247{
2248 PyObject *iterator;
2249 PyObject *item;
2250 listitem_T *li;
2251
2252 if (!(iterator = PyObject_GetIter(obj)))
2253 return -1;
2254
2255 while ((item = PyIter_Next(iterator)))
2256 {
2257 if (!(li = listitem_alloc()))
2258 {
2259 PyErr_NoMemory();
2260 Py_DECREF(item);
2261 Py_DECREF(iterator);
2262 return -1;
2263 }
2264 li->li_tv.v_lock = 0;
2265 li->li_tv.v_type = VAR_UNKNOWN;
2266
2267 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2268 {
2269 Py_DECREF(item);
2270 Py_DECREF(iterator);
2271 listitem_free(li);
2272 return -1;
2273 }
2274
2275 Py_DECREF(item);
2276
2277 list_append(l, li);
2278 }
2279
2280 Py_DECREF(iterator);
2281
2282 /* Iterator may have finished due to an exception */
2283 if (PyErr_Occurred())
2284 return -1;
2285
2286 return 0;
2287}
2288
2289 static PyObject *
2290ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2291{
2292 list_T *list;
2293 PyObject *obj = NULL;
2294
2295 if (kwargs)
2296 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002297 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002298 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002299 return NULL;
2300 }
2301
2302 if (!PyArg_ParseTuple(args, "|O", &obj))
2303 return NULL;
2304
2305 if (!(list = py_list_alloc()))
2306 return NULL;
2307
2308 if (obj)
2309 {
2310 PyObject *lookup_dict;
2311
2312 if (!(lookup_dict = PyDict_New()))
2313 {
2314 list_unref(list);
2315 return NULL;
2316 }
2317
2318 if (list_py_concat(list, obj, lookup_dict) == -1)
2319 {
2320 Py_DECREF(lookup_dict);
2321 list_unref(list);
2322 return NULL;
2323 }
2324
2325 Py_DECREF(lookup_dict);
2326 }
2327
2328 return ListNew(subtype, list);
2329}
2330
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002331 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002332ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002333{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002334 pyll_remove(&self->ref, &lastlist);
2335 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002336
2337 DESTRUCTOR_FINISH(self);
2338}
2339
Bram Moolenaardb913952012-06-29 12:54:53 +02002340 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002341ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002342{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002343 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002344}
2345
2346 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002347ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002348{
2349 listitem_T *li;
2350
Bram Moolenaard6e39182013-05-21 18:30:34 +02002351 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002352 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002353 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002354 return NULL;
2355 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002356 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002357 if (li == NULL)
2358 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002359 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002360 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002361 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002362 return NULL;
2363 }
2364 return ConvertToPyObject(&li->li_tv);
2365}
2366
Bram Moolenaardb913952012-06-29 12:54:53 +02002367 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002368ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2369 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002370{
2371 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002372 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002373
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002374 if (step == 0)
2375 {
2376 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2377 return NULL;
2378 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002379
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002380 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002381 if (list == NULL)
2382 return NULL;
2383
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002384 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002385 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002386 PyObject *item;
2387
2388 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002389 if (item == NULL)
2390 {
2391 Py_DECREF(list);
2392 return NULL;
2393 }
2394
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002395 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002396 }
2397
2398 return list;
2399}
2400
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002401 static PyObject *
2402ListItem(ListObject *self, PyObject* idx)
2403{
2404#if PY_MAJOR_VERSION < 3
2405 if (PyInt_Check(idx))
2406 {
2407 long _idx = PyInt_AsLong(idx);
2408 return ListIndex(self, _idx);
2409 }
2410 else
2411#endif
2412 if (PyLong_Check(idx))
2413 {
2414 long _idx = PyLong_AsLong(idx);
2415 return ListIndex(self, _idx);
2416 }
2417 else if (PySlice_Check(idx))
2418 {
2419 Py_ssize_t start, stop, step, slicelen;
2420
Bram Moolenaar922a4662014-03-30 16:11:43 +02002421 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002422 &start, &stop, &step, &slicelen) < 0)
2423 return NULL;
2424 return ListSlice(self, start, step, slicelen);
2425 }
2426 else
2427 {
2428 RAISE_INVALID_INDEX_TYPE(idx);
2429 return NULL;
2430 }
2431}
2432
2433 static void
2434list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2435 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2436{
2437 while (numreplaced--)
2438 {
2439 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2440 listitem_remove(l, lis[slicelen + numreplaced]);
2441 }
2442 while (numadded--)
2443 {
2444 listitem_T *next;
2445
2446 next = lastaddedli->li_prev;
2447 listitem_remove(l, lastaddedli);
2448 lastaddedli = next;
2449 }
2450}
2451
2452 static int
2453ListAssSlice(ListObject *self, Py_ssize_t first,
2454 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2455{
2456 PyObject *iterator;
2457 PyObject *item;
2458 listitem_T *li;
2459 listitem_T *lastaddedli = NULL;
2460 listitem_T *next;
2461 typval_T v;
2462 list_T *l = self->list;
2463 PyInt i;
2464 PyInt j;
2465 PyInt numreplaced = 0;
2466 PyInt numadded = 0;
2467 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002468 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002469
2470 size = ListLength(self);
2471
2472 if (l->lv_lock)
2473 {
2474 RAISE_LOCKED_LIST;
2475 return -1;
2476 }
2477
2478 if (step == 0)
2479 {
2480 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2481 return -1;
2482 }
2483
2484 if (step != 1 && slicelen == 0)
2485 {
2486 /* Nothing to do. Only error out if obj has some items. */
2487 int ret = 0;
2488
2489 if (obj == NULL)
2490 return 0;
2491
2492 if (!(iterator = PyObject_GetIter(obj)))
2493 return -1;
2494
2495 if ((item = PyIter_Next(iterator)))
2496 {
2497 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002498 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002499 "to extended slice"), 0);
2500 Py_DECREF(item);
2501 ret = -1;
2502 }
2503 Py_DECREF(iterator);
2504 return ret;
2505 }
2506
2507 if (obj != NULL)
2508 /* XXX May allocate zero bytes. */
2509 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2510 {
2511 PyErr_NoMemory();
2512 return -1;
2513 }
2514
2515 if (first == size)
2516 li = NULL;
2517 else
2518 {
2519 li = list_find(l, (long) first);
2520 if (li == NULL)
2521 {
2522 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2523 (int)first);
2524 if (obj != NULL)
2525 PyMem_Free(lis);
2526 return -1;
2527 }
2528 i = slicelen;
2529 while (i-- && li != NULL)
2530 {
2531 j = step;
2532 next = li;
2533 if (step > 0)
2534 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2535 else
2536 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2537
2538 if (obj == NULL)
2539 listitem_remove(l, li);
2540 else
2541 lis[slicelen - i - 1] = li;
2542
2543 li = next;
2544 }
2545 if (li == NULL && i != -1)
2546 {
2547 PyErr_SET_VIM(N_("internal error: not enough list items"));
2548 if (obj != NULL)
2549 PyMem_Free(lis);
2550 return -1;
2551 }
2552 }
2553
2554 if (obj == NULL)
2555 return 0;
2556
2557 if (!(iterator = PyObject_GetIter(obj)))
2558 {
2559 PyMem_Free(lis);
2560 return -1;
2561 }
2562
2563 i = 0;
2564 while ((item = PyIter_Next(iterator)))
2565 {
2566 if (ConvertFromPyObject(item, &v) == -1)
2567 {
2568 Py_DECREF(iterator);
2569 Py_DECREF(item);
2570 PyMem_Free(lis);
2571 return -1;
2572 }
2573 Py_DECREF(item);
2574 if (list_insert_tv(l, &v, numreplaced < slicelen
2575 ? lis[numreplaced]
2576 : li) == FAIL)
2577 {
2578 clear_tv(&v);
2579 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2580 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2581 PyMem_Free(lis);
2582 return -1;
2583 }
2584 if (numreplaced < slicelen)
2585 {
2586 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002587 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002588 numreplaced++;
2589 }
2590 else
2591 {
2592 if (li)
2593 lastaddedli = li->li_prev;
2594 else
2595 lastaddedli = l->lv_last;
2596 numadded++;
2597 }
2598 clear_tv(&v);
2599 if (step != 1 && i >= slicelen)
2600 {
2601 Py_DECREF(iterator);
2602 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002603 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002604 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002605 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2606 PyMem_Free(lis);
2607 return -1;
2608 }
2609 ++i;
2610 }
2611 Py_DECREF(iterator);
2612
2613 if (step != 1 && i != slicelen)
2614 {
2615 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002616 N_("attempt to assign sequence of size %d to extended slice "
2617 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002618 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2619 PyMem_Free(lis);
2620 return -1;
2621 }
2622
2623 if (PyErr_Occurred())
2624 {
2625 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2626 PyMem_Free(lis);
2627 return -1;
2628 }
2629
2630 for (i = 0; i < numreplaced; i++)
2631 listitem_free(lis[i]);
2632 if (step == 1)
2633 for (i = numreplaced; i < slicelen; i++)
2634 listitem_remove(l, lis[i]);
2635
2636 PyMem_Free(lis);
2637
2638 return 0;
2639}
2640
2641 static int
2642ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2643{
2644 typval_T tv;
2645 list_T *l = self->list;
2646 listitem_T *li;
2647 Py_ssize_t length = ListLength(self);
2648
2649 if (l->lv_lock)
2650 {
2651 RAISE_LOCKED_LIST;
2652 return -1;
2653 }
2654 if (index > length || (index == length && obj == NULL))
2655 {
2656 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2657 return -1;
2658 }
2659
2660 if (obj == NULL)
2661 {
2662 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002663 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002664 clear_tv(&li->li_tv);
2665 vim_free(li);
2666 return 0;
2667 }
2668
2669 if (ConvertFromPyObject(obj, &tv) == -1)
2670 return -1;
2671
2672 if (index == length)
2673 {
2674 if (list_append_tv(l, &tv) == FAIL)
2675 {
2676 clear_tv(&tv);
2677 PyErr_SET_VIM(N_("failed to add item to list"));
2678 return -1;
2679 }
2680 }
2681 else
2682 {
2683 li = list_find(l, (long) index);
2684 clear_tv(&li->li_tv);
2685 copy_tv(&tv, &li->li_tv);
2686 clear_tv(&tv);
2687 }
2688 return 0;
2689}
2690
2691 static Py_ssize_t
2692ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2693{
2694#if PY_MAJOR_VERSION < 3
2695 if (PyInt_Check(idx))
2696 {
2697 long _idx = PyInt_AsLong(idx);
2698 return ListAssIndex(self, _idx, obj);
2699 }
2700 else
2701#endif
2702 if (PyLong_Check(idx))
2703 {
2704 long _idx = PyLong_AsLong(idx);
2705 return ListAssIndex(self, _idx, obj);
2706 }
2707 else if (PySlice_Check(idx))
2708 {
2709 Py_ssize_t start, stop, step, slicelen;
2710
Bram Moolenaar922a4662014-03-30 16:11:43 +02002711 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002712 &start, &stop, &step, &slicelen) < 0)
2713 return -1;
2714 return ListAssSlice(self, start, step, slicelen,
2715 obj);
2716 }
2717 else
2718 {
2719 RAISE_INVALID_INDEX_TYPE(idx);
2720 return -1;
2721 }
2722}
2723
2724 static PyObject *
2725ListConcatInPlace(ListObject *self, PyObject *obj)
2726{
2727 list_T *l = self->list;
2728 PyObject *lookup_dict;
2729
2730 if (l->lv_lock)
2731 {
2732 RAISE_LOCKED_LIST;
2733 return NULL;
2734 }
2735
2736 if (!(lookup_dict = PyDict_New()))
2737 return NULL;
2738
2739 if (list_py_concat(l, obj, lookup_dict) == -1)
2740 {
2741 Py_DECREF(lookup_dict);
2742 return NULL;
2743 }
2744 Py_DECREF(lookup_dict);
2745
2746 Py_INCREF(self);
2747 return (PyObject *)(self);
2748}
2749
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002750typedef struct
2751{
2752 listwatch_T lw;
2753 list_T *list;
2754} listiterinfo_T;
2755
2756 static void
2757ListIterDestruct(listiterinfo_T *lii)
2758{
2759 list_rem_watch(lii->list, &lii->lw);
2760 PyMem_Free(lii);
2761}
2762
2763 static PyObject *
2764ListIterNext(listiterinfo_T **lii)
2765{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002766 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002767
2768 if (!((*lii)->lw.lw_item))
2769 return NULL;
2770
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002771 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002772 return NULL;
2773
2774 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2775
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002776 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002777}
2778
2779 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002780ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002781{
2782 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002783 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002784
2785 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2786 {
2787 PyErr_NoMemory();
2788 return NULL;
2789 }
2790
2791 list_add_watch(l, &lii->lw);
2792 lii->lw.lw_item = l->lv_first;
2793 lii->list = l;
2794
2795 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002796 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2797 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002798}
2799
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002800static char *ListAttrs[] = {
2801 "locked",
2802 NULL
2803};
2804
2805 static PyObject *
2806ListDir(PyObject *self)
2807{
2808 return ObjectDir(self, ListAttrs);
2809}
2810
Bram Moolenaar66b79852012-09-21 14:00:35 +02002811 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002812ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002813{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002814 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002815 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002816 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002817 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002818 return -1;
2819 }
2820
2821 if (strcmp(name, "locked") == 0)
2822 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002823 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002824 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002825 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002826 return -1;
2827 }
2828 else
2829 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002830 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002831 if (istrue == -1)
2832 return -1;
2833 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002834 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002835 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002836 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002837 }
2838 return 0;
2839 }
2840 else
2841 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002842 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002843 return -1;
2844 }
2845}
2846
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002847static PySequenceMethods ListAsSeq = {
2848 (lenfunc) ListLength, /* sq_length, len(x) */
2849 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2850 0, /* RangeRepeat, sq_repeat, x*n */
2851 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2852 0, /* was_sq_slice, x[i:j] */
2853 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2854 0, /* was_sq_ass_slice, x[i:j]=v */
2855 0, /* sq_contains */
2856 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2857 0, /* sq_inplace_repeat */
2858};
2859
2860static PyMappingMethods ListAsMapping = {
2861 /* mp_length */ (lenfunc) ListLength,
2862 /* mp_subscript */ (binaryfunc) ListItem,
2863 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2864};
2865
Bram Moolenaardb913952012-06-29 12:54:53 +02002866static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002867 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2868 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2869 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002870};
2871
2872typedef struct
2873{
2874 PyObject_HEAD
2875 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002876 int argc;
2877 typval_T *argv;
2878 dict_T *self;
2879 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002880 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002881} FunctionObject;
2882
2883static PyTypeObject FunctionType;
2884
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002885#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2886 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002887
Bram Moolenaardb913952012-06-29 12:54:53 +02002888 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002889FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002890 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002891{
2892 FunctionObject *self;
2893
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002894 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2895
Bram Moolenaardb913952012-06-29 12:54:53 +02002896 if (self == NULL)
2897 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002898
2899 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002900 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002901 if (!translated_function_exists(name))
2902 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002903 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002904 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002905 return NULL;
2906 }
2907 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002908 }
2909 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002910 if ((self->name = get_expanded_name(name,
2911 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2912 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002913 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002914 PyErr_FORMAT(PyExc_ValueError,
2915 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002916 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002917 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002918
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002919 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002920 self->argc = argc;
2921 self->argv = argv;
2922 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002923 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002924
2925 if (self->argv || self->self)
2926 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2927
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002928 return (PyObject *)(self);
2929}
2930
2931 static PyObject *
2932FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2933{
2934 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002935 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002936 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002937 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002938 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002939 typval_T selfdicttv;
2940 typval_T argstv;
2941 list_T *argslist = NULL;
2942 dict_T *selfdict = NULL;
2943 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002944 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002945 typval_T *argv = NULL;
2946 typval_T *curtv;
2947 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002948
Bram Moolenaar8110a092016-04-14 15:56:09 +02002949 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002950 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002951 selfdictObject = PyDict_GetItemString(kwargs, "self");
2952 if (selfdictObject != NULL)
2953 {
2954 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2955 return NULL;
2956 selfdict = selfdicttv.vval.v_dict;
2957 }
2958 argsObject = PyDict_GetItemString(kwargs, "args");
2959 if (argsObject != NULL)
2960 {
2961 if (ConvertFromPySequence(argsObject, &argstv) == -1)
2962 {
2963 dict_unref(selfdict);
2964 return NULL;
2965 }
2966 argslist = argstv.vval.v_list;
2967
2968 argc = argslist->lv_len;
2969 if (argc != 0)
2970 {
2971 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02002972 if (argv == NULL)
2973 {
2974 PyErr_NoMemory();
2975 dict_unref(selfdict);
2976 list_unref(argslist);
2977 return NULL;
2978 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02002979 curtv = argv;
2980 for (li = argslist->lv_first; li != NULL; li = li->li_next)
2981 copy_tv(&li->li_tv, curtv++);
2982 }
2983 list_unref(argslist);
2984 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002985 if (selfdict != NULL)
2986 {
2987 auto_rebind = FALSE;
2988 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
2989 if (autoRebindObject != NULL)
2990 {
2991 auto_rebind = PyObject_IsTrue(autoRebindObject);
2992 if (auto_rebind == -1)
2993 {
2994 dict_unref(selfdict);
2995 list_unref(argslist);
2996 return NULL;
2997 }
2998 }
2999 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003000 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003001
Bram Moolenaar389a1792013-06-23 13:00:44 +02003002 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003003 {
3004 dict_unref(selfdict);
3005 while (argc--)
3006 clear_tv(&argv[argc]);
3007 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003008 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003009 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003010
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003011 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003012
Bram Moolenaar389a1792013-06-23 13:00:44 +02003013 PyMem_Free(name);
3014
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003015 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003016}
3017
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003018 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003019FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003020{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003021 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003022 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003023 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003024 for (i = 0; i < self->argc; ++i)
3025 clear_tv(&self->argv[i]);
3026 PyMem_Free(self->argv);
3027 dict_unref(self->self);
3028 if (self->argv || self->self)
3029 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003030
3031 DESTRUCTOR_FINISH(self);
3032}
3033
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003034static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003035 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003036 NULL
3037};
3038
3039 static PyObject *
3040FunctionDir(PyObject *self)
3041{
3042 return ObjectDir(self, FunctionAttrs);
3043}
3044
Bram Moolenaardb913952012-06-29 12:54:53 +02003045 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003046FunctionAttr(FunctionObject *self, char *name)
3047{
3048 list_T *list;
3049 int i;
3050 if (strcmp(name, "name") == 0)
3051 return PyString_FromString((char *)(self->name));
3052 else if (strcmp(name, "args") == 0)
3053 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003054 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003055 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003056
Bram Moolenaar8110a092016-04-14 15:56:09 +02003057 for (i = 0; i < self->argc; ++i)
3058 list_append_tv(list, &self->argv[i]);
3059 return NEW_LIST(list);
3060 }
3061 else if (strcmp(name, "self") == 0)
3062 return self->self == NULL
3063 ? AlwaysNone(NULL)
3064 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003065 else if (strcmp(name, "auto_rebind") == 0)
3066 return self->auto_rebind
3067 ? AlwaysTrue(NULL)
3068 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003069 else if (strcmp(name, "__members__") == 0)
3070 return ObjectDir(NULL, FunctionAttrs);
3071 return NULL;
3072}
3073
3074/* Populate partial_T given function object.
3075 *
3076 * "exported" should be set to true when it is needed to construct a partial
3077 * that may be stored in a variable (i.e. may be freed by Vim).
3078 */
3079 static void
3080set_partial(FunctionObject *self, partial_T *pt, int exported)
3081{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003082 int i;
3083
3084 pt->pt_name = self->name;
3085 if (self->argv)
3086 {
3087 pt->pt_argc = self->argc;
3088 if (exported)
3089 {
3090 pt->pt_argv = (typval_T *)alloc_clear(
3091 sizeof(typval_T) * self->argc);
3092 for (i = 0; i < pt->pt_argc; ++i)
3093 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3094 }
3095 else
3096 pt->pt_argv = self->argv;
3097 }
3098 else
3099 {
3100 pt->pt_argc = 0;
3101 pt->pt_argv = NULL;
3102 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003103 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003104 pt->pt_dict = self->self;
3105 if (exported && self->self)
3106 ++pt->pt_dict->dv_refcount;
3107 if (exported)
3108 pt->pt_name = vim_strsave(pt->pt_name);
3109 pt->pt_refcount = 1;
3110}
3111
3112 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003113FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003114{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003115 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003116 typval_T args;
3117 typval_T selfdicttv;
3118 typval_T rettv;
3119 dict_T *selfdict = NULL;
3120 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003121 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003122 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003123 partial_T pt;
3124 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003125
Bram Moolenaar8110a092016-04-14 15:56:09 +02003126 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003127 return NULL;
3128
3129 if (kwargs != NULL)
3130 {
3131 selfdictObject = PyDict_GetItemString(kwargs, "self");
3132 if (selfdictObject != NULL)
3133 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003134 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003135 {
3136 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003137 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003138 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003139 selfdict = selfdicttv.vval.v_dict;
3140 }
3141 }
3142
Bram Moolenaar8110a092016-04-14 15:56:09 +02003143 if (self->argv || self->self)
3144 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003145 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003146 set_partial(self, &pt, FALSE);
3147 pt_ptr = &pt;
3148 }
3149
Bram Moolenaar71700b82013-05-15 17:49:05 +02003150 Py_BEGIN_ALLOW_THREADS
3151 Python_Lock_Vim();
3152
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003153 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003154 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003155
3156 Python_Release_Vim();
3157 Py_END_ALLOW_THREADS
3158
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003159 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003160 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003161 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003162 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003163 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003164 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003165 }
3166 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003167 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003168
Bram Moolenaardb913952012-06-29 12:54:53 +02003169 clear_tv(&args);
3170 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003171 if (selfdict != NULL)
3172 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003173
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003174 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003175}
3176
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003177 static PyObject *
3178FunctionRepr(FunctionObject *self)
3179{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003180 PyObject *ret;
3181 garray_T repr_ga;
3182 int i;
3183 char_u *tofree = NULL;
3184 typval_T tv;
3185 char_u numbuf[NUMBUFLEN];
3186
3187 ga_init2(&repr_ga, (int)sizeof(char), 70);
3188 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3189 if (self->name)
3190 ga_concat(&repr_ga, self->name);
3191 else
3192 ga_concat(&repr_ga, (char_u *)"<NULL>");
3193 ga_append(&repr_ga, '\'');
3194 if (self->argv)
3195 {
3196 ga_concat(&repr_ga, (char_u *)", args=[");
3197 ++emsg_silent;
3198 for (i = 0; i < self->argc; i++)
3199 {
3200 if (i != 0)
3201 ga_concat(&repr_ga, (char_u *)", ");
3202 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3203 get_copyID()));
3204 vim_free(tofree);
3205 }
3206 --emsg_silent;
3207 ga_append(&repr_ga, ']');
3208 }
3209 if (self->self)
3210 {
3211 ga_concat(&repr_ga, (char_u *)", self=");
3212 tv.v_type = VAR_DICT;
3213 tv.vval.v_dict = self->self;
3214 ++emsg_silent;
3215 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3216 --emsg_silent;
3217 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003218 if (self->auto_rebind)
3219 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003220 }
3221 ga_append(&repr_ga, '>');
3222 ret = PyString_FromString((char *)repr_ga.ga_data);
3223 ga_clear(&repr_ga);
3224 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003225}
3226
Bram Moolenaardb913952012-06-29 12:54:53 +02003227static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003228 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3229 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003230};
3231
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003232/*
3233 * Options object
3234 */
3235
3236static PyTypeObject OptionsType;
3237
3238typedef int (*checkfun)(void *);
3239
3240typedef struct
3241{
3242 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003243 int opt_type;
3244 void *from;
3245 checkfun Check;
3246 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003247} OptionsObject;
3248
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003249 static int
3250dummy_check(void *arg UNUSED)
3251{
3252 return 0;
3253}
3254
3255 static PyObject *
3256OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3257{
3258 OptionsObject *self;
3259
Bram Moolenaar774267b2013-05-21 20:51:59 +02003260 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003261 if (self == NULL)
3262 return NULL;
3263
3264 self->opt_type = opt_type;
3265 self->from = from;
3266 self->Check = Check;
3267 self->fromObj = fromObj;
3268 if (fromObj)
3269 Py_INCREF(fromObj);
3270
3271 return (PyObject *)(self);
3272}
3273
3274 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003275OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003276{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003277 PyObject_GC_UnTrack((void *)(self));
3278 Py_XDECREF(self->fromObj);
3279 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003280}
3281
3282 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003283OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003284{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003285 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003286 return 0;
3287}
3288
3289 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003290OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003291{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003292 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003293 return 0;
3294}
3295
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003296 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003297OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003298{
3299 char_u *key;
3300 int flags;
3301 long numval;
3302 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003303 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003304
Bram Moolenaard6e39182013-05-21 18:30:34 +02003305 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003306 return NULL;
3307
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003308 if (!(key = StringToChars(keyObject, &todecref)))
3309 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003310
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003311 if (*key == NUL)
3312 {
3313 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003314 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003315 return NULL;
3316 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003317
3318 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003319 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003320
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003321 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003322
3323 if (flags == 0)
3324 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003325 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003326 return NULL;
3327 }
3328
3329 if (flags & SOPT_UNSET)
3330 {
3331 Py_INCREF(Py_None);
3332 return Py_None;
3333 }
3334 else if (flags & SOPT_BOOL)
3335 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003336 PyObject *ret;
3337 ret = numval ? Py_True : Py_False;
3338 Py_INCREF(ret);
3339 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003340 }
3341 else if (flags & SOPT_NUM)
3342 return PyInt_FromLong(numval);
3343 else if (flags & SOPT_STRING)
3344 {
3345 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003346 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003347 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003348 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003349 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003350 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003351 else
3352 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003353 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003354 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003355 return NULL;
3356 }
3357 }
3358 else
3359 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003360 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003361 return NULL;
3362 }
3363}
3364
3365 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003366OptionsContains(OptionsObject *self, PyObject *keyObject)
3367{
3368 char_u *key;
3369 PyObject *todecref;
3370
3371 if (!(key = StringToChars(keyObject, &todecref)))
3372 return -1;
3373
3374 if (*key == NUL)
3375 {
3376 Py_XDECREF(todecref);
3377 return 0;
3378 }
3379
3380 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3381 {
3382 Py_XDECREF(todecref);
3383 return 1;
3384 }
3385 else
3386 {
3387 Py_XDECREF(todecref);
3388 return 0;
3389 }
3390}
3391
3392typedef struct
3393{
3394 void *lastoption;
3395 int opt_type;
3396} optiterinfo_T;
3397
3398 static PyObject *
3399OptionsIterNext(optiterinfo_T **oii)
3400{
3401 char_u *name;
3402
3403 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3404 return PyString_FromString((char *)name);
3405
3406 return NULL;
3407}
3408
3409 static PyObject *
3410OptionsIter(OptionsObject *self)
3411{
3412 optiterinfo_T *oii;
3413
3414 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3415 {
3416 PyErr_NoMemory();
3417 return NULL;
3418 }
3419
3420 oii->opt_type = self->opt_type;
3421 oii->lastoption = NULL;
3422
3423 return IterNew(oii,
3424 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3425 NULL, NULL);
3426}
3427
3428 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003429set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003430{
3431 char_u *errmsg;
3432
3433 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3434 {
3435 if (VimTryEnd())
3436 return FAIL;
3437 PyErr_SetVim((char *)errmsg);
3438 return FAIL;
3439 }
3440 return OK;
3441}
3442
3443 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003444set_option_value_for(
3445 char_u *key,
3446 int numval,
3447 char_u *stringval,
3448 int opt_flags,
3449 int opt_type,
3450 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003451{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003452 win_T *save_curwin = NULL;
3453 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003454 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003455 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003456
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003457 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003458 switch (opt_type)
3459 {
3460 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003461 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003462 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003463 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003464 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003465 if (VimTryEnd())
3466 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003467 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003468 return -1;
3469 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003470 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3471 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003472 break;
3473 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003474 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003475 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003476 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003477 break;
3478 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003479 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003480 break;
3481 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003482 if (set_ret == FAIL)
3483 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003484 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003485}
3486
3487 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003488OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003489{
3490 char_u *key;
3491 int flags;
3492 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003493 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003494 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003495
Bram Moolenaard6e39182013-05-21 18:30:34 +02003496 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003497 return -1;
3498
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003499 if (!(key = StringToChars(keyObject, &todecref)))
3500 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003501
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003502 if (*key == NUL)
3503 {
3504 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003505 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003506 return -1;
3507 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003508
3509 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003510 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003511
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003512 if (flags == 0)
3513 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003514 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003515 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003516 return -1;
3517 }
3518
3519 if (valObject == NULL)
3520 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003521 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003522 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003523 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003524 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003525 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003526 return -1;
3527 }
3528 else if (!(flags & SOPT_GLOBAL))
3529 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003530 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003531 N_("unable to unset option %s "
3532 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003533 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003534 return -1;
3535 }
3536 else
3537 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003538 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003539 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540 return 0;
3541 }
3542 }
3543
Bram Moolenaard6e39182013-05-21 18:30:34 +02003544 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003545
3546 if (flags & SOPT_BOOL)
3547 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003548 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003549
Bram Moolenaarb983f752013-05-15 16:11:50 +02003550 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003551 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003552 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003553 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003554 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003555 }
3556 else if (flags & SOPT_NUM)
3557 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003558 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003559
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003560 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003561 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003562 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003563 return -1;
3564 }
3565
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003566 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003567 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003568 }
3569 else
3570 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003571 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003572 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003573
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003574 if ((val = StringToChars(valObject, &todecref2)))
3575 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003576 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003577 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003578 Py_XDECREF(todecref2);
3579 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003580 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003581 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003582 }
3583
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003584 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003585
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003586 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003587}
3588
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003589static PySequenceMethods OptionsAsSeq = {
3590 0, /* sq_length */
3591 0, /* sq_concat */
3592 0, /* sq_repeat */
3593 0, /* sq_item */
3594 0, /* sq_slice */
3595 0, /* sq_ass_item */
3596 0, /* sq_ass_slice */
3597 (objobjproc) OptionsContains, /* sq_contains */
3598 0, /* sq_inplace_concat */
3599 0, /* sq_inplace_repeat */
3600};
3601
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003602static PyMappingMethods OptionsAsMapping = {
3603 (lenfunc) NULL,
3604 (binaryfunc) OptionsItem,
3605 (objobjargproc) OptionsAssItem,
3606};
3607
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003608/* Tabpage object
3609 */
3610
3611typedef struct
3612{
3613 PyObject_HEAD
3614 tabpage_T *tab;
3615} TabPageObject;
3616
3617static PyObject *WinListNew(TabPageObject *tabObject);
3618
3619static PyTypeObject TabPageType;
3620
3621 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003622CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003623{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003624 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003625 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003626 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003627 return -1;
3628 }
3629
3630 return 0;
3631}
3632
3633 static PyObject *
3634TabPageNew(tabpage_T *tab)
3635{
3636 TabPageObject *self;
3637
3638 if (TAB_PYTHON_REF(tab))
3639 {
3640 self = TAB_PYTHON_REF(tab);
3641 Py_INCREF(self);
3642 }
3643 else
3644 {
3645 self = PyObject_NEW(TabPageObject, &TabPageType);
3646 if (self == NULL)
3647 return NULL;
3648 self->tab = tab;
3649 TAB_PYTHON_REF(tab) = self;
3650 }
3651
3652 return (PyObject *)(self);
3653}
3654
3655 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003656TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003657{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003658 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3659 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003660
3661 DESTRUCTOR_FINISH(self);
3662}
3663
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003664static char *TabPageAttrs[] = {
3665 "windows", "number", "vars", "window", "valid",
3666 NULL
3667};
3668
3669 static PyObject *
3670TabPageDir(PyObject *self)
3671{
3672 return ObjectDir(self, TabPageAttrs);
3673}
3674
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003675 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003676TabPageAttrValid(TabPageObject *self, char *name)
3677{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003678 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003679
3680 if (strcmp(name, "valid") != 0)
3681 return NULL;
3682
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003683 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3684 Py_INCREF(ret);
3685 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003686}
3687
3688 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003689TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003690{
3691 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003692 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003693 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003694 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003695 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003696 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003697 else if (strcmp(name, "window") == 0)
3698 {
3699 /* For current tab window.c does not bother to set or update tp_curwin
3700 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003701 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003702 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003703 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003704 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003705 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003706 else if (strcmp(name, "__members__") == 0)
3707 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003708 return NULL;
3709}
3710
3711 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003712TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003713{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003714 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003715 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003716 else
3717 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003718 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003719
3720 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003721 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3722 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003723 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003724 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003725 }
3726}
3727
3728static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003729 /* name, function, calling, documentation */
3730 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3731 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003732};
3733
3734/*
3735 * Window list object
3736 */
3737
3738static PyTypeObject TabListType;
3739static PySequenceMethods TabListAsSeq;
3740
3741typedef struct
3742{
3743 PyObject_HEAD
3744} TabListObject;
3745
3746 static PyInt
3747TabListLength(PyObject *self UNUSED)
3748{
3749 tabpage_T *tp = first_tabpage;
3750 PyInt n = 0;
3751
3752 while (tp != NULL)
3753 {
3754 ++n;
3755 tp = tp->tp_next;
3756 }
3757
3758 return n;
3759}
3760
3761 static PyObject *
3762TabListItem(PyObject *self UNUSED, PyInt n)
3763{
3764 tabpage_T *tp;
3765
3766 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3767 if (n == 0)
3768 return TabPageNew(tp);
3769
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003770 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771 return NULL;
3772}
3773
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003774/*
3775 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003776 */
3777
3778typedef struct
3779{
3780 PyObject_HEAD
3781 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003782 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003783} WindowObject;
3784
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003785static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003786
3787 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003788CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003789{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003790 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003791 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003792 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003793 return -1;
3794 }
3795
3796 return 0;
3797}
3798
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003799 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003800WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003801{
3802 /* We need to handle deletion of windows underneath us.
3803 * If we add a "w_python*_ref" field to the win_T structure,
3804 * then we can get at it in win_free() in vim. We then
3805 * need to create only ONE Python object per window - if
3806 * we try to create a second, just INCREF the existing one
3807 * and return it. The (single) Python object referring to
3808 * the window is stored in "w_python*_ref".
3809 * On a win_free() we set the Python object's win_T* field
3810 * to an invalid value. We trap all uses of a window
3811 * object, and reject them if the win_T* field is invalid.
3812 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003813 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003814 * w_python_ref and w_python3_ref fields respectively.
3815 */
3816
3817 WindowObject *self;
3818
3819 if (WIN_PYTHON_REF(win))
3820 {
3821 self = WIN_PYTHON_REF(win);
3822 Py_INCREF(self);
3823 }
3824 else
3825 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003826 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003827 if (self == NULL)
3828 return NULL;
3829 self->win = win;
3830 WIN_PYTHON_REF(win) = self;
3831 }
3832
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003833 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3834
Bram Moolenaar971db462013-05-12 18:44:48 +02003835 return (PyObject *)(self);
3836}
3837
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003838 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003839WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003840{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003841 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003842 if (self->win && self->win != INVALID_WINDOW_VALUE)
3843 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003844 Py_XDECREF(((PyObject *)(self->tabObject)));
3845 PyObject_GC_Del((void *)(self));
3846}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003847
Bram Moolenaar774267b2013-05-21 20:51:59 +02003848 static int
3849WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3850{
3851 Py_VISIT(((PyObject *)(self->tabObject)));
3852 return 0;
3853}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003854
Bram Moolenaar774267b2013-05-21 20:51:59 +02003855 static int
3856WindowClear(WindowObject *self)
3857{
3858 Py_CLEAR(self->tabObject);
3859 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003860}
3861
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003862 static win_T *
3863get_firstwin(TabPageObject *tabObject)
3864{
3865 if (tabObject)
3866 {
3867 if (CheckTabPage(tabObject))
3868 return NULL;
3869 /* For current tab window.c does not bother to set or update tp_firstwin
3870 */
3871 else if (tabObject->tab == curtab)
3872 return firstwin;
3873 else
3874 return tabObject->tab->tp_firstwin;
3875 }
3876 else
3877 return firstwin;
3878}
Bram Moolenaare950f992018-06-10 13:55:55 +02003879
3880// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003881static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003882 "buffer",
3883 "cursor",
3884 "height",
3885 "row",
3886 "width",
3887 "col",
3888 "vars",
3889 "options",
3890 "number",
3891 "tabpage",
3892 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003893 NULL
3894};
3895
3896 static PyObject *
3897WindowDir(PyObject *self)
3898{
3899 return ObjectDir(self, WindowAttrs);
3900}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003901
Bram Moolenaar971db462013-05-12 18:44:48 +02003902 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003903WindowAttrValid(WindowObject *self, char *name)
3904{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003905 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003906
3907 if (strcmp(name, "valid") != 0)
3908 return NULL;
3909
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003910 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3911 Py_INCREF(ret);
3912 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003913}
3914
3915 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003916WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003917{
3918 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003919 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003920 else if (strcmp(name, "cursor") == 0)
3921 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003922 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003923
3924 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3925 }
3926 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003927 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003928 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003929 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003930 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003931 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003932 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003933 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003934 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003935 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003936 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003937 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3938 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003939 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003940 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003941 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003942 return NULL;
3943 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003944 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003945 }
3946 else if (strcmp(name, "tabpage") == 0)
3947 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003948 Py_INCREF(self->tabObject);
3949 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003950 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003951 else if (strcmp(name, "__members__") == 0)
3952 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003953 else
3954 return NULL;
3955}
3956
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003957 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003958WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003959{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003960 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003961 return -1;
3962
3963 if (strcmp(name, "buffer") == 0)
3964 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003965 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003966 return -1;
3967 }
3968 else if (strcmp(name, "cursor") == 0)
3969 {
3970 long lnum;
3971 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003973 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003974 return -1;
3975
Bram Moolenaard6e39182013-05-21 18:30:34 +02003976 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003977 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003978 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003979 return -1;
3980 }
3981
3982 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003983 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003984 return -1;
3985
Bram Moolenaard6e39182013-05-21 18:30:34 +02003986 self->win->w_cursor.lnum = lnum;
3987 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003989 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003990#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003991 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003993
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003994 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003995 return 0;
3996 }
3997 else if (strcmp(name, "height") == 0)
3998 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003999 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004000 win_T *savewin;
4001
Bram Moolenaardee2e312013-06-23 16:35:47 +02004002 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003 return -1;
4004
4005#ifdef FEAT_GUI
4006 need_mouse_correct = TRUE;
4007#endif
4008 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004009 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004010
4011 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004012 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004013 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004014 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004015 return -1;
4016
4017 return 0;
4018 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004019 else if (strcmp(name, "width") == 0)
4020 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004021 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022 win_T *savewin;
4023
Bram Moolenaardee2e312013-06-23 16:35:47 +02004024 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004025 return -1;
4026
4027#ifdef FEAT_GUI
4028 need_mouse_correct = TRUE;
4029#endif
4030 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004031 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004032
4033 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004034 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004036 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037 return -1;
4038
4039 return 0;
4040 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041 else
4042 {
4043 PyErr_SetString(PyExc_AttributeError, name);
4044 return -1;
4045 }
4046}
4047
4048 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004049WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004050{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004051 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004052 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004053 else
4054 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004055 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056
Bram Moolenaar6d216452013-05-12 19:00:41 +02004057 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004058 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 (self));
4060 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004061 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 }
4063}
4064
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004065static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004066 /* name, function, calling, documentation */
4067 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4068 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004069};
4070
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004072 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004074
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004075static PyTypeObject WinListType;
4076static PySequenceMethods WinListAsSeq;
4077
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004078typedef struct
4079{
4080 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004081 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004082} WinListObject;
4083
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004084 static PyObject *
4085WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004087 WinListObject *self;
4088
4089 self = PyObject_NEW(WinListObject, &WinListType);
4090 self->tabObject = tabObject;
4091 Py_INCREF(tabObject);
4092
4093 return (PyObject *)(self);
4094}
4095
4096 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004097WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004098{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004099 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004100
4101 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004102 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004103 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004104 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004105
4106 DESTRUCTOR_FINISH(self);
4107}
4108
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004109 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004110WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004111{
4112 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 PyInt n = 0;
4114
Bram Moolenaard6e39182013-05-21 18:30:34 +02004115 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004116 return -1;
4117
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118 while (w != NULL)
4119 {
4120 ++n;
4121 w = W_NEXT(w);
4122 }
4123
4124 return n;
4125}
4126
4127 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004128WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129{
4130 win_T *w;
4131
Bram Moolenaard6e39182013-05-21 18:30:34 +02004132 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004133 return NULL;
4134
4135 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004137 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004139 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004140 return NULL;
4141}
4142
4143/* Convert a Python string into a Vim line.
4144 *
4145 * The result is in allocated memory. All internal nulls are replaced by
4146 * newline characters. It is an error for the string to contain newline
4147 * characters.
4148 *
4149 * On errors, the Python exception data is set, and NULL is returned.
4150 */
4151 static char *
4152StringToLine(PyObject *obj)
4153{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004154 char *str;
4155 char *save;
4156 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004157 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004158 PyInt i;
4159 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004160
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004161 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004162 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004163 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4164 || str == NULL)
4165 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004166 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004167 else if (PyUnicode_Check(obj))
4168 {
4169 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4170 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004171
Bram Moolenaardaa27022013-06-24 22:33:30 +02004172 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004173 || str == NULL)
4174 {
4175 Py_DECREF(bytes);
4176 return NULL;
4177 }
4178 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004179 else
4180 {
4181#if PY_MAJOR_VERSION < 3
4182 PyErr_FORMAT(PyExc_TypeError,
4183 N_("expected str() or unicode() instance, but got %s"),
4184 Py_TYPE_NAME(obj));
4185#else
4186 PyErr_FORMAT(PyExc_TypeError,
4187 N_("expected bytes() or str() instance, but got %s"),
4188 Py_TYPE_NAME(obj));
4189#endif
4190 return NULL;
4191 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192
4193 /*
4194 * Error checking: String must not contain newlines, as we
4195 * are replacing a single line, and we must replace it with
4196 * a single line.
4197 * A trailing newline is removed, so that append(f.readlines()) works.
4198 */
4199 p = memchr(str, '\n', len);
4200 if (p != NULL)
4201 {
4202 if (p == str + len - 1)
4203 --len;
4204 else
4205 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004206 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004207 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004208 return NULL;
4209 }
4210 }
4211
4212 /* Create a copy of the string, with internal nulls replaced by
4213 * newline characters, as is the vim convention.
4214 */
4215 save = (char *)alloc((unsigned)(len+1));
4216 if (save == NULL)
4217 {
4218 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004219 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004220 return NULL;
4221 }
4222
4223 for (i = 0; i < len; ++i)
4224 {
4225 if (str[i] == '\0')
4226 save[i] = '\n';
4227 else
4228 save[i] = str[i];
4229 }
4230
4231 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004232 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004233
4234 return save;
4235}
4236
4237/* Get a line from the specified buffer. The line number is
4238 * in Vim format (1-based). The line is returned as a Python
4239 * string object.
4240 */
4241 static PyObject *
4242GetBufferLine(buf_T *buf, PyInt n)
4243{
4244 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4245}
4246
4247
4248/* Get a list of lines from the specified buffer. The line numbers
4249 * are in Vim format (1-based). The range is from lo up to, but not
4250 * including, hi. The list is returned as a Python list of string objects.
4251 */
4252 static PyObject *
4253GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4254{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004255 PyInt i;
4256 PyInt n = hi - lo;
4257 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004258
4259 if (list == NULL)
4260 return NULL;
4261
4262 for (i = 0; i < n; ++i)
4263 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004264 PyObject *string = LineToString(
4265 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004266
4267 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004268 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269 {
4270 Py_DECREF(list);
4271 return NULL;
4272 }
4273
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004274 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004275 }
4276
4277 /* The ownership of the Python list is passed to the caller (ie,
4278 * the caller should Py_DECREF() the object when it is finished
4279 * with it).
4280 */
4281
4282 return list;
4283}
4284
4285/*
4286 * Check if deleting lines made the cursor position invalid.
4287 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4288 * deleted).
4289 */
4290 static void
4291py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4292{
4293 if (curwin->w_cursor.lnum >= lo)
4294 {
4295 /* Adjust the cursor position if it's in/after the changed
4296 * lines. */
4297 if (curwin->w_cursor.lnum >= hi)
4298 {
4299 curwin->w_cursor.lnum += extra;
4300 check_cursor_col();
4301 }
4302 else if (extra < 0)
4303 {
4304 curwin->w_cursor.lnum = lo;
4305 check_cursor();
4306 }
4307 else
4308 check_cursor_col();
4309 changed_cline_bef_curs();
4310 }
4311 invalidate_botline();
4312}
4313
Bram Moolenaar19e60942011-06-19 00:27:51 +02004314/*
4315 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004316 * in Vim format (1-based). The replacement line is given as
4317 * a Python string object. The object is checked for validity
4318 * and correct format. Errors are returned as a value of FAIL.
4319 * The return value is OK on success.
4320 * If OK is returned and len_change is not NULL, *len_change
4321 * is set to the change in the buffer length.
4322 */
4323 static int
4324SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4325{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004326 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004327 win_T *save_curwin = NULL;
4328 tabpage_T *save_curtab = NULL;
4329
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004330 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004331 * There are three cases:
4332 * 1. NULL, or None - this is a deletion.
4333 * 2. A string - this is a replacement.
4334 * 3. Anything else - this is an error.
4335 */
4336 if (line == Py_None || line == NULL)
4337 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004338 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004339 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004340
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004341 VimTryStart();
4342
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004343 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004344 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004345 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004346 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004347 else
4348 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004349 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004350 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004351 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004352 /* Only adjust marks if we managed to switch to a window that
4353 * holds the buffer, otherwise line numbers will be invalid. */
4354 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004355 }
4356
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004357 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004358
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004359 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004360 return FAIL;
4361
4362 if (len_change)
4363 *len_change = -1;
4364
4365 return OK;
4366 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004367 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004368 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004369 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004370
4371 if (save == NULL)
4372 return FAIL;
4373
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004374 VimTryStart();
4375
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004376 /* We do not need to free "save" if ml_replace() consumes it. */
4377 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004378 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004379
4380 if (u_savesub((linenr_T)n) == FAIL)
4381 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004382 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383 vim_free(save);
4384 }
4385 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4386 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004387 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004388 vim_free(save);
4389 }
4390 else
4391 changed_bytes((linenr_T)n, 0);
4392
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004393 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004394
4395 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004396 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 check_cursor_col();
4398
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004399 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004400 return FAIL;
4401
4402 if (len_change)
4403 *len_change = 0;
4404
4405 return OK;
4406 }
4407 else
4408 {
4409 PyErr_BadArgument();
4410 return FAIL;
4411 }
4412}
4413
Bram Moolenaar19e60942011-06-19 00:27:51 +02004414/* Replace a range of lines in the specified buffer. The line numbers are in
4415 * Vim format (1-based). The range is from lo up to, but not including, hi.
4416 * The replacement lines are given as a Python list of string objects. The
4417 * list is checked for validity and correct format. Errors are returned as a
4418 * value of FAIL. The return value is OK on success.
4419 * If OK is returned and len_change is not NULL, *len_change
4420 * is set to the change in the buffer length.
4421 */
4422 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004423SetBufferLineList(
4424 buf_T *buf,
4425 PyInt lo,
4426 PyInt hi,
4427 PyObject *list,
4428 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004429{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004430 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004431 win_T *save_curwin = NULL;
4432 tabpage_T *save_curtab = NULL;
4433
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004434 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004435 * There are three cases:
4436 * 1. NULL, or None - this is a deletion.
4437 * 2. A list - this is a replacement.
4438 * 3. Anything else - this is an error.
4439 */
4440 if (list == Py_None || list == NULL)
4441 {
4442 PyInt i;
4443 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004444
4445 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004446 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004447 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004448
4449 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004450 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004451 else
4452 {
4453 for (i = 0; i < n; ++i)
4454 {
4455 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4456 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004457 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004458 break;
4459 }
4460 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004461 if (buf == curbuf && (save_curwin != NULL
4462 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004463 /* Using an existing window for the buffer, adjust the cursor
4464 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004465 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004466 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004467 /* Only adjust marks if we managed to switch to a window that
4468 * holds the buffer, otherwise line numbers will be invalid. */
4469 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004470 }
4471
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004472 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004473
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004474 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004475 return FAIL;
4476
4477 if (len_change)
4478 *len_change = -n;
4479
4480 return OK;
4481 }
4482 else if (PyList_Check(list))
4483 {
4484 PyInt i;
4485 PyInt new_len = PyList_Size(list);
4486 PyInt old_len = hi - lo;
4487 PyInt extra = 0; /* lines added to text, can be negative */
4488 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004489
4490 if (new_len == 0) /* avoid allocating zero bytes */
4491 array = NULL;
4492 else
4493 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004494 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004495 if (array == NULL)
4496 {
4497 PyErr_NoMemory();
4498 return FAIL;
4499 }
4500 }
4501
4502 for (i = 0; i < new_len; ++i)
4503 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004504 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004505
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004506 if (!(line = PyList_GetItem(list, i)) ||
4507 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004508 {
4509 while (i)
4510 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004511 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004512 return FAIL;
4513 }
4514 }
4515
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004516 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004517 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004518
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004519 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004520 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004521
4522 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004523 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004524
4525 /* If the size of the range is reducing (ie, new_len < old_len) we
4526 * need to delete some old_len. We do this at the start, by
4527 * repeatedly deleting line "lo".
4528 */
4529 if (!PyErr_Occurred())
4530 {
4531 for (i = 0; i < old_len - new_len; ++i)
4532 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4533 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004534 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004535 break;
4536 }
4537 extra -= i;
4538 }
4539
4540 /* For as long as possible, replace the existing old_len with the
4541 * new old_len. This is a more efficient operation, as it requires
4542 * less memory allocation and freeing.
4543 */
4544 if (!PyErr_Occurred())
4545 {
4546 for (i = 0; i < old_len && i < new_len; ++i)
4547 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4548 == FAIL)
4549 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004550 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551 break;
4552 }
4553 }
4554 else
4555 i = 0;
4556
4557 /* Now we may need to insert the remaining new old_len. If we do, we
4558 * must free the strings as we finish with them (we can't pass the
4559 * responsibility to vim in this case).
4560 */
4561 if (!PyErr_Occurred())
4562 {
4563 while (i < new_len)
4564 {
4565 if (ml_append((linenr_T)(lo + i - 1),
4566 (char_u *)array[i], 0, FALSE) == FAIL)
4567 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004568 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004569 break;
4570 }
4571 vim_free(array[i]);
4572 ++i;
4573 ++extra;
4574 }
4575 }
4576
4577 /* Free any left-over old_len, as a result of an error */
4578 while (i < new_len)
4579 {
4580 vim_free(array[i]);
4581 ++i;
4582 }
4583
4584 /* Free the array of old_len. All of its contents have now
4585 * been dealt with (either freed, or the responsibility passed
4586 * to vim.
4587 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004588 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004589
4590 /* Adjust marks. Invalidate any which lie in the
4591 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004592 * Only adjust marks if we managed to switch to a window that holds
4593 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004594 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004595 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004596 (long)MAXLNUM, (long)extra);
4597 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4598
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004599 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004600 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4601
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004602 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004603 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004604
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004605 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004606 return FAIL;
4607
4608 if (len_change)
4609 *len_change = new_len - old_len;
4610
4611 return OK;
4612 }
4613 else
4614 {
4615 PyErr_BadArgument();
4616 return FAIL;
4617 }
4618}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004619
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004620/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004621 * The line number is in Vim format (1-based). The lines to be inserted are
4622 * given as a Python list of string objects or as a single string. The lines
4623 * to be added are checked for validity and correct format. Errors are
4624 * returned as a value of FAIL. The return value is OK on success.
4625 * If OK is returned and len_change is not NULL, *len_change
4626 * is set to the change in the buffer length.
4627 */
4628 static int
4629InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4630{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004631 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004632 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004633 tabpage_T *save_curtab = NULL;
4634
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004635 /* First of all, we check the type of the supplied Python object.
4636 * It must be a string or a list, or the call is in error.
4637 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004638 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004639 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004640 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004641
4642 if (str == NULL)
4643 return FAIL;
4644
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004645 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004646 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004647 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004648
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004649 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004650 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004651 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004652 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004653 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004654 /* Only adjust marks if we managed to switch to a window that
4655 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004656 appended_lines_mark((linenr_T)n, 1L);
4657
4658 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004659 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004660 update_screen(VALID);
4661
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004662 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004663 return FAIL;
4664
4665 if (len_change)
4666 *len_change = 1;
4667
4668 return OK;
4669 }
4670 else if (PyList_Check(lines))
4671 {
4672 PyInt i;
4673 PyInt size = PyList_Size(lines);
4674 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004676 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677 if (array == NULL)
4678 {
4679 PyErr_NoMemory();
4680 return FAIL;
4681 }
4682
4683 for (i = 0; i < size; ++i)
4684 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004685 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004686
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004687 if (!(line = PyList_GetItem(lines, i)) ||
4688 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004689 {
4690 while (i)
4691 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004692 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693 return FAIL;
4694 }
4695 }
4696
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004698 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004699 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004700
4701 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004702 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004703 else
4704 {
4705 for (i = 0; i < size; ++i)
4706 {
4707 if (ml_append((linenr_T)(n + i),
4708 (char_u *)array[i], 0, FALSE) == FAIL)
4709 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004710 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711
4712 /* Free the rest of the lines */
4713 while (i < size)
4714 vim_free(array[i++]);
4715
4716 break;
4717 }
4718 vim_free(array[i]);
4719 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004720 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004721 /* Only adjust marks if we managed to switch to a window that
4722 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723 appended_lines_mark((linenr_T)n, (long)i);
4724 }
4725
4726 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004727 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004728 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004729 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731 update_screen(VALID);
4732
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004733 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004734 return FAIL;
4735
4736 if (len_change)
4737 *len_change = size;
4738
4739 return OK;
4740 }
4741 else
4742 {
4743 PyErr_BadArgument();
4744 return FAIL;
4745 }
4746}
4747
4748/*
4749 * Common routines for buffers and line ranges
4750 * -------------------------------------------
4751 */
4752
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004753typedef struct
4754{
4755 PyObject_HEAD
4756 buf_T *buf;
4757} BufferObject;
4758
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004759 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004760CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004761{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004762 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004764 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004765 return -1;
4766 }
4767
4768 return 0;
4769}
4770
4771 static PyObject *
4772RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4773{
4774 if (CheckBuffer(self))
4775 return NULL;
4776
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004777 if (end == -1)
4778 end = self->buf->b_ml.ml_line_count;
4779
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004780 if (n < 0)
4781 n += end - start + 1;
4782
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 if (n < 0 || n > end - start)
4784 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004785 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 return NULL;
4787 }
4788
4789 return GetBufferLine(self->buf, n+start);
4790}
4791
4792 static PyObject *
4793RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4794{
4795 PyInt size;
4796
4797 if (CheckBuffer(self))
4798 return NULL;
4799
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004800 if (end == -1)
4801 end = self->buf->b_ml.ml_line_count;
4802
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004803 size = end - start + 1;
4804
4805 if (lo < 0)
4806 lo = 0;
4807 else if (lo > size)
4808 lo = size;
4809 if (hi < 0)
4810 hi = 0;
4811 if (hi < lo)
4812 hi = lo;
4813 else if (hi > size)
4814 hi = size;
4815
4816 return GetBufferLineList(self->buf, lo+start, hi+start);
4817}
4818
4819 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004820RBAsItem(
4821 BufferObject *self,
4822 PyInt n,
4823 PyObject *valObject,
4824 PyInt start,
4825 PyInt end,
4826 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004827{
4828 PyInt len_change;
4829
4830 if (CheckBuffer(self))
4831 return -1;
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 -1;
4843 }
4844
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004845 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004846 return -1;
4847
4848 if (new_end)
4849 *new_end = end + len_change;
4850
4851 return 0;
4852}
4853
Bram Moolenaar19e60942011-06-19 00:27:51 +02004854 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004855RBAsSlice(
4856 BufferObject *self,
4857 PyInt lo,
4858 PyInt hi,
4859 PyObject *valObject,
4860 PyInt start,
4861 PyInt end,
4862 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004863{
4864 PyInt size;
4865 PyInt len_change;
4866
4867 /* Self must be a valid buffer */
4868 if (CheckBuffer(self))
4869 return -1;
4870
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004871 if (end == -1)
4872 end = self->buf->b_ml.ml_line_count;
4873
Bram Moolenaar19e60942011-06-19 00:27:51 +02004874 /* Sort out the slice range */
4875 size = end - start + 1;
4876
4877 if (lo < 0)
4878 lo = 0;
4879 else if (lo > size)
4880 lo = size;
4881 if (hi < 0)
4882 hi = 0;
4883 if (hi < lo)
4884 hi = lo;
4885 else if (hi > size)
4886 hi = size;
4887
4888 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004889 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004890 return -1;
4891
4892 if (new_end)
4893 *new_end = end + len_change;
4894
4895 return 0;
4896}
4897
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004898
4899 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004900RBAppend(
4901 BufferObject *self,
4902 PyObject *args,
4903 PyInt start,
4904 PyInt end,
4905 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004906{
4907 PyObject *lines;
4908 PyInt len_change;
4909 PyInt max;
4910 PyInt n;
4911
4912 if (CheckBuffer(self))
4913 return NULL;
4914
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004915 if (end == -1)
4916 end = self->buf->b_ml.ml_line_count;
4917
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004918 max = n = end - start + 1;
4919
4920 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4921 return NULL;
4922
4923 if (n < 0 || n > max)
4924 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004925 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004926 return NULL;
4927 }
4928
4929 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4930 return NULL;
4931
4932 if (new_end)
4933 *new_end = end + len_change;
4934
4935 Py_INCREF(Py_None);
4936 return Py_None;
4937}
4938
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004939/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004940 */
4941
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004942static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004943static PySequenceMethods RangeAsSeq;
4944static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004945
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004946typedef struct
4947{
4948 PyObject_HEAD
4949 BufferObject *buf;
4950 PyInt start;
4951 PyInt end;
4952} RangeObject;
4953
4954 static PyObject *
4955RangeNew(buf_T *buf, PyInt start, PyInt end)
4956{
4957 BufferObject *bufr;
4958 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004959 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004960 if (self == NULL)
4961 return NULL;
4962
4963 bufr = (BufferObject *)BufferNew(buf);
4964 if (bufr == NULL)
4965 {
4966 Py_DECREF(self);
4967 return NULL;
4968 }
4969 Py_INCREF(bufr);
4970
4971 self->buf = bufr;
4972 self->start = start;
4973 self->end = end;
4974
4975 return (PyObject *)(self);
4976}
4977
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004978 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004979RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004980{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004981 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004982 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004983 PyObject_GC_Del((void *)(self));
4984}
4985
4986 static int
4987RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4988{
4989 Py_VISIT(((PyObject *)(self->buf)));
4990 return 0;
4991}
4992
4993 static int
4994RangeClear(RangeObject *self)
4995{
4996 Py_CLEAR(self->buf);
4997 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004998}
4999
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005000 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005001RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005002{
5003 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005004 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005005 return -1; /* ??? */
5006
Bram Moolenaard6e39182013-05-21 18:30:34 +02005007 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005008}
5009
5010 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005011RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005012{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005013 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005014}
5015
5016 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005017RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005018{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005019 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005020}
5021
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005022static char *RangeAttrs[] = {
5023 "start", "end",
5024 NULL
5025};
5026
5027 static PyObject *
5028RangeDir(PyObject *self)
5029{
5030 return ObjectDir(self, RangeAttrs);
5031}
5032
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005033 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005034RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005035{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005036 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005037}
5038
5039 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005040RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005041{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005042 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005043 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5044 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005045 else
5046 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005047 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005048
5049 if (name == NULL)
5050 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005051
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005052 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005053 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005054 }
5055}
5056
5057static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005058 /* name, function, calling, documentation */
5059 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005060 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5061 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005062};
5063
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005064static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005065static PySequenceMethods BufferAsSeq;
5066static PyMappingMethods BufferAsMapping;
5067
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005068 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005069BufferNew(buf_T *buf)
5070{
5071 /* We need to handle deletion of buffers underneath us.
5072 * If we add a "b_python*_ref" field to the buf_T structure,
5073 * then we can get at it in buf_freeall() in vim. We then
5074 * need to create only ONE Python object per buffer - if
5075 * we try to create a second, just INCREF the existing one
5076 * and return it. The (single) Python object referring to
5077 * the buffer is stored in "b_python*_ref".
5078 * Question: what to do on a buf_freeall(). We'll probably
5079 * have to either delete the Python object (DECREF it to
5080 * zero - a bad idea, as it leaves dangling refs!) or
5081 * set the buf_T * value to an invalid value (-1?), which
5082 * means we need checks in all access functions... Bah.
5083 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005084 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005085 * b_python_ref and b_python3_ref fields respectively.
5086 */
5087
5088 BufferObject *self;
5089
5090 if (BUF_PYTHON_REF(buf) != NULL)
5091 {
5092 self = BUF_PYTHON_REF(buf);
5093 Py_INCREF(self);
5094 }
5095 else
5096 {
5097 self = PyObject_NEW(BufferObject, &BufferType);
5098 if (self == NULL)
5099 return NULL;
5100 self->buf = buf;
5101 BUF_PYTHON_REF(buf) = self;
5102 }
5103
5104 return (PyObject *)(self);
5105}
5106
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005108BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005109{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005110 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5111 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005112
5113 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005114}
5115
Bram Moolenaar971db462013-05-12 18:44:48 +02005116 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005117BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005118{
5119 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005120 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005121 return -1; /* ??? */
5122
Bram Moolenaard6e39182013-05-21 18:30:34 +02005123 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005124}
5125
5126 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005127BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005128{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005129 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005130}
5131
5132 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005133BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005134{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005135 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005136}
5137
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005138static char *BufferAttrs[] = {
5139 "name", "number", "vars", "options", "valid",
5140 NULL
5141};
5142
5143 static PyObject *
5144BufferDir(PyObject *self)
5145{
5146 return ObjectDir(self, BufferAttrs);
5147}
5148
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005149 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005150BufferAttrValid(BufferObject *self, char *name)
5151{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005152 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005153
5154 if (strcmp(name, "valid") != 0)
5155 return NULL;
5156
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005157 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5158 Py_INCREF(ret);
5159 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005160}
5161
5162 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005163BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005164{
5165 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005166 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005167 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005168 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005170 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005171 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005172 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005173 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5174 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005175 else if (strcmp(name, "__members__") == 0)
5176 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005177 else
5178 return NULL;
5179}
5180
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005181 static int
5182BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5183{
5184 if (CheckBuffer(self))
5185 return -1;
5186
5187 if (strcmp(name, "name") == 0)
5188 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005189 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005190 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005191 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005192 PyObject *todecref;
5193
5194 if (!(val = StringToChars(valObject, &todecref)))
5195 return -1;
5196
5197 VimTryStart();
5198 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5199 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005200 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005201 aucmd_restbuf(&aco);
5202 Py_XDECREF(todecref);
5203 if (VimTryEnd())
5204 return -1;
5205
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005206 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005207 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005208 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005209 return -1;
5210 }
5211 return 0;
5212 }
5213 else
5214 {
5215 PyErr_SetString(PyExc_AttributeError, name);
5216 return -1;
5217 }
5218}
5219
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005220 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005221BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005222{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005223 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005224}
5225
5226 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005227BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005228{
5229 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005230 char_u *pmark;
5231 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005232 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005233 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005234
Bram Moolenaard6e39182013-05-21 18:30:34 +02005235 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005236 return NULL;
5237
Bram Moolenaar389a1792013-06-23 13:00:44 +02005238 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005239 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005240
Bram Moolenaar389a1792013-06-23 13:00:44 +02005241 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005242 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005243 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005244 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005245 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005246 return NULL;
5247 }
5248
5249 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005250
5251 Py_XDECREF(todecref);
5252
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005253 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005254 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005255 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005256 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005257 if (VimTryEnd())
5258 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005259
5260 if (posp == NULL)
5261 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005262 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005263 return NULL;
5264 }
5265
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005266 if (posp->lnum <= 0)
5267 {
5268 /* Or raise an error? */
5269 Py_INCREF(Py_None);
5270 return Py_None;
5271 }
5272
5273 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5274}
5275
5276 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005277BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005278{
5279 PyInt start;
5280 PyInt end;
5281
Bram Moolenaard6e39182013-05-21 18:30:34 +02005282 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005283 return NULL;
5284
5285 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5286 return NULL;
5287
Bram Moolenaard6e39182013-05-21 18:30:34 +02005288 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005289}
5290
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005291 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005292BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005293{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005294 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005295 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005296 else
5297 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005298 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005299
5300 if (name == NULL)
5301 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005302
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005303 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005304 }
5305}
5306
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005307static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005308 /* name, function, calling, documentation */
5309 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005310 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005311 {"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 +02005312 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5313 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005314};
5315
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005316/*
5317 * Buffer list object - Implementation
5318 */
5319
5320static PyTypeObject BufMapType;
5321
5322typedef struct
5323{
5324 PyObject_HEAD
5325} BufMapObject;
5326
5327 static PyInt
5328BufMapLength(PyObject *self UNUSED)
5329{
5330 buf_T *b = firstbuf;
5331 PyInt n = 0;
5332
5333 while (b)
5334 {
5335 ++n;
5336 b = b->b_next;
5337 }
5338
5339 return n;
5340}
5341
5342 static PyObject *
5343BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5344{
5345 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005346 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005347
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005348 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005349 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005350
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005351 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005352
5353 if (b)
5354 return BufferNew(b);
5355 else
5356 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005357 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005358 return NULL;
5359 }
5360}
5361
5362 static void
5363BufMapIterDestruct(PyObject *buffer)
5364{
5365 /* Iteration was stopped before all buffers were processed */
5366 if (buffer)
5367 {
5368 Py_DECREF(buffer);
5369 }
5370}
5371
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005372 static int
5373BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5374{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005375 if (buffer)
5376 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005377 return 0;
5378}
5379
5380 static int
5381BufMapIterClear(PyObject **buffer)
5382{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005383 if (*buffer)
5384 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005385 return 0;
5386}
5387
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005388 static PyObject *
5389BufMapIterNext(PyObject **buffer)
5390{
5391 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005392 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005393
5394 if (!*buffer)
5395 return NULL;
5396
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005397 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005398
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005399 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005400 {
5401 *buffer = NULL;
5402 return NULL;
5403 }
5404
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005405 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005406 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005407 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005408 return NULL;
5409 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005410 /* Do not increment reference: we no longer hold it (decref), but whoever
5411 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005412 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005413}
5414
5415 static PyObject *
5416BufMapIter(PyObject *self UNUSED)
5417{
5418 PyObject *buffer;
5419
5420 buffer = BufferNew(firstbuf);
5421 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005422 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5423 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005424}
5425
5426static PyMappingMethods BufMapAsMapping = {
5427 (lenfunc) BufMapLength,
5428 (binaryfunc) BufMapItem,
5429 (objobjargproc) 0,
5430};
5431
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005432/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005433 */
5434
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005435static char *CurrentAttrs[] = {
5436 "buffer", "window", "line", "range", "tabpage",
5437 NULL
5438};
5439
5440 static PyObject *
5441CurrentDir(PyObject *self)
5442{
5443 return ObjectDir(self, CurrentAttrs);
5444}
5445
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005446 static PyObject *
5447CurrentGetattr(PyObject *self UNUSED, char *name)
5448{
5449 if (strcmp(name, "buffer") == 0)
5450 return (PyObject *)BufferNew(curbuf);
5451 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005452 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005453 else if (strcmp(name, "tabpage") == 0)
5454 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005455 else if (strcmp(name, "line") == 0)
5456 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5457 else if (strcmp(name, "range") == 0)
5458 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005459 else if (strcmp(name, "__members__") == 0)
5460 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005461 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005462#if PY_MAJOR_VERSION < 3
5463 return Py_FindMethod(WindowMethods, self, name);
5464#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005465 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005466#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005467}
5468
5469 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005470CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005471{
5472 if (strcmp(name, "line") == 0)
5473 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5475 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005476 return -1;
5477
5478 return 0;
5479 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005480 else if (strcmp(name, "buffer") == 0)
5481 {
5482 int count;
5483
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005484 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005485 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005486 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005487 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005488 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005489 return -1;
5490 }
5491
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005493 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005494 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005495
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005496 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005497 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5498 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005499 if (VimTryEnd())
5500 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005501 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005502 return -1;
5503 }
5504
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005505 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005506 }
5507 else if (strcmp(name, "window") == 0)
5508 {
5509 int count;
5510
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005511 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005512 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005513 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005514 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005515 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005516 return -1;
5517 }
5518
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005519 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005520 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005521 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005522
5523 if (!count)
5524 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005525 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005526 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005527 return -1;
5528 }
5529
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005530 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005531 win_goto(((WindowObject *)(valObject))->win);
5532 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005533 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005534 if (VimTryEnd())
5535 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005536 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005537 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005538 return -1;
5539 }
5540
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005541 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005542 }
5543 else if (strcmp(name, "tabpage") == 0)
5544 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005545 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005546 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005547 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005548 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005549 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005550 return -1;
5551 }
5552
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005553 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005554 return -1;
5555
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005556 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005557 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5558 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005559 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005560 if (VimTryEnd())
5561 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005562 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005563 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005564 return -1;
5565 }
5566
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005567 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005568 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005569 else
5570 {
5571 PyErr_SetString(PyExc_AttributeError, name);
5572 return -1;
5573 }
5574}
5575
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005576static struct PyMethodDef CurrentMethods[] = {
5577 /* name, function, calling, documentation */
5578 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5579 { NULL, NULL, 0, NULL}
5580};
5581
Bram Moolenaardb913952012-06-29 12:54:53 +02005582 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005583init_range_cmd(exarg_T *eap)
5584{
5585 RangeStart = eap->line1;
5586 RangeEnd = eap->line2;
5587}
5588
5589 static void
5590init_range_eval(typval_T *rettv UNUSED)
5591{
5592 RangeStart = (PyInt) curwin->w_cursor.lnum;
5593 RangeEnd = RangeStart;
5594}
5595
5596 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005597run_cmd(const char *cmd, void *arg UNUSED
5598#ifdef PY_CAN_RECURSE
5599 , PyGILState_STATE *pygilstate UNUSED
5600#endif
5601 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005602{
Bram Moolenaar41009372013-07-01 22:03:04 +02005603 PyObject *run_ret;
5604 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5605 if (run_ret != NULL)
5606 {
5607 Py_DECREF(run_ret);
5608 }
5609 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5610 {
5611 EMSG2(_(e_py_systemexit), "python");
5612 PyErr_Clear();
5613 }
5614 else
5615 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005616}
5617
5618static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5619static int code_hdr_len = 30;
5620
5621 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005622run_do(const char *cmd, void *arg UNUSED
5623#ifdef PY_CAN_RECURSE
5624 , PyGILState_STATE *pygilstate
5625#endif
5626 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005627{
5628 PyInt lnum;
5629 size_t len;
5630 char *code;
5631 int status;
5632 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005633 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005634 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005635
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005636 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005637 {
5638 EMSG(_("cannot save undo information"));
5639 return;
5640 }
5641
5642 len = code_hdr_len + STRLEN(cmd);
5643 code = PyMem_New(char, len + 1);
5644 memcpy(code, code_hdr, code_hdr_len);
5645 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005646 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5647 status = -1;
5648 if (run_ret != NULL)
5649 {
5650 status = 0;
5651 Py_DECREF(run_ret);
5652 }
5653 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5654 {
5655 PyMem_Free(code);
5656 EMSG2(_(e_py_systemexit), "python");
5657 PyErr_Clear();
5658 return;
5659 }
5660 else
5661 PyErr_PrintEx(1);
5662
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005663 PyMem_Free(code);
5664
5665 if (status)
5666 {
5667 EMSG(_("failed to run the code"));
5668 return;
5669 }
5670
5671 status = 0;
5672 pymain = PyImport_AddModule("__main__");
5673 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005674#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005675 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005676#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005677
5678 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5679 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005680 PyObject *line;
5681 PyObject *linenr;
5682 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005683
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005684#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005685 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005686#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005687 /* Check the line number, the command my have deleted lines. */
5688 if (lnum > curbuf->b_ml.ml_line_count
5689 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005690 goto err;
5691 if (!(linenr = PyInt_FromLong((long) lnum)))
5692 {
5693 Py_DECREF(line);
5694 goto err;
5695 }
5696 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5697 Py_DECREF(line);
5698 Py_DECREF(linenr);
5699 if (!ret)
5700 goto err;
5701
Bram Moolenaara58883b2017-01-29 21:31:09 +01005702 /* Check that the command didn't switch to another buffer. */
5703 if (curbuf != was_curbuf)
5704 {
5705 Py_XDECREF(ret);
5706 goto err;
5707 }
5708
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005709 if (ret != Py_None)
5710 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005711 {
5712 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005713 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005714 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005715
5716 Py_XDECREF(ret);
5717 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005718#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005719 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005720#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005721 }
5722 goto out;
5723err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005724#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005725 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005726#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005727 PyErr_PrintEx(0);
5728 PythonIO_Flush();
5729 status = 1;
5730out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005731#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005732 if (!status)
5733 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005734#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005735 Py_DECREF(pyfunc);
5736 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5737 if (status)
5738 return;
5739 check_cursor();
5740 update_curbuf(NOT_VALID);
5741}
5742
5743 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005744run_eval(const char *cmd, typval_T *rettv
5745#ifdef PY_CAN_RECURSE
5746 , PyGILState_STATE *pygilstate UNUSED
5747#endif
5748 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005749{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005750 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005751
Bram Moolenaar41009372013-07-01 22:03:04 +02005752 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005753 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005754 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005755 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005756 {
5757 EMSG2(_(e_py_systemexit), "python");
5758 PyErr_Clear();
5759 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005760 else
5761 {
5762 if (PyErr_Occurred() && !msg_silent)
5763 PyErr_PrintEx(0);
5764 EMSG(_("E858: Eval did not return a valid python object"));
5765 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005766 }
5767 else
5768 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005769 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005770 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005771 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005772 }
5773 PyErr_Clear();
5774}
5775
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005777set_ref_in_py(const int copyID)
5778{
5779 pylinkedlist_T *cur;
5780 dict_T *dd;
5781 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005782 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005783 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005784 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005785
5786 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005787 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005788 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005789 {
5790 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5791 if (dd->dv_copyID != copyID)
5792 {
5793 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005794 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005795 }
5796 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005797 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005798
5799 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005800 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005801 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005802 {
5803 ll = ((ListObject *) (cur->pll_obj))->list;
5804 if (ll->lv_copyID != copyID)
5805 {
5806 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005807 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005808 }
5809 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005810 }
5811
Bram Moolenaar8110a092016-04-14 15:56:09 +02005812 if (lastfunc != NULL)
5813 {
5814 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5815 {
5816 func = (FunctionObject *) cur->pll_obj;
5817 if (func->self != NULL && func->self->dv_copyID != copyID)
5818 {
5819 func->self->dv_copyID = copyID;
5820 abort = abort || set_ref_in_ht(
5821 &func->self->dv_hashtab, copyID, NULL);
5822 }
5823 if (func->argc)
5824 for (i = 0; !abort && i < func->argc; ++i)
5825 abort = abort
5826 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5827 }
5828 }
5829
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005830 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005831}
5832
5833 static int
5834set_string_copy(char_u *str, typval_T *tv)
5835{
5836 tv->vval.v_string = vim_strsave(str);
5837 if (tv->vval.v_string == NULL)
5838 {
5839 PyErr_NoMemory();
5840 return -1;
5841 }
5842 return 0;
5843}
5844
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005845 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005846pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005847{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005848 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005849 char_u *key;
5850 dictitem_T *di;
5851 PyObject *keyObject;
5852 PyObject *valObject;
5853 Py_ssize_t iter = 0;
5854
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005855 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005856 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005857
5858 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005859 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005860
5861 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5862 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005863 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005864
Bram Moolenaara03e6312013-05-29 22:49:26 +02005865 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005866 {
5867 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005868 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005869 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005870
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005871 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005872 {
5873 dict_unref(dict);
5874 return -1;
5875 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005876
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005877 if (*key == NUL)
5878 {
5879 dict_unref(dict);
5880 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005881 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005882 return -1;
5883 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005884
5885 di = dictitem_alloc(key);
5886
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005887 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005888
5889 if (di == NULL)
5890 {
5891 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005892 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005893 return -1;
5894 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005895
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005896 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005897 {
5898 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005899 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005900 return -1;
5901 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005902
5903 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005904 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005905 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005906 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005907 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005908 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005909 return -1;
5910 }
5911 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005912
5913 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005914 return 0;
5915}
5916
5917 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005918pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005919{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005920 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005921 char_u *key;
5922 dictitem_T *di;
5923 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005924 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005925 PyObject *keyObject;
5926 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005927
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005928 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005929 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930
5931 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005932 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005933
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005934 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005935 {
5936 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005938 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005939
5940 if (!(iterator = PyObject_GetIter(list)))
5941 {
5942 dict_unref(dict);
5943 Py_DECREF(list);
5944 return -1;
5945 }
5946 Py_DECREF(list);
5947
5948 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005949 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005950 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005952 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005953 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005954 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005955 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005956 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005957 return -1;
5958 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005959
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005960 if (*key == NUL)
5961 {
5962 Py_DECREF(keyObject);
5963 Py_DECREF(iterator);
5964 Py_XDECREF(todecref);
5965 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005966 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005967 return -1;
5968 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005970 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005972 Py_DECREF(keyObject);
5973 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005974 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976 return -1;
5977 }
5978
5979 di = dictitem_alloc(key);
5980
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005981 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005982 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005983
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005984 if (di == NULL)
5985 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005986 Py_DECREF(iterator);
5987 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005988 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989 PyErr_NoMemory();
5990 return -1;
5991 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005992
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005993 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005994 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005995 Py_DECREF(iterator);
5996 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005997 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005998 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005999 return -1;
6000 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006001
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006002 Py_DECREF(valObject);
6003
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006004 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006005 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006006 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006007 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006008 dictitem_free(di);
6009 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006010 return -1;
6011 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006012 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006013 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006014 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006015 return 0;
6016}
6017
6018 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006019pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006020{
6021 list_T *l;
6022
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006023 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006024 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006025
6026 tv->v_type = VAR_LIST;
6027 tv->vval.v_list = l;
6028
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006029 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006030 {
6031 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006032 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006033 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006034
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006035 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006036 return 0;
6037}
6038
Bram Moolenaardb913952012-06-29 12:54:53 +02006039typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6040
6041 static int
6042convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006043 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006044{
6045 PyObject *capsule;
6046 char hexBuf[sizeof(void *) * 2 + 3];
6047
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006048 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006049
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006050# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006051 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006052# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006053 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006054# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006055 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006056 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006057# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006058 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006059# else
6060 capsule = PyCObject_FromVoidPtr(tv, NULL);
6061# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006062 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6063 {
6064 Py_DECREF(capsule);
6065 tv->v_type = VAR_UNKNOWN;
6066 return -1;
6067 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006068
6069 Py_DECREF(capsule);
6070
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006071 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006072 {
6073 tv->v_type = VAR_UNKNOWN;
6074 return -1;
6075 }
6076 /* As we are not using copy_tv which increments reference count we must
6077 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006078 if (tv->v_type == VAR_DICT)
6079 ++tv->vval.v_dict->dv_refcount;
6080 else if (tv->v_type == VAR_LIST)
6081 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006082 }
6083 else
6084 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006085 typval_T *v;
6086
6087# ifdef PY_USE_CAPSULE
6088 v = PyCapsule_GetPointer(capsule, NULL);
6089# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006090 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006091# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006092 copy_tv(v, tv);
6093 }
6094 return 0;
6095}
6096
6097 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006098ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6099{
6100 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006101 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006102
6103 if (!(lookup_dict = PyDict_New()))
6104 return -1;
6105
6106 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6107 {
6108 tv->v_type = VAR_DICT;
6109 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6110 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006111 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006112 }
6113 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006114 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006115 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006116 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006117 else
6118 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006119 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006120 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006121 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006122 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006123 }
6124 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006125 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006126}
6127
6128 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006129ConvertFromPySequence(PyObject *obj, typval_T *tv)
6130{
6131 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006132 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006133
6134 if (!(lookup_dict = PyDict_New()))
6135 return -1;
6136
6137 if (PyType_IsSubtype(obj->ob_type, &ListType))
6138 {
6139 tv->v_type = VAR_LIST;
6140 tv->vval.v_list = (((ListObject *)(obj))->list);
6141 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006142 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006143 }
6144 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006145 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006146 else
6147 {
6148 PyErr_FORMAT(PyExc_TypeError,
6149 N_("unable to convert %s to vim list"),
6150 Py_TYPE_NAME(obj));
6151 ret = -1;
6152 }
6153 Py_DECREF(lookup_dict);
6154 return ret;
6155}
6156
6157 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006158ConvertFromPyObject(PyObject *obj, typval_T *tv)
6159{
6160 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006161 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006162
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006163 if (!(lookup_dict = PyDict_New()))
6164 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006165 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006166 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006167 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006168}
6169
6170 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006171_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006172{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006173 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006174 {
6175 tv->v_type = VAR_DICT;
6176 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6177 ++tv->vval.v_dict->dv_refcount;
6178 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006179 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006180 {
6181 tv->v_type = VAR_LIST;
6182 tv->vval.v_list = (((ListObject *)(obj))->list);
6183 ++tv->vval.v_list->lv_refcount;
6184 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006185 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006186 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006187 FunctionObject *func = (FunctionObject *) obj;
6188 if (func->self != NULL || func->argv != NULL)
6189 {
6190 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6191 set_partial(func, pt, TRUE);
6192 tv->vval.v_partial = pt;
6193 tv->v_type = VAR_PARTIAL;
6194 }
6195 else
6196 {
6197 if (set_string_copy(func->name, tv) == -1)
6198 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006199
Bram Moolenaar8110a092016-04-14 15:56:09 +02006200 tv->v_type = VAR_FUNC;
6201 }
6202 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006203 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006204 else if (PyBytes_Check(obj))
6205 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006206 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006207
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006208 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006209 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006210 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006211 return -1;
6212
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006213 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006214 return -1;
6215
6216 tv->v_type = VAR_STRING;
6217 }
6218 else if (PyUnicode_Check(obj))
6219 {
6220 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006221 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006222
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006223 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006224 if (bytes == NULL)
6225 return -1;
6226
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006227 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006228 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006229 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006230 return -1;
6231
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006232 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006233 {
6234 Py_XDECREF(bytes);
6235 return -1;
6236 }
6237 Py_XDECREF(bytes);
6238
6239 tv->v_type = VAR_STRING;
6240 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006241#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006242 else if (PyInt_Check(obj))
6243 {
6244 tv->v_type = VAR_NUMBER;
6245 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006246 if (PyErr_Occurred())
6247 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006248 }
6249#endif
6250 else if (PyLong_Check(obj))
6251 {
6252 tv->v_type = VAR_NUMBER;
6253 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006254 if (PyErr_Occurred())
6255 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 }
6257 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006258 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006259#ifdef FEAT_FLOAT
6260 else if (PyFloat_Check(obj))
6261 {
6262 tv->v_type = VAR_FLOAT;
6263 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6264 }
6265#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006266 else if (PyObject_HasAttrString(obj, "keys"))
6267 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006268 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006269 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006270 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006271 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006272 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006273 else if (PyNumber_Check(obj))
6274 {
6275 PyObject *num;
6276
6277 if (!(num = PyNumber_Long(obj)))
6278 return -1;
6279
6280 tv->v_type = VAR_NUMBER;
6281 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6282
6283 Py_DECREF(num);
6284 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006285 else if (obj == Py_None)
6286 {
6287 tv->v_type = VAR_SPECIAL;
6288 tv->vval.v_number = VVAL_NONE;
6289 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006290 else
6291 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006292 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006293 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006294 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006295 return -1;
6296 }
6297 return 0;
6298}
6299
6300 static PyObject *
6301ConvertToPyObject(typval_T *tv)
6302{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006303 typval_T *argv;
6304 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006305 if (tv == NULL)
6306 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006307 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006308 return NULL;
6309 }
6310 switch (tv->v_type)
6311 {
6312 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006313 return PyBytes_FromString(tv->vval.v_string == NULL
6314 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006315 case VAR_NUMBER:
6316 return PyLong_FromLong((long) tv->vval.v_number);
6317#ifdef FEAT_FLOAT
6318 case VAR_FLOAT:
6319 return PyFloat_FromDouble((double) tv->vval.v_float);
6320#endif
6321 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006322 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006323 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006324 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006325 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006326 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006327 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006328 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006329 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006330 if (tv->vval.v_partial->pt_argc)
6331 {
6332 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6333 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6334 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6335 }
6336 else
6337 argv = NULL;
6338 if (tv->vval.v_partial->pt_dict != NULL)
6339 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006340 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006341 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006342 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006343 tv->vval.v_partial->pt_dict,
6344 tv->vval.v_partial->pt_auto);
Bram Moolenaardb913952012-06-29 12:54:53 +02006345 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006346 case VAR_CHANNEL:
6347 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006348 Py_INCREF(Py_None);
6349 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006350 case VAR_SPECIAL:
6351 switch (tv->vval.v_number)
6352 {
6353 case VVAL_FALSE: return AlwaysFalse(NULL);
6354 case VVAL_TRUE: return AlwaysTrue(NULL);
6355 case VVAL_NONE:
6356 case VVAL_NULL: return AlwaysNone(NULL);
6357 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006358 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006359 return NULL;
6360 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006361 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006362}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006363
6364typedef struct
6365{
6366 PyObject_HEAD
6367} CurrentObject;
6368static PyTypeObject CurrentType;
6369
6370 static void
6371init_structs(void)
6372{
6373 vim_memset(&OutputType, 0, sizeof(OutputType));
6374 OutputType.tp_name = "vim.message";
6375 OutputType.tp_basicsize = sizeof(OutputObject);
6376 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6377 OutputType.tp_doc = "vim message object";
6378 OutputType.tp_methods = OutputMethods;
6379#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006380 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6381 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006382 OutputType.tp_alloc = call_PyType_GenericAlloc;
6383 OutputType.tp_new = call_PyType_GenericNew;
6384 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006385 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006386#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006387 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6388 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006389 // Disabled, because this causes a crash in test86
6390 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006391#endif
6392
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006393 vim_memset(&IterType, 0, sizeof(IterType));
6394 IterType.tp_name = "vim.iter";
6395 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006396 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006397 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006398 IterType.tp_iter = (getiterfunc)IterIter;
6399 IterType.tp_iternext = (iternextfunc)IterNext;
6400 IterType.tp_dealloc = (destructor)IterDestructor;
6401 IterType.tp_traverse = (traverseproc)IterTraverse;
6402 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006403
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006404 vim_memset(&BufferType, 0, sizeof(BufferType));
6405 BufferType.tp_name = "vim.buffer";
6406 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006407 BufferType.tp_dealloc = (destructor)BufferDestructor;
6408 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006409 BufferType.tp_as_sequence = &BufferAsSeq;
6410 BufferType.tp_as_mapping = &BufferAsMapping;
6411 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6412 BufferType.tp_doc = "vim buffer object";
6413 BufferType.tp_methods = BufferMethods;
6414#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006415 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006416 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006417 BufferType.tp_alloc = call_PyType_GenericAlloc;
6418 BufferType.tp_new = call_PyType_GenericNew;
6419 BufferType.tp_free = call_PyObject_Free;
6420#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006421 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006422 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006423#endif
6424
6425 vim_memset(&WindowType, 0, sizeof(WindowType));
6426 WindowType.tp_name = "vim.window";
6427 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006428 WindowType.tp_dealloc = (destructor)WindowDestructor;
6429 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006430 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006431 WindowType.tp_doc = "vim Window object";
6432 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006433 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6434 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006435#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006436 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6437 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006438 WindowType.tp_alloc = call_PyType_GenericAlloc;
6439 WindowType.tp_new = call_PyType_GenericNew;
6440 WindowType.tp_free = call_PyObject_Free;
6441#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006442 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6443 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006444#endif
6445
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006446 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6447 TabPageType.tp_name = "vim.tabpage";
6448 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006449 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6450 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006451 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6452 TabPageType.tp_doc = "vim tab page object";
6453 TabPageType.tp_methods = TabPageMethods;
6454#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006455 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006456 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6457 TabPageType.tp_new = call_PyType_GenericNew;
6458 TabPageType.tp_free = call_PyObject_Free;
6459#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006460 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006461#endif
6462
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006463 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6464 BufMapType.tp_name = "vim.bufferlist";
6465 BufMapType.tp_basicsize = sizeof(BufMapObject);
6466 BufMapType.tp_as_mapping = &BufMapAsMapping;
6467 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006468 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006469 BufferType.tp_doc = "vim buffer list";
6470
6471 vim_memset(&WinListType, 0, sizeof(WinListType));
6472 WinListType.tp_name = "vim.windowlist";
6473 WinListType.tp_basicsize = sizeof(WinListType);
6474 WinListType.tp_as_sequence = &WinListAsSeq;
6475 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6476 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006477 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006478
6479 vim_memset(&TabListType, 0, sizeof(TabListType));
6480 TabListType.tp_name = "vim.tabpagelist";
6481 TabListType.tp_basicsize = sizeof(TabListType);
6482 TabListType.tp_as_sequence = &TabListAsSeq;
6483 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6484 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006485
6486 vim_memset(&RangeType, 0, sizeof(RangeType));
6487 RangeType.tp_name = "vim.range";
6488 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 RangeType.tp_dealloc = (destructor)RangeDestructor;
6490 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491 RangeType.tp_as_sequence = &RangeAsSeq;
6492 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006493 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006494 RangeType.tp_doc = "vim Range object";
6495 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006496 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6497 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006498#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006500 RangeType.tp_alloc = call_PyType_GenericAlloc;
6501 RangeType.tp_new = call_PyType_GenericNew;
6502 RangeType.tp_free = call_PyObject_Free;
6503#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006504 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006505#endif
6506
6507 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6508 CurrentType.tp_name = "vim.currentdata";
6509 CurrentType.tp_basicsize = sizeof(CurrentObject);
6510 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6511 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006512 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006513#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006514 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6515 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006516#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006517 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6518 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006519#endif
6520
6521 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6522 DictionaryType.tp_name = "vim.dictionary";
6523 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006524 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006525 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006526 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006527 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006528 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6529 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006530 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6531 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6532 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006533#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006534 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6535 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006536#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006537 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6538 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006539#endif
6540
6541 vim_memset(&ListType, 0, sizeof(ListType));
6542 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006543 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006544 ListType.tp_basicsize = sizeof(ListObject);
6545 ListType.tp_as_sequence = &ListAsSeq;
6546 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006547 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006548 ListType.tp_doc = "list pushing modifications to vim structure";
6549 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006550 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006551 ListType.tp_new = (newfunc)ListConstructor;
6552 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006553#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006554 ListType.tp_getattro = (getattrofunc)ListGetattro;
6555 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006556#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006557 ListType.tp_getattr = (getattrfunc)ListGetattr;
6558 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006559#endif
6560
6561 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006562 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006563 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006564 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6565 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006566 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006567 FunctionType.tp_doc = "object that calls vim function";
6568 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006569 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006570 FunctionType.tp_new = (newfunc)FunctionConstructor;
6571 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006573 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006574#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006575 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006576#endif
6577
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006578 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6579 OptionsType.tp_name = "vim.options";
6580 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006581 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006582 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006583 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006584 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006585 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006586 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6587 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6588 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006589
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006590#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006591 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6592 LoaderType.tp_name = "vim.Loader";
6593 LoaderType.tp_basicsize = sizeof(LoaderObject);
6594 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6595 LoaderType.tp_doc = "vim message object";
6596 LoaderType.tp_methods = LoaderMethods;
6597 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006598#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006599
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600#if PY_MAJOR_VERSION >= 3
6601 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6602 vimmodule.m_name = "vim";
6603 vimmodule.m_doc = "Vim Python interface\n";
6604 vimmodule.m_size = -1;
6605 vimmodule.m_methods = VimMethods;
6606#endif
6607}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006608
6609#define PYTYPE_READY(type) \
6610 if (PyType_Ready(&type)) \
6611 return -1;
6612
6613 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006614init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006615{
6616 PYTYPE_READY(IterType);
6617 PYTYPE_READY(BufferType);
6618 PYTYPE_READY(RangeType);
6619 PYTYPE_READY(WindowType);
6620 PYTYPE_READY(TabPageType);
6621 PYTYPE_READY(BufMapType);
6622 PYTYPE_READY(WinListType);
6623 PYTYPE_READY(TabListType);
6624 PYTYPE_READY(CurrentType);
6625 PYTYPE_READY(DictionaryType);
6626 PYTYPE_READY(ListType);
6627 PYTYPE_READY(FunctionType);
6628 PYTYPE_READY(OptionsType);
6629 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006630#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006631 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006632#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006633 return 0;
6634}
6635
6636 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006637init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006638{
6639 PyObject *path;
6640 PyObject *path_hook;
6641 PyObject *path_hooks;
6642
6643 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6644 return -1;
6645
6646 if (!(path_hooks = PySys_GetObject("path_hooks")))
6647 {
6648 PyErr_Clear();
6649 path_hooks = PyList_New(1);
6650 PyList_SET_ITEM(path_hooks, 0, path_hook);
6651 if (PySys_SetObject("path_hooks", path_hooks))
6652 {
6653 Py_DECREF(path_hooks);
6654 return -1;
6655 }
6656 Py_DECREF(path_hooks);
6657 }
6658 else if (PyList_Check(path_hooks))
6659 {
6660 if (PyList_Append(path_hooks, path_hook))
6661 {
6662 Py_DECREF(path_hook);
6663 return -1;
6664 }
6665 Py_DECREF(path_hook);
6666 }
6667 else
6668 {
6669 VimTryStart();
6670 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6671 "You should now do the following:\n"
6672 "- append vim.path_hook to sys.path_hooks\n"
6673 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6674 VimTryEnd(); /* Discard the error */
6675 Py_DECREF(path_hook);
6676 return 0;
6677 }
6678
6679 if (!(path = PySys_GetObject("path")))
6680 {
6681 PyErr_Clear();
6682 path = PyList_New(1);
6683 Py_INCREF(vim_special_path_object);
6684 PyList_SET_ITEM(path, 0, vim_special_path_object);
6685 if (PySys_SetObject("path", path))
6686 {
6687 Py_DECREF(path);
6688 return -1;
6689 }
6690 Py_DECREF(path);
6691 }
6692 else if (PyList_Check(path))
6693 {
6694 if (PyList_Append(path, vim_special_path_object))
6695 return -1;
6696 }
6697 else
6698 {
6699 VimTryStart();
6700 EMSG(_("Failed to set path: sys.path is not a list\n"
6701 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6702 VimTryEnd(); /* Discard the error */
6703 }
6704
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006705 return 0;
6706}
6707
6708static BufMapObject TheBufferMap =
6709{
6710 PyObject_HEAD_INIT(&BufMapType)
6711};
6712
6713static WinListObject TheWindowList =
6714{
6715 PyObject_HEAD_INIT(&WinListType)
6716 NULL
6717};
6718
6719static CurrentObject TheCurrent =
6720{
6721 PyObject_HEAD_INIT(&CurrentType)
6722};
6723
6724static TabListObject TheTabPageList =
6725{
6726 PyObject_HEAD_INIT(&TabListType)
6727};
6728
6729static struct numeric_constant {
6730 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006731 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006732} numeric_constants[] = {
6733 {"VAR_LOCKED", VAR_LOCKED},
6734 {"VAR_FIXED", VAR_FIXED},
6735 {"VAR_SCOPE", VAR_SCOPE},
6736 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6737};
6738
6739static struct object_constant {
6740 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006741 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006742} object_constants[] = {
6743 {"buffers", (PyObject *)(void *)&TheBufferMap},
6744 {"windows", (PyObject *)(void *)&TheWindowList},
6745 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6746 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006747
6748 {"Buffer", (PyObject *)&BufferType},
6749 {"Range", (PyObject *)&RangeType},
6750 {"Window", (PyObject *)&WindowType},
6751 {"TabPage", (PyObject *)&TabPageType},
6752 {"Dictionary", (PyObject *)&DictionaryType},
6753 {"List", (PyObject *)&ListType},
6754 {"Function", (PyObject *)&FunctionType},
6755 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006756#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006757 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006758#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006759};
6760
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006761#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006762 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006763 return -1;
6764
6765#define ADD_CHECKED_OBJECT(m, name, obj) \
6766 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006767 PyObject *valObject = obj; \
6768 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006769 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006770 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006771 }
6772
6773 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006774populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006775{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006776 int i;
6777 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006778 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006779 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006780#if PY_VERSION_HEX >= 0x030700f0
6781 PyObject *dict;
6782 PyObject *cls;
6783#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006784
6785 for (i = 0; i < (int)(sizeof(numeric_constants)
6786 / sizeof(struct numeric_constant));
6787 ++i)
6788 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006789 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006790
6791 for (i = 0; i < (int)(sizeof(object_constants)
6792 / sizeof(struct object_constant));
6793 ++i)
6794 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006795 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006796
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006797 valObject = object_constants[i].valObject;
6798 Py_INCREF(valObject);
6799 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006800 }
6801
6802 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6803 return -1;
6804 ADD_OBJECT(m, "error", VimError);
6805
Bram Moolenaara9922d62013-05-30 13:01:18 +02006806 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6807 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006808 ADD_CHECKED_OBJECT(m, "options",
6809 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006810
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006811 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006812 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006813 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006814
Bram Moolenaar22081f42016-06-01 20:38:34 +02006815#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006816 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006817 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006818#else
6819 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6820 return -1;
6821#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006822 ADD_OBJECT(m, "_getcwd", py_getcwd)
6823
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006824 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006825 return -1;
6826 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006827 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006828 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006829 if (PyObject_SetAttrString(other_module, "chdir", attr))
6830 {
6831 Py_DECREF(attr);
6832 return -1;
6833 }
6834 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006835
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006836 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006837 {
6838 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006839 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006840 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006841 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6842 {
6843 Py_DECREF(attr);
6844 return -1;
6845 }
6846 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006847 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006848 else
6849 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006850
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006851 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6852 return -1;
6853
6854 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6855
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006856#if PY_VERSION_HEX >= 0x030700f0
6857 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6858 return -1;
6859
6860 dict = PyModule_GetDict(imp);
6861
6862 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6863 {
6864 Py_DECREF(imp);
6865 return -1;
6866 }
6867
6868 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6869 {
6870 Py_DECREF(imp);
6871 return -1;
6872 }
6873
6874 Py_DECREF(imp);
6875
6876 ADD_OBJECT(m, "_find_spec", py_find_spec);
6877#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006878 if (!(imp = PyImport_ImportModule("imp")))
6879 return -1;
6880
6881 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6882 {
6883 Py_DECREF(imp);
6884 return -1;
6885 }
6886
6887 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6888 {
6889 Py_DECREF(py_find_module);
6890 Py_DECREF(imp);
6891 return -1;
6892 }
6893
6894 Py_DECREF(imp);
6895
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006896 ADD_OBJECT(m, "_find_module", py_find_module);
6897 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006898#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006899
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006900 return 0;
6901}