blob: 459bc50cddc5cffe58b8b619db2508b1a3300043 [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 Moolenaar53901442018-07-25 22:02:36 +02003988 self->win->w_set_curswant = TRUE;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003989#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003990 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003991#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003992 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003993 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003994
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003995 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003996 return 0;
3997 }
3998 else if (strcmp(name, "height") == 0)
3999 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004000 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004001 win_T *savewin;
4002
Bram Moolenaardee2e312013-06-23 16:35:47 +02004003 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004004 return -1;
4005
4006#ifdef FEAT_GUI
4007 need_mouse_correct = TRUE;
4008#endif
4009 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004010 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004011
4012 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004013 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004014 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004015 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004016 return -1;
4017
4018 return 0;
4019 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004020 else if (strcmp(name, "width") == 0)
4021 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004022 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004023 win_T *savewin;
4024
Bram Moolenaardee2e312013-06-23 16:35:47 +02004025 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026 return -1;
4027
4028#ifdef FEAT_GUI
4029 need_mouse_correct = TRUE;
4030#endif
4031 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004032 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004033
4034 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004035 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004037 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 return -1;
4039
4040 return 0;
4041 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042 else
4043 {
4044 PyErr_SetString(PyExc_AttributeError, name);
4045 return -1;
4046 }
4047}
4048
4049 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004050WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004052 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004053 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 else
4055 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004056 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057
Bram Moolenaar6d216452013-05-12 19:00:41 +02004058 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004059 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004060 (self));
4061 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004062 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004063 }
4064}
4065
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004066static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004067 /* name, function, calling, documentation */
4068 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4069 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004070};
4071
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004073 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004075
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004076static PyTypeObject WinListType;
4077static PySequenceMethods WinListAsSeq;
4078
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004079typedef struct
4080{
4081 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004082 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004083} WinListObject;
4084
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004085 static PyObject *
4086WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004088 WinListObject *self;
4089
4090 self = PyObject_NEW(WinListObject, &WinListType);
4091 self->tabObject = tabObject;
4092 Py_INCREF(tabObject);
4093
4094 return (PyObject *)(self);
4095}
4096
4097 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004098WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004099{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004100 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004101
4102 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004103 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004104 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004105 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004106
4107 DESTRUCTOR_FINISH(self);
4108}
4109
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004110 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004111WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004112{
4113 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 PyInt n = 0;
4115
Bram Moolenaard6e39182013-05-21 18:30:34 +02004116 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004117 return -1;
4118
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 while (w != NULL)
4120 {
4121 ++n;
4122 w = W_NEXT(w);
4123 }
4124
4125 return n;
4126}
4127
4128 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004129WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130{
4131 win_T *w;
4132
Bram Moolenaard6e39182013-05-21 18:30:34 +02004133 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004134 return NULL;
4135
4136 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004138 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004140 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004141 return NULL;
4142}
4143
4144/* Convert a Python string into a Vim line.
4145 *
4146 * The result is in allocated memory. All internal nulls are replaced by
4147 * newline characters. It is an error for the string to contain newline
4148 * characters.
4149 *
4150 * On errors, the Python exception data is set, and NULL is returned.
4151 */
4152 static char *
4153StringToLine(PyObject *obj)
4154{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004155 char *str;
4156 char *save;
4157 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004158 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004159 PyInt i;
4160 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004161
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004162 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004163 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004164 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4165 || str == NULL)
4166 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004168 else if (PyUnicode_Check(obj))
4169 {
4170 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4171 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004172
Bram Moolenaardaa27022013-06-24 22:33:30 +02004173 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004174 || str == NULL)
4175 {
4176 Py_DECREF(bytes);
4177 return NULL;
4178 }
4179 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004180 else
4181 {
4182#if PY_MAJOR_VERSION < 3
4183 PyErr_FORMAT(PyExc_TypeError,
4184 N_("expected str() or unicode() instance, but got %s"),
4185 Py_TYPE_NAME(obj));
4186#else
4187 PyErr_FORMAT(PyExc_TypeError,
4188 N_("expected bytes() or str() instance, but got %s"),
4189 Py_TYPE_NAME(obj));
4190#endif
4191 return NULL;
4192 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004193
4194 /*
4195 * Error checking: String must not contain newlines, as we
4196 * are replacing a single line, and we must replace it with
4197 * a single line.
4198 * A trailing newline is removed, so that append(f.readlines()) works.
4199 */
4200 p = memchr(str, '\n', len);
4201 if (p != NULL)
4202 {
4203 if (p == str + len - 1)
4204 --len;
4205 else
4206 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004207 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004208 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004209 return NULL;
4210 }
4211 }
4212
4213 /* Create a copy of the string, with internal nulls replaced by
4214 * newline characters, as is the vim convention.
4215 */
4216 save = (char *)alloc((unsigned)(len+1));
4217 if (save == NULL)
4218 {
4219 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004220 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004221 return NULL;
4222 }
4223
4224 for (i = 0; i < len; ++i)
4225 {
4226 if (str[i] == '\0')
4227 save[i] = '\n';
4228 else
4229 save[i] = str[i];
4230 }
4231
4232 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004233 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004234
4235 return save;
4236}
4237
4238/* Get a line from the specified buffer. The line number is
4239 * in Vim format (1-based). The line is returned as a Python
4240 * string object.
4241 */
4242 static PyObject *
4243GetBufferLine(buf_T *buf, PyInt n)
4244{
4245 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4246}
4247
4248
4249/* Get a list of lines from the specified buffer. The line numbers
4250 * are in Vim format (1-based). The range is from lo up to, but not
4251 * including, hi. The list is returned as a Python list of string objects.
4252 */
4253 static PyObject *
4254GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4255{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004256 PyInt i;
4257 PyInt n = hi - lo;
4258 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004259
4260 if (list == NULL)
4261 return NULL;
4262
4263 for (i = 0; i < n; ++i)
4264 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004265 PyObject *string = LineToString(
4266 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004267
4268 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004269 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270 {
4271 Py_DECREF(list);
4272 return NULL;
4273 }
4274
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004275 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004276 }
4277
4278 /* The ownership of the Python list is passed to the caller (ie,
4279 * the caller should Py_DECREF() the object when it is finished
4280 * with it).
4281 */
4282
4283 return list;
4284}
4285
4286/*
4287 * Check if deleting lines made the cursor position invalid.
4288 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4289 * deleted).
4290 */
4291 static void
4292py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4293{
4294 if (curwin->w_cursor.lnum >= lo)
4295 {
4296 /* Adjust the cursor position if it's in/after the changed
4297 * lines. */
4298 if (curwin->w_cursor.lnum >= hi)
4299 {
4300 curwin->w_cursor.lnum += extra;
4301 check_cursor_col();
4302 }
4303 else if (extra < 0)
4304 {
4305 curwin->w_cursor.lnum = lo;
4306 check_cursor();
4307 }
4308 else
4309 check_cursor_col();
4310 changed_cline_bef_curs();
4311 }
4312 invalidate_botline();
4313}
4314
Bram Moolenaar19e60942011-06-19 00:27:51 +02004315/*
4316 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004317 * in Vim format (1-based). The replacement line is given as
4318 * a Python string object. The object is checked for validity
4319 * and correct format. Errors are returned as a value of FAIL.
4320 * The return value is OK on success.
4321 * If OK is returned and len_change is not NULL, *len_change
4322 * is set to the change in the buffer length.
4323 */
4324 static int
4325SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4326{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004327 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004328 win_T *save_curwin = NULL;
4329 tabpage_T *save_curtab = NULL;
4330
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004331 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004332 * There are three cases:
4333 * 1. NULL, or None - this is a deletion.
4334 * 2. A string - this is a replacement.
4335 * 3. Anything else - this is an error.
4336 */
4337 if (line == Py_None || line == NULL)
4338 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004339 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004340 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004341
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004342 VimTryStart();
4343
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004344 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004345 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004346 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004347 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004348 else
4349 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004350 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004351 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004352 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004353 /* Only adjust marks if we managed to switch to a window that
4354 * holds the buffer, otherwise line numbers will be invalid. */
4355 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004356 }
4357
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004358 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004359
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004360 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004361 return FAIL;
4362
4363 if (len_change)
4364 *len_change = -1;
4365
4366 return OK;
4367 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004368 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004369 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004370 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004371
4372 if (save == NULL)
4373 return FAIL;
4374
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004375 VimTryStart();
4376
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004377 /* We do not need to free "save" if ml_replace() consumes it. */
4378 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004379 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004380
4381 if (u_savesub((linenr_T)n) == FAIL)
4382 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004383 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004384 vim_free(save);
4385 }
4386 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4387 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004388 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004389 vim_free(save);
4390 }
4391 else
4392 changed_bytes((linenr_T)n, 0);
4393
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004394 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004395
4396 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004397 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004398 check_cursor_col();
4399
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004400 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 return FAIL;
4402
4403 if (len_change)
4404 *len_change = 0;
4405
4406 return OK;
4407 }
4408 else
4409 {
4410 PyErr_BadArgument();
4411 return FAIL;
4412 }
4413}
4414
Bram Moolenaar19e60942011-06-19 00:27:51 +02004415/* Replace a range of lines in the specified buffer. The line numbers are in
4416 * Vim format (1-based). The range is from lo up to, but not including, hi.
4417 * The replacement lines are given as a Python list of string objects. The
4418 * list is checked for validity and correct format. Errors are returned as a
4419 * value of FAIL. The return value is OK on success.
4420 * If OK is returned and len_change is not NULL, *len_change
4421 * is set to the change in the buffer length.
4422 */
4423 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004424SetBufferLineList(
4425 buf_T *buf,
4426 PyInt lo,
4427 PyInt hi,
4428 PyObject *list,
4429 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004430{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004431 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004432 win_T *save_curwin = NULL;
4433 tabpage_T *save_curtab = NULL;
4434
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004435 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004436 * There are three cases:
4437 * 1. NULL, or None - this is a deletion.
4438 * 2. A list - this is a replacement.
4439 * 3. Anything else - this is an error.
4440 */
4441 if (list == Py_None || list == NULL)
4442 {
4443 PyInt i;
4444 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004445
4446 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004447 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004448 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004449
4450 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004451 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004452 else
4453 {
4454 for (i = 0; i < n; ++i)
4455 {
4456 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4457 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004458 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004459 break;
4460 }
4461 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004462 if (buf == curbuf && (save_curwin != NULL
4463 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004464 /* Using an existing window for the buffer, adjust the cursor
4465 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004466 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004467 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004468 /* Only adjust marks if we managed to switch to a window that
4469 * holds the buffer, otherwise line numbers will be invalid. */
4470 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004471 }
4472
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004473 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004474
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004475 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004476 return FAIL;
4477
4478 if (len_change)
4479 *len_change = -n;
4480
4481 return OK;
4482 }
4483 else if (PyList_Check(list))
4484 {
4485 PyInt i;
4486 PyInt new_len = PyList_Size(list);
4487 PyInt old_len = hi - lo;
4488 PyInt extra = 0; /* lines added to text, can be negative */
4489 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004490
4491 if (new_len == 0) /* avoid allocating zero bytes */
4492 array = NULL;
4493 else
4494 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004495 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004496 if (array == NULL)
4497 {
4498 PyErr_NoMemory();
4499 return FAIL;
4500 }
4501 }
4502
4503 for (i = 0; i < new_len; ++i)
4504 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004505 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004506
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004507 if (!(line = PyList_GetItem(list, i)) ||
4508 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004509 {
4510 while (i)
4511 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004512 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004513 return FAIL;
4514 }
4515 }
4516
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004517 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004518 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004519
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004520 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004521 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004522
4523 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004524 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004525
4526 /* If the size of the range is reducing (ie, new_len < old_len) we
4527 * need to delete some old_len. We do this at the start, by
4528 * repeatedly deleting line "lo".
4529 */
4530 if (!PyErr_Occurred())
4531 {
4532 for (i = 0; i < old_len - new_len; ++i)
4533 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4534 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004535 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004536 break;
4537 }
4538 extra -= i;
4539 }
4540
4541 /* For as long as possible, replace the existing old_len with the
4542 * new old_len. This is a more efficient operation, as it requires
4543 * less memory allocation and freeing.
4544 */
4545 if (!PyErr_Occurred())
4546 {
4547 for (i = 0; i < old_len && i < new_len; ++i)
4548 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4549 == FAIL)
4550 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004551 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004552 break;
4553 }
4554 }
4555 else
4556 i = 0;
4557
4558 /* Now we may need to insert the remaining new old_len. If we do, we
4559 * must free the strings as we finish with them (we can't pass the
4560 * responsibility to vim in this case).
4561 */
4562 if (!PyErr_Occurred())
4563 {
4564 while (i < new_len)
4565 {
4566 if (ml_append((linenr_T)(lo + i - 1),
4567 (char_u *)array[i], 0, FALSE) == FAIL)
4568 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004569 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004570 break;
4571 }
4572 vim_free(array[i]);
4573 ++i;
4574 ++extra;
4575 }
4576 }
4577
4578 /* Free any left-over old_len, as a result of an error */
4579 while (i < new_len)
4580 {
4581 vim_free(array[i]);
4582 ++i;
4583 }
4584
4585 /* Free the array of old_len. All of its contents have now
4586 * been dealt with (either freed, or the responsibility passed
4587 * to vim.
4588 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004589 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004590
4591 /* Adjust marks. Invalidate any which lie in the
4592 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004593 * Only adjust marks if we managed to switch to a window that holds
4594 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004595 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004596 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597 (long)MAXLNUM, (long)extra);
4598 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4599
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004600 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004601 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4602
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004603 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004604 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004605
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004606 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004607 return FAIL;
4608
4609 if (len_change)
4610 *len_change = new_len - old_len;
4611
4612 return OK;
4613 }
4614 else
4615 {
4616 PyErr_BadArgument();
4617 return FAIL;
4618 }
4619}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004620
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004621/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004622 * The line number is in Vim format (1-based). The lines to be inserted are
4623 * given as a Python list of string objects or as a single string. The lines
4624 * to be added are checked for validity and correct format. Errors are
4625 * returned as a value of FAIL. The return value is OK on success.
4626 * If OK is returned and len_change is not NULL, *len_change
4627 * is set to the change in the buffer length.
4628 */
4629 static int
4630InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4631{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004632 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004633 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004634 tabpage_T *save_curtab = NULL;
4635
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004636 /* First of all, we check the type of the supplied Python object.
4637 * It must be a string or a list, or the call is in error.
4638 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004639 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004640 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004641 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004642
4643 if (str == NULL)
4644 return FAIL;
4645
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004646 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004647 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004648 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004649
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004650 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004651 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004653 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004654 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004655 /* Only adjust marks if we managed to switch to a window that
4656 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004657 appended_lines_mark((linenr_T)n, 1L);
4658
4659 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004660 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004661 update_screen(VALID);
4662
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004663 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004664 return FAIL;
4665
4666 if (len_change)
4667 *len_change = 1;
4668
4669 return OK;
4670 }
4671 else if (PyList_Check(lines))
4672 {
4673 PyInt i;
4674 PyInt size = PyList_Size(lines);
4675 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004676
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004677 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004678 if (array == NULL)
4679 {
4680 PyErr_NoMemory();
4681 return FAIL;
4682 }
4683
4684 for (i = 0; i < size; ++i)
4685 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004686 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004687
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004688 if (!(line = PyList_GetItem(lines, i)) ||
4689 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004690 {
4691 while (i)
4692 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004693 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694 return FAIL;
4695 }
4696 }
4697
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004698 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004699 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004700 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004701
4702 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004703 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004704 else
4705 {
4706 for (i = 0; i < size; ++i)
4707 {
4708 if (ml_append((linenr_T)(n + i),
4709 (char_u *)array[i], 0, FALSE) == FAIL)
4710 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004711 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712
4713 /* Free the rest of the lines */
4714 while (i < size)
4715 vim_free(array[i++]);
4716
4717 break;
4718 }
4719 vim_free(array[i]);
4720 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004721 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004722 /* Only adjust marks if we managed to switch to a window that
4723 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004724 appended_lines_mark((linenr_T)n, (long)i);
4725 }
4726
4727 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004728 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004729 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004730 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732 update_screen(VALID);
4733
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004734 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735 return FAIL;
4736
4737 if (len_change)
4738 *len_change = size;
4739
4740 return OK;
4741 }
4742 else
4743 {
4744 PyErr_BadArgument();
4745 return FAIL;
4746 }
4747}
4748
4749/*
4750 * Common routines for buffers and line ranges
4751 * -------------------------------------------
4752 */
4753
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004754typedef struct
4755{
4756 PyObject_HEAD
4757 buf_T *buf;
4758} BufferObject;
4759
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004760 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004761CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004762{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004763 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004764 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004765 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004766 return -1;
4767 }
4768
4769 return 0;
4770}
4771
4772 static PyObject *
4773RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4774{
4775 if (CheckBuffer(self))
4776 return NULL;
4777
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004778 if (end == -1)
4779 end = self->buf->b_ml.ml_line_count;
4780
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004781 if (n < 0)
4782 n += end - start + 1;
4783
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004784 if (n < 0 || n > end - start)
4785 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004786 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004787 return NULL;
4788 }
4789
4790 return GetBufferLine(self->buf, n+start);
4791}
4792
4793 static PyObject *
4794RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4795{
4796 PyInt size;
4797
4798 if (CheckBuffer(self))
4799 return NULL;
4800
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004801 if (end == -1)
4802 end = self->buf->b_ml.ml_line_count;
4803
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004804 size = end - start + 1;
4805
4806 if (lo < 0)
4807 lo = 0;
4808 else if (lo > size)
4809 lo = size;
4810 if (hi < 0)
4811 hi = 0;
4812 if (hi < lo)
4813 hi = lo;
4814 else if (hi > size)
4815 hi = size;
4816
4817 return GetBufferLineList(self->buf, lo+start, hi+start);
4818}
4819
4820 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004821RBAsItem(
4822 BufferObject *self,
4823 PyInt n,
4824 PyObject *valObject,
4825 PyInt start,
4826 PyInt end,
4827 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004828{
4829 PyInt len_change;
4830
4831 if (CheckBuffer(self))
4832 return -1;
4833
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004834 if (end == -1)
4835 end = self->buf->b_ml.ml_line_count;
4836
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004837 if (n < 0)
4838 n += end - start + 1;
4839
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004840 if (n < 0 || n > end - start)
4841 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004842 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004843 return -1;
4844 }
4845
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004846 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004847 return -1;
4848
4849 if (new_end)
4850 *new_end = end + len_change;
4851
4852 return 0;
4853}
4854
Bram Moolenaar19e60942011-06-19 00:27:51 +02004855 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004856RBAsSlice(
4857 BufferObject *self,
4858 PyInt lo,
4859 PyInt hi,
4860 PyObject *valObject,
4861 PyInt start,
4862 PyInt end,
4863 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004864{
4865 PyInt size;
4866 PyInt len_change;
4867
4868 /* Self must be a valid buffer */
4869 if (CheckBuffer(self))
4870 return -1;
4871
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004872 if (end == -1)
4873 end = self->buf->b_ml.ml_line_count;
4874
Bram Moolenaar19e60942011-06-19 00:27:51 +02004875 /* Sort out the slice range */
4876 size = end - start + 1;
4877
4878 if (lo < 0)
4879 lo = 0;
4880 else if (lo > size)
4881 lo = size;
4882 if (hi < 0)
4883 hi = 0;
4884 if (hi < lo)
4885 hi = lo;
4886 else if (hi > size)
4887 hi = size;
4888
4889 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004890 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004891 return -1;
4892
4893 if (new_end)
4894 *new_end = end + len_change;
4895
4896 return 0;
4897}
4898
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004899
4900 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004901RBAppend(
4902 BufferObject *self,
4903 PyObject *args,
4904 PyInt start,
4905 PyInt end,
4906 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004907{
4908 PyObject *lines;
4909 PyInt len_change;
4910 PyInt max;
4911 PyInt n;
4912
4913 if (CheckBuffer(self))
4914 return NULL;
4915
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004916 if (end == -1)
4917 end = self->buf->b_ml.ml_line_count;
4918
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004919 max = n = end - start + 1;
4920
4921 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4922 return NULL;
4923
4924 if (n < 0 || n > max)
4925 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004926 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004927 return NULL;
4928 }
4929
4930 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4931 return NULL;
4932
4933 if (new_end)
4934 *new_end = end + len_change;
4935
4936 Py_INCREF(Py_None);
4937 return Py_None;
4938}
4939
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004940/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004941 */
4942
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004943static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004944static PySequenceMethods RangeAsSeq;
4945static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004946
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004947typedef struct
4948{
4949 PyObject_HEAD
4950 BufferObject *buf;
4951 PyInt start;
4952 PyInt end;
4953} RangeObject;
4954
4955 static PyObject *
4956RangeNew(buf_T *buf, PyInt start, PyInt end)
4957{
4958 BufferObject *bufr;
4959 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004960 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004961 if (self == NULL)
4962 return NULL;
4963
4964 bufr = (BufferObject *)BufferNew(buf);
4965 if (bufr == NULL)
4966 {
4967 Py_DECREF(self);
4968 return NULL;
4969 }
4970 Py_INCREF(bufr);
4971
4972 self->buf = bufr;
4973 self->start = start;
4974 self->end = end;
4975
4976 return (PyObject *)(self);
4977}
4978
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004979 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004980RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004981{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004982 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004983 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004984 PyObject_GC_Del((void *)(self));
4985}
4986
4987 static int
4988RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4989{
4990 Py_VISIT(((PyObject *)(self->buf)));
4991 return 0;
4992}
4993
4994 static int
4995RangeClear(RangeObject *self)
4996{
4997 Py_CLEAR(self->buf);
4998 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004999}
5000
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005001 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005002RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005003{
5004 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005005 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005006 return -1; /* ??? */
5007
Bram Moolenaard6e39182013-05-21 18:30:34 +02005008 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005009}
5010
5011 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005012RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005013{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005014 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005015}
5016
5017 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005018RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005019{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005020 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005021}
5022
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005023static char *RangeAttrs[] = {
5024 "start", "end",
5025 NULL
5026};
5027
5028 static PyObject *
5029RangeDir(PyObject *self)
5030{
5031 return ObjectDir(self, RangeAttrs);
5032}
5033
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005034 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005035RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005036{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005037 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005038}
5039
5040 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005041RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005042{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005043 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005044 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5045 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005046 else
5047 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005048 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005049
5050 if (name == NULL)
5051 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005052
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005053 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005054 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005055 }
5056}
5057
5058static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005059 /* name, function, calling, documentation */
5060 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005061 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5062 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005063};
5064
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005065static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005066static PySequenceMethods BufferAsSeq;
5067static PyMappingMethods BufferAsMapping;
5068
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005069 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005070BufferNew(buf_T *buf)
5071{
5072 /* We need to handle deletion of buffers underneath us.
5073 * If we add a "b_python*_ref" field to the buf_T structure,
5074 * then we can get at it in buf_freeall() in vim. We then
5075 * need to create only ONE Python object per buffer - if
5076 * we try to create a second, just INCREF the existing one
5077 * and return it. The (single) Python object referring to
5078 * the buffer is stored in "b_python*_ref".
5079 * Question: what to do on a buf_freeall(). We'll probably
5080 * have to either delete the Python object (DECREF it to
5081 * zero - a bad idea, as it leaves dangling refs!) or
5082 * set the buf_T * value to an invalid value (-1?), which
5083 * means we need checks in all access functions... Bah.
5084 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005085 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005086 * b_python_ref and b_python3_ref fields respectively.
5087 */
5088
5089 BufferObject *self;
5090
5091 if (BUF_PYTHON_REF(buf) != NULL)
5092 {
5093 self = BUF_PYTHON_REF(buf);
5094 Py_INCREF(self);
5095 }
5096 else
5097 {
5098 self = PyObject_NEW(BufferObject, &BufferType);
5099 if (self == NULL)
5100 return NULL;
5101 self->buf = buf;
5102 BUF_PYTHON_REF(buf) = self;
5103 }
5104
5105 return (PyObject *)(self);
5106}
5107
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005108 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005109BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005110{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005111 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5112 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005113
5114 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005115}
5116
Bram Moolenaar971db462013-05-12 18:44:48 +02005117 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005118BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005119{
5120 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005122 return -1; /* ??? */
5123
Bram Moolenaard6e39182013-05-21 18:30:34 +02005124 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005125}
5126
5127 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005129{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005130 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005131}
5132
5133 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005134BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005135{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005136 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005137}
5138
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005139static char *BufferAttrs[] = {
5140 "name", "number", "vars", "options", "valid",
5141 NULL
5142};
5143
5144 static PyObject *
5145BufferDir(PyObject *self)
5146{
5147 return ObjectDir(self, BufferAttrs);
5148}
5149
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005150 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005151BufferAttrValid(BufferObject *self, char *name)
5152{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005153 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005154
5155 if (strcmp(name, "valid") != 0)
5156 return NULL;
5157
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005158 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5159 Py_INCREF(ret);
5160 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005161}
5162
5163 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005164BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005165{
5166 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005167 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005168 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005170 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005171 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005172 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005173 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005174 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5175 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005176 else if (strcmp(name, "__members__") == 0)
5177 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005178 else
5179 return NULL;
5180}
5181
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005182 static int
5183BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5184{
5185 if (CheckBuffer(self))
5186 return -1;
5187
5188 if (strcmp(name, "name") == 0)
5189 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005190 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005191 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005192 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005193 PyObject *todecref;
5194
5195 if (!(val = StringToChars(valObject, &todecref)))
5196 return -1;
5197
5198 VimTryStart();
5199 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5200 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005201 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005202 aucmd_restbuf(&aco);
5203 Py_XDECREF(todecref);
5204 if (VimTryEnd())
5205 return -1;
5206
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005207 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005208 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005209 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005210 return -1;
5211 }
5212 return 0;
5213 }
5214 else
5215 {
5216 PyErr_SetString(PyExc_AttributeError, name);
5217 return -1;
5218 }
5219}
5220
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005221 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005222BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005223{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005224 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005225}
5226
5227 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005228BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005229{
5230 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005231 char_u *pmark;
5232 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005233 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005234 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005235
Bram Moolenaard6e39182013-05-21 18:30:34 +02005236 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005237 return NULL;
5238
Bram Moolenaar389a1792013-06-23 13:00:44 +02005239 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005240 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005241
Bram Moolenaar389a1792013-06-23 13:00:44 +02005242 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005243 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005244 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005245 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005246 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005247 return NULL;
5248 }
5249
5250 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005251
5252 Py_XDECREF(todecref);
5253
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005254 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005255 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005256 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005257 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005258 if (VimTryEnd())
5259 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005260
5261 if (posp == NULL)
5262 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005263 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005264 return NULL;
5265 }
5266
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005267 if (posp->lnum <= 0)
5268 {
5269 /* Or raise an error? */
5270 Py_INCREF(Py_None);
5271 return Py_None;
5272 }
5273
5274 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5275}
5276
5277 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005278BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005279{
5280 PyInt start;
5281 PyInt end;
5282
Bram Moolenaard6e39182013-05-21 18:30:34 +02005283 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005284 return NULL;
5285
5286 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5287 return NULL;
5288
Bram Moolenaard6e39182013-05-21 18:30:34 +02005289 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005290}
5291
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005292 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005293BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005294{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005295 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005296 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005297 else
5298 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005299 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005300
5301 if (name == NULL)
5302 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005303
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005304 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005305 }
5306}
5307
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005308static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005309 /* name, function, calling, documentation */
5310 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005311 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005312 {"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 +02005313 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5314 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315};
5316
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005317/*
5318 * Buffer list object - Implementation
5319 */
5320
5321static PyTypeObject BufMapType;
5322
5323typedef struct
5324{
5325 PyObject_HEAD
5326} BufMapObject;
5327
5328 static PyInt
5329BufMapLength(PyObject *self UNUSED)
5330{
5331 buf_T *b = firstbuf;
5332 PyInt n = 0;
5333
5334 while (b)
5335 {
5336 ++n;
5337 b = b->b_next;
5338 }
5339
5340 return n;
5341}
5342
5343 static PyObject *
5344BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5345{
5346 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005347 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005348
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005349 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005350 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005351
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005352 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005353
5354 if (b)
5355 return BufferNew(b);
5356 else
5357 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005358 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005359 return NULL;
5360 }
5361}
5362
5363 static void
5364BufMapIterDestruct(PyObject *buffer)
5365{
5366 /* Iteration was stopped before all buffers were processed */
5367 if (buffer)
5368 {
5369 Py_DECREF(buffer);
5370 }
5371}
5372
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005373 static int
5374BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5375{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005376 if (buffer)
5377 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005378 return 0;
5379}
5380
5381 static int
5382BufMapIterClear(PyObject **buffer)
5383{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005384 if (*buffer)
5385 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005386 return 0;
5387}
5388
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005389 static PyObject *
5390BufMapIterNext(PyObject **buffer)
5391{
5392 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005393 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005394
5395 if (!*buffer)
5396 return NULL;
5397
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005398 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005399
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005400 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005401 {
5402 *buffer = NULL;
5403 return NULL;
5404 }
5405
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005406 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005407 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005408 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005409 return NULL;
5410 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005411 /* Do not increment reference: we no longer hold it (decref), but whoever
5412 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005413 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005414}
5415
5416 static PyObject *
5417BufMapIter(PyObject *self UNUSED)
5418{
5419 PyObject *buffer;
5420
5421 buffer = BufferNew(firstbuf);
5422 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005423 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5424 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005425}
5426
5427static PyMappingMethods BufMapAsMapping = {
5428 (lenfunc) BufMapLength,
5429 (binaryfunc) BufMapItem,
5430 (objobjargproc) 0,
5431};
5432
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005433/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005434 */
5435
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005436static char *CurrentAttrs[] = {
5437 "buffer", "window", "line", "range", "tabpage",
5438 NULL
5439};
5440
5441 static PyObject *
5442CurrentDir(PyObject *self)
5443{
5444 return ObjectDir(self, CurrentAttrs);
5445}
5446
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005447 static PyObject *
5448CurrentGetattr(PyObject *self UNUSED, char *name)
5449{
5450 if (strcmp(name, "buffer") == 0)
5451 return (PyObject *)BufferNew(curbuf);
5452 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005453 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005454 else if (strcmp(name, "tabpage") == 0)
5455 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005456 else if (strcmp(name, "line") == 0)
5457 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5458 else if (strcmp(name, "range") == 0)
5459 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005460 else if (strcmp(name, "__members__") == 0)
5461 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005462 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005463#if PY_MAJOR_VERSION < 3
5464 return Py_FindMethod(WindowMethods, self, name);
5465#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005466 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005467#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005468}
5469
5470 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005471CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005472{
5473 if (strcmp(name, "line") == 0)
5474 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005475 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5476 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005477 return -1;
5478
5479 return 0;
5480 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005481 else if (strcmp(name, "buffer") == 0)
5482 {
5483 int count;
5484
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005485 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005486 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005487 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005488 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005489 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005490 return -1;
5491 }
5492
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005493 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005494 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005495 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005496
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005497 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005498 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5499 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005500 if (VimTryEnd())
5501 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005502 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005503 return -1;
5504 }
5505
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005506 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005507 }
5508 else if (strcmp(name, "window") == 0)
5509 {
5510 int count;
5511
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005512 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005513 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005514 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005515 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005516 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005517 return -1;
5518 }
5519
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005520 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005521 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005522 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005523
5524 if (!count)
5525 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005526 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005527 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005528 return -1;
5529 }
5530
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005531 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005532 win_goto(((WindowObject *)(valObject))->win);
5533 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005534 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005535 if (VimTryEnd())
5536 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005537 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005538 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005539 return -1;
5540 }
5541
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005542 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005543 }
5544 else if (strcmp(name, "tabpage") == 0)
5545 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005546 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005547 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005548 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005549 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005550 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005551 return -1;
5552 }
5553
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005554 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005555 return -1;
5556
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005557 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005558 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5559 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005560 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005561 if (VimTryEnd())
5562 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005563 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005564 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005565 return -1;
5566 }
5567
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005568 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005569 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005570 else
5571 {
5572 PyErr_SetString(PyExc_AttributeError, name);
5573 return -1;
5574 }
5575}
5576
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005577static struct PyMethodDef CurrentMethods[] = {
5578 /* name, function, calling, documentation */
5579 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5580 { NULL, NULL, 0, NULL}
5581};
5582
Bram Moolenaardb913952012-06-29 12:54:53 +02005583 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005584init_range_cmd(exarg_T *eap)
5585{
5586 RangeStart = eap->line1;
5587 RangeEnd = eap->line2;
5588}
5589
5590 static void
5591init_range_eval(typval_T *rettv UNUSED)
5592{
5593 RangeStart = (PyInt) curwin->w_cursor.lnum;
5594 RangeEnd = RangeStart;
5595}
5596
5597 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005598run_cmd(const char *cmd, void *arg UNUSED
5599#ifdef PY_CAN_RECURSE
5600 , PyGILState_STATE *pygilstate UNUSED
5601#endif
5602 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005603{
Bram Moolenaar41009372013-07-01 22:03:04 +02005604 PyObject *run_ret;
5605 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5606 if (run_ret != NULL)
5607 {
5608 Py_DECREF(run_ret);
5609 }
5610 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5611 {
5612 EMSG2(_(e_py_systemexit), "python");
5613 PyErr_Clear();
5614 }
5615 else
5616 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005617}
5618
5619static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5620static int code_hdr_len = 30;
5621
5622 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005623run_do(const char *cmd, void *arg UNUSED
5624#ifdef PY_CAN_RECURSE
5625 , PyGILState_STATE *pygilstate
5626#endif
5627 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005628{
5629 PyInt lnum;
5630 size_t len;
5631 char *code;
5632 int status;
5633 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005634 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005635 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005636
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005637 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005638 {
5639 EMSG(_("cannot save undo information"));
5640 return;
5641 }
5642
5643 len = code_hdr_len + STRLEN(cmd);
5644 code = PyMem_New(char, len + 1);
5645 memcpy(code, code_hdr, code_hdr_len);
5646 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005647 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5648 status = -1;
5649 if (run_ret != NULL)
5650 {
5651 status = 0;
5652 Py_DECREF(run_ret);
5653 }
5654 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5655 {
5656 PyMem_Free(code);
5657 EMSG2(_(e_py_systemexit), "python");
5658 PyErr_Clear();
5659 return;
5660 }
5661 else
5662 PyErr_PrintEx(1);
5663
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005664 PyMem_Free(code);
5665
5666 if (status)
5667 {
5668 EMSG(_("failed to run the code"));
5669 return;
5670 }
5671
5672 status = 0;
5673 pymain = PyImport_AddModule("__main__");
5674 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005675#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005676 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005677#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005678
5679 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5680 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005681 PyObject *line;
5682 PyObject *linenr;
5683 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005684
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005685#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005686 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005687#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005688 /* Check the line number, the command my have deleted lines. */
5689 if (lnum > curbuf->b_ml.ml_line_count
5690 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691 goto err;
5692 if (!(linenr = PyInt_FromLong((long) lnum)))
5693 {
5694 Py_DECREF(line);
5695 goto err;
5696 }
5697 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5698 Py_DECREF(line);
5699 Py_DECREF(linenr);
5700 if (!ret)
5701 goto err;
5702
Bram Moolenaara58883b2017-01-29 21:31:09 +01005703 /* Check that the command didn't switch to another buffer. */
5704 if (curbuf != was_curbuf)
5705 {
5706 Py_XDECREF(ret);
5707 goto err;
5708 }
5709
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005710 if (ret != Py_None)
5711 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005712 {
5713 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005714 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005715 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005716
5717 Py_XDECREF(ret);
5718 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005719#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005720 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005721#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005722 }
5723 goto out;
5724err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005725#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005726 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005727#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005728 PyErr_PrintEx(0);
5729 PythonIO_Flush();
5730 status = 1;
5731out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005732#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005733 if (!status)
5734 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005735#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005736 Py_DECREF(pyfunc);
5737 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5738 if (status)
5739 return;
5740 check_cursor();
5741 update_curbuf(NOT_VALID);
5742}
5743
5744 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005745run_eval(const char *cmd, typval_T *rettv
5746#ifdef PY_CAN_RECURSE
5747 , PyGILState_STATE *pygilstate UNUSED
5748#endif
5749 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005750{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005751 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005752
Bram Moolenaar41009372013-07-01 22:03:04 +02005753 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005754 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005755 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005756 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005757 {
5758 EMSG2(_(e_py_systemexit), "python");
5759 PyErr_Clear();
5760 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005761 else
5762 {
5763 if (PyErr_Occurred() && !msg_silent)
5764 PyErr_PrintEx(0);
5765 EMSG(_("E858: Eval did not return a valid python object"));
5766 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005767 }
5768 else
5769 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005770 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005772 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005773 }
5774 PyErr_Clear();
5775}
5776
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005777 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005778set_ref_in_py(const int copyID)
5779{
5780 pylinkedlist_T *cur;
5781 dict_T *dd;
5782 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005783 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005784 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005785 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005786
5787 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005788 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005789 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005790 {
5791 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5792 if (dd->dv_copyID != copyID)
5793 {
5794 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005795 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005796 }
5797 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005798 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005799
5800 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005801 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005802 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005803 {
5804 ll = ((ListObject *) (cur->pll_obj))->list;
5805 if (ll->lv_copyID != copyID)
5806 {
5807 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005808 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005809 }
5810 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005811 }
5812
Bram Moolenaar8110a092016-04-14 15:56:09 +02005813 if (lastfunc != NULL)
5814 {
5815 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5816 {
5817 func = (FunctionObject *) cur->pll_obj;
5818 if (func->self != NULL && func->self->dv_copyID != copyID)
5819 {
5820 func->self->dv_copyID = copyID;
5821 abort = abort || set_ref_in_ht(
5822 &func->self->dv_hashtab, copyID, NULL);
5823 }
5824 if (func->argc)
5825 for (i = 0; !abort && i < func->argc; ++i)
5826 abort = abort
5827 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5828 }
5829 }
5830
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005831 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005832}
5833
5834 static int
5835set_string_copy(char_u *str, typval_T *tv)
5836{
5837 tv->vval.v_string = vim_strsave(str);
5838 if (tv->vval.v_string == NULL)
5839 {
5840 PyErr_NoMemory();
5841 return -1;
5842 }
5843 return 0;
5844}
5845
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005846 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005847pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005848{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005849 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005850 char_u *key;
5851 dictitem_T *di;
5852 PyObject *keyObject;
5853 PyObject *valObject;
5854 Py_ssize_t iter = 0;
5855
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005856 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005857 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005858
5859 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005860 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005861
5862 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5863 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005864 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005865
Bram Moolenaara03e6312013-05-29 22:49:26 +02005866 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005867 {
5868 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005869 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005870 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005871
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005872 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005873 {
5874 dict_unref(dict);
5875 return -1;
5876 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005877
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005878 if (*key == NUL)
5879 {
5880 dict_unref(dict);
5881 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005882 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005883 return -1;
5884 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005885
5886 di = dictitem_alloc(key);
5887
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005888 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005889
5890 if (di == NULL)
5891 {
5892 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005893 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005894 return -1;
5895 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005896
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005897 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005898 {
5899 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005900 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901 return -1;
5902 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005903
5904 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005905 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005906 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005907 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005908 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005909 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005910 return -1;
5911 }
5912 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005913
5914 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005915 return 0;
5916}
5917
5918 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005919pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005921 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922 char_u *key;
5923 dictitem_T *di;
5924 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005925 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926 PyObject *keyObject;
5927 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005928
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005929 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005931
5932 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005933 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005935 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005936 {
5937 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005938 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005939 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005940
5941 if (!(iterator = PyObject_GetIter(list)))
5942 {
5943 dict_unref(dict);
5944 Py_DECREF(list);
5945 return -1;
5946 }
5947 Py_DECREF(list);
5948
5949 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005950 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005951 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005952
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005953 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005954 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005955 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005956 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005957 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005958 return -1;
5959 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005960
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005961 if (*key == NUL)
5962 {
5963 Py_DECREF(keyObject);
5964 Py_DECREF(iterator);
5965 Py_XDECREF(todecref);
5966 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005967 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005968 return -1;
5969 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005971 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005972 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005973 Py_DECREF(keyObject);
5974 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005975 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005976 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 return -1;
5978 }
5979
5980 di = dictitem_alloc(key);
5981
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005982 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005983 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005984
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985 if (di == NULL)
5986 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005987 Py_DECREF(iterator);
5988 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005989 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005990 PyErr_NoMemory();
5991 return -1;
5992 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005993
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005994 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005995 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005996 Py_DECREF(iterator);
5997 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005999 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006000 return -1;
6001 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006002
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006003 Py_DECREF(valObject);
6004
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006005 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006006 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006007 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006008 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006009 dictitem_free(di);
6010 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006011 return -1;
6012 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006013 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006014 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006015 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006016 return 0;
6017}
6018
6019 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006020pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006021{
6022 list_T *l;
6023
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006024 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006025 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006026
6027 tv->v_type = VAR_LIST;
6028 tv->vval.v_list = l;
6029
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006030 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006031 {
6032 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006033 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006034 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006035
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006036 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006037 return 0;
6038}
6039
Bram Moolenaardb913952012-06-29 12:54:53 +02006040typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6041
6042 static int
6043convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006044 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006045{
6046 PyObject *capsule;
6047 char hexBuf[sizeof(void *) * 2 + 3];
6048
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006049 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006050
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006051# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006052 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006053# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006054 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006055# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006056 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006057 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006058# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006059 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006060# else
6061 capsule = PyCObject_FromVoidPtr(tv, NULL);
6062# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006063 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6064 {
6065 Py_DECREF(capsule);
6066 tv->v_type = VAR_UNKNOWN;
6067 return -1;
6068 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006069
6070 Py_DECREF(capsule);
6071
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006072 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006073 {
6074 tv->v_type = VAR_UNKNOWN;
6075 return -1;
6076 }
6077 /* As we are not using copy_tv which increments reference count we must
6078 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006079 if (tv->v_type == VAR_DICT)
6080 ++tv->vval.v_dict->dv_refcount;
6081 else if (tv->v_type == VAR_LIST)
6082 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006083 }
6084 else
6085 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006086 typval_T *v;
6087
6088# ifdef PY_USE_CAPSULE
6089 v = PyCapsule_GetPointer(capsule, NULL);
6090# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006091 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006092# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006093 copy_tv(v, tv);
6094 }
6095 return 0;
6096}
6097
6098 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006099ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6100{
6101 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006102 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006103
6104 if (!(lookup_dict = PyDict_New()))
6105 return -1;
6106
6107 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6108 {
6109 tv->v_type = VAR_DICT;
6110 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6111 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006112 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006113 }
6114 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006115 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006116 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006117 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006118 else
6119 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006120 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006121 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006122 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006123 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006124 }
6125 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006126 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006127}
6128
6129 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006130ConvertFromPySequence(PyObject *obj, typval_T *tv)
6131{
6132 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006133 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006134
6135 if (!(lookup_dict = PyDict_New()))
6136 return -1;
6137
6138 if (PyType_IsSubtype(obj->ob_type, &ListType))
6139 {
6140 tv->v_type = VAR_LIST;
6141 tv->vval.v_list = (((ListObject *)(obj))->list);
6142 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006143 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006144 }
6145 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006146 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006147 else
6148 {
6149 PyErr_FORMAT(PyExc_TypeError,
6150 N_("unable to convert %s to vim list"),
6151 Py_TYPE_NAME(obj));
6152 ret = -1;
6153 }
6154 Py_DECREF(lookup_dict);
6155 return ret;
6156}
6157
6158 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006159ConvertFromPyObject(PyObject *obj, typval_T *tv)
6160{
6161 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006162 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006163
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006164 if (!(lookup_dict = PyDict_New()))
6165 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006166 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006167 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006168 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006169}
6170
6171 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006172_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006173{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006174 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006175 {
6176 tv->v_type = VAR_DICT;
6177 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6178 ++tv->vval.v_dict->dv_refcount;
6179 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006180 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006181 {
6182 tv->v_type = VAR_LIST;
6183 tv->vval.v_list = (((ListObject *)(obj))->list);
6184 ++tv->vval.v_list->lv_refcount;
6185 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006186 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006187 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006188 FunctionObject *func = (FunctionObject *) obj;
6189 if (func->self != NULL || func->argv != NULL)
6190 {
6191 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6192 set_partial(func, pt, TRUE);
6193 tv->vval.v_partial = pt;
6194 tv->v_type = VAR_PARTIAL;
6195 }
6196 else
6197 {
6198 if (set_string_copy(func->name, tv) == -1)
6199 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006200
Bram Moolenaar8110a092016-04-14 15:56:09 +02006201 tv->v_type = VAR_FUNC;
6202 }
6203 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006204 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006205 else if (PyBytes_Check(obj))
6206 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006207 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006208
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006209 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006210 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006211 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 return -1;
6213
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006214 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006215 return -1;
6216
6217 tv->v_type = VAR_STRING;
6218 }
6219 else if (PyUnicode_Check(obj))
6220 {
6221 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006222 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006223
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006224 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006225 if (bytes == NULL)
6226 return -1;
6227
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006228 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006229 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006230 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006231 return -1;
6232
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006233 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006234 {
6235 Py_XDECREF(bytes);
6236 return -1;
6237 }
6238 Py_XDECREF(bytes);
6239
6240 tv->v_type = VAR_STRING;
6241 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006242#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006243 else if (PyInt_Check(obj))
6244 {
6245 tv->v_type = VAR_NUMBER;
6246 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006247 if (PyErr_Occurred())
6248 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006249 }
6250#endif
6251 else if (PyLong_Check(obj))
6252 {
6253 tv->v_type = VAR_NUMBER;
6254 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006255 if (PyErr_Occurred())
6256 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 }
6258 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006259 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006260#ifdef FEAT_FLOAT
6261 else if (PyFloat_Check(obj))
6262 {
6263 tv->v_type = VAR_FLOAT;
6264 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6265 }
6266#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006267 else if (PyObject_HasAttrString(obj, "keys"))
6268 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006269 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006270 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006271 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006272 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006273 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006274 else if (PyNumber_Check(obj))
6275 {
6276 PyObject *num;
6277
6278 if (!(num = PyNumber_Long(obj)))
6279 return -1;
6280
6281 tv->v_type = VAR_NUMBER;
6282 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6283
6284 Py_DECREF(num);
6285 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006286 else if (obj == Py_None)
6287 {
6288 tv->v_type = VAR_SPECIAL;
6289 tv->vval.v_number = VVAL_NONE;
6290 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006291 else
6292 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006293 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006294 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006295 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006296 return -1;
6297 }
6298 return 0;
6299}
6300
6301 static PyObject *
6302ConvertToPyObject(typval_T *tv)
6303{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006304 typval_T *argv;
6305 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006306 if (tv == NULL)
6307 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006308 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006309 return NULL;
6310 }
6311 switch (tv->v_type)
6312 {
6313 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006314 return PyBytes_FromString(tv->vval.v_string == NULL
6315 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006316 case VAR_NUMBER:
6317 return PyLong_FromLong((long) tv->vval.v_number);
6318#ifdef FEAT_FLOAT
6319 case VAR_FLOAT:
6320 return PyFloat_FromDouble((double) tv->vval.v_float);
6321#endif
6322 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006323 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006324 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006325 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006326 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006327 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006328 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006329 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006330 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006331 if (tv->vval.v_partial->pt_argc)
6332 {
6333 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6334 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6335 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6336 }
6337 else
6338 argv = NULL;
6339 if (tv->vval.v_partial->pt_dict != NULL)
6340 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006341 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006342 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006343 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006344 tv->vval.v_partial->pt_dict,
6345 tv->vval.v_partial->pt_auto);
Bram Moolenaardb913952012-06-29 12:54:53 +02006346 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006347 case VAR_CHANNEL:
6348 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006349 Py_INCREF(Py_None);
6350 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006351 case VAR_SPECIAL:
6352 switch (tv->vval.v_number)
6353 {
6354 case VVAL_FALSE: return AlwaysFalse(NULL);
6355 case VVAL_TRUE: return AlwaysTrue(NULL);
6356 case VVAL_NONE:
6357 case VVAL_NULL: return AlwaysNone(NULL);
6358 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006359 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006360 return NULL;
6361 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006362 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006363}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006364
6365typedef struct
6366{
6367 PyObject_HEAD
6368} CurrentObject;
6369static PyTypeObject CurrentType;
6370
6371 static void
6372init_structs(void)
6373{
6374 vim_memset(&OutputType, 0, sizeof(OutputType));
6375 OutputType.tp_name = "vim.message";
6376 OutputType.tp_basicsize = sizeof(OutputObject);
6377 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6378 OutputType.tp_doc = "vim message object";
6379 OutputType.tp_methods = OutputMethods;
6380#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006381 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6382 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006383 OutputType.tp_alloc = call_PyType_GenericAlloc;
6384 OutputType.tp_new = call_PyType_GenericNew;
6385 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006386 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006387#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006388 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6389 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006390 // Disabled, because this causes a crash in test86
6391 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006392#endif
6393
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006394 vim_memset(&IterType, 0, sizeof(IterType));
6395 IterType.tp_name = "vim.iter";
6396 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006397 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006398 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006399 IterType.tp_iter = (getiterfunc)IterIter;
6400 IterType.tp_iternext = (iternextfunc)IterNext;
6401 IterType.tp_dealloc = (destructor)IterDestructor;
6402 IterType.tp_traverse = (traverseproc)IterTraverse;
6403 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006404
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006405 vim_memset(&BufferType, 0, sizeof(BufferType));
6406 BufferType.tp_name = "vim.buffer";
6407 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006408 BufferType.tp_dealloc = (destructor)BufferDestructor;
6409 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006410 BufferType.tp_as_sequence = &BufferAsSeq;
6411 BufferType.tp_as_mapping = &BufferAsMapping;
6412 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6413 BufferType.tp_doc = "vim buffer object";
6414 BufferType.tp_methods = BufferMethods;
6415#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006416 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006417 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006418 BufferType.tp_alloc = call_PyType_GenericAlloc;
6419 BufferType.tp_new = call_PyType_GenericNew;
6420 BufferType.tp_free = call_PyObject_Free;
6421#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006422 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006423 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006424#endif
6425
6426 vim_memset(&WindowType, 0, sizeof(WindowType));
6427 WindowType.tp_name = "vim.window";
6428 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006429 WindowType.tp_dealloc = (destructor)WindowDestructor;
6430 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006431 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006432 WindowType.tp_doc = "vim Window object";
6433 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006434 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6435 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006436#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006437 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6438 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006439 WindowType.tp_alloc = call_PyType_GenericAlloc;
6440 WindowType.tp_new = call_PyType_GenericNew;
6441 WindowType.tp_free = call_PyObject_Free;
6442#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006443 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6444 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006445#endif
6446
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006447 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6448 TabPageType.tp_name = "vim.tabpage";
6449 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006450 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6451 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006452 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6453 TabPageType.tp_doc = "vim tab page object";
6454 TabPageType.tp_methods = TabPageMethods;
6455#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006456 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006457 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6458 TabPageType.tp_new = call_PyType_GenericNew;
6459 TabPageType.tp_free = call_PyObject_Free;
6460#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006461 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006462#endif
6463
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006464 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6465 BufMapType.tp_name = "vim.bufferlist";
6466 BufMapType.tp_basicsize = sizeof(BufMapObject);
6467 BufMapType.tp_as_mapping = &BufMapAsMapping;
6468 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006469 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006470 BufferType.tp_doc = "vim buffer list";
6471
6472 vim_memset(&WinListType, 0, sizeof(WinListType));
6473 WinListType.tp_name = "vim.windowlist";
6474 WinListType.tp_basicsize = sizeof(WinListType);
6475 WinListType.tp_as_sequence = &WinListAsSeq;
6476 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6477 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006478 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006479
6480 vim_memset(&TabListType, 0, sizeof(TabListType));
6481 TabListType.tp_name = "vim.tabpagelist";
6482 TabListType.tp_basicsize = sizeof(TabListType);
6483 TabListType.tp_as_sequence = &TabListAsSeq;
6484 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6485 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006486
6487 vim_memset(&RangeType, 0, sizeof(RangeType));
6488 RangeType.tp_name = "vim.range";
6489 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006490 RangeType.tp_dealloc = (destructor)RangeDestructor;
6491 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006492 RangeType.tp_as_sequence = &RangeAsSeq;
6493 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006494 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006495 RangeType.tp_doc = "vim Range object";
6496 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006497 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6498 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006499#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006500 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006501 RangeType.tp_alloc = call_PyType_GenericAlloc;
6502 RangeType.tp_new = call_PyType_GenericNew;
6503 RangeType.tp_free = call_PyObject_Free;
6504#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006506#endif
6507
6508 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6509 CurrentType.tp_name = "vim.currentdata";
6510 CurrentType.tp_basicsize = sizeof(CurrentObject);
6511 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6512 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006513 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006514#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6516 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006517#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006518 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6519 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006520#endif
6521
6522 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6523 DictionaryType.tp_name = "vim.dictionary";
6524 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006525 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006526 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006527 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006528 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006529 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6530 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006531 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6532 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6533 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006534#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006535 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6536 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006537#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006538 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6539 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006540#endif
6541
6542 vim_memset(&ListType, 0, sizeof(ListType));
6543 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006544 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006545 ListType.tp_basicsize = sizeof(ListObject);
6546 ListType.tp_as_sequence = &ListAsSeq;
6547 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006548 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006549 ListType.tp_doc = "list pushing modifications to vim structure";
6550 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006551 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006552 ListType.tp_new = (newfunc)ListConstructor;
6553 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006554#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006555 ListType.tp_getattro = (getattrofunc)ListGetattro;
6556 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006557#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006558 ListType.tp_getattr = (getattrfunc)ListGetattr;
6559 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560#endif
6561
6562 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006563 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006564 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006565 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6566 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006567 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568 FunctionType.tp_doc = "object that calls vim function";
6569 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006570 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006571 FunctionType.tp_new = (newfunc)FunctionConstructor;
6572 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006573#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006574 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006575#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006576 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006577#endif
6578
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006579 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6580 OptionsType.tp_name = "vim.options";
6581 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006582 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006583 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006584 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006585 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006586 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006587 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6588 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6589 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006590
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006591#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006592 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6593 LoaderType.tp_name = "vim.Loader";
6594 LoaderType.tp_basicsize = sizeof(LoaderObject);
6595 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6596 LoaderType.tp_doc = "vim message object";
6597 LoaderType.tp_methods = LoaderMethods;
6598 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006599#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006600
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006601#if PY_MAJOR_VERSION >= 3
6602 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6603 vimmodule.m_name = "vim";
6604 vimmodule.m_doc = "Vim Python interface\n";
6605 vimmodule.m_size = -1;
6606 vimmodule.m_methods = VimMethods;
6607#endif
6608}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006609
6610#define PYTYPE_READY(type) \
6611 if (PyType_Ready(&type)) \
6612 return -1;
6613
6614 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006615init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006616{
6617 PYTYPE_READY(IterType);
6618 PYTYPE_READY(BufferType);
6619 PYTYPE_READY(RangeType);
6620 PYTYPE_READY(WindowType);
6621 PYTYPE_READY(TabPageType);
6622 PYTYPE_READY(BufMapType);
6623 PYTYPE_READY(WinListType);
6624 PYTYPE_READY(TabListType);
6625 PYTYPE_READY(CurrentType);
6626 PYTYPE_READY(DictionaryType);
6627 PYTYPE_READY(ListType);
6628 PYTYPE_READY(FunctionType);
6629 PYTYPE_READY(OptionsType);
6630 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006631#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006632 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006633#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006634 return 0;
6635}
6636
6637 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006638init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006639{
6640 PyObject *path;
6641 PyObject *path_hook;
6642 PyObject *path_hooks;
6643
6644 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6645 return -1;
6646
6647 if (!(path_hooks = PySys_GetObject("path_hooks")))
6648 {
6649 PyErr_Clear();
6650 path_hooks = PyList_New(1);
6651 PyList_SET_ITEM(path_hooks, 0, path_hook);
6652 if (PySys_SetObject("path_hooks", path_hooks))
6653 {
6654 Py_DECREF(path_hooks);
6655 return -1;
6656 }
6657 Py_DECREF(path_hooks);
6658 }
6659 else if (PyList_Check(path_hooks))
6660 {
6661 if (PyList_Append(path_hooks, path_hook))
6662 {
6663 Py_DECREF(path_hook);
6664 return -1;
6665 }
6666 Py_DECREF(path_hook);
6667 }
6668 else
6669 {
6670 VimTryStart();
6671 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6672 "You should now do the following:\n"
6673 "- append vim.path_hook to sys.path_hooks\n"
6674 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6675 VimTryEnd(); /* Discard the error */
6676 Py_DECREF(path_hook);
6677 return 0;
6678 }
6679
6680 if (!(path = PySys_GetObject("path")))
6681 {
6682 PyErr_Clear();
6683 path = PyList_New(1);
6684 Py_INCREF(vim_special_path_object);
6685 PyList_SET_ITEM(path, 0, vim_special_path_object);
6686 if (PySys_SetObject("path", path))
6687 {
6688 Py_DECREF(path);
6689 return -1;
6690 }
6691 Py_DECREF(path);
6692 }
6693 else if (PyList_Check(path))
6694 {
6695 if (PyList_Append(path, vim_special_path_object))
6696 return -1;
6697 }
6698 else
6699 {
6700 VimTryStart();
6701 EMSG(_("Failed to set path: sys.path is not a list\n"
6702 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6703 VimTryEnd(); /* Discard the error */
6704 }
6705
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006706 return 0;
6707}
6708
6709static BufMapObject TheBufferMap =
6710{
6711 PyObject_HEAD_INIT(&BufMapType)
6712};
6713
6714static WinListObject TheWindowList =
6715{
6716 PyObject_HEAD_INIT(&WinListType)
6717 NULL
6718};
6719
6720static CurrentObject TheCurrent =
6721{
6722 PyObject_HEAD_INIT(&CurrentType)
6723};
6724
6725static TabListObject TheTabPageList =
6726{
6727 PyObject_HEAD_INIT(&TabListType)
6728};
6729
6730static struct numeric_constant {
6731 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006732 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006733} numeric_constants[] = {
6734 {"VAR_LOCKED", VAR_LOCKED},
6735 {"VAR_FIXED", VAR_FIXED},
6736 {"VAR_SCOPE", VAR_SCOPE},
6737 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6738};
6739
6740static struct object_constant {
6741 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006742 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006743} object_constants[] = {
6744 {"buffers", (PyObject *)(void *)&TheBufferMap},
6745 {"windows", (PyObject *)(void *)&TheWindowList},
6746 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6747 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006748
6749 {"Buffer", (PyObject *)&BufferType},
6750 {"Range", (PyObject *)&RangeType},
6751 {"Window", (PyObject *)&WindowType},
6752 {"TabPage", (PyObject *)&TabPageType},
6753 {"Dictionary", (PyObject *)&DictionaryType},
6754 {"List", (PyObject *)&ListType},
6755 {"Function", (PyObject *)&FunctionType},
6756 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006757#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006758 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006759#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006760};
6761
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006762#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006763 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006764 return -1;
6765
6766#define ADD_CHECKED_OBJECT(m, name, obj) \
6767 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006768 PyObject *valObject = obj; \
6769 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006770 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006771 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006772 }
6773
6774 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006775populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006776{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006777 int i;
6778 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006779 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006780 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006781#if PY_VERSION_HEX >= 0x030700f0
6782 PyObject *dict;
6783 PyObject *cls;
6784#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006785
6786 for (i = 0; i < (int)(sizeof(numeric_constants)
6787 / sizeof(struct numeric_constant));
6788 ++i)
6789 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006790 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006791
6792 for (i = 0; i < (int)(sizeof(object_constants)
6793 / sizeof(struct object_constant));
6794 ++i)
6795 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006796 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006797
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006798 valObject = object_constants[i].valObject;
6799 Py_INCREF(valObject);
6800 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006801 }
6802
6803 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6804 return -1;
6805 ADD_OBJECT(m, "error", VimError);
6806
Bram Moolenaara9922d62013-05-30 13:01:18 +02006807 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6808 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006809 ADD_CHECKED_OBJECT(m, "options",
6810 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006811
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006812 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006813 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006814 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006815
Bram Moolenaar22081f42016-06-01 20:38:34 +02006816#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006817 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006818 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006819#else
6820 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6821 return -1;
6822#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006823 ADD_OBJECT(m, "_getcwd", py_getcwd)
6824
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006825 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006826 return -1;
6827 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006828 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006829 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006830 if (PyObject_SetAttrString(other_module, "chdir", attr))
6831 {
6832 Py_DECREF(attr);
6833 return -1;
6834 }
6835 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006836
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006837 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006838 {
6839 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006840 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006841 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006842 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6843 {
6844 Py_DECREF(attr);
6845 return -1;
6846 }
6847 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006848 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006849 else
6850 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006851
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006852 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6853 return -1;
6854
6855 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6856
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006857#if PY_VERSION_HEX >= 0x030700f0
6858 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6859 return -1;
6860
6861 dict = PyModule_GetDict(imp);
6862
6863 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6864 {
6865 Py_DECREF(imp);
6866 return -1;
6867 }
6868
6869 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6870 {
6871 Py_DECREF(imp);
6872 return -1;
6873 }
6874
6875 Py_DECREF(imp);
6876
6877 ADD_OBJECT(m, "_find_spec", py_find_spec);
6878#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006879 if (!(imp = PyImport_ImportModule("imp")))
6880 return -1;
6881
6882 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6883 {
6884 Py_DECREF(imp);
6885 return -1;
6886 }
6887
6888 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6889 {
6890 Py_DECREF(py_find_module);
6891 Py_DECREF(imp);
6892 return -1;
6893 }
6894
6895 Py_DECREF(imp);
6896
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006897 ADD_OBJECT(m, "_find_module", py_find_module);
6898 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006899#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006900
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006901 return 0;
6902}