blob: 85141e11a01b35e1633fd093677e506a1b103a88 [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
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100413msg_wrapper(char *text)
414{
415 return msg((char_u *)text);
416}
417
418 static int
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200419write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200420{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200421 Py_ssize_t len = 0;
422 char *str = NULL;
423 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
426 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200427
428 Py_BEGIN_ALLOW_THREADS
429 Python_Lock_Vim();
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100430 writer((writefn)(error ? emsg : msg_wrapper), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200431 Python_Release_Vim();
432 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200433 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200435 return 0;
436}
437
438 static PyObject *
439OutputWrite(OutputObject *self, PyObject *string)
440{
441 if (write_output(self, string))
442 return NULL;
443
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 Py_INCREF(Py_None);
445 return Py_None;
446}
447
448 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200449OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 PyObject *iterator;
452 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200453
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200458 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200459 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200460 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200461 Py_DECREF(iterator);
462 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463 return NULL;
464 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200465 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200466 }
467
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200468 Py_DECREF(iterator);
469
470 /* Iterator may have finished due to an exception */
471 if (PyErr_Occurred())
472 return NULL;
473
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200474 Py_INCREF(Py_None);
475 return Py_None;
476}
477
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100478 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100479AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100480{
481 /* do nothing */
482 Py_INCREF(Py_None);
483 return Py_None;
484}
485
Bram Moolenaard4247472015-11-02 13:28:59 +0100486 static PyObject *
487AlwaysFalse(PyObject *self UNUSED)
488{
489 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100490 PyObject *ret = Py_False;
491 Py_INCREF(ret);
492 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100493}
494
495 static PyObject *
496AlwaysTrue(PyObject *self UNUSED)
497{
498 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100499 PyObject *ret = Py_True;
500 Py_INCREF(ret);
501 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100502}
503
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200504/***************/
505
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200506static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200507 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200508 {"write", (PyCFunction)OutputWrite, METH_O, ""},
509 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100510 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
511 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
512 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
513 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
514 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
515 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200516 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200517 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200518 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200519};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200520
521static OutputObject Output =
522{
523 PyObject_HEAD_INIT(&OutputType)
524 0,
525 0
526};
527
528static OutputObject Error =
529{
530 PyObject_HEAD_INIT(&OutputType)
531 0,
532 1
533};
534
535 static int
536PythonIO_Init_io(void)
537{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200538 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
539 return -1;
540 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
541 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200542
543 if (PyErr_Occurred())
544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200546 return -1;
547 }
548
549 return 0;
550}
551
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200552#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200553static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
554
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200555typedef struct
556{
557 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200558 char *fullname;
559 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200560} LoaderObject;
561static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200562
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200563 static void
564LoaderDestructor(LoaderObject *self)
565{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200566 vim_free(self->fullname);
567 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200568 DESTRUCTOR_FINISH(self);
569}
570
571 static PyObject *
572LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
573{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200574 char *fullname = self->fullname;
575 PyObject *result = self->result;
576 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200577
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200578 if (!fullname)
579 {
580 module = result ? result : Py_None;
581 Py_INCREF(module);
582 return module;
583 }
584
585 module = call_load_module(fullname, (int)STRLEN(fullname), result);
586
587 self->fullname = NULL;
588 self->result = module;
589
590 vim_free(fullname);
591 Py_DECREF(result);
592
593 if (!module)
594 {
595 if (PyErr_Occurred())
596 return NULL;
597
598 Py_INCREF(Py_None);
599 return Py_None;
600 }
601
602 Py_INCREF(module);
603 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200604}
605
606static struct PyMethodDef LoaderMethods[] = {
607 /* name, function, calling, doc */
608 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
609 { NULL, NULL, 0, NULL}
610};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200611#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200612
613/* Check to see whether a Vim error has been reported, or a keyboard
614 * interrupt has been detected.
615 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200616
617 static void
618VimTryStart(void)
619{
620 ++trylevel;
621}
622
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200623 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200624VimTryEnd(void)
625{
626 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100627 /* Without this it stops processing all subsequent Vim script commands and
628 * generates strange error messages if I e.g. try calling Test() in a cycle
629 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200630 did_emsg = FALSE;
631 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200632 if (got_int)
633 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100634 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100635 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100636 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200637 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200638 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200639 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100640 else if (msg_list != NULL && *msg_list != NULL)
641 {
642 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100643 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100644
645 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
646
647 if (msg == NULL)
648 {
649 PyErr_NoMemory();
650 return -1;
651 }
652
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100653 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100654
655 free_global_msglist();
656
657 if (should_free)
658 vim_free(msg);
659
660 return -1;
661 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200662 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200663 return (PyErr_Occurred() ? -1 : 0);
664 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200666 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100667 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200668 return -1;
669 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100670 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200671 else
672 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200673 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200674 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200675 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200676 }
677}
678
679 static int
680VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200681{
682 if (got_int)
683 {
684 PyErr_SetNone(PyExc_KeyboardInterrupt);
685 return 1;
686 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200687 return 0;
688}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689
690/* Vim module - Implementation
691 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200692
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200694VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200696 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200697 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200698 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200699
Bram Moolenaar389a1792013-06-23 13:00:44 +0200700 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701 return NULL;
702
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200703 Py_BEGIN_ALLOW_THREADS
704 Python_Lock_Vim();
705
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200706 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200707 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200708 update_screen(VALID);
709
710 Python_Release_Vim();
711 Py_END_ALLOW_THREADS
712
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200713 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200714 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200716 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200718 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200719 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200720 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721}
722
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200723/*
724 * Function to translate a typval_T into a PyObject; this will recursively
725 * translate lists/dictionaries into their Python equivalents.
726 *
727 * The depth parameter is to avoid infinite recursion, set it to 1 when
728 * you call VimToPython.
729 */
730 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200731VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200732{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200733 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200735 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736
737 /* Avoid infinite recursion */
738 if (depth > 100)
739 {
740 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200741 ret = Py_None;
742 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200743 }
744
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200745 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200746 * then and we can use it again. */
747 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
748 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
749 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200750 sprintf(ptrBuf, "%p",
751 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
752 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200753
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200754 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200755 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200756 Py_INCREF(ret);
757 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 }
759 }
760
761 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200763 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200764 else if (our_tv->v_type == VAR_NUMBER)
765 {
766 char buf[NUMBUFLEN];
767
768 /* For backwards compatibility numbers are stored as strings. */
769 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200770 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 }
772# ifdef FEAT_FLOAT
773 else if (our_tv->v_type == VAR_FLOAT)
774 {
775 char buf[NUMBUFLEN];
776
777 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200778 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200779 }
780# endif
781 else if (our_tv->v_type == VAR_LIST)
782 {
783 list_T *list = our_tv->vval.v_list;
784 listitem_T *curr;
785
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200786 if (list == NULL)
787 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200788
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200789 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200790 return NULL;
791
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200792 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200793 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200794 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200795 return NULL;
796 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200798 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
799 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200800 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200801 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200802 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200803 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200804 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200805 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200806 {
807 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200808 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200809 return NULL;
810 }
811 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200812 }
813 }
814 else if (our_tv->v_type == VAR_DICT)
815 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100817 hashtab_T *ht;
818 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200819 hashitem_T *hi;
820 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100821
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200822 if (our_tv->vval.v_dict == NULL)
823 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100824 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200825
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200826 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200827 return NULL;
828
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200829 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200830 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200831 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200832 return NULL;
833 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200834
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100835 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200836 for (hi = ht->ht_array; todo > 0; ++hi)
837 {
838 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200840 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200842 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200843 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200844 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200845 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200846 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200847 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200848 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200849 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200850 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200851 Py_DECREF(newObj);
852 return NULL;
853 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200854 }
855 }
856 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100857 else if (our_tv->v_type == VAR_SPECIAL)
858 {
859 if (our_tv->vval.v_number == VVAL_FALSE)
860 {
861 ret = Py_False;
862 Py_INCREF(ret);
863 }
864 else if (our_tv->vval.v_number == VVAL_TRUE)
865 {
866 ret = Py_True;
867 Py_INCREF(ret);
868 }
869 else
870 {
871 Py_INCREF(Py_None);
872 ret = Py_None;
873 }
874 return ret;
875 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100876 else if (our_tv->v_type == VAR_BLOB)
877 ret = PyBytes_FromStringAndSize(
878 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
879 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200880 else
881 {
882 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200883 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200884 }
885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200887}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200888
889 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200890VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200891{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200892 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200893 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200894 PyObject *string;
895 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200896 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200897 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200898
Bram Moolenaar389a1792013-06-23 13:00:44 +0200899 if (!PyArg_ParseTuple(args, "O", &string))
900 return NULL;
901
902 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200903 return NULL;
904
905 Py_BEGIN_ALLOW_THREADS
906 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200907 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200908 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200909 Python_Release_Vim();
910 Py_END_ALLOW_THREADS
911
Bram Moolenaar389a1792013-06-23 13:00:44 +0200912 Py_XDECREF(todecref);
913
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200914 if (VimTryEnd())
915 return NULL;
916
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200917 if (our_tv == NULL)
918 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200919 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200920 return NULL;
921 }
922
923 /* Convert the Vim type into a Python type. Create a dictionary that's
924 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200925 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200926 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200927 else
928 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200929 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200930 Py_DECREF(lookup_dict);
931 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200932
933
934 Py_BEGIN_ALLOW_THREADS
935 Python_Lock_Vim();
936 free_tv(our_tv);
937 Python_Release_Vim();
938 Py_END_ALLOW_THREADS
939
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200940 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200941}
942
Bram Moolenaardb913952012-06-29 12:54:53 +0200943static PyObject *ConvertToPyObject(typval_T *);
944
945 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200946VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200947{
Bram Moolenaardb913952012-06-29 12:54:53 +0200948 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200949 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200950 char_u *expr;
951 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200952
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200954 return NULL;
955
956 Py_BEGIN_ALLOW_THREADS
957 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200958 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200959 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200960 Python_Release_Vim();
961 Py_END_ALLOW_THREADS
962
Bram Moolenaar389a1792013-06-23 13:00:44 +0200963 Py_XDECREF(todecref);
964
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200965 if (VimTryEnd())
966 return NULL;
967
Bram Moolenaardb913952012-06-29 12:54:53 +0200968 if (our_tv == NULL)
969 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200970 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200971 return NULL;
972 }
973
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200974 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200975 Py_BEGIN_ALLOW_THREADS
976 Python_Lock_Vim();
977 free_tv(our_tv);
978 Python_Release_Vim();
979 Py_END_ALLOW_THREADS
980
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200981 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200982}
983
984 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200985VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200986{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200987 char_u *str;
988 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200989 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200990
Bram Moolenaar389a1792013-06-23 13:00:44 +0200991 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200992 return NULL;
993
Bram Moolenaara54bf402012-12-05 16:30:07 +0100994#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200995 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100996#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200997 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100998#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200999
1000 Py_XDECREF(todecref);
1001
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001002 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +02001003}
1004
Bram Moolenaarf4258302013-06-02 18:20:17 +02001005 static PyObject *
1006_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
1007{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001008 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001009 PyObject *newwd;
1010 PyObject *todecref;
1011 char_u *new_dir;
1012
Bram Moolenaard4209d22013-06-05 20:34:15 +02001013 if (_chdir == NULL)
1014 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001015 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001016 return NULL;
1017
1018 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1019 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001020 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001021 return NULL;
1022 }
1023
1024 if (!(new_dir = StringToChars(newwd, &todecref)))
1025 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001026 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001027 Py_DECREF(newwd);
1028 return NULL;
1029 }
1030
1031 VimTryStart();
1032
1033 if (vim_chdir(new_dir))
1034 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001035 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001036 Py_DECREF(newwd);
1037 Py_XDECREF(todecref);
1038
1039 if (VimTryEnd())
1040 return NULL;
1041
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001042 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001043 return NULL;
1044 }
1045
1046 Py_DECREF(newwd);
1047 Py_XDECREF(todecref);
1048
1049 post_chdir(FALSE);
1050
1051 if (VimTryEnd())
1052 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001053 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001054 return NULL;
1055 }
1056
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001057 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001058}
1059
1060 static PyObject *
1061VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1062{
1063 return _VimChdir(py_chdir, args, kwargs);
1064}
1065
1066 static PyObject *
1067VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1068{
1069 return _VimChdir(py_fchdir, args, kwargs);
1070}
1071
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001072typedef struct {
1073 PyObject *callable;
1074 PyObject *result;
1075} map_rtp_data;
1076
1077 static void
1078map_rtp_callback(char_u *path, void *_data)
1079{
1080 void **data = (void **) _data;
1081 PyObject *pathObject;
1082 map_rtp_data *mr_data = *((map_rtp_data **) data);
1083
Bram Moolenaar41009372013-07-01 22:03:04 +02001084 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001085 {
1086 *data = NULL;
1087 return;
1088 }
1089
1090 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1091 pathObject, NULL);
1092
1093 Py_DECREF(pathObject);
1094
1095 if (!mr_data->result || mr_data->result != Py_None)
1096 *data = NULL;
1097 else
1098 {
1099 Py_DECREF(mr_data->result);
1100 mr_data->result = NULL;
1101 }
1102}
1103
1104 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001105VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001106{
1107 map_rtp_data data;
1108
Bram Moolenaar389a1792013-06-23 13:00:44 +02001109 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001110 data.result = NULL;
1111
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001112 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001113
1114 if (data.result == NULL)
1115 {
1116 if (PyErr_Occurred())
1117 return NULL;
1118 else
1119 {
1120 Py_INCREF(Py_None);
1121 return Py_None;
1122 }
1123 }
1124 return data.result;
1125}
1126
1127/*
1128 * _vim_runtimepath_ special path implementation.
1129 */
1130
1131 static void
1132map_finder_callback(char_u *path, void *_data)
1133{
1134 void **data = (void **) _data;
1135 PyObject *list = *((PyObject **) data);
1136 PyObject *pathObject1, *pathObject2;
1137 char *pathbuf;
1138 size_t pathlen;
1139
1140 pathlen = STRLEN(path);
1141
1142#if PY_MAJOR_VERSION < 3
1143# define PY_MAIN_DIR_STRING "python2"
1144#else
1145# define PY_MAIN_DIR_STRING "python3"
1146#endif
1147#define PY_ALTERNATE_DIR_STRING "pythonx"
1148
1149#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1150 if (!(pathbuf = PyMem_New(char,
1151 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1152 {
1153 PyErr_NoMemory();
1154 *data = NULL;
1155 return;
1156 }
1157
1158 mch_memmove(pathbuf, path, pathlen + 1);
1159 add_pathsep((char_u *) pathbuf);
1160
1161 pathlen = STRLEN(pathbuf);
1162 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1163 PYTHONX_STRING_LENGTH + 1);
1164
1165 if (!(pathObject1 = PyString_FromString(pathbuf)))
1166 {
1167 *data = NULL;
1168 PyMem_Free(pathbuf);
1169 return;
1170 }
1171
1172 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1173 PYTHONX_STRING_LENGTH + 1);
1174
1175 if (!(pathObject2 = PyString_FromString(pathbuf)))
1176 {
1177 Py_DECREF(pathObject1);
1178 PyMem_Free(pathbuf);
1179 *data = NULL;
1180 return;
1181 }
1182
1183 PyMem_Free(pathbuf);
1184
1185 if (PyList_Append(list, pathObject1)
1186 || PyList_Append(list, pathObject2))
1187 *data = NULL;
1188
1189 Py_DECREF(pathObject1);
1190 Py_DECREF(pathObject2);
1191}
1192
1193 static PyObject *
1194Vim_GetPaths(PyObject *self UNUSED)
1195{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001196 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001197
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001198 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001199 return NULL;
1200
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001201 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001202
1203 if (PyErr_Occurred())
1204 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001205 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001206 return NULL;
1207 }
1208
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001209 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001210}
1211
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001212#if PY_VERSION_HEX >= 0x030700f0
1213 static PyObject *
1214FinderFindSpec(PyObject *self, PyObject *args)
1215{
1216 char *fullname;
1217 PyObject *paths;
1218 PyObject *target = Py_None;
1219 PyObject *spec;
1220
1221 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1222 return NULL;
1223
1224 if (!(paths = Vim_GetPaths(self)))
1225 return NULL;
1226
1227 spec = PyObject_CallFunction(py_find_spec, "sNN", fullname, paths, target);
1228
1229 Py_DECREF(paths);
1230
1231 if (!spec)
1232 {
1233 if (PyErr_Occurred())
1234 return NULL;
1235
1236 Py_INCREF(Py_None);
1237 return Py_None;
1238 }
1239
1240 return spec;
1241}
1242#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001243 static PyObject *
1244call_load_module(char *name, int len, PyObject *find_module_result)
1245{
1246 PyObject *fd, *pathname, *description;
1247
Bram Moolenaarc476e522013-06-23 13:46:40 +02001248 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001249 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001250 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001251 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001252 Py_TYPE_NAME(find_module_result));
1253 return NULL;
1254 }
1255 if (PyTuple_GET_SIZE(find_module_result) != 3)
1256 {
1257 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001258 N_("expected 3-tuple as imp.find_module() result, but got "
1259 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001260 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001261 return NULL;
1262 }
1263
1264 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1265 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1266 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1267 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001268 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001269 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001270 return NULL;
1271 }
1272
1273 return PyObject_CallFunction(py_load_module,
1274 "s#OOO", name, len, fd, pathname, description);
1275}
1276
1277 static PyObject *
1278find_module(char *fullname, char *tail, PyObject *new_path)
1279{
1280 PyObject *find_module_result;
1281 PyObject *module;
1282 char *dot;
1283
Bram Moolenaar41009372013-07-01 22:03:04 +02001284 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001285 {
1286 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001287 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001288 * first component
1289 */
1290 PyObject *newest_path;
1291 int partlen = (int) (dot - 1 - tail);
1292
1293 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1294 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001295 {
1296 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1297 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001298 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001299 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001300
1301 if (!(module = call_load_module(
1302 fullname,
1303 ((int) (tail - fullname)) + partlen,
1304 find_module_result)))
1305 {
1306 Py_DECREF(find_module_result);
1307 return NULL;
1308 }
1309
1310 Py_DECREF(find_module_result);
1311
1312 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1313 {
1314 Py_DECREF(module);
1315 return NULL;
1316 }
1317
1318 Py_DECREF(module);
1319
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001320 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001321
1322 Py_DECREF(newest_path);
1323
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001324 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001325 }
1326 else
1327 {
1328 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1329 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001330 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001331 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1332 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001333 return NULL;
1334 }
1335
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001336 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001337 }
1338}
1339
1340 static PyObject *
1341FinderFindModule(PyObject *self, PyObject *args)
1342{
1343 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001344 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001345 PyObject *new_path;
1346 LoaderObject *loader;
1347
1348 if (!PyArg_ParseTuple(args, "s", &fullname))
1349 return NULL;
1350
1351 if (!(new_path = Vim_GetPaths(self)))
1352 return NULL;
1353
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001354 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001355
1356 Py_DECREF(new_path);
1357
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001358 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001359 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001360 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001361 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001362
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001363 Py_INCREF(Py_None);
1364 return Py_None;
1365 }
1366
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001367 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001368 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001369 Py_DECREF(result);
1370 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001371 return NULL;
1372 }
1373
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001374 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1375 {
1376 vim_free(fullname);
1377 Py_DECREF(result);
1378 return NULL;
1379 }
1380
1381 loader->fullname = fullname;
1382 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001383
1384 return (PyObject *) loader;
1385}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001386#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001387
1388 static PyObject *
1389VimPathHook(PyObject *self UNUSED, PyObject *args)
1390{
1391 char *path;
1392
1393 if (PyArg_ParseTuple(args, "s", &path)
1394 && STRCMP(path, vim_special_path) == 0)
1395 {
1396 Py_INCREF(vim_module);
1397 return vim_module;
1398 }
1399
1400 PyErr_Clear();
1401 PyErr_SetNone(PyExc_ImportError);
1402 return NULL;
1403}
1404
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001405/*
1406 * Vim module - Definitions
1407 */
1408
1409static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001410 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001411 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001412 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001413 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1414 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001415 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1416 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001417 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001418#if PY_VERSION_HEX >= 0x030700f0
1419 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
1420#else
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001421 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001422#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001423 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1424 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1425 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001426};
1427
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001428/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001429 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001430 */
1431
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001432static PyTypeObject IterType;
1433
1434typedef PyObject *(*nextfun)(void **);
1435typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001436typedef int (*traversefun)(void *, visitproc, void *);
1437typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001438
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001439/* Main purpose of this object is removing the need for do python
1440 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1441 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001442
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001443typedef struct
1444{
1445 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001446 void *cur;
1447 nextfun next;
1448 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001449 traversefun traverse;
1450 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001451} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001452
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001453 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001454IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1455 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001456{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001457 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001458
Bram Moolenaar774267b2013-05-21 20:51:59 +02001459 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001460 self->cur = start;
1461 self->next = next;
1462 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001463 self->traverse = traverse;
1464 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001465
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001466 return (PyObject *)(self);
1467}
1468
1469 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001470IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001471{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001472 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001473 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001474 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001475}
1476
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001477 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001478IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001479{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001480 if (self->traverse != NULL)
1481 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001482 else
1483 return 0;
1484}
1485
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001486/* Mac OSX defines clear() somewhere. */
1487#ifdef clear
1488# undef clear
1489#endif
1490
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001491 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001492IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001493{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001494 if (self->clear != NULL)
1495 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001496 else
1497 return 0;
1498}
1499
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001500 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001501IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001502{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001503 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001504}
1505
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001506 static PyObject *
1507IterIter(PyObject *self)
1508{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001509 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001510 return self;
1511}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001512
Bram Moolenaardb913952012-06-29 12:54:53 +02001513typedef struct pylinkedlist_S {
1514 struct pylinkedlist_S *pll_next;
1515 struct pylinkedlist_S *pll_prev;
1516 PyObject *pll_obj;
1517} pylinkedlist_T;
1518
1519static pylinkedlist_T *lastdict = NULL;
1520static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001521static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001522
1523 static void
1524pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1525{
1526 if (ref->pll_prev == NULL)
1527 {
1528 if (ref->pll_next == NULL)
1529 {
1530 *last = NULL;
1531 return;
1532 }
1533 }
1534 else
1535 ref->pll_prev->pll_next = ref->pll_next;
1536
1537 if (ref->pll_next == NULL)
1538 *last = ref->pll_prev;
1539 else
1540 ref->pll_next->pll_prev = ref->pll_prev;
1541}
1542
1543 static void
1544pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1545{
1546 if (*last == NULL)
1547 ref->pll_prev = NULL;
1548 else
1549 {
1550 (*last)->pll_next = ref;
1551 ref->pll_prev = *last;
1552 }
1553 ref->pll_next = NULL;
1554 ref->pll_obj = self;
1555 *last = ref;
1556}
1557
1558static PyTypeObject DictionaryType;
1559
1560typedef struct
1561{
1562 PyObject_HEAD
1563 dict_T *dict;
1564 pylinkedlist_T ref;
1565} DictionaryObject;
1566
Bram Moolenaara9922d62013-05-30 13:01:18 +02001567static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1568
1569#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1570
Bram Moolenaardb913952012-06-29 12:54:53 +02001571 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001572DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001573{
1574 DictionaryObject *self;
1575
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001577 if (self == NULL)
1578 return NULL;
1579 self->dict = dict;
1580 ++dict->dv_refcount;
1581
1582 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1583
1584 return (PyObject *)(self);
1585}
1586
Bram Moolenaara9922d62013-05-30 13:01:18 +02001587 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001588py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001589{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001590 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001591
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001592 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001593 {
1594 PyErr_NoMemory();
1595 return NULL;
1596 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001597 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001598
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001599 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001600}
1601
1602 static PyObject *
1603DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1604{
1605 DictionaryObject *self;
1606 dict_T *dict;
1607
1608 if (!(dict = py_dict_alloc()))
1609 return NULL;
1610
1611 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1612
1613 --dict->dv_refcount;
1614
1615 if (kwargs || PyTuple_Size(args))
1616 {
1617 PyObject *tmp;
1618 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1619 {
1620 Py_DECREF(self);
1621 return NULL;
1622 }
1623
1624 Py_DECREF(tmp);
1625 }
1626
1627 return (PyObject *)(self);
1628}
1629
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001630 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001631DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001632{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001633 pyll_remove(&self->ref, &lastdict);
1634 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001635
1636 DESTRUCTOR_FINISH(self);
1637}
1638
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001639static char *DictionaryAttrs[] = {
1640 "locked", "scope",
1641 NULL
1642};
1643
1644 static PyObject *
1645DictionaryDir(PyObject *self)
1646{
1647 return ObjectDir(self, DictionaryAttrs);
1648}
1649
Bram Moolenaardb913952012-06-29 12:54:53 +02001650 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001651DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001652{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001653 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001654 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001655 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001656 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001657 return -1;
1658 }
1659
1660 if (strcmp(name, "locked") == 0)
1661 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001662 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001663 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001664 PyErr_SET_STRING(PyExc_TypeError,
1665 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001666 return -1;
1667 }
1668 else
1669 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001670 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001671 if (istrue == -1)
1672 return -1;
1673 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001674 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001675 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001676 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001677 }
1678 return 0;
1679 }
1680 else
1681 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001682 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001683 return -1;
1684 }
1685}
1686
1687 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001688DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001689{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001690 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001691}
1692
Bram Moolenaara9922d62013-05-30 13:01:18 +02001693#define DICT_FLAG_HAS_DEFAULT 0x01
1694#define DICT_FLAG_POP 0x02
1695#define DICT_FLAG_NONE_DEFAULT 0x04
1696#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1697#define DICT_FLAG_RETURN_PAIR 0x10
1698
Bram Moolenaardb913952012-06-29 12:54:53 +02001699 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001700_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001701{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702 PyObject *keyObject;
1703 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001704 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001705 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001706 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001707 dict_T *dict = self->dict;
1708 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001709 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001710
Bram Moolenaara9922d62013-05-30 13:01:18 +02001711 if (flags & DICT_FLAG_HAS_DEFAULT)
1712 {
1713 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1714 return NULL;
1715 }
1716 else
1717 keyObject = args;
1718
1719 if (flags & DICT_FLAG_RETURN_BOOL)
1720 defObject = Py_False;
1721
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001722 if (!(key = StringToChars(keyObject, &todecref)))
1723 return NULL;
1724
1725 if (*key == NUL)
1726 {
1727 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001728 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001729 return NULL;
1730 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001731
Bram Moolenaara9922d62013-05-30 13:01:18 +02001732 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001733
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001734 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001735
Bram Moolenaara9922d62013-05-30 13:01:18 +02001736 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001737 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001738 if (defObject)
1739 {
1740 Py_INCREF(defObject);
1741 return defObject;
1742 }
1743 else
1744 {
1745 PyErr_SetObject(PyExc_KeyError, keyObject);
1746 return NULL;
1747 }
1748 }
1749 else if (flags & DICT_FLAG_RETURN_BOOL)
1750 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001751 ret = Py_True;
1752 Py_INCREF(ret);
1753 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001754 }
1755
1756 di = dict_lookup(hi);
1757
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001758 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001759 return NULL;
1760
1761 if (flags & DICT_FLAG_POP)
1762 {
1763 if (dict->dv_lock)
1764 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001765 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001766 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001767 return NULL;
1768 }
1769
1770 hash_remove(&dict->dv_hashtab, hi);
1771 dictitem_free(di);
1772 }
1773
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001774 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001775}
1776
1777 static PyObject *
1778DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1779{
1780 return _DictionaryItem(self, keyObject, 0);
1781}
1782
1783 static int
1784DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1785{
1786 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001787 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001789 if (rObj == NULL)
1790 return -1;
1791
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001792 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001793
Bram Moolenaardee2e312013-06-23 16:35:47 +02001794 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001795
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001796 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001797}
1798
1799typedef struct
1800{
1801 hashitem_T *ht_array;
1802 long_u ht_used;
1803 hashtab_T *ht;
1804 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001805 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001806} dictiterinfo_T;
1807
1808 static PyObject *
1809DictionaryIterNext(dictiterinfo_T **dii)
1810{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001811 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001812
1813 if (!(*dii)->todo)
1814 return NULL;
1815
1816 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1817 (*dii)->ht->ht_used != (*dii)->ht_used)
1818 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001819 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001820 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001821 return NULL;
1822 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001823
Bram Moolenaara9922d62013-05-30 13:01:18 +02001824 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1825 ++((*dii)->hi);
1826
1827 --((*dii)->todo);
1828
Bram Moolenaar41009372013-07-01 22:03:04 +02001829 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001830 return NULL;
1831
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001832 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001833}
1834
1835 static PyObject *
1836DictionaryIter(DictionaryObject *self)
1837{
1838 dictiterinfo_T *dii;
1839 hashtab_T *ht;
1840
1841 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1842 {
1843 PyErr_NoMemory();
1844 return NULL;
1845 }
1846
1847 ht = &self->dict->dv_hashtab;
1848 dii->ht_array = ht->ht_array;
1849 dii->ht_used = ht->ht_used;
1850 dii->ht = ht;
1851 dii->hi = dii->ht_array;
1852 dii->todo = dii->ht_used;
1853
1854 return IterNew(dii,
1855 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1856 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001857}
1858
1859 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001860DictionaryAssItem(
1861 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001862{
1863 char_u *key;
1864 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001865 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001867 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001868
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001869 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001871 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001872 return -1;
1873 }
1874
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001875 if (!(key = StringToChars(keyObject, &todecref)))
1876 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001877
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001878 if (*key == NUL)
1879 {
1880 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001881 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001882 return -1;
1883 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001884
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001885 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001886
1887 if (valObject == NULL)
1888 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001889 hashitem_T *hi;
1890
Bram Moolenaardb913952012-06-29 12:54:53 +02001891 if (di == NULL)
1892 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001893 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001894 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 return -1;
1896 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001897 hi = hash_find(&dict->dv_hashtab, di->di_key);
1898 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001900 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 return 0;
1902 }
1903
1904 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001905 {
1906 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001908 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001909
1910 if (di == NULL)
1911 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001912 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001913 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001914 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001915 PyErr_NoMemory();
1916 return -1;
1917 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001918 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001919
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001920 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001921 {
1922 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001923 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001924 RAISE_KEY_ADD_FAIL(key);
1925 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001926 return -1;
1927 }
1928 }
1929 else
1930 clear_tv(&di->di_tv);
1931
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001932 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001933
1934 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001935 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001936 return 0;
1937}
1938
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939typedef PyObject *(*hi_to_py)(hashitem_T *);
1940
Bram Moolenaardb913952012-06-29 12:54:53 +02001941 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001942DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001943{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001944 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001945 long_u todo = dict->dv_hashtab.ht_used;
1946 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001947 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001948 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001950
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001951 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001952 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1953 {
1954 if (!HASHITEM_EMPTY(hi))
1955 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001956 if (!(newObj = hiconvert(hi)))
1957 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001958 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001959 return NULL;
1960 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001961 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001962 --todo;
1963 ++i;
1964 }
1965 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001966 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001967}
1968
Bram Moolenaara9922d62013-05-30 13:01:18 +02001969 static PyObject *
1970dict_key(hashitem_T *hi)
1971{
1972 return PyBytes_FromString((char *)(hi->hi_key));
1973}
1974
1975 static PyObject *
1976DictionaryListKeys(DictionaryObject *self)
1977{
1978 return DictionaryListObjects(self, dict_key);
1979}
1980
1981 static PyObject *
1982dict_val(hashitem_T *hi)
1983{
1984 dictitem_T *di;
1985
1986 di = dict_lookup(hi);
1987 return ConvertToPyObject(&di->di_tv);
1988}
1989
1990 static PyObject *
1991DictionaryListValues(DictionaryObject *self)
1992{
1993 return DictionaryListObjects(self, dict_val);
1994}
1995
1996 static PyObject *
1997dict_item(hashitem_T *hi)
1998{
1999 PyObject *keyObject;
2000 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002001 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002002
2003 if (!(keyObject = dict_key(hi)))
2004 return NULL;
2005
2006 if (!(valObject = dict_val(hi)))
2007 {
2008 Py_DECREF(keyObject);
2009 return NULL;
2010 }
2011
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002012 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002013
2014 Py_DECREF(keyObject);
2015 Py_DECREF(valObject);
2016
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002017 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002018}
2019
2020 static PyObject *
2021DictionaryListItems(DictionaryObject *self)
2022{
2023 return DictionaryListObjects(self, dict_item);
2024}
2025
2026 static PyObject *
2027DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2028{
2029 dict_T *dict = self->dict;
2030
2031 if (dict->dv_lock)
2032 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002033 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002034 return NULL;
2035 }
2036
2037 if (kwargs)
2038 {
2039 typval_T tv;
2040
2041 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2042 return NULL;
2043
2044 VimTryStart();
2045 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2046 clear_tv(&tv);
2047 if (VimTryEnd())
2048 return NULL;
2049 }
2050 else
2051 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002052 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002053
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002054 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 return NULL;
2056
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002057 if (obj == NULL)
2058 {
2059 Py_INCREF(Py_None);
2060 return Py_None;
2061 }
2062
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002063 if (PyObject_HasAttrString(obj, "keys"))
2064 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002065 else
2066 {
2067 PyObject *iterator;
2068 PyObject *item;
2069
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002070 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002071 return NULL;
2072
2073 while ((item = PyIter_Next(iterator)))
2074 {
2075 PyObject *fast;
2076 PyObject *keyObject;
2077 PyObject *valObject;
2078 PyObject *todecref;
2079 char_u *key;
2080 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002081 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002082
2083 if (!(fast = PySequence_Fast(item, "")))
2084 {
2085 Py_DECREF(iterator);
2086 Py_DECREF(item);
2087 return NULL;
2088 }
2089
2090 Py_DECREF(item);
2091
2092 if (PySequence_Fast_GET_SIZE(fast) != 2)
2093 {
2094 Py_DECREF(iterator);
2095 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002096 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002097 N_("expected sequence element of size 2, "
2098 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002099 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002100 return NULL;
2101 }
2102
2103 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2104
2105 if (!(key = StringToChars(keyObject, &todecref)))
2106 {
2107 Py_DECREF(iterator);
2108 Py_DECREF(fast);
2109 return NULL;
2110 }
2111
2112 di = dictitem_alloc(key);
2113
2114 Py_XDECREF(todecref);
2115
2116 if (di == NULL)
2117 {
2118 Py_DECREF(fast);
2119 Py_DECREF(iterator);
2120 PyErr_NoMemory();
2121 return NULL;
2122 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002123 di->di_tv.v_type = VAR_UNKNOWN;
2124
2125 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2126
2127 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2128 {
2129 Py_DECREF(iterator);
2130 Py_DECREF(fast);
2131 dictitem_free(di);
2132 return NULL;
2133 }
2134
2135 Py_DECREF(fast);
2136
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002137 hi = hash_find(&dict->dv_hashtab, di->di_key);
2138 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002139 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002140 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002141 Py_DECREF(iterator);
2142 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002143 return NULL;
2144 }
2145 }
2146
2147 Py_DECREF(iterator);
2148
2149 /* Iterator may have finished due to an exception */
2150 if (PyErr_Occurred())
2151 return NULL;
2152 }
2153 }
2154 Py_INCREF(Py_None);
2155 return Py_None;
2156}
2157
2158 static PyObject *
2159DictionaryGet(DictionaryObject *self, PyObject *args)
2160{
2161 return _DictionaryItem(self, args,
2162 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2163}
2164
2165 static PyObject *
2166DictionaryPop(DictionaryObject *self, PyObject *args)
2167{
2168 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2169}
2170
2171 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002172DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002173{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002174 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002175 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002176 PyObject *valObject;
2177 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002178
Bram Moolenaarde71b562013-06-02 17:41:54 +02002179 if (self->dict->dv_hashtab.ht_used == 0)
2180 {
2181 PyErr_SetNone(PyExc_KeyError);
2182 return NULL;
2183 }
2184
2185 hi = self->dict->dv_hashtab.ht_array;
2186 while (HASHITEM_EMPTY(hi))
2187 ++hi;
2188
2189 di = dict_lookup(hi);
2190
2191 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002192 return NULL;
2193
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002194 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002195 {
2196 Py_DECREF(valObject);
2197 return NULL;
2198 }
2199
2200 hash_remove(&self->dict->dv_hashtab, hi);
2201 dictitem_free(di);
2202
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002203 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002204}
2205
2206 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002207DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002208{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002209 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2210}
2211
2212static PySequenceMethods DictionaryAsSeq = {
2213 0, /* sq_length */
2214 0, /* sq_concat */
2215 0, /* sq_repeat */
2216 0, /* sq_item */
2217 0, /* sq_slice */
2218 0, /* sq_ass_item */
2219 0, /* sq_ass_slice */
2220 (objobjproc) DictionaryContains, /* sq_contains */
2221 0, /* sq_inplace_concat */
2222 0, /* sq_inplace_repeat */
2223};
2224
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002225static PyMappingMethods DictionaryAsMapping = {
2226 (lenfunc) DictionaryLength,
2227 (binaryfunc) DictionaryItem,
2228 (objobjargproc) DictionaryAssItem,
2229};
2230
Bram Moolenaardb913952012-06-29 12:54:53 +02002231static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002232 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002233 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2234 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2235 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2236 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2237 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002238 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002239 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002240 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2241 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002242};
2243
2244static PyTypeObject ListType;
2245
2246typedef struct
2247{
2248 PyObject_HEAD
2249 list_T *list;
2250 pylinkedlist_T ref;
2251} ListObject;
2252
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002253#define NEW_LIST(list) ListNew(&ListType, list)
2254
Bram Moolenaardb913952012-06-29 12:54:53 +02002255 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002256ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002257{
2258 ListObject *self;
2259
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002260 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002261 if (self == NULL)
2262 return NULL;
2263 self->list = list;
2264 ++list->lv_refcount;
2265
2266 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2267
2268 return (PyObject *)(self);
2269}
2270
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002271 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002272py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002273{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002274 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002275
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002276 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002277 {
2278 PyErr_NoMemory();
2279 return NULL;
2280 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002281 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002282
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002283 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002284}
2285
2286 static int
2287list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2288{
2289 PyObject *iterator;
2290 PyObject *item;
2291 listitem_T *li;
2292
2293 if (!(iterator = PyObject_GetIter(obj)))
2294 return -1;
2295
2296 while ((item = PyIter_Next(iterator)))
2297 {
2298 if (!(li = listitem_alloc()))
2299 {
2300 PyErr_NoMemory();
2301 Py_DECREF(item);
2302 Py_DECREF(iterator);
2303 return -1;
2304 }
2305 li->li_tv.v_lock = 0;
2306 li->li_tv.v_type = VAR_UNKNOWN;
2307
2308 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2309 {
2310 Py_DECREF(item);
2311 Py_DECREF(iterator);
2312 listitem_free(li);
2313 return -1;
2314 }
2315
2316 Py_DECREF(item);
2317
2318 list_append(l, li);
2319 }
2320
2321 Py_DECREF(iterator);
2322
2323 /* Iterator may have finished due to an exception */
2324 if (PyErr_Occurred())
2325 return -1;
2326
2327 return 0;
2328}
2329
2330 static PyObject *
2331ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2332{
2333 list_T *list;
2334 PyObject *obj = NULL;
2335
2336 if (kwargs)
2337 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002338 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002339 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002340 return NULL;
2341 }
2342
2343 if (!PyArg_ParseTuple(args, "|O", &obj))
2344 return NULL;
2345
2346 if (!(list = py_list_alloc()))
2347 return NULL;
2348
2349 if (obj)
2350 {
2351 PyObject *lookup_dict;
2352
2353 if (!(lookup_dict = PyDict_New()))
2354 {
2355 list_unref(list);
2356 return NULL;
2357 }
2358
2359 if (list_py_concat(list, obj, lookup_dict) == -1)
2360 {
2361 Py_DECREF(lookup_dict);
2362 list_unref(list);
2363 return NULL;
2364 }
2365
2366 Py_DECREF(lookup_dict);
2367 }
2368
2369 return ListNew(subtype, list);
2370}
2371
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002372 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002373ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002374{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002375 pyll_remove(&self->ref, &lastlist);
2376 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002377
2378 DESTRUCTOR_FINISH(self);
2379}
2380
Bram Moolenaardb913952012-06-29 12:54:53 +02002381 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002382ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002383{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002384 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002385}
2386
2387 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002388ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002389{
2390 listitem_T *li;
2391
Bram Moolenaard6e39182013-05-21 18:30:34 +02002392 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002393 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002394 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002395 return NULL;
2396 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002397 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 if (li == NULL)
2399 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002400 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002401 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002402 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002403 return NULL;
2404 }
2405 return ConvertToPyObject(&li->li_tv);
2406}
2407
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002409ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2410 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002411{
2412 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002413 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002414
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002415 if (step == 0)
2416 {
2417 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2418 return NULL;
2419 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002420
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002421 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002422 if (list == NULL)
2423 return NULL;
2424
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002425 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002426 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002427 PyObject *item;
2428
2429 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002430 if (item == NULL)
2431 {
2432 Py_DECREF(list);
2433 return NULL;
2434 }
2435
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002436 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002437 }
2438
2439 return list;
2440}
2441
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002442 static PyObject *
2443ListItem(ListObject *self, PyObject* idx)
2444{
2445#if PY_MAJOR_VERSION < 3
2446 if (PyInt_Check(idx))
2447 {
2448 long _idx = PyInt_AsLong(idx);
2449 return ListIndex(self, _idx);
2450 }
2451 else
2452#endif
2453 if (PyLong_Check(idx))
2454 {
2455 long _idx = PyLong_AsLong(idx);
2456 return ListIndex(self, _idx);
2457 }
2458 else if (PySlice_Check(idx))
2459 {
2460 Py_ssize_t start, stop, step, slicelen;
2461
Bram Moolenaar922a4662014-03-30 16:11:43 +02002462 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002463 &start, &stop, &step, &slicelen) < 0)
2464 return NULL;
2465 return ListSlice(self, start, step, slicelen);
2466 }
2467 else
2468 {
2469 RAISE_INVALID_INDEX_TYPE(idx);
2470 return NULL;
2471 }
2472}
2473
2474 static void
2475list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2476 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2477{
2478 while (numreplaced--)
2479 {
2480 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2481 listitem_remove(l, lis[slicelen + numreplaced]);
2482 }
2483 while (numadded--)
2484 {
2485 listitem_T *next;
2486
2487 next = lastaddedli->li_prev;
2488 listitem_remove(l, lastaddedli);
2489 lastaddedli = next;
2490 }
2491}
2492
2493 static int
2494ListAssSlice(ListObject *self, Py_ssize_t first,
2495 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2496{
2497 PyObject *iterator;
2498 PyObject *item;
2499 listitem_T *li;
2500 listitem_T *lastaddedli = NULL;
2501 listitem_T *next;
2502 typval_T v;
2503 list_T *l = self->list;
2504 PyInt i;
2505 PyInt j;
2506 PyInt numreplaced = 0;
2507 PyInt numadded = 0;
2508 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002509 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002510
2511 size = ListLength(self);
2512
2513 if (l->lv_lock)
2514 {
2515 RAISE_LOCKED_LIST;
2516 return -1;
2517 }
2518
2519 if (step == 0)
2520 {
2521 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2522 return -1;
2523 }
2524
2525 if (step != 1 && slicelen == 0)
2526 {
2527 /* Nothing to do. Only error out if obj has some items. */
2528 int ret = 0;
2529
2530 if (obj == NULL)
2531 return 0;
2532
2533 if (!(iterator = PyObject_GetIter(obj)))
2534 return -1;
2535
2536 if ((item = PyIter_Next(iterator)))
2537 {
2538 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002539 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002540 "to extended slice"), 0);
2541 Py_DECREF(item);
2542 ret = -1;
2543 }
2544 Py_DECREF(iterator);
2545 return ret;
2546 }
2547
2548 if (obj != NULL)
2549 /* XXX May allocate zero bytes. */
2550 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2551 {
2552 PyErr_NoMemory();
2553 return -1;
2554 }
2555
2556 if (first == size)
2557 li = NULL;
2558 else
2559 {
2560 li = list_find(l, (long) first);
2561 if (li == NULL)
2562 {
2563 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2564 (int)first);
2565 if (obj != NULL)
2566 PyMem_Free(lis);
2567 return -1;
2568 }
2569 i = slicelen;
2570 while (i-- && li != NULL)
2571 {
2572 j = step;
2573 next = li;
2574 if (step > 0)
2575 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2576 else
2577 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2578
2579 if (obj == NULL)
2580 listitem_remove(l, li);
2581 else
2582 lis[slicelen - i - 1] = li;
2583
2584 li = next;
2585 }
2586 if (li == NULL && i != -1)
2587 {
2588 PyErr_SET_VIM(N_("internal error: not enough list items"));
2589 if (obj != NULL)
2590 PyMem_Free(lis);
2591 return -1;
2592 }
2593 }
2594
2595 if (obj == NULL)
2596 return 0;
2597
2598 if (!(iterator = PyObject_GetIter(obj)))
2599 {
2600 PyMem_Free(lis);
2601 return -1;
2602 }
2603
2604 i = 0;
2605 while ((item = PyIter_Next(iterator)))
2606 {
2607 if (ConvertFromPyObject(item, &v) == -1)
2608 {
2609 Py_DECREF(iterator);
2610 Py_DECREF(item);
2611 PyMem_Free(lis);
2612 return -1;
2613 }
2614 Py_DECREF(item);
2615 if (list_insert_tv(l, &v, numreplaced < slicelen
2616 ? lis[numreplaced]
2617 : li) == FAIL)
2618 {
2619 clear_tv(&v);
2620 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2621 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2622 PyMem_Free(lis);
2623 return -1;
2624 }
2625 if (numreplaced < slicelen)
2626 {
2627 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002628 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002629 numreplaced++;
2630 }
2631 else
2632 {
2633 if (li)
2634 lastaddedli = li->li_prev;
2635 else
2636 lastaddedli = l->lv_last;
2637 numadded++;
2638 }
2639 clear_tv(&v);
2640 if (step != 1 && i >= slicelen)
2641 {
2642 Py_DECREF(iterator);
2643 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002644 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002645 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002646 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2647 PyMem_Free(lis);
2648 return -1;
2649 }
2650 ++i;
2651 }
2652 Py_DECREF(iterator);
2653
2654 if (step != 1 && i != slicelen)
2655 {
2656 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002657 N_("attempt to assign sequence of size %d to extended slice "
2658 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002659 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2660 PyMem_Free(lis);
2661 return -1;
2662 }
2663
2664 if (PyErr_Occurred())
2665 {
2666 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2667 PyMem_Free(lis);
2668 return -1;
2669 }
2670
2671 for (i = 0; i < numreplaced; i++)
2672 listitem_free(lis[i]);
2673 if (step == 1)
2674 for (i = numreplaced; i < slicelen; i++)
2675 listitem_remove(l, lis[i]);
2676
2677 PyMem_Free(lis);
2678
2679 return 0;
2680}
2681
2682 static int
2683ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2684{
2685 typval_T tv;
2686 list_T *l = self->list;
2687 listitem_T *li;
2688 Py_ssize_t length = ListLength(self);
2689
2690 if (l->lv_lock)
2691 {
2692 RAISE_LOCKED_LIST;
2693 return -1;
2694 }
2695 if (index > length || (index == length && obj == NULL))
2696 {
2697 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2698 return -1;
2699 }
2700
2701 if (obj == NULL)
2702 {
2703 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002704 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002705 clear_tv(&li->li_tv);
2706 vim_free(li);
2707 return 0;
2708 }
2709
2710 if (ConvertFromPyObject(obj, &tv) == -1)
2711 return -1;
2712
2713 if (index == length)
2714 {
2715 if (list_append_tv(l, &tv) == FAIL)
2716 {
2717 clear_tv(&tv);
2718 PyErr_SET_VIM(N_("failed to add item to list"));
2719 return -1;
2720 }
2721 }
2722 else
2723 {
2724 li = list_find(l, (long) index);
2725 clear_tv(&li->li_tv);
2726 copy_tv(&tv, &li->li_tv);
2727 clear_tv(&tv);
2728 }
2729 return 0;
2730}
2731
2732 static Py_ssize_t
2733ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2734{
2735#if PY_MAJOR_VERSION < 3
2736 if (PyInt_Check(idx))
2737 {
2738 long _idx = PyInt_AsLong(idx);
2739 return ListAssIndex(self, _idx, obj);
2740 }
2741 else
2742#endif
2743 if (PyLong_Check(idx))
2744 {
2745 long _idx = PyLong_AsLong(idx);
2746 return ListAssIndex(self, _idx, obj);
2747 }
2748 else if (PySlice_Check(idx))
2749 {
2750 Py_ssize_t start, stop, step, slicelen;
2751
Bram Moolenaar922a4662014-03-30 16:11:43 +02002752 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002753 &start, &stop, &step, &slicelen) < 0)
2754 return -1;
2755 return ListAssSlice(self, start, step, slicelen,
2756 obj);
2757 }
2758 else
2759 {
2760 RAISE_INVALID_INDEX_TYPE(idx);
2761 return -1;
2762 }
2763}
2764
2765 static PyObject *
2766ListConcatInPlace(ListObject *self, PyObject *obj)
2767{
2768 list_T *l = self->list;
2769 PyObject *lookup_dict;
2770
2771 if (l->lv_lock)
2772 {
2773 RAISE_LOCKED_LIST;
2774 return NULL;
2775 }
2776
2777 if (!(lookup_dict = PyDict_New()))
2778 return NULL;
2779
2780 if (list_py_concat(l, obj, lookup_dict) == -1)
2781 {
2782 Py_DECREF(lookup_dict);
2783 return NULL;
2784 }
2785 Py_DECREF(lookup_dict);
2786
2787 Py_INCREF(self);
2788 return (PyObject *)(self);
2789}
2790
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002791typedef struct
2792{
2793 listwatch_T lw;
2794 list_T *list;
2795} listiterinfo_T;
2796
2797 static void
2798ListIterDestruct(listiterinfo_T *lii)
2799{
2800 list_rem_watch(lii->list, &lii->lw);
2801 PyMem_Free(lii);
2802}
2803
2804 static PyObject *
2805ListIterNext(listiterinfo_T **lii)
2806{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002807 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002808
2809 if (!((*lii)->lw.lw_item))
2810 return NULL;
2811
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002812 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002813 return NULL;
2814
2815 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2816
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002817 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002818}
2819
2820 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002821ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002822{
2823 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002824 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002825
2826 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2827 {
2828 PyErr_NoMemory();
2829 return NULL;
2830 }
2831
2832 list_add_watch(l, &lii->lw);
2833 lii->lw.lw_item = l->lv_first;
2834 lii->list = l;
2835
2836 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002837 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2838 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002839}
2840
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002841static char *ListAttrs[] = {
2842 "locked",
2843 NULL
2844};
2845
2846 static PyObject *
2847ListDir(PyObject *self)
2848{
2849 return ObjectDir(self, ListAttrs);
2850}
2851
Bram Moolenaar66b79852012-09-21 14:00:35 +02002852 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002853ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002854{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002855 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002856 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002857 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002858 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002859 return -1;
2860 }
2861
2862 if (strcmp(name, "locked") == 0)
2863 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002864 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002865 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002866 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002867 return -1;
2868 }
2869 else
2870 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002872 if (istrue == -1)
2873 return -1;
2874 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002875 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002876 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002877 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002878 }
2879 return 0;
2880 }
2881 else
2882 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002883 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002884 return -1;
2885 }
2886}
2887
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002888static PySequenceMethods ListAsSeq = {
2889 (lenfunc) ListLength, /* sq_length, len(x) */
2890 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2891 0, /* RangeRepeat, sq_repeat, x*n */
2892 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2893 0, /* was_sq_slice, x[i:j] */
2894 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2895 0, /* was_sq_ass_slice, x[i:j]=v */
2896 0, /* sq_contains */
2897 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2898 0, /* sq_inplace_repeat */
2899};
2900
2901static PyMappingMethods ListAsMapping = {
2902 /* mp_length */ (lenfunc) ListLength,
2903 /* mp_subscript */ (binaryfunc) ListItem,
2904 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2905};
2906
Bram Moolenaardb913952012-06-29 12:54:53 +02002907static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002908 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2909 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2910 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002911};
2912
2913typedef struct
2914{
2915 PyObject_HEAD
2916 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002917 int argc;
2918 typval_T *argv;
2919 dict_T *self;
2920 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002921 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002922} FunctionObject;
2923
2924static PyTypeObject FunctionType;
2925
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002926#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2927 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002928
Bram Moolenaardb913952012-06-29 12:54:53 +02002929 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002930FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002931 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002932{
2933 FunctionObject *self;
2934
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002935 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002936 if (self == NULL)
2937 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002938
2939 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002940 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002941 if (!translated_function_exists(name))
2942 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002943 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002944 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002945 return NULL;
2946 }
2947 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002948 }
2949 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002950 {
2951 char_u *p;
2952
2953 if ((p = get_expanded_name(name,
2954 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002955 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002956 PyErr_FORMAT(PyExc_ValueError,
2957 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002958 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002959 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002960
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002961 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2962 {
2963 char_u *np;
2964 size_t len = STRLEN(p) + 1;
2965
Bram Moolenaar80dae042018-12-23 13:36:40 +01002966 if ((np = alloc((int)len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002967 {
2968 vim_free(p);
2969 return NULL;
2970 }
2971 mch_memmove(np, "<SNR>", 5);
2972 mch_memmove(np + 5, p + 3, len - 3);
2973 vim_free(p);
2974 self->name = np;
2975 }
2976 else
2977 self->name = p;
2978 }
2979
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002980 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002981 self->argc = argc;
2982 self->argv = argv;
2983 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002984 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002985
2986 if (self->argv || self->self)
2987 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2988
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002989 return (PyObject *)(self);
2990}
2991
2992 static PyObject *
2993FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2994{
2995 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002996 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002997 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002998 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002999 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003000 typval_T selfdicttv;
3001 typval_T argstv;
3002 list_T *argslist = NULL;
3003 dict_T *selfdict = NULL;
3004 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003005 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003006 typval_T *argv = NULL;
3007 typval_T *curtv;
3008 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003009
Bram Moolenaar8110a092016-04-14 15:56:09 +02003010 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003011 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003012 selfdictObject = PyDict_GetItemString(kwargs, "self");
3013 if (selfdictObject != NULL)
3014 {
3015 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3016 return NULL;
3017 selfdict = selfdicttv.vval.v_dict;
3018 }
3019 argsObject = PyDict_GetItemString(kwargs, "args");
3020 if (argsObject != NULL)
3021 {
3022 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3023 {
3024 dict_unref(selfdict);
3025 return NULL;
3026 }
3027 argslist = argstv.vval.v_list;
3028
3029 argc = argslist->lv_len;
3030 if (argc != 0)
3031 {
3032 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003033 if (argv == NULL)
3034 {
3035 PyErr_NoMemory();
3036 dict_unref(selfdict);
3037 list_unref(argslist);
3038 return NULL;
3039 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003040 curtv = argv;
3041 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3042 copy_tv(&li->li_tv, curtv++);
3043 }
3044 list_unref(argslist);
3045 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003046 if (selfdict != NULL)
3047 {
3048 auto_rebind = FALSE;
3049 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3050 if (autoRebindObject != NULL)
3051 {
3052 auto_rebind = PyObject_IsTrue(autoRebindObject);
3053 if (auto_rebind == -1)
3054 {
3055 dict_unref(selfdict);
3056 list_unref(argslist);
3057 return NULL;
3058 }
3059 }
3060 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003061 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003062
Bram Moolenaar389a1792013-06-23 13:00:44 +02003063 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003064 {
3065 dict_unref(selfdict);
3066 while (argc--)
3067 clear_tv(&argv[argc]);
3068 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003069 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003070 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003071
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003072 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003073
Bram Moolenaar389a1792013-06-23 13:00:44 +02003074 PyMem_Free(name);
3075
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003076 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003077}
3078
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003079 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003080FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003081{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003082 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003083 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003084 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003085 for (i = 0; i < self->argc; ++i)
3086 clear_tv(&self->argv[i]);
3087 PyMem_Free(self->argv);
3088 dict_unref(self->self);
3089 if (self->argv || self->self)
3090 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003091
3092 DESTRUCTOR_FINISH(self);
3093}
3094
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003095static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003096 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003097 NULL
3098};
3099
3100 static PyObject *
3101FunctionDir(PyObject *self)
3102{
3103 return ObjectDir(self, FunctionAttrs);
3104}
3105
Bram Moolenaardb913952012-06-29 12:54:53 +02003106 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003107FunctionAttr(FunctionObject *self, char *name)
3108{
3109 list_T *list;
3110 int i;
3111 if (strcmp(name, "name") == 0)
3112 return PyString_FromString((char *)(self->name));
3113 else if (strcmp(name, "args") == 0)
3114 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003115 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003116 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003117
Bram Moolenaar8110a092016-04-14 15:56:09 +02003118 for (i = 0; i < self->argc; ++i)
3119 list_append_tv(list, &self->argv[i]);
3120 return NEW_LIST(list);
3121 }
3122 else if (strcmp(name, "self") == 0)
3123 return self->self == NULL
3124 ? AlwaysNone(NULL)
3125 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003126 else if (strcmp(name, "auto_rebind") == 0)
3127 return self->auto_rebind
3128 ? AlwaysTrue(NULL)
3129 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003130 else if (strcmp(name, "__members__") == 0)
3131 return ObjectDir(NULL, FunctionAttrs);
3132 return NULL;
3133}
3134
3135/* Populate partial_T given function object.
3136 *
3137 * "exported" should be set to true when it is needed to construct a partial
3138 * that may be stored in a variable (i.e. may be freed by Vim).
3139 */
3140 static void
3141set_partial(FunctionObject *self, partial_T *pt, int exported)
3142{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003143 int i;
3144
3145 pt->pt_name = self->name;
3146 if (self->argv)
3147 {
3148 pt->pt_argc = self->argc;
3149 if (exported)
3150 {
3151 pt->pt_argv = (typval_T *)alloc_clear(
3152 sizeof(typval_T) * self->argc);
3153 for (i = 0; i < pt->pt_argc; ++i)
3154 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3155 }
3156 else
3157 pt->pt_argv = self->argv;
3158 }
3159 else
3160 {
3161 pt->pt_argc = 0;
3162 pt->pt_argv = NULL;
3163 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003164 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003165 pt->pt_dict = self->self;
3166 if (exported && self->self)
3167 ++pt->pt_dict->dv_refcount;
3168 if (exported)
3169 pt->pt_name = vim_strsave(pt->pt_name);
3170 pt->pt_refcount = 1;
3171}
3172
3173 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003174FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003175{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003176 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003177 typval_T args;
3178 typval_T selfdicttv;
3179 typval_T rettv;
3180 dict_T *selfdict = NULL;
3181 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003182 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003183 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003184 partial_T pt;
3185 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003186
Bram Moolenaar8110a092016-04-14 15:56:09 +02003187 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003188 return NULL;
3189
3190 if (kwargs != NULL)
3191 {
3192 selfdictObject = PyDict_GetItemString(kwargs, "self");
3193 if (selfdictObject != NULL)
3194 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003195 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003196 {
3197 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003198 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003199 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003200 selfdict = selfdicttv.vval.v_dict;
3201 }
3202 }
3203
Bram Moolenaar8110a092016-04-14 15:56:09 +02003204 if (self->argv || self->self)
3205 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003206 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003207 set_partial(self, &pt, FALSE);
3208 pt_ptr = &pt;
3209 }
3210
Bram Moolenaar71700b82013-05-15 17:49:05 +02003211 Py_BEGIN_ALLOW_THREADS
3212 Python_Lock_Vim();
3213
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003214 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003215 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003216
3217 Python_Release_Vim();
3218 Py_END_ALLOW_THREADS
3219
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003220 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003221 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003222 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003223 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003224 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003225 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003226 }
3227 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003228 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003229
Bram Moolenaardb913952012-06-29 12:54:53 +02003230 clear_tv(&args);
3231 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003232 if (selfdict != NULL)
3233 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003234
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003235 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003236}
3237
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003238 static PyObject *
3239FunctionRepr(FunctionObject *self)
3240{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003241 PyObject *ret;
3242 garray_T repr_ga;
3243 int i;
3244 char_u *tofree = NULL;
3245 typval_T tv;
3246 char_u numbuf[NUMBUFLEN];
3247
3248 ga_init2(&repr_ga, (int)sizeof(char), 70);
3249 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3250 if (self->name)
3251 ga_concat(&repr_ga, self->name);
3252 else
3253 ga_concat(&repr_ga, (char_u *)"<NULL>");
3254 ga_append(&repr_ga, '\'');
3255 if (self->argv)
3256 {
3257 ga_concat(&repr_ga, (char_u *)", args=[");
3258 ++emsg_silent;
3259 for (i = 0; i < self->argc; i++)
3260 {
3261 if (i != 0)
3262 ga_concat(&repr_ga, (char_u *)", ");
3263 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3264 get_copyID()));
3265 vim_free(tofree);
3266 }
3267 --emsg_silent;
3268 ga_append(&repr_ga, ']');
3269 }
3270 if (self->self)
3271 {
3272 ga_concat(&repr_ga, (char_u *)", self=");
3273 tv.v_type = VAR_DICT;
3274 tv.vval.v_dict = self->self;
3275 ++emsg_silent;
3276 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3277 --emsg_silent;
3278 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003279 if (self->auto_rebind)
3280 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003281 }
3282 ga_append(&repr_ga, '>');
3283 ret = PyString_FromString((char *)repr_ga.ga_data);
3284 ga_clear(&repr_ga);
3285 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003286}
3287
Bram Moolenaardb913952012-06-29 12:54:53 +02003288static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003289 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3290 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003291};
3292
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003293/*
3294 * Options object
3295 */
3296
3297static PyTypeObject OptionsType;
3298
3299typedef int (*checkfun)(void *);
3300
3301typedef struct
3302{
3303 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003304 int opt_type;
3305 void *from;
3306 checkfun Check;
3307 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003308} OptionsObject;
3309
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003310 static int
3311dummy_check(void *arg UNUSED)
3312{
3313 return 0;
3314}
3315
3316 static PyObject *
3317OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3318{
3319 OptionsObject *self;
3320
Bram Moolenaar774267b2013-05-21 20:51:59 +02003321 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003322 if (self == NULL)
3323 return NULL;
3324
3325 self->opt_type = opt_type;
3326 self->from = from;
3327 self->Check = Check;
3328 self->fromObj = fromObj;
3329 if (fromObj)
3330 Py_INCREF(fromObj);
3331
3332 return (PyObject *)(self);
3333}
3334
3335 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003337{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003338 PyObject_GC_UnTrack((void *)(self));
3339 Py_XDECREF(self->fromObj);
3340 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341}
3342
3343 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003345{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003346 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003347 return 0;
3348}
3349
3350 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003351OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003352{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003353 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003354 return 0;
3355}
3356
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003357 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003358OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003359{
3360 char_u *key;
3361 int flags;
3362 long numval;
3363 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003364 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003365
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003367 return NULL;
3368
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003369 if (!(key = StringToChars(keyObject, &todecref)))
3370 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003371
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003372 if (*key == NUL)
3373 {
3374 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003375 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003376 return NULL;
3377 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003378
3379 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003380 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003381
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003382 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003383
3384 if (flags == 0)
3385 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003386 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003387 return NULL;
3388 }
3389
3390 if (flags & SOPT_UNSET)
3391 {
3392 Py_INCREF(Py_None);
3393 return Py_None;
3394 }
3395 else if (flags & SOPT_BOOL)
3396 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003397 PyObject *ret;
3398 ret = numval ? Py_True : Py_False;
3399 Py_INCREF(ret);
3400 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003401 }
3402 else if (flags & SOPT_NUM)
3403 return PyInt_FromLong(numval);
3404 else if (flags & SOPT_STRING)
3405 {
3406 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003407 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003408 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003409 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003410 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003411 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003412 else
3413 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003414 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003415 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003416 return NULL;
3417 }
3418 }
3419 else
3420 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003421 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003422 return NULL;
3423 }
3424}
3425
3426 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003427OptionsContains(OptionsObject *self, PyObject *keyObject)
3428{
3429 char_u *key;
3430 PyObject *todecref;
3431
3432 if (!(key = StringToChars(keyObject, &todecref)))
3433 return -1;
3434
3435 if (*key == NUL)
3436 {
3437 Py_XDECREF(todecref);
3438 return 0;
3439 }
3440
3441 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3442 {
3443 Py_XDECREF(todecref);
3444 return 1;
3445 }
3446 else
3447 {
3448 Py_XDECREF(todecref);
3449 return 0;
3450 }
3451}
3452
3453typedef struct
3454{
3455 void *lastoption;
3456 int opt_type;
3457} optiterinfo_T;
3458
3459 static PyObject *
3460OptionsIterNext(optiterinfo_T **oii)
3461{
3462 char_u *name;
3463
3464 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3465 return PyString_FromString((char *)name);
3466
3467 return NULL;
3468}
3469
3470 static PyObject *
3471OptionsIter(OptionsObject *self)
3472{
3473 optiterinfo_T *oii;
3474
3475 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3476 {
3477 PyErr_NoMemory();
3478 return NULL;
3479 }
3480
3481 oii->opt_type = self->opt_type;
3482 oii->lastoption = NULL;
3483
3484 return IterNew(oii,
3485 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3486 NULL, NULL);
3487}
3488
3489 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003490set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003491{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003492 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003493
3494 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3495 {
3496 if (VimTryEnd())
3497 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003498 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003499 return FAIL;
3500 }
3501 return OK;
3502}
3503
3504 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003505set_option_value_for(
3506 char_u *key,
3507 int numval,
3508 char_u *stringval,
3509 int opt_flags,
3510 int opt_type,
3511 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003512{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003513 win_T *save_curwin = NULL;
3514 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003515 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003516 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003517
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003518 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003519 switch (opt_type)
3520 {
3521 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003522 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003523 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003524 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003525 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003526 if (VimTryEnd())
3527 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003528 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003529 return -1;
3530 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003531 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3532 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003533 break;
3534 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003535 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003536 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003537 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003538 break;
3539 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003540 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003541 break;
3542 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003543 if (set_ret == FAIL)
3544 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003545 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003546}
3547
3548 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003549OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003550{
3551 char_u *key;
3552 int flags;
3553 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003554 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003555 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003556
Bram Moolenaard6e39182013-05-21 18:30:34 +02003557 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003558 return -1;
3559
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003560 if (!(key = StringToChars(keyObject, &todecref)))
3561 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003562
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003563 if (*key == NUL)
3564 {
3565 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003566 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003567 return -1;
3568 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003569
3570 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003571 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003572
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003573 if (flags == 0)
3574 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003575 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003576 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577 return -1;
3578 }
3579
3580 if (valObject == NULL)
3581 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003582 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003583 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003584 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003585 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003586 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003587 return -1;
3588 }
3589 else if (!(flags & SOPT_GLOBAL))
3590 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003591 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003592 N_("unable to unset option %s "
3593 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003594 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003595 return -1;
3596 }
3597 else
3598 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003599 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003600 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003601 return 0;
3602 }
3603 }
3604
Bram Moolenaard6e39182013-05-21 18:30:34 +02003605 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003606
3607 if (flags & SOPT_BOOL)
3608 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003609 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003610
Bram Moolenaarb983f752013-05-15 16:11:50 +02003611 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003612 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003613 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003614 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003615 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003616 }
3617 else if (flags & SOPT_NUM)
3618 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003619 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003620
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003621 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003622 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003623 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003624 return -1;
3625 }
3626
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003627 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003628 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003629 }
3630 else
3631 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003632 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003633 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003634
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003635 if ((val = StringToChars(valObject, &todecref2)))
3636 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003637 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003638 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003639 Py_XDECREF(todecref2);
3640 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003641 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003642 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003643 }
3644
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003645 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003646
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003647 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003648}
3649
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003650static PySequenceMethods OptionsAsSeq = {
3651 0, /* sq_length */
3652 0, /* sq_concat */
3653 0, /* sq_repeat */
3654 0, /* sq_item */
3655 0, /* sq_slice */
3656 0, /* sq_ass_item */
3657 0, /* sq_ass_slice */
3658 (objobjproc) OptionsContains, /* sq_contains */
3659 0, /* sq_inplace_concat */
3660 0, /* sq_inplace_repeat */
3661};
3662
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003663static PyMappingMethods OptionsAsMapping = {
3664 (lenfunc) NULL,
3665 (binaryfunc) OptionsItem,
3666 (objobjargproc) OptionsAssItem,
3667};
3668
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003669/* Tabpage object
3670 */
3671
3672typedef struct
3673{
3674 PyObject_HEAD
3675 tabpage_T *tab;
3676} TabPageObject;
3677
3678static PyObject *WinListNew(TabPageObject *tabObject);
3679
3680static PyTypeObject TabPageType;
3681
3682 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003683CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003684{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003685 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003686 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003687 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003688 return -1;
3689 }
3690
3691 return 0;
3692}
3693
3694 static PyObject *
3695TabPageNew(tabpage_T *tab)
3696{
3697 TabPageObject *self;
3698
3699 if (TAB_PYTHON_REF(tab))
3700 {
3701 self = TAB_PYTHON_REF(tab);
3702 Py_INCREF(self);
3703 }
3704 else
3705 {
3706 self = PyObject_NEW(TabPageObject, &TabPageType);
3707 if (self == NULL)
3708 return NULL;
3709 self->tab = tab;
3710 TAB_PYTHON_REF(tab) = self;
3711 }
3712
3713 return (PyObject *)(self);
3714}
3715
3716 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003717TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003718{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003719 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3720 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003721
3722 DESTRUCTOR_FINISH(self);
3723}
3724
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003725static char *TabPageAttrs[] = {
3726 "windows", "number", "vars", "window", "valid",
3727 NULL
3728};
3729
3730 static PyObject *
3731TabPageDir(PyObject *self)
3732{
3733 return ObjectDir(self, TabPageAttrs);
3734}
3735
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003736 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003737TabPageAttrValid(TabPageObject *self, char *name)
3738{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003739 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003740
3741 if (strcmp(name, "valid") != 0)
3742 return NULL;
3743
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003744 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3745 Py_INCREF(ret);
3746 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003747}
3748
3749 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003750TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003751{
3752 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003753 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003754 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003755 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003756 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003757 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003758 else if (strcmp(name, "window") == 0)
3759 {
3760 /* For current tab window.c does not bother to set or update tp_curwin
3761 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003762 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003763 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003764 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003766 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003767 else if (strcmp(name, "__members__") == 0)
3768 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003769 return NULL;
3770}
3771
3772 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003773TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003775 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003776 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003777 else
3778 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003779 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003780
3781 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003782 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3783 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003784 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003785 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003786 }
3787}
3788
3789static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003790 /* name, function, calling, documentation */
3791 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3792 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003793};
3794
3795/*
3796 * Window list object
3797 */
3798
3799static PyTypeObject TabListType;
3800static PySequenceMethods TabListAsSeq;
3801
3802typedef struct
3803{
3804 PyObject_HEAD
3805} TabListObject;
3806
3807 static PyInt
3808TabListLength(PyObject *self UNUSED)
3809{
3810 tabpage_T *tp = first_tabpage;
3811 PyInt n = 0;
3812
3813 while (tp != NULL)
3814 {
3815 ++n;
3816 tp = tp->tp_next;
3817 }
3818
3819 return n;
3820}
3821
3822 static PyObject *
3823TabListItem(PyObject *self UNUSED, PyInt n)
3824{
3825 tabpage_T *tp;
3826
3827 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3828 if (n == 0)
3829 return TabPageNew(tp);
3830
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003831 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003832 return NULL;
3833}
3834
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003835/*
3836 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003837 */
3838
3839typedef struct
3840{
3841 PyObject_HEAD
3842 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003843 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003844} WindowObject;
3845
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003846static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003847
3848 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003849CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003850{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003851 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003852 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003853 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003854 return -1;
3855 }
3856
3857 return 0;
3858}
3859
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003860 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003861WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003862{
3863 /* We need to handle deletion of windows underneath us.
3864 * If we add a "w_python*_ref" field to the win_T structure,
3865 * then we can get at it in win_free() in vim. We then
3866 * need to create only ONE Python object per window - if
3867 * we try to create a second, just INCREF the existing one
3868 * and return it. The (single) Python object referring to
3869 * the window is stored in "w_python*_ref".
3870 * On a win_free() we set the Python object's win_T* field
3871 * to an invalid value. We trap all uses of a window
3872 * object, and reject them if the win_T* field is invalid.
3873 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003874 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003875 * w_python_ref and w_python3_ref fields respectively.
3876 */
3877
3878 WindowObject *self;
3879
3880 if (WIN_PYTHON_REF(win))
3881 {
3882 self = WIN_PYTHON_REF(win);
3883 Py_INCREF(self);
3884 }
3885 else
3886 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003887 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003888 if (self == NULL)
3889 return NULL;
3890 self->win = win;
3891 WIN_PYTHON_REF(win) = self;
3892 }
3893
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003894 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3895
Bram Moolenaar971db462013-05-12 18:44:48 +02003896 return (PyObject *)(self);
3897}
3898
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003899 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003900WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003901{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003902 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003903 if (self->win && self->win != INVALID_WINDOW_VALUE)
3904 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003905 Py_XDECREF(((PyObject *)(self->tabObject)));
3906 PyObject_GC_Del((void *)(self));
3907}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003908
Bram Moolenaar774267b2013-05-21 20:51:59 +02003909 static int
3910WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3911{
3912 Py_VISIT(((PyObject *)(self->tabObject)));
3913 return 0;
3914}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003915
Bram Moolenaar774267b2013-05-21 20:51:59 +02003916 static int
3917WindowClear(WindowObject *self)
3918{
3919 Py_CLEAR(self->tabObject);
3920 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003921}
3922
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003923 static win_T *
3924get_firstwin(TabPageObject *tabObject)
3925{
3926 if (tabObject)
3927 {
3928 if (CheckTabPage(tabObject))
3929 return NULL;
3930 /* For current tab window.c does not bother to set or update tp_firstwin
3931 */
3932 else if (tabObject->tab == curtab)
3933 return firstwin;
3934 else
3935 return tabObject->tab->tp_firstwin;
3936 }
3937 else
3938 return firstwin;
3939}
Bram Moolenaare950f992018-06-10 13:55:55 +02003940
3941// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003942static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003943 "buffer",
3944 "cursor",
3945 "height",
3946 "row",
3947 "width",
3948 "col",
3949 "vars",
3950 "options",
3951 "number",
3952 "tabpage",
3953 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003954 NULL
3955};
3956
3957 static PyObject *
3958WindowDir(PyObject *self)
3959{
3960 return ObjectDir(self, WindowAttrs);
3961}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003962
Bram Moolenaar971db462013-05-12 18:44:48 +02003963 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003964WindowAttrValid(WindowObject *self, char *name)
3965{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003966 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003967
3968 if (strcmp(name, "valid") != 0)
3969 return NULL;
3970
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003971 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3972 Py_INCREF(ret);
3973 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003974}
3975
3976 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003977WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003978{
3979 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003981 else if (strcmp(name, "cursor") == 0)
3982 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003983 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003984
3985 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3986 }
3987 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003989 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003990 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003991 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003992 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003993 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003994 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003995 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003996 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003997 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003998 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3999 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004000 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004001 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004002 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004003 return NULL;
4004 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004005 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004006 }
4007 else if (strcmp(name, "tabpage") == 0)
4008 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004009 Py_INCREF(self->tabObject);
4010 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004011 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004012 else if (strcmp(name, "__members__") == 0)
4013 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004014 else
4015 return NULL;
4016}
4017
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004019WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004020{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004021 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022 return -1;
4023
4024 if (strcmp(name, "buffer") == 0)
4025 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004026 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004027 return -1;
4028 }
4029 else if (strcmp(name, "cursor") == 0)
4030 {
4031 long lnum;
4032 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004033
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004034 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 return -1;
4036
Bram Moolenaard6e39182013-05-21 18:30:34 +02004037 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004039 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004040 return -1;
4041 }
4042
4043 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004044 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004045 return -1;
4046
Bram Moolenaard6e39182013-05-21 18:30:34 +02004047 self->win->w_cursor.lnum = lnum;
4048 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004049 self->win->w_set_curswant = TRUE;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004050#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02004051 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004053 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004054 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004056 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 return 0;
4058 }
4059 else if (strcmp(name, "height") == 0)
4060 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004061 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 win_T *savewin;
4063
Bram Moolenaardee2e312013-06-23 16:35:47 +02004064 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 return -1;
4066
4067#ifdef FEAT_GUI
4068 need_mouse_correct = TRUE;
4069#endif
4070 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004071 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004072
4073 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004074 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004075 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004076 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077 return -1;
4078
4079 return 0;
4080 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 else if (strcmp(name, "width") == 0)
4082 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004083 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004084 win_T *savewin;
4085
Bram Moolenaardee2e312013-06-23 16:35:47 +02004086 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 return -1;
4088
4089#ifdef FEAT_GUI
4090 need_mouse_correct = TRUE;
4091#endif
4092 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004093 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004094
4095 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004096 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004097 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004098 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099 return -1;
4100
4101 return 0;
4102 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103 else
4104 {
4105 PyErr_SetString(PyExc_AttributeError, name);
4106 return -1;
4107 }
4108}
4109
4110 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004111WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004113 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004114 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115 else
4116 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004117 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118
Bram Moolenaar6d216452013-05-12 19:00:41 +02004119 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004120 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004121 (self));
4122 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004123 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124 }
4125}
4126
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004127static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004128 /* name, function, calling, documentation */
4129 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4130 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004131};
4132
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004133/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004134 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004136
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004137static PyTypeObject WinListType;
4138static PySequenceMethods WinListAsSeq;
4139
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004140typedef struct
4141{
4142 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004143 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004144} WinListObject;
4145
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004146 static PyObject *
4147WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004148{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004149 WinListObject *self;
4150
4151 self = PyObject_NEW(WinListObject, &WinListType);
4152 self->tabObject = tabObject;
4153 Py_INCREF(tabObject);
4154
4155 return (PyObject *)(self);
4156}
4157
4158 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004159WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004160{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004161 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004162
4163 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004164 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004165 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004166 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004167
4168 DESTRUCTOR_FINISH(self);
4169}
4170
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004171 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004172WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004173{
4174 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004175 PyInt n = 0;
4176
Bram Moolenaard6e39182013-05-21 18:30:34 +02004177 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004178 return -1;
4179
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004180 while (w != NULL)
4181 {
4182 ++n;
4183 w = W_NEXT(w);
4184 }
4185
4186 return n;
4187}
4188
4189 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004190WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004191{
4192 win_T *w;
4193
Bram Moolenaard6e39182013-05-21 18:30:34 +02004194 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004195 return NULL;
4196
4197 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004198 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004199 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004200
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004201 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004202 return NULL;
4203}
4204
4205/* Convert a Python string into a Vim line.
4206 *
4207 * The result is in allocated memory. All internal nulls are replaced by
4208 * newline characters. It is an error for the string to contain newline
4209 * characters.
4210 *
4211 * On errors, the Python exception data is set, and NULL is returned.
4212 */
4213 static char *
4214StringToLine(PyObject *obj)
4215{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004216 char *str;
4217 char *save;
4218 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004219 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004220 PyInt i;
4221 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004222
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004223 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004224 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004225 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4226 || str == NULL)
4227 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004228 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004229 else if (PyUnicode_Check(obj))
4230 {
4231 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4232 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004233
Bram Moolenaardaa27022013-06-24 22:33:30 +02004234 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004235 || str == NULL)
4236 {
4237 Py_DECREF(bytes);
4238 return NULL;
4239 }
4240 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004241 else
4242 {
4243#if PY_MAJOR_VERSION < 3
4244 PyErr_FORMAT(PyExc_TypeError,
4245 N_("expected str() or unicode() instance, but got %s"),
4246 Py_TYPE_NAME(obj));
4247#else
4248 PyErr_FORMAT(PyExc_TypeError,
4249 N_("expected bytes() or str() instance, but got %s"),
4250 Py_TYPE_NAME(obj));
4251#endif
4252 return NULL;
4253 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004254
4255 /*
4256 * Error checking: String must not contain newlines, as we
4257 * are replacing a single line, and we must replace it with
4258 * a single line.
4259 * A trailing newline is removed, so that append(f.readlines()) works.
4260 */
4261 p = memchr(str, '\n', len);
4262 if (p != NULL)
4263 {
4264 if (p == str + len - 1)
4265 --len;
4266 else
4267 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004268 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004269 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270 return NULL;
4271 }
4272 }
4273
4274 /* Create a copy of the string, with internal nulls replaced by
4275 * newline characters, as is the vim convention.
4276 */
4277 save = (char *)alloc((unsigned)(len+1));
4278 if (save == NULL)
4279 {
4280 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004281 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004282 return NULL;
4283 }
4284
4285 for (i = 0; i < len; ++i)
4286 {
4287 if (str[i] == '\0')
4288 save[i] = '\n';
4289 else
4290 save[i] = str[i];
4291 }
4292
4293 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004294 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004295
4296 return save;
4297}
4298
4299/* Get a line from the specified buffer. The line number is
4300 * in Vim format (1-based). The line is returned as a Python
4301 * string object.
4302 */
4303 static PyObject *
4304GetBufferLine(buf_T *buf, PyInt n)
4305{
4306 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4307}
4308
4309
4310/* Get a list of lines from the specified buffer. The line numbers
4311 * are in Vim format (1-based). The range is from lo up to, but not
4312 * including, hi. The list is returned as a Python list of string objects.
4313 */
4314 static PyObject *
4315GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4316{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004317 PyInt i;
4318 PyInt n = hi - lo;
4319 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004320
4321 if (list == NULL)
4322 return NULL;
4323
4324 for (i = 0; i < n; ++i)
4325 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004326 PyObject *string = LineToString(
4327 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004328
4329 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004330 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004331 {
4332 Py_DECREF(list);
4333 return NULL;
4334 }
4335
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004336 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004337 }
4338
4339 /* The ownership of the Python list is passed to the caller (ie,
4340 * the caller should Py_DECREF() the object when it is finished
4341 * with it).
4342 */
4343
4344 return list;
4345}
4346
4347/*
4348 * Check if deleting lines made the cursor position invalid.
4349 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4350 * deleted).
4351 */
4352 static void
4353py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4354{
4355 if (curwin->w_cursor.lnum >= lo)
4356 {
4357 /* Adjust the cursor position if it's in/after the changed
4358 * lines. */
4359 if (curwin->w_cursor.lnum >= hi)
4360 {
4361 curwin->w_cursor.lnum += extra;
4362 check_cursor_col();
4363 }
4364 else if (extra < 0)
4365 {
4366 curwin->w_cursor.lnum = lo;
4367 check_cursor();
4368 }
4369 else
4370 check_cursor_col();
4371 changed_cline_bef_curs();
4372 }
4373 invalidate_botline();
4374}
4375
Bram Moolenaar19e60942011-06-19 00:27:51 +02004376/*
4377 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004378 * in Vim format (1-based). The replacement line is given as
4379 * a Python string object. The object is checked for validity
4380 * and correct format. Errors are returned as a value of FAIL.
4381 * The return value is OK on success.
4382 * If OK is returned and len_change is not NULL, *len_change
4383 * is set to the change in the buffer length.
4384 */
4385 static int
4386SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4387{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004388 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004389 win_T *save_curwin = NULL;
4390 tabpage_T *save_curtab = NULL;
4391
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004392 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 * There are three cases:
4394 * 1. NULL, or None - this is a deletion.
4395 * 2. A string - this is a replacement.
4396 * 3. Anything else - this is an error.
4397 */
4398 if (line == Py_None || line == NULL)
4399 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004400 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004401 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004402
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004403 VimTryStart();
4404
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004405 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004406 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004408 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004409 else
4410 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004411 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004413 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004414 /* Only adjust marks if we managed to switch to a window that
4415 * holds the buffer, otherwise line numbers will be invalid. */
4416 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004417 }
4418
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004419 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004421 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422 return FAIL;
4423
4424 if (len_change)
4425 *len_change = -1;
4426
4427 return OK;
4428 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004429 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004430 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004431 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432
4433 if (save == NULL)
4434 return FAIL;
4435
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004436 VimTryStart();
4437
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 /* We do not need to free "save" if ml_replace() consumes it. */
4439 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004440 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441
4442 if (u_savesub((linenr_T)n) == FAIL)
4443 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004444 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 vim_free(save);
4446 }
4447 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4448 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004449 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004450 vim_free(save);
4451 }
4452 else
4453 changed_bytes((linenr_T)n, 0);
4454
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004455 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456
4457 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004458 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004459 check_cursor_col();
4460
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004461 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004462 return FAIL;
4463
4464 if (len_change)
4465 *len_change = 0;
4466
4467 return OK;
4468 }
4469 else
4470 {
4471 PyErr_BadArgument();
4472 return FAIL;
4473 }
4474}
4475
Bram Moolenaar19e60942011-06-19 00:27:51 +02004476/* Replace a range of lines in the specified buffer. The line numbers are in
4477 * Vim format (1-based). The range is from lo up to, but not including, hi.
4478 * The replacement lines are given as a Python list of string objects. The
4479 * list is checked for validity and correct format. Errors are returned as a
4480 * value of FAIL. The return value is OK on success.
4481 * If OK is returned and len_change is not NULL, *len_change
4482 * is set to the change in the buffer length.
4483 */
4484 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004485SetBufferLineList(
4486 buf_T *buf,
4487 PyInt lo,
4488 PyInt hi,
4489 PyObject *list,
4490 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004491{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004492 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004493 win_T *save_curwin = NULL;
4494 tabpage_T *save_curtab = NULL;
4495
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004496 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004497 * There are three cases:
4498 * 1. NULL, or None - this is a deletion.
4499 * 2. A list - this is a replacement.
4500 * 3. Anything else - this is an error.
4501 */
4502 if (list == Py_None || list == NULL)
4503 {
4504 PyInt i;
4505 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004506
4507 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004508 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004509 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004510
4511 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004512 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004513 else
4514 {
4515 for (i = 0; i < n; ++i)
4516 {
4517 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4518 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004519 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004520 break;
4521 }
4522 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004523 if (buf == curbuf && (save_curwin != NULL
4524 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004525 /* Using an existing window for the buffer, adjust the cursor
4526 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004527 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004528 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004529 /* Only adjust marks if we managed to switch to a window that
4530 * holds the buffer, otherwise line numbers will be invalid. */
4531 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004532 }
4533
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004534 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004535
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004536 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004537 return FAIL;
4538
4539 if (len_change)
4540 *len_change = -n;
4541
4542 return OK;
4543 }
4544 else if (PyList_Check(list))
4545 {
4546 PyInt i;
4547 PyInt new_len = PyList_Size(list);
4548 PyInt old_len = hi - lo;
4549 PyInt extra = 0; /* lines added to text, can be negative */
4550 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551
4552 if (new_len == 0) /* avoid allocating zero bytes */
4553 array = NULL;
4554 else
4555 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004556 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004557 if (array == NULL)
4558 {
4559 PyErr_NoMemory();
4560 return FAIL;
4561 }
4562 }
4563
4564 for (i = 0; i < new_len; ++i)
4565 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004566 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004567
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004568 if (!(line = PyList_GetItem(list, i)) ||
4569 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004570 {
4571 while (i)
4572 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004573 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004574 return FAIL;
4575 }
4576 }
4577
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004578 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004579 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004580
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004581 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004582 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004583
4584 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004585 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004586
4587 /* If the size of the range is reducing (ie, new_len < old_len) we
4588 * need to delete some old_len. We do this at the start, by
4589 * repeatedly deleting line "lo".
4590 */
4591 if (!PyErr_Occurred())
4592 {
4593 for (i = 0; i < old_len - new_len; ++i)
4594 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4595 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004596 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597 break;
4598 }
4599 extra -= i;
4600 }
4601
4602 /* For as long as possible, replace the existing old_len with the
4603 * new old_len. This is a more efficient operation, as it requires
4604 * less memory allocation and freeing.
4605 */
4606 if (!PyErr_Occurred())
4607 {
4608 for (i = 0; i < old_len && i < new_len; ++i)
4609 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4610 == FAIL)
4611 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004612 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004613 break;
4614 }
4615 }
4616 else
4617 i = 0;
4618
4619 /* Now we may need to insert the remaining new old_len. If we do, we
4620 * must free the strings as we finish with them (we can't pass the
4621 * responsibility to vim in this case).
4622 */
4623 if (!PyErr_Occurred())
4624 {
4625 while (i < new_len)
4626 {
4627 if (ml_append((linenr_T)(lo + i - 1),
4628 (char_u *)array[i], 0, FALSE) == FAIL)
4629 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004630 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004631 break;
4632 }
4633 vim_free(array[i]);
4634 ++i;
4635 ++extra;
4636 }
4637 }
4638
4639 /* Free any left-over old_len, as a result of an error */
4640 while (i < new_len)
4641 {
4642 vim_free(array[i]);
4643 ++i;
4644 }
4645
4646 /* Free the array of old_len. All of its contents have now
4647 * been dealt with (either freed, or the responsibility passed
4648 * to vim.
4649 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004650 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004651
4652 /* Adjust marks. Invalidate any which lie in the
4653 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004654 * Only adjust marks if we managed to switch to a window that holds
4655 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004656 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004657 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004658 (long)MAXLNUM, (long)extra);
4659 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4660
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004661 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004662 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4663
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004664 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004665 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004666
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004667 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004668 return FAIL;
4669
4670 if (len_change)
4671 *len_change = new_len - old_len;
4672
4673 return OK;
4674 }
4675 else
4676 {
4677 PyErr_BadArgument();
4678 return FAIL;
4679 }
4680}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004681
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004682/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683 * The line number is in Vim format (1-based). The lines to be inserted are
4684 * given as a Python list of string objects or as a single string. The lines
4685 * to be added are checked for validity and correct format. Errors are
4686 * returned as a value of FAIL. The return value is OK on success.
4687 * If OK is returned and len_change is not NULL, *len_change
4688 * is set to the change in the buffer length.
4689 */
4690 static int
4691InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4692{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004693 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004694 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004695 tabpage_T *save_curtab = NULL;
4696
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697 /* First of all, we check the type of the supplied Python object.
4698 * It must be a string or a list, or the call is in error.
4699 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004700 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004701 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004702 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004703
4704 if (str == NULL)
4705 return FAIL;
4706
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004707 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004708 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004709 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004710
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004711 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004712 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004713 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004714 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004715 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004716 /* Only adjust marks if we managed to switch to a window that
4717 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004718 appended_lines_mark((linenr_T)n, 1L);
4719
4720 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004721 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004722 update_screen(VALID);
4723
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004724 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004725 return FAIL;
4726
4727 if (len_change)
4728 *len_change = 1;
4729
4730 return OK;
4731 }
4732 else if (PyList_Check(lines))
4733 {
4734 PyInt i;
4735 PyInt size = PyList_Size(lines);
4736 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004737
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004738 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004739 if (array == NULL)
4740 {
4741 PyErr_NoMemory();
4742 return FAIL;
4743 }
4744
4745 for (i = 0; i < size; ++i)
4746 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004747 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004748
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004749 if (!(line = PyList_GetItem(lines, i)) ||
4750 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004751 {
4752 while (i)
4753 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004754 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004755 return FAIL;
4756 }
4757 }
4758
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004759 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004760 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004761 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004762
4763 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004764 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004765 else
4766 {
4767 for (i = 0; i < size; ++i)
4768 {
4769 if (ml_append((linenr_T)(n + i),
4770 (char_u *)array[i], 0, FALSE) == FAIL)
4771 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004772 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004773
4774 /* Free the rest of the lines */
4775 while (i < size)
4776 vim_free(array[i++]);
4777
4778 break;
4779 }
4780 vim_free(array[i]);
4781 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004782 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004783 /* Only adjust marks if we managed to switch to a window that
4784 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004785 appended_lines_mark((linenr_T)n, (long)i);
4786 }
4787
4788 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004789 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004790 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004791 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004792
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004793 update_screen(VALID);
4794
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004795 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004796 return FAIL;
4797
4798 if (len_change)
4799 *len_change = size;
4800
4801 return OK;
4802 }
4803 else
4804 {
4805 PyErr_BadArgument();
4806 return FAIL;
4807 }
4808}
4809
4810/*
4811 * Common routines for buffers and line ranges
4812 * -------------------------------------------
4813 */
4814
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004815typedef struct
4816{
4817 PyObject_HEAD
4818 buf_T *buf;
4819} BufferObject;
4820
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004821 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004822CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004823{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004824 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004825 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004826 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004827 return -1;
4828 }
4829
4830 return 0;
4831}
4832
4833 static PyObject *
4834RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4835{
4836 if (CheckBuffer(self))
4837 return NULL;
4838
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004839 if (end == -1)
4840 end = self->buf->b_ml.ml_line_count;
4841
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004842 if (n < 0)
4843 n += end - start + 1;
4844
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004845 if (n < 0 || n > end - start)
4846 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004847 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004848 return NULL;
4849 }
4850
4851 return GetBufferLine(self->buf, n+start);
4852}
4853
4854 static PyObject *
4855RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4856{
4857 PyInt size;
4858
4859 if (CheckBuffer(self))
4860 return NULL;
4861
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004862 if (end == -1)
4863 end = self->buf->b_ml.ml_line_count;
4864
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004865 size = end - start + 1;
4866
4867 if (lo < 0)
4868 lo = 0;
4869 else if (lo > size)
4870 lo = size;
4871 if (hi < 0)
4872 hi = 0;
4873 if (hi < lo)
4874 hi = lo;
4875 else if (hi > size)
4876 hi = size;
4877
4878 return GetBufferLineList(self->buf, lo+start, hi+start);
4879}
4880
4881 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004882RBAsItem(
4883 BufferObject *self,
4884 PyInt n,
4885 PyObject *valObject,
4886 PyInt start,
4887 PyInt end,
4888 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004889{
4890 PyInt len_change;
4891
4892 if (CheckBuffer(self))
4893 return -1;
4894
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004895 if (end == -1)
4896 end = self->buf->b_ml.ml_line_count;
4897
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004898 if (n < 0)
4899 n += end - start + 1;
4900
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004901 if (n < 0 || n > end - start)
4902 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004903 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004904 return -1;
4905 }
4906
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004907 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004908 return -1;
4909
4910 if (new_end)
4911 *new_end = end + len_change;
4912
4913 return 0;
4914}
4915
Bram Moolenaar19e60942011-06-19 00:27:51 +02004916 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004917RBAsSlice(
4918 BufferObject *self,
4919 PyInt lo,
4920 PyInt hi,
4921 PyObject *valObject,
4922 PyInt start,
4923 PyInt end,
4924 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004925{
4926 PyInt size;
4927 PyInt len_change;
4928
4929 /* Self must be a valid buffer */
4930 if (CheckBuffer(self))
4931 return -1;
4932
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004933 if (end == -1)
4934 end = self->buf->b_ml.ml_line_count;
4935
Bram Moolenaar19e60942011-06-19 00:27:51 +02004936 /* Sort out the slice range */
4937 size = end - start + 1;
4938
4939 if (lo < 0)
4940 lo = 0;
4941 else if (lo > size)
4942 lo = size;
4943 if (hi < 0)
4944 hi = 0;
4945 if (hi < lo)
4946 hi = lo;
4947 else if (hi > size)
4948 hi = size;
4949
4950 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004951 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004952 return -1;
4953
4954 if (new_end)
4955 *new_end = end + len_change;
4956
4957 return 0;
4958}
4959
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004960
4961 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004962RBAppend(
4963 BufferObject *self,
4964 PyObject *args,
4965 PyInt start,
4966 PyInt end,
4967 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004968{
4969 PyObject *lines;
4970 PyInt len_change;
4971 PyInt max;
4972 PyInt n;
4973
4974 if (CheckBuffer(self))
4975 return NULL;
4976
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004977 if (end == -1)
4978 end = self->buf->b_ml.ml_line_count;
4979
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004980 max = n = end - start + 1;
4981
4982 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4983 return NULL;
4984
4985 if (n < 0 || n > max)
4986 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004987 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004988 return NULL;
4989 }
4990
4991 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4992 return NULL;
4993
4994 if (new_end)
4995 *new_end = end + len_change;
4996
4997 Py_INCREF(Py_None);
4998 return Py_None;
4999}
5000
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005001/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005002 */
5003
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005004static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005005static PySequenceMethods RangeAsSeq;
5006static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005007
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005008typedef struct
5009{
5010 PyObject_HEAD
5011 BufferObject *buf;
5012 PyInt start;
5013 PyInt end;
5014} RangeObject;
5015
5016 static PyObject *
5017RangeNew(buf_T *buf, PyInt start, PyInt end)
5018{
5019 BufferObject *bufr;
5020 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005021 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005022 if (self == NULL)
5023 return NULL;
5024
5025 bufr = (BufferObject *)BufferNew(buf);
5026 if (bufr == NULL)
5027 {
5028 Py_DECREF(self);
5029 return NULL;
5030 }
5031 Py_INCREF(bufr);
5032
5033 self->buf = bufr;
5034 self->start = start;
5035 self->end = end;
5036
5037 return (PyObject *)(self);
5038}
5039
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005040 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005041RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005042{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005043 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005044 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005045 PyObject_GC_Del((void *)(self));
5046}
5047
5048 static int
5049RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5050{
5051 Py_VISIT(((PyObject *)(self->buf)));
5052 return 0;
5053}
5054
5055 static int
5056RangeClear(RangeObject *self)
5057{
5058 Py_CLEAR(self->buf);
5059 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005060}
5061
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005062 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064{
5065 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005067 return -1; /* ??? */
5068
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005070}
5071
5072 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005073RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005074{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005075 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005076}
5077
5078 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005079RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005080{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005081 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005082}
5083
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005084static char *RangeAttrs[] = {
5085 "start", "end",
5086 NULL
5087};
5088
5089 static PyObject *
5090RangeDir(PyObject *self)
5091{
5092 return ObjectDir(self, RangeAttrs);
5093}
5094
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005098 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005099}
5100
5101 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005104 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005105 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5106 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107 else
5108 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005109 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005110
5111 if (name == NULL)
5112 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005113
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005114 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005115 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116 }
5117}
5118
5119static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005120 /* name, function, calling, documentation */
5121 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005122 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5123 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124};
5125
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005126static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005127static PySequenceMethods BufferAsSeq;
5128static PyMappingMethods BufferAsMapping;
5129
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005130 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005131BufferNew(buf_T *buf)
5132{
5133 /* We need to handle deletion of buffers underneath us.
5134 * If we add a "b_python*_ref" field to the buf_T structure,
5135 * then we can get at it in buf_freeall() in vim. We then
5136 * need to create only ONE Python object per buffer - if
5137 * we try to create a second, just INCREF the existing one
5138 * and return it. The (single) Python object referring to
5139 * the buffer is stored in "b_python*_ref".
5140 * Question: what to do on a buf_freeall(). We'll probably
5141 * have to either delete the Python object (DECREF it to
5142 * zero - a bad idea, as it leaves dangling refs!) or
5143 * set the buf_T * value to an invalid value (-1?), which
5144 * means we need checks in all access functions... Bah.
5145 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005146 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005147 * b_python_ref and b_python3_ref fields respectively.
5148 */
5149
5150 BufferObject *self;
5151
5152 if (BUF_PYTHON_REF(buf) != NULL)
5153 {
5154 self = BUF_PYTHON_REF(buf);
5155 Py_INCREF(self);
5156 }
5157 else
5158 {
5159 self = PyObject_NEW(BufferObject, &BufferType);
5160 if (self == NULL)
5161 return NULL;
5162 self->buf = buf;
5163 BUF_PYTHON_REF(buf) = self;
5164 }
5165
5166 return (PyObject *)(self);
5167}
5168
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005170BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005171{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5173 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005174
5175 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005176}
5177
Bram Moolenaar971db462013-05-12 18:44:48 +02005178 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005179BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005180{
5181 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005182 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005183 return -1; /* ??? */
5184
Bram Moolenaard6e39182013-05-21 18:30:34 +02005185 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005186}
5187
5188 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005189BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005190{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005191 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005192}
5193
5194 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005196{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005197 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005198}
5199
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005200static char *BufferAttrs[] = {
5201 "name", "number", "vars", "options", "valid",
5202 NULL
5203};
5204
5205 static PyObject *
5206BufferDir(PyObject *self)
5207{
5208 return ObjectDir(self, BufferAttrs);
5209}
5210
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005211 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005212BufferAttrValid(BufferObject *self, char *name)
5213{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005214 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005215
5216 if (strcmp(name, "valid") != 0)
5217 return NULL;
5218
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005219 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5220 Py_INCREF(ret);
5221 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005222}
5223
5224 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005225BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005226{
5227 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005228 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005229 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005230 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005231 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005233 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005234 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005235 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5236 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005237 else if (strcmp(name, "__members__") == 0)
5238 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005239 else
5240 return NULL;
5241}
5242
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005243 static int
5244BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5245{
5246 if (CheckBuffer(self))
5247 return -1;
5248
5249 if (strcmp(name, "name") == 0)
5250 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005251 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005252 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005253 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005254 PyObject *todecref;
5255
5256 if (!(val = StringToChars(valObject, &todecref)))
5257 return -1;
5258
5259 VimTryStart();
5260 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5261 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005262 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005263 aucmd_restbuf(&aco);
5264 Py_XDECREF(todecref);
5265 if (VimTryEnd())
5266 return -1;
5267
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005268 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005269 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005270 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005271 return -1;
5272 }
5273 return 0;
5274 }
5275 else
5276 {
5277 PyErr_SetString(PyExc_AttributeError, name);
5278 return -1;
5279 }
5280}
5281
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005282 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005283BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005284{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005285 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005286}
5287
5288 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005289BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005290{
5291 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005292 char_u *pmark;
5293 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005294 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005295 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005296
Bram Moolenaard6e39182013-05-21 18:30:34 +02005297 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005298 return NULL;
5299
Bram Moolenaar389a1792013-06-23 13:00:44 +02005300 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005301 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005302
Bram Moolenaar389a1792013-06-23 13:00:44 +02005303 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005304 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005305 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005306 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005307 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005308 return NULL;
5309 }
5310
5311 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005312
5313 Py_XDECREF(todecref);
5314
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005315 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005316 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005317 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005318 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005319 if (VimTryEnd())
5320 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321
5322 if (posp == NULL)
5323 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005324 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005325 return NULL;
5326 }
5327
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005328 if (posp->lnum <= 0)
5329 {
5330 /* Or raise an error? */
5331 Py_INCREF(Py_None);
5332 return Py_None;
5333 }
5334
5335 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5336}
5337
5338 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005339BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005340{
5341 PyInt start;
5342 PyInt end;
5343
Bram Moolenaard6e39182013-05-21 18:30:34 +02005344 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005345 return NULL;
5346
5347 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5348 return NULL;
5349
Bram Moolenaard6e39182013-05-21 18:30:34 +02005350 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005351}
5352
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005353 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005354BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005355{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005356 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005357 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005358 else
5359 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005360 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005361
5362 if (name == NULL)
5363 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005364
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005365 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005366 }
5367}
5368
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005369static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005370 /* name, function, calling, documentation */
5371 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005372 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005373 {"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 +02005374 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5375 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005376};
5377
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005378/*
5379 * Buffer list object - Implementation
5380 */
5381
5382static PyTypeObject BufMapType;
5383
5384typedef struct
5385{
5386 PyObject_HEAD
5387} BufMapObject;
5388
5389 static PyInt
5390BufMapLength(PyObject *self UNUSED)
5391{
5392 buf_T *b = firstbuf;
5393 PyInt n = 0;
5394
5395 while (b)
5396 {
5397 ++n;
5398 b = b->b_next;
5399 }
5400
5401 return n;
5402}
5403
5404 static PyObject *
5405BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5406{
5407 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005408 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005409
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005410 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005411 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005412
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005413 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005414
5415 if (b)
5416 return BufferNew(b);
5417 else
5418 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005419 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005420 return NULL;
5421 }
5422}
5423
5424 static void
5425BufMapIterDestruct(PyObject *buffer)
5426{
5427 /* Iteration was stopped before all buffers were processed */
5428 if (buffer)
5429 {
5430 Py_DECREF(buffer);
5431 }
5432}
5433
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005434 static int
5435BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5436{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005437 if (buffer)
5438 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005439 return 0;
5440}
5441
5442 static int
5443BufMapIterClear(PyObject **buffer)
5444{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005445 if (*buffer)
5446 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005447 return 0;
5448}
5449
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005450 static PyObject *
5451BufMapIterNext(PyObject **buffer)
5452{
5453 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005454 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005455
5456 if (!*buffer)
5457 return NULL;
5458
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005459 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005461 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005462 {
5463 *buffer = NULL;
5464 return NULL;
5465 }
5466
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005467 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005468 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005469 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005470 return NULL;
5471 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005472 /* Do not increment reference: we no longer hold it (decref), but whoever
5473 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005475}
5476
5477 static PyObject *
5478BufMapIter(PyObject *self UNUSED)
5479{
5480 PyObject *buffer;
5481
5482 buffer = BufferNew(firstbuf);
5483 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005484 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5485 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005486}
5487
5488static PyMappingMethods BufMapAsMapping = {
5489 (lenfunc) BufMapLength,
5490 (binaryfunc) BufMapItem,
5491 (objobjargproc) 0,
5492};
5493
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005494/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005495 */
5496
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005497static char *CurrentAttrs[] = {
5498 "buffer", "window", "line", "range", "tabpage",
5499 NULL
5500};
5501
5502 static PyObject *
5503CurrentDir(PyObject *self)
5504{
5505 return ObjectDir(self, CurrentAttrs);
5506}
5507
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005508 static PyObject *
5509CurrentGetattr(PyObject *self UNUSED, char *name)
5510{
5511 if (strcmp(name, "buffer") == 0)
5512 return (PyObject *)BufferNew(curbuf);
5513 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005514 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005515 else if (strcmp(name, "tabpage") == 0)
5516 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005517 else if (strcmp(name, "line") == 0)
5518 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5519 else if (strcmp(name, "range") == 0)
5520 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005521 else if (strcmp(name, "__members__") == 0)
5522 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005523 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005524#if PY_MAJOR_VERSION < 3
5525 return Py_FindMethod(WindowMethods, self, name);
5526#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005527 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005528#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005529}
5530
5531 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005532CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005533{
5534 if (strcmp(name, "line") == 0)
5535 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005536 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5537 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005538 return -1;
5539
5540 return 0;
5541 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005542 else if (strcmp(name, "buffer") == 0)
5543 {
5544 int count;
5545
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005546 if (valObject->ob_type != &BufferType)
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.Buffer 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 (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005555 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005556 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005557
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005558 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005559 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5560 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005561 if (VimTryEnd())
5562 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005563 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005564 return -1;
5565 }
5566
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005567 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005568 }
5569 else if (strcmp(name, "window") == 0)
5570 {
5571 int count;
5572
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005574 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005575 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005576 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005578 return -1;
5579 }
5580
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005583 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005584
5585 if (!count)
5586 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005587 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005588 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005589 return -1;
5590 }
5591
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005592 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005593 win_goto(((WindowObject *)(valObject))->win);
5594 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005595 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005596 if (VimTryEnd())
5597 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005598 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005599 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005600 return -1;
5601 }
5602
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005603 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005604 }
5605 else if (strcmp(name, "tabpage") == 0)
5606 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005607 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005608 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005609 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005610 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005611 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005612 return -1;
5613 }
5614
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005615 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005616 return -1;
5617
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005618 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005619 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5620 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005621 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005622 if (VimTryEnd())
5623 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005624 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005625 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005626 return -1;
5627 }
5628
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005629 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005630 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005631 else
5632 {
5633 PyErr_SetString(PyExc_AttributeError, name);
5634 return -1;
5635 }
5636}
5637
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005638static struct PyMethodDef CurrentMethods[] = {
5639 /* name, function, calling, documentation */
5640 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5641 { NULL, NULL, 0, NULL}
5642};
5643
Bram Moolenaardb913952012-06-29 12:54:53 +02005644 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005645init_range_cmd(exarg_T *eap)
5646{
5647 RangeStart = eap->line1;
5648 RangeEnd = eap->line2;
5649}
5650
5651 static void
5652init_range_eval(typval_T *rettv UNUSED)
5653{
5654 RangeStart = (PyInt) curwin->w_cursor.lnum;
5655 RangeEnd = RangeStart;
5656}
5657
5658 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005659run_cmd(const char *cmd, void *arg UNUSED
5660#ifdef PY_CAN_RECURSE
5661 , PyGILState_STATE *pygilstate UNUSED
5662#endif
5663 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005664{
Bram Moolenaar41009372013-07-01 22:03:04 +02005665 PyObject *run_ret;
5666 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5667 if (run_ret != NULL)
5668 {
5669 Py_DECREF(run_ret);
5670 }
5671 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005673 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005674 PyErr_Clear();
5675 }
5676 else
5677 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005678}
5679
5680static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5681static int code_hdr_len = 30;
5682
5683 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005684run_do(const char *cmd, void *arg UNUSED
5685#ifdef PY_CAN_RECURSE
5686 , PyGILState_STATE *pygilstate
5687#endif
5688 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005689{
5690 PyInt lnum;
5691 size_t len;
5692 char *code;
5693 int status;
5694 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005695 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005696 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005697
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005698 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005700 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005701 return;
5702 }
5703
5704 len = code_hdr_len + STRLEN(cmd);
5705 code = PyMem_New(char, len + 1);
5706 memcpy(code, code_hdr, code_hdr_len);
5707 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005708 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5709 status = -1;
5710 if (run_ret != NULL)
5711 {
5712 status = 0;
5713 Py_DECREF(run_ret);
5714 }
5715 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5716 {
5717 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005718 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005719 PyErr_Clear();
5720 return;
5721 }
5722 else
5723 PyErr_PrintEx(1);
5724
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005725 PyMem_Free(code);
5726
5727 if (status)
5728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005729 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005730 return;
5731 }
5732
5733 status = 0;
5734 pymain = PyImport_AddModule("__main__");
5735 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005736#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005737 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005738#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005739
5740 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5741 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005742 PyObject *line;
5743 PyObject *linenr;
5744 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005745
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005746#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005747 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005748#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005749 /* Check the line number, the command my have deleted lines. */
5750 if (lnum > curbuf->b_ml.ml_line_count
5751 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005752 goto err;
5753 if (!(linenr = PyInt_FromLong((long) lnum)))
5754 {
5755 Py_DECREF(line);
5756 goto err;
5757 }
5758 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5759 Py_DECREF(line);
5760 Py_DECREF(linenr);
5761 if (!ret)
5762 goto err;
5763
Bram Moolenaara58883b2017-01-29 21:31:09 +01005764 /* Check that the command didn't switch to another buffer. */
5765 if (curbuf != was_curbuf)
5766 {
5767 Py_XDECREF(ret);
5768 goto err;
5769 }
5770
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771 if (ret != Py_None)
5772 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005773 {
5774 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005775 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005776 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005777
5778 Py_XDECREF(ret);
5779 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005780#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005782#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005783 }
5784 goto out;
5785err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005786#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005787 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005788#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005789 PyErr_PrintEx(0);
5790 PythonIO_Flush();
5791 status = 1;
5792out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005793#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005794 if (!status)
5795 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005796#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005797 Py_DECREF(pyfunc);
5798 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5799 if (status)
5800 return;
5801 check_cursor();
5802 update_curbuf(NOT_VALID);
5803}
5804
5805 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005806run_eval(const char *cmd, typval_T *rettv
5807#ifdef PY_CAN_RECURSE
5808 , PyGILState_STATE *pygilstate UNUSED
5809#endif
5810 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005811{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005812 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005813
Bram Moolenaar41009372013-07-01 22:03:04 +02005814 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005815 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005816 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005817 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005819 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005820 PyErr_Clear();
5821 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005822 else
5823 {
5824 if (PyErr_Occurred() && !msg_silent)
5825 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005826 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005827 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005828 }
5829 else
5830 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005831 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005832 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005833 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005834 }
5835 PyErr_Clear();
5836}
5837
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005838 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005839set_ref_in_py(const int copyID)
5840{
5841 pylinkedlist_T *cur;
5842 dict_T *dd;
5843 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005844 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005845 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005846 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005847
5848 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005849 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005850 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005851 {
5852 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5853 if (dd->dv_copyID != copyID)
5854 {
5855 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005857 }
5858 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005859 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005860
5861 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005862 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005863 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005864 {
5865 ll = ((ListObject *) (cur->pll_obj))->list;
5866 if (ll->lv_copyID != copyID)
5867 {
5868 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005869 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005870 }
5871 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005872 }
5873
Bram Moolenaar8110a092016-04-14 15:56:09 +02005874 if (lastfunc != NULL)
5875 {
5876 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5877 {
5878 func = (FunctionObject *) cur->pll_obj;
5879 if (func->self != NULL && func->self->dv_copyID != copyID)
5880 {
5881 func->self->dv_copyID = copyID;
5882 abort = abort || set_ref_in_ht(
5883 &func->self->dv_hashtab, copyID, NULL);
5884 }
5885 if (func->argc)
5886 for (i = 0; !abort && i < func->argc; ++i)
5887 abort = abort
5888 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5889 }
5890 }
5891
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005892 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005893}
5894
5895 static int
5896set_string_copy(char_u *str, typval_T *tv)
5897{
5898 tv->vval.v_string = vim_strsave(str);
5899 if (tv->vval.v_string == NULL)
5900 {
5901 PyErr_NoMemory();
5902 return -1;
5903 }
5904 return 0;
5905}
5906
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005907 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005908pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005909{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005910 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005911 char_u *key;
5912 dictitem_T *di;
5913 PyObject *keyObject;
5914 PyObject *valObject;
5915 Py_ssize_t iter = 0;
5916
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005917 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005919
5920 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005921 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922
5923 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5924 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005925 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926
Bram Moolenaara03e6312013-05-29 22:49:26 +02005927 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005928 {
5929 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005931 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005932
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005933 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005934 {
5935 dict_unref(dict);
5936 return -1;
5937 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005938
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005939 if (*key == NUL)
5940 {
5941 dict_unref(dict);
5942 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005943 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005944 return -1;
5945 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946
5947 di = dictitem_alloc(key);
5948
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005949 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005950
5951 if (di == NULL)
5952 {
5953 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005954 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005955 return -1;
5956 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005957
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005958 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959 {
5960 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005961 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005962 return -1;
5963 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005964
5965 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005967 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005968 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005970 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971 return -1;
5972 }
5973 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005974
5975 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976 return 0;
5977}
5978
5979 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005980pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005982 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983 char_u *key;
5984 dictitem_T *di;
5985 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005986 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005987 PyObject *keyObject;
5988 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005990 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005991 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005992
5993 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005994 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005995
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005996 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005997 {
5998 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005999 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006000 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006001
6002 if (!(iterator = PyObject_GetIter(list)))
6003 {
6004 dict_unref(dict);
6005 Py_DECREF(list);
6006 return -1;
6007 }
6008 Py_DECREF(list);
6009
6010 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006011 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006012 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006013
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006014 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006015 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006016 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006017 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006018 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006019 return -1;
6020 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006021
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006022 if (*key == NUL)
6023 {
6024 Py_DECREF(keyObject);
6025 Py_DECREF(iterator);
6026 Py_XDECREF(todecref);
6027 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006028 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006029 return -1;
6030 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006031
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006032 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006033 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006034 Py_DECREF(keyObject);
6035 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006036 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006037 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006038 return -1;
6039 }
6040
6041 di = dictitem_alloc(key);
6042
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006043 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006044 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006045
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006046 if (di == NULL)
6047 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006048 Py_DECREF(iterator);
6049 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006050 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006051 PyErr_NoMemory();
6052 return -1;
6053 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006054
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006055 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006056 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006057 Py_DECREF(iterator);
6058 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006059 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 return -1;
6062 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006063
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006064 Py_DECREF(valObject);
6065
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006066 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006067 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006068 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006069 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 dictitem_free(di);
6071 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006072 return -1;
6073 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006074 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006075 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006076 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006077 return 0;
6078}
6079
6080 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006081pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006082{
6083 list_T *l;
6084
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006085 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006086 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006087
6088 tv->v_type = VAR_LIST;
6089 tv->vval.v_list = l;
6090
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006091 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006092 {
6093 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006094 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006095 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006096
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006097 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006098 return 0;
6099}
6100
Bram Moolenaardb913952012-06-29 12:54:53 +02006101typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6102
6103 static int
6104convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006105 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006106{
6107 PyObject *capsule;
6108 char hexBuf[sizeof(void *) * 2 + 3];
6109
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006110 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006111
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006112# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006113 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006114# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006115 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006116# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006117 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006118 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006119# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006120 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006121# else
6122 capsule = PyCObject_FromVoidPtr(tv, NULL);
6123# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006124 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6125 {
6126 Py_DECREF(capsule);
6127 tv->v_type = VAR_UNKNOWN;
6128 return -1;
6129 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006130
6131 Py_DECREF(capsule);
6132
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006133 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006134 {
6135 tv->v_type = VAR_UNKNOWN;
6136 return -1;
6137 }
6138 /* As we are not using copy_tv which increments reference count we must
6139 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006140 if (tv->v_type == VAR_DICT)
6141 ++tv->vval.v_dict->dv_refcount;
6142 else if (tv->v_type == VAR_LIST)
6143 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006144 }
6145 else
6146 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006147 typval_T *v;
6148
6149# ifdef PY_USE_CAPSULE
6150 v = PyCapsule_GetPointer(capsule, NULL);
6151# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006152 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006153# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006154 copy_tv(v, tv);
6155 }
6156 return 0;
6157}
6158
6159 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006160ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6161{
6162 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006163 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006164
6165 if (!(lookup_dict = PyDict_New()))
6166 return -1;
6167
6168 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6169 {
6170 tv->v_type = VAR_DICT;
6171 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6172 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006173 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006174 }
6175 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006176 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006177 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006178 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006179 else
6180 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006181 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006182 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006183 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006184 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006185 }
6186 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006187 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006188}
6189
6190 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006191ConvertFromPySequence(PyObject *obj, typval_T *tv)
6192{
6193 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006194 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006195
6196 if (!(lookup_dict = PyDict_New()))
6197 return -1;
6198
6199 if (PyType_IsSubtype(obj->ob_type, &ListType))
6200 {
6201 tv->v_type = VAR_LIST;
6202 tv->vval.v_list = (((ListObject *)(obj))->list);
6203 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006204 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006205 }
6206 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006207 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006208 else
6209 {
6210 PyErr_FORMAT(PyExc_TypeError,
6211 N_("unable to convert %s to vim list"),
6212 Py_TYPE_NAME(obj));
6213 ret = -1;
6214 }
6215 Py_DECREF(lookup_dict);
6216 return ret;
6217}
6218
6219 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006220ConvertFromPyObject(PyObject *obj, typval_T *tv)
6221{
6222 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006223 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006224
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006225 if (!(lookup_dict = PyDict_New()))
6226 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006227 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006228 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006229 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006230}
6231
6232 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006233_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006234{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006235 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006236 {
6237 tv->v_type = VAR_DICT;
6238 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6239 ++tv->vval.v_dict->dv_refcount;
6240 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006241 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006242 {
6243 tv->v_type = VAR_LIST;
6244 tv->vval.v_list = (((ListObject *)(obj))->list);
6245 ++tv->vval.v_list->lv_refcount;
6246 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006247 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006248 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006249 FunctionObject *func = (FunctionObject *) obj;
6250 if (func->self != NULL || func->argv != NULL)
6251 {
6252 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6253 set_partial(func, pt, TRUE);
6254 tv->vval.v_partial = pt;
6255 tv->v_type = VAR_PARTIAL;
6256 }
6257 else
6258 {
6259 if (set_string_copy(func->name, tv) == -1)
6260 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006261
Bram Moolenaar8110a092016-04-14 15:56:09 +02006262 tv->v_type = VAR_FUNC;
6263 }
6264 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006265 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006266 else if (PyBytes_Check(obj))
6267 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006268 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006269
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006270 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006271 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006272 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006273 return -1;
6274
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006275 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 return -1;
6277
6278 tv->v_type = VAR_STRING;
6279 }
6280 else if (PyUnicode_Check(obj))
6281 {
6282 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006283 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006284
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006285 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006286 if (bytes == NULL)
6287 return -1;
6288
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006289 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006290 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006291 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 return -1;
6293
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006294 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006295 {
6296 Py_XDECREF(bytes);
6297 return -1;
6298 }
6299 Py_XDECREF(bytes);
6300
6301 tv->v_type = VAR_STRING;
6302 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006303#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 else if (PyInt_Check(obj))
6305 {
6306 tv->v_type = VAR_NUMBER;
6307 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006308 if (PyErr_Occurred())
6309 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006310 }
6311#endif
6312 else if (PyLong_Check(obj))
6313 {
6314 tv->v_type = VAR_NUMBER;
6315 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006316 if (PyErr_Occurred())
6317 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006318 }
6319 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006320 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006321#ifdef FEAT_FLOAT
6322 else if (PyFloat_Check(obj))
6323 {
6324 tv->v_type = VAR_FLOAT;
6325 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6326 }
6327#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006328 else if (PyObject_HasAttrString(obj, "keys"))
6329 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006330 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006331 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006332 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006333 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006334 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006335 else if (PyNumber_Check(obj))
6336 {
6337 PyObject *num;
6338
6339 if (!(num = PyNumber_Long(obj)))
6340 return -1;
6341
6342 tv->v_type = VAR_NUMBER;
6343 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6344
6345 Py_DECREF(num);
6346 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006347 else if (obj == Py_None)
6348 {
6349 tv->v_type = VAR_SPECIAL;
6350 tv->vval.v_number = VVAL_NONE;
6351 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006352 else
6353 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006354 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006355 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006356 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006357 return -1;
6358 }
6359 return 0;
6360}
6361
6362 static PyObject *
6363ConvertToPyObject(typval_T *tv)
6364{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006365 typval_T *argv;
6366 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006367 if (tv == NULL)
6368 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006369 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006370 return NULL;
6371 }
6372 switch (tv->v_type)
6373 {
6374 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006375 return PyBytes_FromString(tv->vval.v_string == NULL
6376 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006377 case VAR_NUMBER:
6378 return PyLong_FromLong((long) tv->vval.v_number);
6379#ifdef FEAT_FLOAT
6380 case VAR_FLOAT:
6381 return PyFloat_FromDouble((double) tv->vval.v_float);
6382#endif
6383 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006384 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006385 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006386 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006387 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006388 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006389 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006390 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006391 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006392 if (tv->vval.v_partial->pt_argc)
6393 {
6394 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6395 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6396 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6397 }
6398 else
6399 argv = NULL;
6400 if (tv->vval.v_partial->pt_dict != NULL)
6401 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006402 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006403 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006404 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006405 tv->vval.v_partial->pt_dict,
6406 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006407 case VAR_BLOB:
6408 return PyBytes_FromStringAndSize(
6409 (char*) tv->vval.v_blob->bv_ga.ga_data,
6410 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006411 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006412 case VAR_CHANNEL:
6413 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006414 Py_INCREF(Py_None);
6415 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006416 case VAR_SPECIAL:
6417 switch (tv->vval.v_number)
6418 {
6419 case VVAL_FALSE: return AlwaysFalse(NULL);
6420 case VVAL_TRUE: return AlwaysTrue(NULL);
6421 case VVAL_NONE:
6422 case VVAL_NULL: return AlwaysNone(NULL);
6423 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006424 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006425 return NULL;
6426 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006427 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006428}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006429
6430typedef struct
6431{
6432 PyObject_HEAD
6433} CurrentObject;
6434static PyTypeObject CurrentType;
6435
6436 static void
6437init_structs(void)
6438{
6439 vim_memset(&OutputType, 0, sizeof(OutputType));
6440 OutputType.tp_name = "vim.message";
6441 OutputType.tp_basicsize = sizeof(OutputObject);
6442 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6443 OutputType.tp_doc = "vim message object";
6444 OutputType.tp_methods = OutputMethods;
6445#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006446 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6447 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448 OutputType.tp_alloc = call_PyType_GenericAlloc;
6449 OutputType.tp_new = call_PyType_GenericNew;
6450 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006451 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006452#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006453 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6454 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006455 // Disabled, because this causes a crash in test86
6456 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006457#endif
6458
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006459 vim_memset(&IterType, 0, sizeof(IterType));
6460 IterType.tp_name = "vim.iter";
6461 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006462 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006463 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006464 IterType.tp_iter = (getiterfunc)IterIter;
6465 IterType.tp_iternext = (iternextfunc)IterNext;
6466 IterType.tp_dealloc = (destructor)IterDestructor;
6467 IterType.tp_traverse = (traverseproc)IterTraverse;
6468 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006469
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006470 vim_memset(&BufferType, 0, sizeof(BufferType));
6471 BufferType.tp_name = "vim.buffer";
6472 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006473 BufferType.tp_dealloc = (destructor)BufferDestructor;
6474 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006475 BufferType.tp_as_sequence = &BufferAsSeq;
6476 BufferType.tp_as_mapping = &BufferAsMapping;
6477 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6478 BufferType.tp_doc = "vim buffer object";
6479 BufferType.tp_methods = BufferMethods;
6480#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006481 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006482 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483 BufferType.tp_alloc = call_PyType_GenericAlloc;
6484 BufferType.tp_new = call_PyType_GenericNew;
6485 BufferType.tp_free = call_PyObject_Free;
6486#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006487 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006488 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006489#endif
6490
6491 vim_memset(&WindowType, 0, sizeof(WindowType));
6492 WindowType.tp_name = "vim.window";
6493 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006494 WindowType.tp_dealloc = (destructor)WindowDestructor;
6495 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006496 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006497 WindowType.tp_doc = "vim Window object";
6498 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6500 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006501#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6503 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504 WindowType.tp_alloc = call_PyType_GenericAlloc;
6505 WindowType.tp_new = call_PyType_GenericNew;
6506 WindowType.tp_free = call_PyObject_Free;
6507#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006508 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6509 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006510#endif
6511
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006512 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6513 TabPageType.tp_name = "vim.tabpage";
6514 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6516 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006517 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6518 TabPageType.tp_doc = "vim tab page object";
6519 TabPageType.tp_methods = TabPageMethods;
6520#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006522 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6523 TabPageType.tp_new = call_PyType_GenericNew;
6524 TabPageType.tp_free = call_PyObject_Free;
6525#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006526 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006527#endif
6528
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006529 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6530 BufMapType.tp_name = "vim.bufferlist";
6531 BufMapType.tp_basicsize = sizeof(BufMapObject);
6532 BufMapType.tp_as_mapping = &BufMapAsMapping;
6533 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006534 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006535 BufferType.tp_doc = "vim buffer list";
6536
6537 vim_memset(&WinListType, 0, sizeof(WinListType));
6538 WinListType.tp_name = "vim.windowlist";
6539 WinListType.tp_basicsize = sizeof(WinListType);
6540 WinListType.tp_as_sequence = &WinListAsSeq;
6541 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6542 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006543 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006544
6545 vim_memset(&TabListType, 0, sizeof(TabListType));
6546 TabListType.tp_name = "vim.tabpagelist";
6547 TabListType.tp_basicsize = sizeof(TabListType);
6548 TabListType.tp_as_sequence = &TabListAsSeq;
6549 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6550 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551
6552 vim_memset(&RangeType, 0, sizeof(RangeType));
6553 RangeType.tp_name = "vim.range";
6554 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006555 RangeType.tp_dealloc = (destructor)RangeDestructor;
6556 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006557 RangeType.tp_as_sequence = &RangeAsSeq;
6558 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006559 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560 RangeType.tp_doc = "vim Range object";
6561 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006562 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6563 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006564#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006565 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006566 RangeType.tp_alloc = call_PyType_GenericAlloc;
6567 RangeType.tp_new = call_PyType_GenericNew;
6568 RangeType.tp_free = call_PyObject_Free;
6569#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006570 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006571#endif
6572
6573 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6574 CurrentType.tp_name = "vim.currentdata";
6575 CurrentType.tp_basicsize = sizeof(CurrentObject);
6576 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6577 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006578 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006580 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6581 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006583 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6584 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006585#endif
6586
6587 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6588 DictionaryType.tp_name = "vim.dictionary";
6589 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006590 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006591 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006593 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006594 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6595 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006596 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6597 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6598 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006599#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006600 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6601 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006602#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6604 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006605#endif
6606
6607 vim_memset(&ListType, 0, sizeof(ListType));
6608 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006609 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006610 ListType.tp_basicsize = sizeof(ListObject);
6611 ListType.tp_as_sequence = &ListAsSeq;
6612 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006613 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006614 ListType.tp_doc = "list pushing modifications to vim structure";
6615 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006616 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006617 ListType.tp_new = (newfunc)ListConstructor;
6618 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006620 ListType.tp_getattro = (getattrofunc)ListGetattro;
6621 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006622#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006623 ListType.tp_getattr = (getattrfunc)ListGetattr;
6624 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006625#endif
6626
6627 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006628 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006629 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006630 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6631 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006632 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006633 FunctionType.tp_doc = "object that calls vim function";
6634 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006635 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006636 FunctionType.tp_new = (newfunc)FunctionConstructor;
6637 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006638#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006639 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006640#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006641 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006642#endif
6643
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006644 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6645 OptionsType.tp_name = "vim.options";
6646 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006647 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006648 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006649 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006650 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006651 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006652 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6653 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6654 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006655
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006656#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006657 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6658 LoaderType.tp_name = "vim.Loader";
6659 LoaderType.tp_basicsize = sizeof(LoaderObject);
6660 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6661 LoaderType.tp_doc = "vim message object";
6662 LoaderType.tp_methods = LoaderMethods;
6663 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006664#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006665
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006666#if PY_MAJOR_VERSION >= 3
6667 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6668 vimmodule.m_name = "vim";
6669 vimmodule.m_doc = "Vim Python interface\n";
6670 vimmodule.m_size = -1;
6671 vimmodule.m_methods = VimMethods;
6672#endif
6673}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006674
6675#define PYTYPE_READY(type) \
6676 if (PyType_Ready(&type)) \
6677 return -1;
6678
6679 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006680init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006681{
6682 PYTYPE_READY(IterType);
6683 PYTYPE_READY(BufferType);
6684 PYTYPE_READY(RangeType);
6685 PYTYPE_READY(WindowType);
6686 PYTYPE_READY(TabPageType);
6687 PYTYPE_READY(BufMapType);
6688 PYTYPE_READY(WinListType);
6689 PYTYPE_READY(TabListType);
6690 PYTYPE_READY(CurrentType);
6691 PYTYPE_READY(DictionaryType);
6692 PYTYPE_READY(ListType);
6693 PYTYPE_READY(FunctionType);
6694 PYTYPE_READY(OptionsType);
6695 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006696#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006697 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006698#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006699 return 0;
6700}
6701
6702 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006703init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006704{
6705 PyObject *path;
6706 PyObject *path_hook;
6707 PyObject *path_hooks;
6708
6709 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6710 return -1;
6711
6712 if (!(path_hooks = PySys_GetObject("path_hooks")))
6713 {
6714 PyErr_Clear();
6715 path_hooks = PyList_New(1);
6716 PyList_SET_ITEM(path_hooks, 0, path_hook);
6717 if (PySys_SetObject("path_hooks", path_hooks))
6718 {
6719 Py_DECREF(path_hooks);
6720 return -1;
6721 }
6722 Py_DECREF(path_hooks);
6723 }
6724 else if (PyList_Check(path_hooks))
6725 {
6726 if (PyList_Append(path_hooks, path_hook))
6727 {
6728 Py_DECREF(path_hook);
6729 return -1;
6730 }
6731 Py_DECREF(path_hook);
6732 }
6733 else
6734 {
6735 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006736 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006737 "You should now do the following:\n"
6738 "- append vim.path_hook to sys.path_hooks\n"
6739 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6740 VimTryEnd(); /* Discard the error */
6741 Py_DECREF(path_hook);
6742 return 0;
6743 }
6744
6745 if (!(path = PySys_GetObject("path")))
6746 {
6747 PyErr_Clear();
6748 path = PyList_New(1);
6749 Py_INCREF(vim_special_path_object);
6750 PyList_SET_ITEM(path, 0, vim_special_path_object);
6751 if (PySys_SetObject("path", path))
6752 {
6753 Py_DECREF(path);
6754 return -1;
6755 }
6756 Py_DECREF(path);
6757 }
6758 else if (PyList_Check(path))
6759 {
6760 if (PyList_Append(path, vim_special_path_object))
6761 return -1;
6762 }
6763 else
6764 {
6765 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006766 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006767 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6768 VimTryEnd(); /* Discard the error */
6769 }
6770
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006771 return 0;
6772}
6773
6774static BufMapObject TheBufferMap =
6775{
6776 PyObject_HEAD_INIT(&BufMapType)
6777};
6778
6779static WinListObject TheWindowList =
6780{
6781 PyObject_HEAD_INIT(&WinListType)
6782 NULL
6783};
6784
6785static CurrentObject TheCurrent =
6786{
6787 PyObject_HEAD_INIT(&CurrentType)
6788};
6789
6790static TabListObject TheTabPageList =
6791{
6792 PyObject_HEAD_INIT(&TabListType)
6793};
6794
6795static struct numeric_constant {
6796 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006797 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006798} numeric_constants[] = {
6799 {"VAR_LOCKED", VAR_LOCKED},
6800 {"VAR_FIXED", VAR_FIXED},
6801 {"VAR_SCOPE", VAR_SCOPE},
6802 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6803};
6804
6805static struct object_constant {
6806 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006807 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006808} object_constants[] = {
6809 {"buffers", (PyObject *)(void *)&TheBufferMap},
6810 {"windows", (PyObject *)(void *)&TheWindowList},
6811 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6812 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006813
6814 {"Buffer", (PyObject *)&BufferType},
6815 {"Range", (PyObject *)&RangeType},
6816 {"Window", (PyObject *)&WindowType},
6817 {"TabPage", (PyObject *)&TabPageType},
6818 {"Dictionary", (PyObject *)&DictionaryType},
6819 {"List", (PyObject *)&ListType},
6820 {"Function", (PyObject *)&FunctionType},
6821 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006822#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006823 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006824#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006825};
6826
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006827#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006828 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006829 return -1;
6830
6831#define ADD_CHECKED_OBJECT(m, name, obj) \
6832 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006833 PyObject *valObject = obj; \
6834 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006835 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006836 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006837 }
6838
6839 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006840populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006841{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006842 int i;
6843 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006844 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006845 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006846#if PY_VERSION_HEX >= 0x030700f0
6847 PyObject *dict;
6848 PyObject *cls;
6849#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850
6851 for (i = 0; i < (int)(sizeof(numeric_constants)
6852 / sizeof(struct numeric_constant));
6853 ++i)
6854 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006855 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006856
6857 for (i = 0; i < (int)(sizeof(object_constants)
6858 / sizeof(struct object_constant));
6859 ++i)
6860 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006861 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006862
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006863 valObject = object_constants[i].valObject;
6864 Py_INCREF(valObject);
6865 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006866 }
6867
6868 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6869 return -1;
6870 ADD_OBJECT(m, "error", VimError);
6871
Bram Moolenaara9922d62013-05-30 13:01:18 +02006872 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6873 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006874 ADD_CHECKED_OBJECT(m, "options",
6875 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006876
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006877 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006878 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006879 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006880
Bram Moolenaar22081f42016-06-01 20:38:34 +02006881#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006882 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006883 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006884#else
6885 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6886 return -1;
6887#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888 ADD_OBJECT(m, "_getcwd", py_getcwd)
6889
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006890 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891 return -1;
6892 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006893 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006895 if (PyObject_SetAttrString(other_module, "chdir", attr))
6896 {
6897 Py_DECREF(attr);
6898 return -1;
6899 }
6900 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006901
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006902 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006903 {
6904 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006905 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006906 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006907 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6908 {
6909 Py_DECREF(attr);
6910 return -1;
6911 }
6912 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006913 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006914 else
6915 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006916
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006917 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6918 return -1;
6919
6920 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6921
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006922#if PY_VERSION_HEX >= 0x030700f0
6923 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6924 return -1;
6925
6926 dict = PyModule_GetDict(imp);
6927
6928 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6929 {
6930 Py_DECREF(imp);
6931 return -1;
6932 }
6933
6934 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6935 {
6936 Py_DECREF(imp);
6937 return -1;
6938 }
6939
6940 Py_DECREF(imp);
6941
6942 ADD_OBJECT(m, "_find_spec", py_find_spec);
6943#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006944 if (!(imp = PyImport_ImportModule("imp")))
6945 return -1;
6946
6947 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6948 {
6949 Py_DECREF(imp);
6950 return -1;
6951 }
6952
6953 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6954 {
6955 Py_DECREF(py_find_module);
6956 Py_DECREF(imp);
6957 return -1;
6958 }
6959
6960 Py_DECREF(imp);
6961
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006962 ADD_OBJECT(m, "_find_module", py_find_module);
6963 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006964#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006965
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006966 return 0;
6967}