blob: b27f93e49eb333a545659458f7f79a4afabf4831 [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 Moolenaar264b74f2019-01-24 17:18:42 +010027#define ENC_OPT ((char *)p_enc)
Bram Moolenaard620aa92013-05-17 16:40:06 +020028#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020030static const char *vim_special_path = "_vim_path_";
31
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020032#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020033#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020034#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010035#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
36#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
37#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020038
39#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
40 ? "(NULL)" \
41 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020042
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020043#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020044 N_("empty keys are not allowed"))
45#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
46#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
47#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
48#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
49#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
50#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020051#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020052 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020053#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020054 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020055 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020056
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020057#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
58#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020059#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020060
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020061typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020062typedef void (*runner)(const char *, void *
63#ifdef PY_CAN_RECURSE
64 , PyGILState_STATE *
65#endif
66 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020067
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020068static int ConvertFromPyObject(PyObject *, typval_T *);
69static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020070static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020071static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020072static PyObject *WindowNew(win_T *, tabpage_T *);
73static PyObject *BufferNew (buf_T *);
74static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020075
76static PyInt RangeStart;
77static PyInt RangeEnd;
78
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020079static PyObject *globals;
80
Bram Moolenaarf4258302013-06-02 18:20:17 +020081static PyObject *py_chdir;
82static PyObject *py_fchdir;
83static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020084static PyObject *vim_module;
85static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020086
Bram Moolenaar79a494d2018-07-22 04:30:21 +020087#if PY_VERSION_HEX >= 0x030700f0
88static PyObject *py_find_spec;
89#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020090static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020091#endif
Bram Moolenaarb999ba22019-02-14 13:28:45 +010092static PyObject *py_find_module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +020093
94static PyObject *VimError;
95
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020096/*
97 * obtain a lock on the Vim data structures
98 */
99 static void
100Python_Lock_Vim(void)
101{
102}
103
104/*
105 * release a lock on the Vim data structures
106 */
107 static void
108Python_Release_Vim(void)
109{
110}
111
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200112/*
113 * The "todecref" argument holds a pointer to PyObject * that must be
114 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
115 * was needed to generate returned value is object.
116 *
117 * Use Py_XDECREF to decrement reference count.
118 */
119 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200122 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200123
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 {
126
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200127 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
128 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 return NULL;
130
131 *todecref = NULL;
132 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 PyObject *bytes;
136
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200137 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200138 return NULL;
139
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200140 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
141 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200142 {
143 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200144 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200145 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200146
147 *todecref = bytes;
148 }
149 else
150 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected str() or unicode() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200156 PyErr_FORMAT(PyExc_TypeError,
157 N_("expected bytes() or str() instance, but got %s"),
158 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200159#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160 return NULL;
161 }
162
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200163 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200164}
165
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200166#define NUMBER_LONG 1
167#define NUMBER_INT 2
168#define NUMBER_NATURAL 4
169#define NUMBER_UNSIGNED 8
170
171 static int
172NumberToLong(PyObject *obj, long *result, int flags)
173{
174#if PY_MAJOR_VERSION < 3
175 if (PyInt_Check(obj))
176 {
177 *result = PyInt_AsLong(obj);
178 if (PyErr_Occurred())
179 return -1;
180 }
181 else
182#endif
183 if (PyLong_Check(obj))
184 {
185 *result = PyLong_AsLong(obj);
186 if (PyErr_Occurred())
187 return -1;
188 }
189 else if (PyNumber_Check(obj))
190 {
191 PyObject *num;
192
193 if (!(num = PyNumber_Long(obj)))
194 return -1;
195
196 *result = PyLong_AsLong(num);
197
198 Py_DECREF(num);
199
200 if (PyErr_Occurred())
201 return -1;
202 }
203 else
204 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200205#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200207 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 "coercing to long(), but got %s"),
209 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200210#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200211 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200212 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200213 "but got %s"),
214 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200216 return -1;
217 }
218
219 if (flags & NUMBER_INT)
220 {
221 if (*result > INT_MAX)
222 {
223 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200224 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200225 return -1;
226 }
227 else if (*result < INT_MIN)
228 {
229 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200230 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200231 return -1;
232 }
233 }
234
235 if (flags & NUMBER_NATURAL)
236 {
237 if (*result <= 0)
238 {
239 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100240 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200241 return -1;
242 }
243 }
244 else if (flags & NUMBER_UNSIGNED)
245 {
246 if (*result < 0)
247 {
248 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200249 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200250 return -1;
251 }
252 }
253
254 return 0;
255}
256
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200257 static int
258add_string(PyObject *list, char *s)
259{
260 PyObject *string;
261
262 if (!(string = PyString_FromString(s)))
263 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200264
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200265 if (PyList_Append(list, string))
266 {
267 Py_DECREF(string);
268 return -1;
269 }
270
271 Py_DECREF(string);
272 return 0;
273}
274
275 static PyObject *
276ObjectDir(PyObject *self, char **attributes)
277{
278 PyMethodDef *method;
279 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200282 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 return NULL;
284
285 if (self)
286 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200287 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 return NULL;
291 }
292
293 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 return NULL;
298 }
299
300#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200301 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200302 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200303 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200304 return NULL;
305 }
306#endif
307
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200308 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200309}
310
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312 */
313
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200314/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200316
317static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318
319typedef struct
320{
321 PyObject_HEAD
322 long softspace;
323 long error;
324} OutputObject;
325
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200326static char *OutputAttrs[] = {
327 "softspace",
328 NULL
329};
330
331 static PyObject *
332OutputDir(PyObject *self)
333{
334 return ObjectDir(self, OutputAttrs);
335}
336
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200338OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200340 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200342 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200343 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200344 return -1;
345 }
346
347 if (strcmp(name, "softspace") == 0)
348 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200349 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200351 return 0;
352 }
353
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200354 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200355 return -1;
356}
357
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200358/* Buffer IO, we write one whole line at a time. */
359static garray_T io_ga = {0, 0, 1, 80, NULL};
360static writefn old_fn = NULL;
361
362 static void
363PythonIO_Flush(void)
364{
365 if (old_fn != NULL && io_ga.ga_len > 0)
366 {
367 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
368 old_fn((char_u *)io_ga.ga_data);
369 }
370 io_ga.ga_len = 0;
371}
372
373 static void
374writer(writefn fn, char_u *str, PyInt n)
375{
376 char_u *ptr;
377
378 /* Flush when switching output function. */
379 if (fn != old_fn)
380 PythonIO_Flush();
381 old_fn = fn;
382
383 /* Write each NL separated line. Text after the last NL is kept for
384 * writing later. */
385 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
386 {
387 PyInt len = ptr - str;
388
389 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
390 break;
391
392 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
393 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
394 fn((char_u *)io_ga.ga_data);
395 str = ptr + 1;
396 n -= len + 1;
397 io_ga.ga_len = 0;
398 }
399
400 /* Put the remaining text into io_ga for later printing. */
401 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100420 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200421 Python_Release_Vim();
422 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425 return 0;
426}
427
428 static PyObject *
429OutputWrite(OutputObject *self, PyObject *string)
430{
431 if (write_output(self, string))
432 return NULL;
433
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 Py_INCREF(Py_None);
435 return Py_None;
436}
437
438 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200439OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 PyObject *iterator;
442 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200447 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200449 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200450 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 Py_DECREF(iterator);
452 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200453 return NULL;
454 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456 }
457
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200458 Py_DECREF(iterator);
459
460 /* Iterator may have finished due to an exception */
461 if (PyErr_Occurred())
462 return NULL;
463
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200464 Py_INCREF(Py_None);
465 return Py_None;
466}
467
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100468 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100469AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470{
471 /* do nothing */
472 Py_INCREF(Py_None);
473 return Py_None;
474}
475
Bram Moolenaard4247472015-11-02 13:28:59 +0100476 static PyObject *
477AlwaysFalse(PyObject *self UNUSED)
478{
479 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100480 PyObject *ret = Py_False;
481 Py_INCREF(ret);
482 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100483}
484
485 static PyObject *
486AlwaysTrue(PyObject *self UNUSED)
487{
488 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100489 PyObject *ret = Py_True;
490 Py_INCREF(ret);
491 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100492}
493
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494/***************/
495
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200496static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200497 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200498 {"write", (PyCFunction)OutputWrite, METH_O, ""},
499 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100500 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
501 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
502 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
503 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
504 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
505 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200506 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200507 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200508 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200509};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200510
511static OutputObject Output =
512{
513 PyObject_HEAD_INIT(&OutputType)
514 0,
515 0
516};
517
518static OutputObject Error =
519{
520 PyObject_HEAD_INIT(&OutputType)
521 0,
522 1
523};
524
525 static int
526PythonIO_Init_io(void)
527{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200528 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
529 return -1;
530 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
531 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200532
533 if (PyErr_Occurred())
534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100535 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200536 return -1;
537 }
538
539 return 0;
540}
541
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200542#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200543static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
544
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200545typedef struct
546{
547 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200548 char *fullname;
549 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200550} LoaderObject;
551static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200552
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200553 static void
554LoaderDestructor(LoaderObject *self)
555{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200556 vim_free(self->fullname);
557 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200558 DESTRUCTOR_FINISH(self);
559}
560
561 static PyObject *
562LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
563{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200564 char *fullname = self->fullname;
565 PyObject *result = self->result;
566 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200567
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200568 if (!fullname)
569 {
570 module = result ? result : Py_None;
571 Py_INCREF(module);
572 return module;
573 }
574
575 module = call_load_module(fullname, (int)STRLEN(fullname), result);
576
577 self->fullname = NULL;
578 self->result = module;
579
580 vim_free(fullname);
581 Py_DECREF(result);
582
583 if (!module)
584 {
585 if (PyErr_Occurred())
586 return NULL;
587
588 Py_INCREF(Py_None);
589 return Py_None;
590 }
591
592 Py_INCREF(module);
593 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200594}
595
596static struct PyMethodDef LoaderMethods[] = {
597 /* name, function, calling, doc */
598 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
599 { NULL, NULL, 0, NULL}
600};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200601#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200602
603/* Check to see whether a Vim error has been reported, or a keyboard
604 * interrupt has been detected.
605 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200606
607 static void
608VimTryStart(void)
609{
610 ++trylevel;
611}
612
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200613 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200614VimTryEnd(void)
615{
616 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100617 /* Without this it stops processing all subsequent Vim script commands and
618 * generates strange error messages if I e.g. try calling Test() in a cycle
619 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200620 did_emsg = FALSE;
621 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200622 if (got_int)
623 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100624 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100625 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100626 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200627 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200628 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200629 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100630 else if (msg_list != NULL && *msg_list != NULL)
631 {
632 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100633 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100634
635 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
636
637 if (msg == NULL)
638 {
639 PyErr_NoMemory();
640 return -1;
641 }
642
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100643 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100644
645 free_global_msglist();
646
647 if (should_free)
648 vim_free(msg);
649
650 return -1;
651 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200652 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200653 return (PyErr_Occurred() ? -1 : 0);
654 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200655 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200656 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100657 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200658 return -1;
659 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100660 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200661 else
662 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200663 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200664 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200665 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200666 }
667}
668
669 static int
670VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200671{
672 if (got_int)
673 {
674 PyErr_SetNone(PyExc_KeyboardInterrupt);
675 return 1;
676 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200677 return 0;
678}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679
680/* Vim module - Implementation
681 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200682
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200684VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200685{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200686 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200687 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200688 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689
Bram Moolenaar389a1792013-06-23 13:00:44 +0200690 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 return NULL;
692
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 Py_BEGIN_ALLOW_THREADS
694 Python_Lock_Vim();
695
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200696 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200697 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200698 update_screen(VALID);
699
700 Python_Release_Vim();
701 Py_END_ALLOW_THREADS
702
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200703 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200704 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200705 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200706 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200709 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711}
712
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713/*
714 * Function to translate a typval_T into a PyObject; this will recursively
715 * translate lists/dictionaries into their Python equivalents.
716 *
717 * The depth parameter is to avoid infinite recursion, set it to 1 when
718 * you call VimToPython.
719 */
720 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200721VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200722{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200723 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200724 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200725 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726
727 /* Avoid infinite recursion */
728 if (depth > 100)
729 {
730 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200731 ret = Py_None;
732 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200733 }
734
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200735 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736 * then and we can use it again. */
737 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
738 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
739 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200740 sprintf(ptrBuf, "%p",
741 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
742 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200743
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200744 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 Py_INCREF(ret);
747 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200748 }
749 }
750
751 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200752 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200753 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754 else if (our_tv->v_type == VAR_NUMBER)
755 {
756 char buf[NUMBUFLEN];
757
758 /* For backwards compatibility numbers are stored as strings. */
759 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200760 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100762#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 else if (our_tv->v_type == VAR_FLOAT)
764 {
765 char buf[NUMBUFLEN];
766
767 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200768 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100770#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 else if (our_tv->v_type == VAR_LIST)
772 {
773 list_T *list = our_tv->vval.v_list;
774 listitem_T *curr;
775
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 if (list == NULL)
777 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200779 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 return NULL;
781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200783 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 return NULL;
786 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200788 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
789 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200790 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200792 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200793 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 {
797 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200798 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200799 return NULL;
800 }
801 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200802 }
803 }
804 else if (our_tv->v_type == VAR_DICT)
805 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200806
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100807 hashtab_T *ht;
808 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200809 hashitem_T *hi;
810 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100811
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200812 if (our_tv->vval.v_dict == NULL)
813 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100814 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200815
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200816 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200817 return NULL;
818
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200819 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200820 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200821 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200822 return NULL;
823 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100825 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200826 for (hi = ht->ht_array; todo > 0; ++hi)
827 {
828 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200829 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200830 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200831
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200832 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200833 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200834 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200835 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200836 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200837 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200839 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200840 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200841 Py_DECREF(newObj);
842 return NULL;
843 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200844 }
845 }
846 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100847 else if (our_tv->v_type == VAR_SPECIAL)
848 {
849 if (our_tv->vval.v_number == VVAL_FALSE)
850 {
851 ret = Py_False;
852 Py_INCREF(ret);
853 }
854 else if (our_tv->vval.v_number == VVAL_TRUE)
855 {
856 ret = Py_True;
857 Py_INCREF(ret);
858 }
859 else
860 {
861 Py_INCREF(Py_None);
862 ret = Py_None;
863 }
864 return ret;
865 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100866 else if (our_tv->v_type == VAR_BLOB)
867 ret = PyBytes_FromStringAndSize(
868 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
869 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200870 else
871 {
872 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200873 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 }
875
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200876 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200877}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878
879 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200880VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200881{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200882 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200883 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200884 PyObject *string;
885 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200887 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200888
Bram Moolenaar389a1792013-06-23 13:00:44 +0200889 if (!PyArg_ParseTuple(args, "O", &string))
890 return NULL;
891
892 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200893 return NULL;
894
895 Py_BEGIN_ALLOW_THREADS
896 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200897 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200899 Python_Release_Vim();
900 Py_END_ALLOW_THREADS
901
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 Py_XDECREF(todecref);
903
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200904 if (VimTryEnd())
905 return NULL;
906
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200907 if (our_tv == NULL)
908 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200909 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200910 return NULL;
911 }
912
913 /* Convert the Vim type into a Python type. Create a dictionary that's
914 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200915 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200916 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200917 else
918 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200919 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200920 Py_DECREF(lookup_dict);
921 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200922
923
924 Py_BEGIN_ALLOW_THREADS
925 Python_Lock_Vim();
926 free_tv(our_tv);
927 Python_Release_Vim();
928 Py_END_ALLOW_THREADS
929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200930 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200931}
932
Bram Moolenaardb913952012-06-29 12:54:53 +0200933static PyObject *ConvertToPyObject(typval_T *);
934
935 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200936VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200937{
Bram Moolenaardb913952012-06-29 12:54:53 +0200938 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200939 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940 char_u *expr;
941 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200942
Bram Moolenaar389a1792013-06-23 13:00:44 +0200943 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200944 return NULL;
945
946 Py_BEGIN_ALLOW_THREADS
947 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200948 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200949 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200950 Python_Release_Vim();
951 Py_END_ALLOW_THREADS
952
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953 Py_XDECREF(todecref);
954
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200955 if (VimTryEnd())
956 return NULL;
957
Bram Moolenaardb913952012-06-29 12:54:53 +0200958 if (our_tv == NULL)
959 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200960 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200961 return NULL;
962 }
963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200965 Py_BEGIN_ALLOW_THREADS
966 Python_Lock_Vim();
967 free_tv(our_tv);
968 Python_Release_Vim();
969 Py_END_ALLOW_THREADS
970
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200971 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200972}
973
974 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200975VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200976{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200977 char_u *str;
978 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200979 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200980
Bram Moolenaar389a1792013-06-23 13:00:44 +0200981 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200982 return NULL;
983
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200984 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200985
986 Py_XDECREF(todecref);
987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200988 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200989}
990
Bram Moolenaarf4258302013-06-02 18:20:17 +0200991 static PyObject *
992_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
993{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200994 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200995 PyObject *newwd;
996 PyObject *todecref;
997 char_u *new_dir;
998
Bram Moolenaard4209d22013-06-05 20:34:15 +0200999 if (_chdir == NULL)
1000 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001001 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001002 return NULL;
1003
1004 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1005 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001006 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001007 return NULL;
1008 }
1009
1010 if (!(new_dir = StringToChars(newwd, &todecref)))
1011 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001012 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001013 Py_DECREF(newwd);
1014 return NULL;
1015 }
1016
1017 VimTryStart();
1018
1019 if (vim_chdir(new_dir))
1020 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001021 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001022 Py_DECREF(newwd);
1023 Py_XDECREF(todecref);
1024
1025 if (VimTryEnd())
1026 return NULL;
1027
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001028 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001029 return NULL;
1030 }
1031
1032 Py_DECREF(newwd);
1033 Py_XDECREF(todecref);
1034
1035 post_chdir(FALSE);
1036
1037 if (VimTryEnd())
1038 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001039 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001040 return NULL;
1041 }
1042
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001043 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001044}
1045
1046 static PyObject *
1047VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1048{
1049 return _VimChdir(py_chdir, args, kwargs);
1050}
1051
1052 static PyObject *
1053VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1054{
1055 return _VimChdir(py_fchdir, args, kwargs);
1056}
1057
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001058typedef struct {
1059 PyObject *callable;
1060 PyObject *result;
1061} map_rtp_data;
1062
1063 static void
1064map_rtp_callback(char_u *path, void *_data)
1065{
1066 void **data = (void **) _data;
1067 PyObject *pathObject;
1068 map_rtp_data *mr_data = *((map_rtp_data **) data);
1069
Bram Moolenaar41009372013-07-01 22:03:04 +02001070 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001071 {
1072 *data = NULL;
1073 return;
1074 }
1075
1076 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1077 pathObject, NULL);
1078
1079 Py_DECREF(pathObject);
1080
1081 if (!mr_data->result || mr_data->result != Py_None)
1082 *data = NULL;
1083 else
1084 {
1085 Py_DECREF(mr_data->result);
1086 mr_data->result = NULL;
1087 }
1088}
1089
1090 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001091VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001092{
1093 map_rtp_data data;
1094
Bram Moolenaar389a1792013-06-23 13:00:44 +02001095 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001096 data.result = NULL;
1097
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001098 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001099
1100 if (data.result == NULL)
1101 {
1102 if (PyErr_Occurred())
1103 return NULL;
1104 else
1105 {
1106 Py_INCREF(Py_None);
1107 return Py_None;
1108 }
1109 }
1110 return data.result;
1111}
1112
1113/*
1114 * _vim_runtimepath_ special path implementation.
1115 */
1116
1117 static void
1118map_finder_callback(char_u *path, void *_data)
1119{
1120 void **data = (void **) _data;
1121 PyObject *list = *((PyObject **) data);
1122 PyObject *pathObject1, *pathObject2;
1123 char *pathbuf;
1124 size_t pathlen;
1125
1126 pathlen = STRLEN(path);
1127
1128#if PY_MAJOR_VERSION < 3
1129# define PY_MAIN_DIR_STRING "python2"
1130#else
1131# define PY_MAIN_DIR_STRING "python3"
1132#endif
1133#define PY_ALTERNATE_DIR_STRING "pythonx"
1134
1135#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1136 if (!(pathbuf = PyMem_New(char,
1137 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1138 {
1139 PyErr_NoMemory();
1140 *data = NULL;
1141 return;
1142 }
1143
1144 mch_memmove(pathbuf, path, pathlen + 1);
1145 add_pathsep((char_u *) pathbuf);
1146
1147 pathlen = STRLEN(pathbuf);
1148 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1149 PYTHONX_STRING_LENGTH + 1);
1150
1151 if (!(pathObject1 = PyString_FromString(pathbuf)))
1152 {
1153 *data = NULL;
1154 PyMem_Free(pathbuf);
1155 return;
1156 }
1157
1158 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1159 PYTHONX_STRING_LENGTH + 1);
1160
1161 if (!(pathObject2 = PyString_FromString(pathbuf)))
1162 {
1163 Py_DECREF(pathObject1);
1164 PyMem_Free(pathbuf);
1165 *data = NULL;
1166 return;
1167 }
1168
1169 PyMem_Free(pathbuf);
1170
1171 if (PyList_Append(list, pathObject1)
1172 || PyList_Append(list, pathObject2))
1173 *data = NULL;
1174
1175 Py_DECREF(pathObject1);
1176 Py_DECREF(pathObject2);
1177}
1178
1179 static PyObject *
1180Vim_GetPaths(PyObject *self UNUSED)
1181{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001182 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001183
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001184 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001185 return NULL;
1186
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001187 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001188
1189 if (PyErr_Occurred())
1190 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001191 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001192 return NULL;
1193 }
1194
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001195 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001196}
1197
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001198#if PY_VERSION_HEX >= 0x030700f0
1199 static PyObject *
1200FinderFindSpec(PyObject *self, PyObject *args)
1201{
1202 char *fullname;
1203 PyObject *paths;
1204 PyObject *target = Py_None;
1205 PyObject *spec;
1206
1207 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1208 return NULL;
1209
1210 if (!(paths = Vim_GetPaths(self)))
1211 return NULL;
1212
1213 spec = PyObject_CallFunction(py_find_spec, "sNN", fullname, paths, target);
1214
1215 Py_DECREF(paths);
1216
1217 if (!spec)
1218 {
1219 if (PyErr_Occurred())
1220 return NULL;
1221
1222 Py_INCREF(Py_None);
1223 return Py_None;
1224 }
1225
1226 return spec;
1227}
1228#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001229 static PyObject *
1230call_load_module(char *name, int len, PyObject *find_module_result)
1231{
1232 PyObject *fd, *pathname, *description;
1233
Bram Moolenaarc476e522013-06-23 13:46:40 +02001234 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001235 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001236 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001237 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001238 Py_TYPE_NAME(find_module_result));
1239 return NULL;
1240 }
1241 if (PyTuple_GET_SIZE(find_module_result) != 3)
1242 {
1243 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001244 N_("expected 3-tuple as imp.find_module() result, but got "
1245 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001246 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001247 return NULL;
1248 }
1249
1250 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1251 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1252 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1253 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001254 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001255 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001256 return NULL;
1257 }
1258
1259 return PyObject_CallFunction(py_load_module,
1260 "s#OOO", name, len, fd, pathname, description);
1261}
1262
1263 static PyObject *
1264find_module(char *fullname, char *tail, PyObject *new_path)
1265{
1266 PyObject *find_module_result;
1267 PyObject *module;
1268 char *dot;
1269
Bram Moolenaar41009372013-07-01 22:03:04 +02001270 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001271 {
1272 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001273 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001274 * first component
1275 */
1276 PyObject *newest_path;
1277 int partlen = (int) (dot - 1 - tail);
1278
1279 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1280 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001281 {
1282 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1283 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001284 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001285 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001286
1287 if (!(module = call_load_module(
1288 fullname,
1289 ((int) (tail - fullname)) + partlen,
1290 find_module_result)))
1291 {
1292 Py_DECREF(find_module_result);
1293 return NULL;
1294 }
1295
1296 Py_DECREF(find_module_result);
1297
1298 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1299 {
1300 Py_DECREF(module);
1301 return NULL;
1302 }
1303
1304 Py_DECREF(module);
1305
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001306 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001307
1308 Py_DECREF(newest_path);
1309
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001310 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001311 }
1312 else
1313 {
1314 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1315 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001316 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001317 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1318 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001319 return NULL;
1320 }
1321
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001322 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001323 }
1324}
1325
1326 static PyObject *
1327FinderFindModule(PyObject *self, PyObject *args)
1328{
1329 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001330 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001331 PyObject *new_path;
1332 LoaderObject *loader;
1333
1334 if (!PyArg_ParseTuple(args, "s", &fullname))
1335 return NULL;
1336
1337 if (!(new_path = Vim_GetPaths(self)))
1338 return NULL;
1339
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001340 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001341
1342 Py_DECREF(new_path);
1343
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001344 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001345 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001346 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001347 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001348
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001349 Py_INCREF(Py_None);
1350 return Py_None;
1351 }
1352
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001353 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001354 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001355 Py_DECREF(result);
1356 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001357 return NULL;
1358 }
1359
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001360 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1361 {
1362 vim_free(fullname);
1363 Py_DECREF(result);
1364 return NULL;
1365 }
1366
1367 loader->fullname = fullname;
1368 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001369
1370 return (PyObject *) loader;
1371}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001372#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001373
1374 static PyObject *
1375VimPathHook(PyObject *self UNUSED, PyObject *args)
1376{
1377 char *path;
1378
1379 if (PyArg_ParseTuple(args, "s", &path)
1380 && STRCMP(path, vim_special_path) == 0)
1381 {
1382 Py_INCREF(vim_module);
1383 return vim_module;
1384 }
1385
1386 PyErr_Clear();
1387 PyErr_SetNone(PyExc_ImportError);
1388 return NULL;
1389}
1390
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001391/*
1392 * Vim module - Definitions
1393 */
1394
1395static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001396 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001397 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001398 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001399 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1400 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001401 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1402 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001403 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001404#if PY_VERSION_HEX >= 0x030700f0
1405 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
1406#else
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001407 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001408#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001409 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1410 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1411 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001412};
1413
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001414/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001415 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001416 */
1417
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001418static PyTypeObject IterType;
1419
1420typedef PyObject *(*nextfun)(void **);
1421typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001422typedef int (*traversefun)(void *, visitproc, void *);
1423typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001424
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001425/* Main purpose of this object is removing the need for do python
1426 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1427 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001428
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001429typedef struct
1430{
1431 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001432 void *cur;
1433 nextfun next;
1434 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001435 traversefun traverse;
1436 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001437} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001438
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001439 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001440IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1441 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001442{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001443 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001444
Bram Moolenaar774267b2013-05-21 20:51:59 +02001445 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001446 self->cur = start;
1447 self->next = next;
1448 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001449 self->traverse = traverse;
1450 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001451
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001452 return (PyObject *)(self);
1453}
1454
1455 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001456IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001457{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001458 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001459 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001460 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001461}
1462
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001463 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001464IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001465{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001466 if (self->traverse != NULL)
1467 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001468 else
1469 return 0;
1470}
1471
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001472/* Mac OSX defines clear() somewhere. */
1473#ifdef clear
1474# undef clear
1475#endif
1476
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001477 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001478IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001479{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001480 if (self->clear != NULL)
1481 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001482 else
1483 return 0;
1484}
1485
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001486 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001487IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001488{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001489 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001490}
1491
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001492 static PyObject *
1493IterIter(PyObject *self)
1494{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001495 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001496 return self;
1497}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001498
Bram Moolenaardb913952012-06-29 12:54:53 +02001499typedef struct pylinkedlist_S {
1500 struct pylinkedlist_S *pll_next;
1501 struct pylinkedlist_S *pll_prev;
1502 PyObject *pll_obj;
1503} pylinkedlist_T;
1504
1505static pylinkedlist_T *lastdict = NULL;
1506static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001507static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001508
1509 static void
1510pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1511{
1512 if (ref->pll_prev == NULL)
1513 {
1514 if (ref->pll_next == NULL)
1515 {
1516 *last = NULL;
1517 return;
1518 }
1519 }
1520 else
1521 ref->pll_prev->pll_next = ref->pll_next;
1522
1523 if (ref->pll_next == NULL)
1524 *last = ref->pll_prev;
1525 else
1526 ref->pll_next->pll_prev = ref->pll_prev;
1527}
1528
1529 static void
1530pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1531{
1532 if (*last == NULL)
1533 ref->pll_prev = NULL;
1534 else
1535 {
1536 (*last)->pll_next = ref;
1537 ref->pll_prev = *last;
1538 }
1539 ref->pll_next = NULL;
1540 ref->pll_obj = self;
1541 *last = ref;
1542}
1543
1544static PyTypeObject DictionaryType;
1545
1546typedef struct
1547{
1548 PyObject_HEAD
1549 dict_T *dict;
1550 pylinkedlist_T ref;
1551} DictionaryObject;
1552
Bram Moolenaara9922d62013-05-30 13:01:18 +02001553static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1554
1555#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1556
Bram Moolenaardb913952012-06-29 12:54:53 +02001557 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001558DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001559{
1560 DictionaryObject *self;
1561
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001563 if (self == NULL)
1564 return NULL;
1565 self->dict = dict;
1566 ++dict->dv_refcount;
1567
1568 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1569
1570 return (PyObject *)(self);
1571}
1572
Bram Moolenaara9922d62013-05-30 13:01:18 +02001573 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001574py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001575{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001576 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001578 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001579 {
1580 PyErr_NoMemory();
1581 return NULL;
1582 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001583 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001584
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001585 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001586}
1587
1588 static PyObject *
1589DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1590{
1591 DictionaryObject *self;
1592 dict_T *dict;
1593
1594 if (!(dict = py_dict_alloc()))
1595 return NULL;
1596
1597 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1598
1599 --dict->dv_refcount;
1600
1601 if (kwargs || PyTuple_Size(args))
1602 {
1603 PyObject *tmp;
1604 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1605 {
1606 Py_DECREF(self);
1607 return NULL;
1608 }
1609
1610 Py_DECREF(tmp);
1611 }
1612
1613 return (PyObject *)(self);
1614}
1615
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001616 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001617DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001618{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001619 pyll_remove(&self->ref, &lastdict);
1620 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001621
1622 DESTRUCTOR_FINISH(self);
1623}
1624
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001625static char *DictionaryAttrs[] = {
1626 "locked", "scope",
1627 NULL
1628};
1629
1630 static PyObject *
1631DictionaryDir(PyObject *self)
1632{
1633 return ObjectDir(self, DictionaryAttrs);
1634}
1635
Bram Moolenaardb913952012-06-29 12:54:53 +02001636 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001637DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001638{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001639 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001640 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001641 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001642 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001643 return -1;
1644 }
1645
1646 if (strcmp(name, "locked") == 0)
1647 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001648 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001649 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001650 PyErr_SET_STRING(PyExc_TypeError,
1651 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001652 return -1;
1653 }
1654 else
1655 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001656 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001657 if (istrue == -1)
1658 return -1;
1659 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001660 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001661 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001662 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001663 }
1664 return 0;
1665 }
1666 else
1667 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001668 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001669 return -1;
1670 }
1671}
1672
1673 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001674DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001675{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001676 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001677}
1678
Bram Moolenaara9922d62013-05-30 13:01:18 +02001679#define DICT_FLAG_HAS_DEFAULT 0x01
1680#define DICT_FLAG_POP 0x02
1681#define DICT_FLAG_NONE_DEFAULT 0x04
1682#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1683#define DICT_FLAG_RETURN_PAIR 0x10
1684
Bram Moolenaardb913952012-06-29 12:54:53 +02001685 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001686_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001687{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001688 PyObject *keyObject;
1689 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001690 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001691 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001692 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001693 dict_T *dict = self->dict;
1694 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001695 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001696
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697 if (flags & DICT_FLAG_HAS_DEFAULT)
1698 {
1699 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1700 return NULL;
1701 }
1702 else
1703 keyObject = args;
1704
1705 if (flags & DICT_FLAG_RETURN_BOOL)
1706 defObject = Py_False;
1707
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001708 if (!(key = StringToChars(keyObject, &todecref)))
1709 return NULL;
1710
1711 if (*key == NUL)
1712 {
1713 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001714 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001715 return NULL;
1716 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001717
Bram Moolenaara9922d62013-05-30 13:01:18 +02001718 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001719
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001720 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001721
Bram Moolenaara9922d62013-05-30 13:01:18 +02001722 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001723 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001724 if (defObject)
1725 {
1726 Py_INCREF(defObject);
1727 return defObject;
1728 }
1729 else
1730 {
1731 PyErr_SetObject(PyExc_KeyError, keyObject);
1732 return NULL;
1733 }
1734 }
1735 else if (flags & DICT_FLAG_RETURN_BOOL)
1736 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001737 ret = Py_True;
1738 Py_INCREF(ret);
1739 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001740 }
1741
1742 di = dict_lookup(hi);
1743
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001744 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001745 return NULL;
1746
1747 if (flags & DICT_FLAG_POP)
1748 {
1749 if (dict->dv_lock)
1750 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001751 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001752 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001753 return NULL;
1754 }
1755
1756 hash_remove(&dict->dv_hashtab, hi);
1757 dictitem_free(di);
1758 }
1759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001760 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001761}
1762
1763 static PyObject *
1764DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1765{
1766 return _DictionaryItem(self, keyObject, 0);
1767}
1768
1769 static int
1770DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1771{
1772 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001773 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001774
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001775 if (rObj == NULL)
1776 return -1;
1777
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001778 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001779
Bram Moolenaardee2e312013-06-23 16:35:47 +02001780 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001782 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001783}
1784
1785typedef struct
1786{
1787 hashitem_T *ht_array;
1788 long_u ht_used;
1789 hashtab_T *ht;
1790 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001791 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792} dictiterinfo_T;
1793
1794 static PyObject *
1795DictionaryIterNext(dictiterinfo_T **dii)
1796{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001797 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798
1799 if (!(*dii)->todo)
1800 return NULL;
1801
1802 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1803 (*dii)->ht->ht_used != (*dii)->ht_used)
1804 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001805 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001806 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001807 return NULL;
1808 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001809
Bram Moolenaara9922d62013-05-30 13:01:18 +02001810 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1811 ++((*dii)->hi);
1812
1813 --((*dii)->todo);
1814
Bram Moolenaar41009372013-07-01 22:03:04 +02001815 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816 return NULL;
1817
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001818 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001819}
1820
1821 static PyObject *
1822DictionaryIter(DictionaryObject *self)
1823{
1824 dictiterinfo_T *dii;
1825 hashtab_T *ht;
1826
1827 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1828 {
1829 PyErr_NoMemory();
1830 return NULL;
1831 }
1832
1833 ht = &self->dict->dv_hashtab;
1834 dii->ht_array = ht->ht_array;
1835 dii->ht_used = ht->ht_used;
1836 dii->ht = ht;
1837 dii->hi = dii->ht_array;
1838 dii->todo = dii->ht_used;
1839
1840 return IterNew(dii,
1841 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1842 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001843}
1844
1845 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001846DictionaryAssItem(
1847 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001848{
1849 char_u *key;
1850 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001851 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001852 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001853 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001854
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001855 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001857 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001858 return -1;
1859 }
1860
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001861 if (!(key = StringToChars(keyObject, &todecref)))
1862 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001863
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001864 if (*key == NUL)
1865 {
1866 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001867 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001868 return -1;
1869 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001870
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001871 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001872
1873 if (valObject == NULL)
1874 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001875 hashitem_T *hi;
1876
Bram Moolenaardb913952012-06-29 12:54:53 +02001877 if (di == NULL)
1878 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001879 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001880 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001881 return -1;
1882 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001883 hi = hash_find(&dict->dv_hashtab, di->di_key);
1884 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001885 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001886 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001887 return 0;
1888 }
1889
1890 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001891 {
1892 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001894 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001895
1896 if (di == NULL)
1897 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001898 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001900 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 PyErr_NoMemory();
1902 return -1;
1903 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001904 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001905
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001906 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 {
1908 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001909 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001910 RAISE_KEY_ADD_FAIL(key);
1911 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001912 return -1;
1913 }
1914 }
1915 else
1916 clear_tv(&di->di_tv);
1917
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001918 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001919
1920 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001921 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001922 return 0;
1923}
1924
Bram Moolenaara9922d62013-05-30 13:01:18 +02001925typedef PyObject *(*hi_to_py)(hashitem_T *);
1926
Bram Moolenaardb913952012-06-29 12:54:53 +02001927 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001928DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001929{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001930 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 long_u todo = dict->dv_hashtab.ht_used;
1932 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001933 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001934 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001935 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001936
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001937 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001938 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1939 {
1940 if (!HASHITEM_EMPTY(hi))
1941 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001942 if (!(newObj = hiconvert(hi)))
1943 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001944 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001945 return NULL;
1946 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001947 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001948 --todo;
1949 ++i;
1950 }
1951 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001952 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001953}
1954
Bram Moolenaara9922d62013-05-30 13:01:18 +02001955 static PyObject *
1956dict_key(hashitem_T *hi)
1957{
1958 return PyBytes_FromString((char *)(hi->hi_key));
1959}
1960
1961 static PyObject *
1962DictionaryListKeys(DictionaryObject *self)
1963{
1964 return DictionaryListObjects(self, dict_key);
1965}
1966
1967 static PyObject *
1968dict_val(hashitem_T *hi)
1969{
1970 dictitem_T *di;
1971
1972 di = dict_lookup(hi);
1973 return ConvertToPyObject(&di->di_tv);
1974}
1975
1976 static PyObject *
1977DictionaryListValues(DictionaryObject *self)
1978{
1979 return DictionaryListObjects(self, dict_val);
1980}
1981
1982 static PyObject *
1983dict_item(hashitem_T *hi)
1984{
1985 PyObject *keyObject;
1986 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001987 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001988
1989 if (!(keyObject = dict_key(hi)))
1990 return NULL;
1991
1992 if (!(valObject = dict_val(hi)))
1993 {
1994 Py_DECREF(keyObject);
1995 return NULL;
1996 }
1997
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001998 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001999
2000 Py_DECREF(keyObject);
2001 Py_DECREF(valObject);
2002
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002003 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002004}
2005
2006 static PyObject *
2007DictionaryListItems(DictionaryObject *self)
2008{
2009 return DictionaryListObjects(self, dict_item);
2010}
2011
2012 static PyObject *
2013DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2014{
2015 dict_T *dict = self->dict;
2016
2017 if (dict->dv_lock)
2018 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002019 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002020 return NULL;
2021 }
2022
2023 if (kwargs)
2024 {
2025 typval_T tv;
2026
2027 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2028 return NULL;
2029
2030 VimTryStart();
2031 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2032 clear_tv(&tv);
2033 if (VimTryEnd())
2034 return NULL;
2035 }
2036 else
2037 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002038 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002039
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002040 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002041 return NULL;
2042
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002043 if (obj == NULL)
2044 {
2045 Py_INCREF(Py_None);
2046 return Py_None;
2047 }
2048
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002049 if (PyObject_HasAttrString(obj, "keys"))
2050 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002051 else
2052 {
2053 PyObject *iterator;
2054 PyObject *item;
2055
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002056 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002057 return NULL;
2058
2059 while ((item = PyIter_Next(iterator)))
2060 {
2061 PyObject *fast;
2062 PyObject *keyObject;
2063 PyObject *valObject;
2064 PyObject *todecref;
2065 char_u *key;
2066 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002067 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002068
2069 if (!(fast = PySequence_Fast(item, "")))
2070 {
2071 Py_DECREF(iterator);
2072 Py_DECREF(item);
2073 return NULL;
2074 }
2075
2076 Py_DECREF(item);
2077
2078 if (PySequence_Fast_GET_SIZE(fast) != 2)
2079 {
2080 Py_DECREF(iterator);
2081 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002082 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002083 N_("expected sequence element of size 2, "
2084 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002085 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002086 return NULL;
2087 }
2088
2089 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2090
2091 if (!(key = StringToChars(keyObject, &todecref)))
2092 {
2093 Py_DECREF(iterator);
2094 Py_DECREF(fast);
2095 return NULL;
2096 }
2097
2098 di = dictitem_alloc(key);
2099
2100 Py_XDECREF(todecref);
2101
2102 if (di == NULL)
2103 {
2104 Py_DECREF(fast);
2105 Py_DECREF(iterator);
2106 PyErr_NoMemory();
2107 return NULL;
2108 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002109 di->di_tv.v_type = VAR_UNKNOWN;
2110
2111 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2112
2113 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2114 {
2115 Py_DECREF(iterator);
2116 Py_DECREF(fast);
2117 dictitem_free(di);
2118 return NULL;
2119 }
2120
2121 Py_DECREF(fast);
2122
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002123 hi = hash_find(&dict->dv_hashtab, di->di_key);
2124 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002125 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002126 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002127 Py_DECREF(iterator);
2128 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002129 return NULL;
2130 }
2131 }
2132
2133 Py_DECREF(iterator);
2134
2135 /* Iterator may have finished due to an exception */
2136 if (PyErr_Occurred())
2137 return NULL;
2138 }
2139 }
2140 Py_INCREF(Py_None);
2141 return Py_None;
2142}
2143
2144 static PyObject *
2145DictionaryGet(DictionaryObject *self, PyObject *args)
2146{
2147 return _DictionaryItem(self, args,
2148 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2149}
2150
2151 static PyObject *
2152DictionaryPop(DictionaryObject *self, PyObject *args)
2153{
2154 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2155}
2156
2157 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002158DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002159{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002160 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002161 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002162 PyObject *valObject;
2163 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002164
Bram Moolenaarde71b562013-06-02 17:41:54 +02002165 if (self->dict->dv_hashtab.ht_used == 0)
2166 {
2167 PyErr_SetNone(PyExc_KeyError);
2168 return NULL;
2169 }
2170
2171 hi = self->dict->dv_hashtab.ht_array;
2172 while (HASHITEM_EMPTY(hi))
2173 ++hi;
2174
2175 di = dict_lookup(hi);
2176
2177 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002178 return NULL;
2179
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002180 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002181 {
2182 Py_DECREF(valObject);
2183 return NULL;
2184 }
2185
2186 hash_remove(&self->dict->dv_hashtab, hi);
2187 dictitem_free(di);
2188
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002189 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002190}
2191
2192 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002193DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002194{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002195 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2196}
2197
2198static PySequenceMethods DictionaryAsSeq = {
2199 0, /* sq_length */
2200 0, /* sq_concat */
2201 0, /* sq_repeat */
2202 0, /* sq_item */
2203 0, /* sq_slice */
2204 0, /* sq_ass_item */
2205 0, /* sq_ass_slice */
2206 (objobjproc) DictionaryContains, /* sq_contains */
2207 0, /* sq_inplace_concat */
2208 0, /* sq_inplace_repeat */
2209};
2210
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002211static PyMappingMethods DictionaryAsMapping = {
2212 (lenfunc) DictionaryLength,
2213 (binaryfunc) DictionaryItem,
2214 (objobjargproc) DictionaryAssItem,
2215};
2216
Bram Moolenaardb913952012-06-29 12:54:53 +02002217static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002218 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002219 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2220 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2221 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2222 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2223 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002224 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002225 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002226 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2227 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002228};
2229
2230static PyTypeObject ListType;
2231
2232typedef struct
2233{
2234 PyObject_HEAD
2235 list_T *list;
2236 pylinkedlist_T ref;
2237} ListObject;
2238
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002239#define NEW_LIST(list) ListNew(&ListType, list)
2240
Bram Moolenaardb913952012-06-29 12:54:53 +02002241 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002242ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002243{
2244 ListObject *self;
2245
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002246 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002247 if (self == NULL)
2248 return NULL;
2249 self->list = list;
2250 ++list->lv_refcount;
2251
2252 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2253
2254 return (PyObject *)(self);
2255}
2256
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002257 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002258py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002259{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002260 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002261
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002262 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002263 {
2264 PyErr_NoMemory();
2265 return NULL;
2266 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002267 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002268
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002269 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002270}
2271
2272 static int
2273list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2274{
2275 PyObject *iterator;
2276 PyObject *item;
2277 listitem_T *li;
2278
2279 if (!(iterator = PyObject_GetIter(obj)))
2280 return -1;
2281
2282 while ((item = PyIter_Next(iterator)))
2283 {
2284 if (!(li = listitem_alloc()))
2285 {
2286 PyErr_NoMemory();
2287 Py_DECREF(item);
2288 Py_DECREF(iterator);
2289 return -1;
2290 }
2291 li->li_tv.v_lock = 0;
2292 li->li_tv.v_type = VAR_UNKNOWN;
2293
2294 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2295 {
2296 Py_DECREF(item);
2297 Py_DECREF(iterator);
2298 listitem_free(li);
2299 return -1;
2300 }
2301
2302 Py_DECREF(item);
2303
2304 list_append(l, li);
2305 }
2306
2307 Py_DECREF(iterator);
2308
2309 /* Iterator may have finished due to an exception */
2310 if (PyErr_Occurred())
2311 return -1;
2312
2313 return 0;
2314}
2315
2316 static PyObject *
2317ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2318{
2319 list_T *list;
2320 PyObject *obj = NULL;
2321
2322 if (kwargs)
2323 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002324 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002325 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002326 return NULL;
2327 }
2328
2329 if (!PyArg_ParseTuple(args, "|O", &obj))
2330 return NULL;
2331
2332 if (!(list = py_list_alloc()))
2333 return NULL;
2334
2335 if (obj)
2336 {
2337 PyObject *lookup_dict;
2338
2339 if (!(lookup_dict = PyDict_New()))
2340 {
2341 list_unref(list);
2342 return NULL;
2343 }
2344
2345 if (list_py_concat(list, obj, lookup_dict) == -1)
2346 {
2347 Py_DECREF(lookup_dict);
2348 list_unref(list);
2349 return NULL;
2350 }
2351
2352 Py_DECREF(lookup_dict);
2353 }
2354
2355 return ListNew(subtype, list);
2356}
2357
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002358 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002359ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002360{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002361 pyll_remove(&self->ref, &lastlist);
2362 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002363
2364 DESTRUCTOR_FINISH(self);
2365}
2366
Bram Moolenaardb913952012-06-29 12:54:53 +02002367 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002368ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002369{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002370 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002371}
2372
2373 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002374ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002375{
2376 listitem_T *li;
2377
Bram Moolenaard6e39182013-05-21 18:30:34 +02002378 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002379 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002380 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002381 return NULL;
2382 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002383 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002384 if (li == NULL)
2385 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002386 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002387 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002388 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002389 return NULL;
2390 }
2391 return ConvertToPyObject(&li->li_tv);
2392}
2393
Bram Moolenaardb913952012-06-29 12:54:53 +02002394 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002395ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2396 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002397{
2398 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002399 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002400
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002401 if (step == 0)
2402 {
2403 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2404 return NULL;
2405 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002406
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002407 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 if (list == NULL)
2409 return NULL;
2410
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002411 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002412 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002413 PyObject *item;
2414
2415 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 if (item == NULL)
2417 {
2418 Py_DECREF(list);
2419 return NULL;
2420 }
2421
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002422 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002423 }
2424
2425 return list;
2426}
2427
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002428 static PyObject *
2429ListItem(ListObject *self, PyObject* idx)
2430{
2431#if PY_MAJOR_VERSION < 3
2432 if (PyInt_Check(idx))
2433 {
2434 long _idx = PyInt_AsLong(idx);
2435 return ListIndex(self, _idx);
2436 }
2437 else
2438#endif
2439 if (PyLong_Check(idx))
2440 {
2441 long _idx = PyLong_AsLong(idx);
2442 return ListIndex(self, _idx);
2443 }
2444 else if (PySlice_Check(idx))
2445 {
2446 Py_ssize_t start, stop, step, slicelen;
2447
Bram Moolenaar922a4662014-03-30 16:11:43 +02002448 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002449 &start, &stop, &step, &slicelen) < 0)
2450 return NULL;
2451 return ListSlice(self, start, step, slicelen);
2452 }
2453 else
2454 {
2455 RAISE_INVALID_INDEX_TYPE(idx);
2456 return NULL;
2457 }
2458}
2459
2460 static void
2461list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2462 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2463{
2464 while (numreplaced--)
2465 {
2466 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2467 listitem_remove(l, lis[slicelen + numreplaced]);
2468 }
2469 while (numadded--)
2470 {
2471 listitem_T *next;
2472
2473 next = lastaddedli->li_prev;
2474 listitem_remove(l, lastaddedli);
2475 lastaddedli = next;
2476 }
2477}
2478
2479 static int
2480ListAssSlice(ListObject *self, Py_ssize_t first,
2481 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2482{
2483 PyObject *iterator;
2484 PyObject *item;
2485 listitem_T *li;
2486 listitem_T *lastaddedli = NULL;
2487 listitem_T *next;
2488 typval_T v;
2489 list_T *l = self->list;
2490 PyInt i;
2491 PyInt j;
2492 PyInt numreplaced = 0;
2493 PyInt numadded = 0;
2494 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002495 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002496
2497 size = ListLength(self);
2498
2499 if (l->lv_lock)
2500 {
2501 RAISE_LOCKED_LIST;
2502 return -1;
2503 }
2504
2505 if (step == 0)
2506 {
2507 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2508 return -1;
2509 }
2510
2511 if (step != 1 && slicelen == 0)
2512 {
2513 /* Nothing to do. Only error out if obj has some items. */
2514 int ret = 0;
2515
2516 if (obj == NULL)
2517 return 0;
2518
2519 if (!(iterator = PyObject_GetIter(obj)))
2520 return -1;
2521
2522 if ((item = PyIter_Next(iterator)))
2523 {
2524 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002525 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002526 "to extended slice"), 0);
2527 Py_DECREF(item);
2528 ret = -1;
2529 }
2530 Py_DECREF(iterator);
2531 return ret;
2532 }
2533
2534 if (obj != NULL)
2535 /* XXX May allocate zero bytes. */
2536 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2537 {
2538 PyErr_NoMemory();
2539 return -1;
2540 }
2541
2542 if (first == size)
2543 li = NULL;
2544 else
2545 {
2546 li = list_find(l, (long) first);
2547 if (li == NULL)
2548 {
2549 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2550 (int)first);
2551 if (obj != NULL)
2552 PyMem_Free(lis);
2553 return -1;
2554 }
2555 i = slicelen;
2556 while (i-- && li != NULL)
2557 {
2558 j = step;
2559 next = li;
2560 if (step > 0)
2561 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2562 else
2563 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2564
2565 if (obj == NULL)
2566 listitem_remove(l, li);
2567 else
2568 lis[slicelen - i - 1] = li;
2569
2570 li = next;
2571 }
2572 if (li == NULL && i != -1)
2573 {
2574 PyErr_SET_VIM(N_("internal error: not enough list items"));
2575 if (obj != NULL)
2576 PyMem_Free(lis);
2577 return -1;
2578 }
2579 }
2580
2581 if (obj == NULL)
2582 return 0;
2583
2584 if (!(iterator = PyObject_GetIter(obj)))
2585 {
2586 PyMem_Free(lis);
2587 return -1;
2588 }
2589
2590 i = 0;
2591 while ((item = PyIter_Next(iterator)))
2592 {
2593 if (ConvertFromPyObject(item, &v) == -1)
2594 {
2595 Py_DECREF(iterator);
2596 Py_DECREF(item);
2597 PyMem_Free(lis);
2598 return -1;
2599 }
2600 Py_DECREF(item);
2601 if (list_insert_tv(l, &v, numreplaced < slicelen
2602 ? lis[numreplaced]
2603 : li) == FAIL)
2604 {
2605 clear_tv(&v);
2606 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2607 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2608 PyMem_Free(lis);
2609 return -1;
2610 }
2611 if (numreplaced < slicelen)
2612 {
2613 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002614 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002615 numreplaced++;
2616 }
2617 else
2618 {
2619 if (li)
2620 lastaddedli = li->li_prev;
2621 else
2622 lastaddedli = l->lv_last;
2623 numadded++;
2624 }
2625 clear_tv(&v);
2626 if (step != 1 && i >= slicelen)
2627 {
2628 Py_DECREF(iterator);
2629 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002630 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002631 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002632 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2633 PyMem_Free(lis);
2634 return -1;
2635 }
2636 ++i;
2637 }
2638 Py_DECREF(iterator);
2639
2640 if (step != 1 && i != slicelen)
2641 {
2642 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002643 N_("attempt to assign sequence of size %d to extended slice "
2644 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002645 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2646 PyMem_Free(lis);
2647 return -1;
2648 }
2649
2650 if (PyErr_Occurred())
2651 {
2652 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2653 PyMem_Free(lis);
2654 return -1;
2655 }
2656
2657 for (i = 0; i < numreplaced; i++)
2658 listitem_free(lis[i]);
2659 if (step == 1)
2660 for (i = numreplaced; i < slicelen; i++)
2661 listitem_remove(l, lis[i]);
2662
2663 PyMem_Free(lis);
2664
2665 return 0;
2666}
2667
2668 static int
2669ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2670{
2671 typval_T tv;
2672 list_T *l = self->list;
2673 listitem_T *li;
2674 Py_ssize_t length = ListLength(self);
2675
2676 if (l->lv_lock)
2677 {
2678 RAISE_LOCKED_LIST;
2679 return -1;
2680 }
2681 if (index > length || (index == length && obj == NULL))
2682 {
2683 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2684 return -1;
2685 }
2686
2687 if (obj == NULL)
2688 {
2689 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002690 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002691 clear_tv(&li->li_tv);
2692 vim_free(li);
2693 return 0;
2694 }
2695
2696 if (ConvertFromPyObject(obj, &tv) == -1)
2697 return -1;
2698
2699 if (index == length)
2700 {
2701 if (list_append_tv(l, &tv) == FAIL)
2702 {
2703 clear_tv(&tv);
2704 PyErr_SET_VIM(N_("failed to add item to list"));
2705 return -1;
2706 }
2707 }
2708 else
2709 {
2710 li = list_find(l, (long) index);
2711 clear_tv(&li->li_tv);
2712 copy_tv(&tv, &li->li_tv);
2713 clear_tv(&tv);
2714 }
2715 return 0;
2716}
2717
2718 static Py_ssize_t
2719ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2720{
2721#if PY_MAJOR_VERSION < 3
2722 if (PyInt_Check(idx))
2723 {
2724 long _idx = PyInt_AsLong(idx);
2725 return ListAssIndex(self, _idx, obj);
2726 }
2727 else
2728#endif
2729 if (PyLong_Check(idx))
2730 {
2731 long _idx = PyLong_AsLong(idx);
2732 return ListAssIndex(self, _idx, obj);
2733 }
2734 else if (PySlice_Check(idx))
2735 {
2736 Py_ssize_t start, stop, step, slicelen;
2737
Bram Moolenaar922a4662014-03-30 16:11:43 +02002738 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002739 &start, &stop, &step, &slicelen) < 0)
2740 return -1;
2741 return ListAssSlice(self, start, step, slicelen,
2742 obj);
2743 }
2744 else
2745 {
2746 RAISE_INVALID_INDEX_TYPE(idx);
2747 return -1;
2748 }
2749}
2750
2751 static PyObject *
2752ListConcatInPlace(ListObject *self, PyObject *obj)
2753{
2754 list_T *l = self->list;
2755 PyObject *lookup_dict;
2756
2757 if (l->lv_lock)
2758 {
2759 RAISE_LOCKED_LIST;
2760 return NULL;
2761 }
2762
2763 if (!(lookup_dict = PyDict_New()))
2764 return NULL;
2765
2766 if (list_py_concat(l, obj, lookup_dict) == -1)
2767 {
2768 Py_DECREF(lookup_dict);
2769 return NULL;
2770 }
2771 Py_DECREF(lookup_dict);
2772
2773 Py_INCREF(self);
2774 return (PyObject *)(self);
2775}
2776
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002777typedef struct
2778{
2779 listwatch_T lw;
2780 list_T *list;
2781} listiterinfo_T;
2782
2783 static void
2784ListIterDestruct(listiterinfo_T *lii)
2785{
2786 list_rem_watch(lii->list, &lii->lw);
2787 PyMem_Free(lii);
2788}
2789
2790 static PyObject *
2791ListIterNext(listiterinfo_T **lii)
2792{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002793 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002794
2795 if (!((*lii)->lw.lw_item))
2796 return NULL;
2797
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002798 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002799 return NULL;
2800
2801 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2802
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002803 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002804}
2805
2806 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002807ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002808{
2809 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002810 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002811
2812 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2813 {
2814 PyErr_NoMemory();
2815 return NULL;
2816 }
2817
2818 list_add_watch(l, &lii->lw);
2819 lii->lw.lw_item = l->lv_first;
2820 lii->list = l;
2821
2822 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002823 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2824 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002825}
2826
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002827static char *ListAttrs[] = {
2828 "locked",
2829 NULL
2830};
2831
2832 static PyObject *
2833ListDir(PyObject *self)
2834{
2835 return ObjectDir(self, ListAttrs);
2836}
2837
Bram Moolenaar66b79852012-09-21 14:00:35 +02002838 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002839ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002840{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002841 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002842 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002843 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002844 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002845 return -1;
2846 }
2847
2848 if (strcmp(name, "locked") == 0)
2849 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002850 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002851 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002852 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002853 return -1;
2854 }
2855 else
2856 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002857 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002858 if (istrue == -1)
2859 return -1;
2860 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002861 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002862 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002863 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002864 }
2865 return 0;
2866 }
2867 else
2868 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002869 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870 return -1;
2871 }
2872}
2873
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002874static PySequenceMethods ListAsSeq = {
2875 (lenfunc) ListLength, /* sq_length, len(x) */
2876 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2877 0, /* RangeRepeat, sq_repeat, x*n */
2878 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2879 0, /* was_sq_slice, x[i:j] */
2880 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2881 0, /* was_sq_ass_slice, x[i:j]=v */
2882 0, /* sq_contains */
2883 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2884 0, /* sq_inplace_repeat */
2885};
2886
2887static PyMappingMethods ListAsMapping = {
2888 /* mp_length */ (lenfunc) ListLength,
2889 /* mp_subscript */ (binaryfunc) ListItem,
2890 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2891};
2892
Bram Moolenaardb913952012-06-29 12:54:53 +02002893static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002894 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2895 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2896 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002897};
2898
2899typedef struct
2900{
2901 PyObject_HEAD
2902 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002903 int argc;
2904 typval_T *argv;
2905 dict_T *self;
2906 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002907 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002908} FunctionObject;
2909
2910static PyTypeObject FunctionType;
2911
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002912#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2913 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002914
Bram Moolenaardb913952012-06-29 12:54:53 +02002915 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002916FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002917 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002918{
2919 FunctionObject *self;
2920
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002921 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002922 if (self == NULL)
2923 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002924
2925 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002926 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002927 if (!translated_function_exists(name))
2928 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002929 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002930 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002931 return NULL;
2932 }
2933 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002934 }
2935 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002936 {
2937 char_u *p;
2938
2939 if ((p = get_expanded_name(name,
2940 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002941 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002942 PyErr_FORMAT(PyExc_ValueError,
2943 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002944 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002945 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002946
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002947 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2948 {
2949 char_u *np;
2950 size_t len = STRLEN(p) + 1;
2951
Bram Moolenaar80dae042018-12-23 13:36:40 +01002952 if ((np = alloc((int)len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002953 {
2954 vim_free(p);
2955 return NULL;
2956 }
2957 mch_memmove(np, "<SNR>", 5);
2958 mch_memmove(np + 5, p + 3, len - 3);
2959 vim_free(p);
2960 self->name = np;
2961 }
2962 else
2963 self->name = p;
2964 }
2965
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002966 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002967 self->argc = argc;
2968 self->argv = argv;
2969 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002970 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002971
2972 if (self->argv || self->self)
2973 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2974
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002975 return (PyObject *)(self);
2976}
2977
2978 static PyObject *
2979FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2980{
2981 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002982 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002983 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002984 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002985 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002986 typval_T selfdicttv;
2987 typval_T argstv;
2988 list_T *argslist = NULL;
2989 dict_T *selfdict = NULL;
2990 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002991 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002992 typval_T *argv = NULL;
2993 typval_T *curtv;
2994 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002995
Bram Moolenaar8110a092016-04-14 15:56:09 +02002996 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002997 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002998 selfdictObject = PyDict_GetItemString(kwargs, "self");
2999 if (selfdictObject != NULL)
3000 {
3001 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3002 return NULL;
3003 selfdict = selfdicttv.vval.v_dict;
3004 }
3005 argsObject = PyDict_GetItemString(kwargs, "args");
3006 if (argsObject != NULL)
3007 {
3008 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3009 {
3010 dict_unref(selfdict);
3011 return NULL;
3012 }
3013 argslist = argstv.vval.v_list;
3014
3015 argc = argslist->lv_len;
3016 if (argc != 0)
3017 {
3018 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003019 if (argv == NULL)
3020 {
3021 PyErr_NoMemory();
3022 dict_unref(selfdict);
3023 list_unref(argslist);
3024 return NULL;
3025 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003026 curtv = argv;
3027 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3028 copy_tv(&li->li_tv, curtv++);
3029 }
3030 list_unref(argslist);
3031 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003032 if (selfdict != NULL)
3033 {
3034 auto_rebind = FALSE;
3035 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3036 if (autoRebindObject != NULL)
3037 {
3038 auto_rebind = PyObject_IsTrue(autoRebindObject);
3039 if (auto_rebind == -1)
3040 {
3041 dict_unref(selfdict);
3042 list_unref(argslist);
3043 return NULL;
3044 }
3045 }
3046 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003047 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003048
Bram Moolenaar389a1792013-06-23 13:00:44 +02003049 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003050 {
3051 dict_unref(selfdict);
3052 while (argc--)
3053 clear_tv(&argv[argc]);
3054 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003055 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003056 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003057
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003058 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003059
Bram Moolenaar389a1792013-06-23 13:00:44 +02003060 PyMem_Free(name);
3061
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003062 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003063}
3064
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003065 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003066FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003067{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003068 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003069 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003070 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003071 for (i = 0; i < self->argc; ++i)
3072 clear_tv(&self->argv[i]);
3073 PyMem_Free(self->argv);
3074 dict_unref(self->self);
3075 if (self->argv || self->self)
3076 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003077
3078 DESTRUCTOR_FINISH(self);
3079}
3080
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003081static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003082 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003083 NULL
3084};
3085
3086 static PyObject *
3087FunctionDir(PyObject *self)
3088{
3089 return ObjectDir(self, FunctionAttrs);
3090}
3091
Bram Moolenaardb913952012-06-29 12:54:53 +02003092 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003093FunctionAttr(FunctionObject *self, char *name)
3094{
3095 list_T *list;
3096 int i;
3097 if (strcmp(name, "name") == 0)
3098 return PyString_FromString((char *)(self->name));
3099 else if (strcmp(name, "args") == 0)
3100 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003101 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003102 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003103
Bram Moolenaar8110a092016-04-14 15:56:09 +02003104 for (i = 0; i < self->argc; ++i)
3105 list_append_tv(list, &self->argv[i]);
3106 return NEW_LIST(list);
3107 }
3108 else if (strcmp(name, "self") == 0)
3109 return self->self == NULL
3110 ? AlwaysNone(NULL)
3111 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003112 else if (strcmp(name, "auto_rebind") == 0)
3113 return self->auto_rebind
3114 ? AlwaysTrue(NULL)
3115 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003116 else if (strcmp(name, "__members__") == 0)
3117 return ObjectDir(NULL, FunctionAttrs);
3118 return NULL;
3119}
3120
3121/* Populate partial_T given function object.
3122 *
3123 * "exported" should be set to true when it is needed to construct a partial
3124 * that may be stored in a variable (i.e. may be freed by Vim).
3125 */
3126 static void
3127set_partial(FunctionObject *self, partial_T *pt, int exported)
3128{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003129 int i;
3130
3131 pt->pt_name = self->name;
3132 if (self->argv)
3133 {
3134 pt->pt_argc = self->argc;
3135 if (exported)
3136 {
3137 pt->pt_argv = (typval_T *)alloc_clear(
3138 sizeof(typval_T) * self->argc);
3139 for (i = 0; i < pt->pt_argc; ++i)
3140 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3141 }
3142 else
3143 pt->pt_argv = self->argv;
3144 }
3145 else
3146 {
3147 pt->pt_argc = 0;
3148 pt->pt_argv = NULL;
3149 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003150 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003151 pt->pt_dict = self->self;
3152 if (exported && self->self)
3153 ++pt->pt_dict->dv_refcount;
3154 if (exported)
3155 pt->pt_name = vim_strsave(pt->pt_name);
3156 pt->pt_refcount = 1;
3157}
3158
3159 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003160FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003161{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003162 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003163 typval_T args;
3164 typval_T selfdicttv;
3165 typval_T rettv;
3166 dict_T *selfdict = NULL;
3167 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003168 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003169 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003170 partial_T pt;
3171 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003172
Bram Moolenaar8110a092016-04-14 15:56:09 +02003173 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003174 return NULL;
3175
3176 if (kwargs != NULL)
3177 {
3178 selfdictObject = PyDict_GetItemString(kwargs, "self");
3179 if (selfdictObject != NULL)
3180 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003181 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003182 {
3183 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003184 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003185 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003186 selfdict = selfdicttv.vval.v_dict;
3187 }
3188 }
3189
Bram Moolenaar8110a092016-04-14 15:56:09 +02003190 if (self->argv || self->self)
3191 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003192 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003193 set_partial(self, &pt, FALSE);
3194 pt_ptr = &pt;
3195 }
3196
Bram Moolenaar71700b82013-05-15 17:49:05 +02003197 Py_BEGIN_ALLOW_THREADS
3198 Python_Lock_Vim();
3199
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003200 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003201 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003202
3203 Python_Release_Vim();
3204 Py_END_ALLOW_THREADS
3205
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003206 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003207 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003208 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003209 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003210 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003211 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003212 }
3213 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003214 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003215
Bram Moolenaardb913952012-06-29 12:54:53 +02003216 clear_tv(&args);
3217 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003218 if (selfdict != NULL)
3219 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003220
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003221 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003222}
3223
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003224 static PyObject *
3225FunctionRepr(FunctionObject *self)
3226{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003227 PyObject *ret;
3228 garray_T repr_ga;
3229 int i;
3230 char_u *tofree = NULL;
3231 typval_T tv;
3232 char_u numbuf[NUMBUFLEN];
3233
3234 ga_init2(&repr_ga, (int)sizeof(char), 70);
3235 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3236 if (self->name)
3237 ga_concat(&repr_ga, self->name);
3238 else
3239 ga_concat(&repr_ga, (char_u *)"<NULL>");
3240 ga_append(&repr_ga, '\'');
3241 if (self->argv)
3242 {
3243 ga_concat(&repr_ga, (char_u *)", args=[");
3244 ++emsg_silent;
3245 for (i = 0; i < self->argc; i++)
3246 {
3247 if (i != 0)
3248 ga_concat(&repr_ga, (char_u *)", ");
3249 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3250 get_copyID()));
3251 vim_free(tofree);
3252 }
3253 --emsg_silent;
3254 ga_append(&repr_ga, ']');
3255 }
3256 if (self->self)
3257 {
3258 ga_concat(&repr_ga, (char_u *)", self=");
3259 tv.v_type = VAR_DICT;
3260 tv.vval.v_dict = self->self;
3261 ++emsg_silent;
3262 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3263 --emsg_silent;
3264 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003265 if (self->auto_rebind)
3266 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003267 }
3268 ga_append(&repr_ga, '>');
3269 ret = PyString_FromString((char *)repr_ga.ga_data);
3270 ga_clear(&repr_ga);
3271 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003272}
3273
Bram Moolenaardb913952012-06-29 12:54:53 +02003274static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003275 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3276 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003277};
3278
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003279/*
3280 * Options object
3281 */
3282
3283static PyTypeObject OptionsType;
3284
3285typedef int (*checkfun)(void *);
3286
3287typedef struct
3288{
3289 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003290 int opt_type;
3291 void *from;
3292 checkfun Check;
3293 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003294} OptionsObject;
3295
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003296 static int
3297dummy_check(void *arg UNUSED)
3298{
3299 return 0;
3300}
3301
3302 static PyObject *
3303OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3304{
3305 OptionsObject *self;
3306
Bram Moolenaar774267b2013-05-21 20:51:59 +02003307 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003308 if (self == NULL)
3309 return NULL;
3310
3311 self->opt_type = opt_type;
3312 self->from = from;
3313 self->Check = Check;
3314 self->fromObj = fromObj;
3315 if (fromObj)
3316 Py_INCREF(fromObj);
3317
3318 return (PyObject *)(self);
3319}
3320
3321 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003322OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003323{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003324 PyObject_GC_UnTrack((void *)(self));
3325 Py_XDECREF(self->fromObj);
3326 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003327}
3328
3329 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003330OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003331{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003332 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003333 return 0;
3334}
3335
3336 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003337OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003338{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003339 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003340 return 0;
3341}
3342
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003343 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003345{
3346 char_u *key;
3347 int flags;
3348 long numval;
3349 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003350 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003351
Bram Moolenaard6e39182013-05-21 18:30:34 +02003352 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003353 return NULL;
3354
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003355 if (!(key = StringToChars(keyObject, &todecref)))
3356 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003357
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003358 if (*key == NUL)
3359 {
3360 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003361 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003362 return NULL;
3363 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003364
3365 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003367
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003368 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003369
3370 if (flags == 0)
3371 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003372 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003373 return NULL;
3374 }
3375
3376 if (flags & SOPT_UNSET)
3377 {
3378 Py_INCREF(Py_None);
3379 return Py_None;
3380 }
3381 else if (flags & SOPT_BOOL)
3382 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003383 PyObject *ret;
3384 ret = numval ? Py_True : Py_False;
3385 Py_INCREF(ret);
3386 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003387 }
3388 else if (flags & SOPT_NUM)
3389 return PyInt_FromLong(numval);
3390 else if (flags & SOPT_STRING)
3391 {
3392 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003393 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003394 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003395 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003396 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003397 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003398 else
3399 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003400 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003401 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402 return NULL;
3403 }
3404 }
3405 else
3406 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003407 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003408 return NULL;
3409 }
3410}
3411
3412 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003413OptionsContains(OptionsObject *self, PyObject *keyObject)
3414{
3415 char_u *key;
3416 PyObject *todecref;
3417
3418 if (!(key = StringToChars(keyObject, &todecref)))
3419 return -1;
3420
3421 if (*key == NUL)
3422 {
3423 Py_XDECREF(todecref);
3424 return 0;
3425 }
3426
3427 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3428 {
3429 Py_XDECREF(todecref);
3430 return 1;
3431 }
3432 else
3433 {
3434 Py_XDECREF(todecref);
3435 return 0;
3436 }
3437}
3438
3439typedef struct
3440{
3441 void *lastoption;
3442 int opt_type;
3443} optiterinfo_T;
3444
3445 static PyObject *
3446OptionsIterNext(optiterinfo_T **oii)
3447{
3448 char_u *name;
3449
3450 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3451 return PyString_FromString((char *)name);
3452
3453 return NULL;
3454}
3455
3456 static PyObject *
3457OptionsIter(OptionsObject *self)
3458{
3459 optiterinfo_T *oii;
3460
3461 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3462 {
3463 PyErr_NoMemory();
3464 return NULL;
3465 }
3466
3467 oii->opt_type = self->opt_type;
3468 oii->lastoption = NULL;
3469
3470 return IterNew(oii,
3471 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3472 NULL, NULL);
3473}
3474
3475 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003476set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003477{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003478 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003479
3480 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3481 {
3482 if (VimTryEnd())
3483 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003484 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003485 return FAIL;
3486 }
3487 return OK;
3488}
3489
3490 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003491set_option_value_for(
3492 char_u *key,
3493 int numval,
3494 char_u *stringval,
3495 int opt_flags,
3496 int opt_type,
3497 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003498{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003499 win_T *save_curwin = NULL;
3500 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003501 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003502 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003503
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003504 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003505 switch (opt_type)
3506 {
3507 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003508 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003509 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003510 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003511 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003512 if (VimTryEnd())
3513 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003514 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003515 return -1;
3516 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003517 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3518 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003519 break;
3520 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003521 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003522 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003523 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003524 break;
3525 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003526 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003527 break;
3528 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003529 if (set_ret == FAIL)
3530 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003531 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003532}
3533
3534 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003535OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003536{
3537 char_u *key;
3538 int flags;
3539 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003540 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003541 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003542
Bram Moolenaard6e39182013-05-21 18:30:34 +02003543 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003544 return -1;
3545
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003546 if (!(key = StringToChars(keyObject, &todecref)))
3547 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003548
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003549 if (*key == NUL)
3550 {
3551 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003552 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003553 return -1;
3554 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003555
3556 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003557 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003558
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003559 if (flags == 0)
3560 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003561 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003562 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003563 return -1;
3564 }
3565
3566 if (valObject == NULL)
3567 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003568 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003569 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003570 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003571 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003572 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003573 return -1;
3574 }
3575 else if (!(flags & SOPT_GLOBAL))
3576 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003577 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003578 N_("unable to unset option %s "
3579 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003580 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003581 return -1;
3582 }
3583 else
3584 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003585 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003586 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003587 return 0;
3588 }
3589 }
3590
Bram Moolenaard6e39182013-05-21 18:30:34 +02003591 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003592
3593 if (flags & SOPT_BOOL)
3594 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003595 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003596
Bram Moolenaarb983f752013-05-15 16:11:50 +02003597 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003598 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003599 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003600 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003601 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003602 }
3603 else if (flags & SOPT_NUM)
3604 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003605 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003606
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003607 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003608 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003609 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003610 return -1;
3611 }
3612
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003613 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003614 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003615 }
3616 else
3617 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003618 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003619 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003620
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003621 if ((val = StringToChars(valObject, &todecref2)))
3622 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003623 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003624 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003625 Py_XDECREF(todecref2);
3626 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003627 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003628 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003629 }
3630
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003631 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003632
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003633 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003634}
3635
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003636static PySequenceMethods OptionsAsSeq = {
3637 0, /* sq_length */
3638 0, /* sq_concat */
3639 0, /* sq_repeat */
3640 0, /* sq_item */
3641 0, /* sq_slice */
3642 0, /* sq_ass_item */
3643 0, /* sq_ass_slice */
3644 (objobjproc) OptionsContains, /* sq_contains */
3645 0, /* sq_inplace_concat */
3646 0, /* sq_inplace_repeat */
3647};
3648
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003649static PyMappingMethods OptionsAsMapping = {
3650 (lenfunc) NULL,
3651 (binaryfunc) OptionsItem,
3652 (objobjargproc) OptionsAssItem,
3653};
3654
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003655/* Tabpage object
3656 */
3657
3658typedef struct
3659{
3660 PyObject_HEAD
3661 tabpage_T *tab;
3662} TabPageObject;
3663
3664static PyObject *WinListNew(TabPageObject *tabObject);
3665
3666static PyTypeObject TabPageType;
3667
3668 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003669CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003670{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003671 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003672 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003673 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003674 return -1;
3675 }
3676
3677 return 0;
3678}
3679
3680 static PyObject *
3681TabPageNew(tabpage_T *tab)
3682{
3683 TabPageObject *self;
3684
3685 if (TAB_PYTHON_REF(tab))
3686 {
3687 self = TAB_PYTHON_REF(tab);
3688 Py_INCREF(self);
3689 }
3690 else
3691 {
3692 self = PyObject_NEW(TabPageObject, &TabPageType);
3693 if (self == NULL)
3694 return NULL;
3695 self->tab = tab;
3696 TAB_PYTHON_REF(tab) = self;
3697 }
3698
3699 return (PyObject *)(self);
3700}
3701
3702 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003703TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003704{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003705 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3706 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003707
3708 DESTRUCTOR_FINISH(self);
3709}
3710
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003711static char *TabPageAttrs[] = {
3712 "windows", "number", "vars", "window", "valid",
3713 NULL
3714};
3715
3716 static PyObject *
3717TabPageDir(PyObject *self)
3718{
3719 return ObjectDir(self, TabPageAttrs);
3720}
3721
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003722 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003723TabPageAttrValid(TabPageObject *self, char *name)
3724{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003725 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003726
3727 if (strcmp(name, "valid") != 0)
3728 return NULL;
3729
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003730 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3731 Py_INCREF(ret);
3732 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003733}
3734
3735 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003736TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003737{
3738 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003739 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003740 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003741 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003742 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003743 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003744 else if (strcmp(name, "window") == 0)
3745 {
3746 /* For current tab window.c does not bother to set or update tp_curwin
3747 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003748 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003749 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003750 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003751 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003752 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003753 else if (strcmp(name, "__members__") == 0)
3754 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003755 return NULL;
3756}
3757
3758 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003759TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003760{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003761 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003762 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003763 else
3764 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003766
3767 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003768 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3769 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003770 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003771 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003772 }
3773}
3774
3775static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003776 /* name, function, calling, documentation */
3777 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3778 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003779};
3780
3781/*
3782 * Window list object
3783 */
3784
3785static PyTypeObject TabListType;
3786static PySequenceMethods TabListAsSeq;
3787
3788typedef struct
3789{
3790 PyObject_HEAD
3791} TabListObject;
3792
3793 static PyInt
3794TabListLength(PyObject *self UNUSED)
3795{
3796 tabpage_T *tp = first_tabpage;
3797 PyInt n = 0;
3798
3799 while (tp != NULL)
3800 {
3801 ++n;
3802 tp = tp->tp_next;
3803 }
3804
3805 return n;
3806}
3807
3808 static PyObject *
3809TabListItem(PyObject *self UNUSED, PyInt n)
3810{
3811 tabpage_T *tp;
3812
3813 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3814 if (n == 0)
3815 return TabPageNew(tp);
3816
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003817 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003818 return NULL;
3819}
3820
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003821/*
3822 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003823 */
3824
3825typedef struct
3826{
3827 PyObject_HEAD
3828 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003829 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003830} WindowObject;
3831
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003832static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003833
3834 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003835CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003836{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003837 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003838 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003839 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003840 return -1;
3841 }
3842
3843 return 0;
3844}
3845
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003846 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003847WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003848{
3849 /* We need to handle deletion of windows underneath us.
3850 * If we add a "w_python*_ref" field to the win_T structure,
3851 * then we can get at it in win_free() in vim. We then
3852 * need to create only ONE Python object per window - if
3853 * we try to create a second, just INCREF the existing one
3854 * and return it. The (single) Python object referring to
3855 * the window is stored in "w_python*_ref".
3856 * On a win_free() we set the Python object's win_T* field
3857 * to an invalid value. We trap all uses of a window
3858 * object, and reject them if the win_T* field is invalid.
3859 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003860 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003861 * w_python_ref and w_python3_ref fields respectively.
3862 */
3863
3864 WindowObject *self;
3865
3866 if (WIN_PYTHON_REF(win))
3867 {
3868 self = WIN_PYTHON_REF(win);
3869 Py_INCREF(self);
3870 }
3871 else
3872 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003873 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003874 if (self == NULL)
3875 return NULL;
3876 self->win = win;
3877 WIN_PYTHON_REF(win) = self;
3878 }
3879
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003880 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3881
Bram Moolenaar971db462013-05-12 18:44:48 +02003882 return (PyObject *)(self);
3883}
3884
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003885 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003886WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003887{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003888 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003889 if (self->win && self->win != INVALID_WINDOW_VALUE)
3890 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003891 Py_XDECREF(((PyObject *)(self->tabObject)));
3892 PyObject_GC_Del((void *)(self));
3893}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003894
Bram Moolenaar774267b2013-05-21 20:51:59 +02003895 static int
3896WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3897{
3898 Py_VISIT(((PyObject *)(self->tabObject)));
3899 return 0;
3900}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003901
Bram Moolenaar774267b2013-05-21 20:51:59 +02003902 static int
3903WindowClear(WindowObject *self)
3904{
3905 Py_CLEAR(self->tabObject);
3906 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003907}
3908
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003909 static win_T *
3910get_firstwin(TabPageObject *tabObject)
3911{
3912 if (tabObject)
3913 {
3914 if (CheckTabPage(tabObject))
3915 return NULL;
3916 /* For current tab window.c does not bother to set or update tp_firstwin
3917 */
3918 else if (tabObject->tab == curtab)
3919 return firstwin;
3920 else
3921 return tabObject->tab->tp_firstwin;
3922 }
3923 else
3924 return firstwin;
3925}
Bram Moolenaare950f992018-06-10 13:55:55 +02003926
3927// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003928static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003929 "buffer",
3930 "cursor",
3931 "height",
3932 "row",
3933 "width",
3934 "col",
3935 "vars",
3936 "options",
3937 "number",
3938 "tabpage",
3939 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003940 NULL
3941};
3942
3943 static PyObject *
3944WindowDir(PyObject *self)
3945{
3946 return ObjectDir(self, WindowAttrs);
3947}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003948
Bram Moolenaar971db462013-05-12 18:44:48 +02003949 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003950WindowAttrValid(WindowObject *self, char *name)
3951{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003952 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003953
3954 if (strcmp(name, "valid") != 0)
3955 return NULL;
3956
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003957 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3958 Py_INCREF(ret);
3959 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003960}
3961
3962 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003963WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003964{
3965 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003966 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003967 else if (strcmp(name, "cursor") == 0)
3968 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003969 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003970
3971 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3972 }
3973 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003974 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003975 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003976 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003977 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003978 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003979 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003980 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003981 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003982 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003983 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003984 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3985 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003986 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003987 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003989 return NULL;
3990 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003991 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003992 }
3993 else if (strcmp(name, "tabpage") == 0)
3994 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003995 Py_INCREF(self->tabObject);
3996 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003997 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003998 else if (strcmp(name, "__members__") == 0)
3999 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004000 else
4001 return NULL;
4002}
4003
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004004 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004005WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004006{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004007 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 return -1;
4009
4010 if (strcmp(name, "buffer") == 0)
4011 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004012 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004013 return -1;
4014 }
4015 else if (strcmp(name, "cursor") == 0)
4016 {
4017 long lnum;
4018 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004019
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004020 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004021 return -1;
4022
Bram Moolenaard6e39182013-05-21 18:30:34 +02004023 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004024 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004025 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026 return -1;
4027 }
4028
4029 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004030 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004031 return -1;
4032
Bram Moolenaard6e39182013-05-21 18:30:34 +02004033 self->win->w_cursor.lnum = lnum;
4034 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004035 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004036 self->win->w_cursor.coladd = 0;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004037 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004038 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004040 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041 return 0;
4042 }
4043 else if (strcmp(name, "height") == 0)
4044 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004045 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046 win_T *savewin;
4047
Bram Moolenaardee2e312013-06-23 16:35:47 +02004048 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049 return -1;
4050
4051#ifdef FEAT_GUI
4052 need_mouse_correct = TRUE;
4053#endif
4054 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004055 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004056
4057 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004058 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004060 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061 return -1;
4062
4063 return 0;
4064 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 else if (strcmp(name, "width") == 0)
4066 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004067 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 win_T *savewin;
4069
Bram Moolenaardee2e312013-06-23 16:35:47 +02004070 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071 return -1;
4072
4073#ifdef FEAT_GUI
4074 need_mouse_correct = TRUE;
4075#endif
4076 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004077 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004078
4079 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004080 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004082 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 return -1;
4084
4085 return 0;
4086 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 else
4088 {
4089 PyErr_SetString(PyExc_AttributeError, name);
4090 return -1;
4091 }
4092}
4093
4094 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004095WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004097 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004098 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099 else
4100 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004101 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102
Bram Moolenaar6d216452013-05-12 19:00:41 +02004103 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004104 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105 (self));
4106 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004107 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108 }
4109}
4110
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004111static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004112 /* name, function, calling, documentation */
4113 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4114 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004115};
4116
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004117/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004118 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004120
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004121static PyTypeObject WinListType;
4122static PySequenceMethods WinListAsSeq;
4123
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004124typedef struct
4125{
4126 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004127 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004128} WinListObject;
4129
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004130 static PyObject *
4131WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004132{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004133 WinListObject *self;
4134
4135 self = PyObject_NEW(WinListObject, &WinListType);
4136 self->tabObject = tabObject;
4137 Py_INCREF(tabObject);
4138
4139 return (PyObject *)(self);
4140}
4141
4142 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004143WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004144{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004145 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004146
4147 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004148 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004149 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004150 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004151
4152 DESTRUCTOR_FINISH(self);
4153}
4154
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004155 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004156WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004157{
4158 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004159 PyInt n = 0;
4160
Bram Moolenaard6e39182013-05-21 18:30:34 +02004161 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004162 return -1;
4163
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164 while (w != NULL)
4165 {
4166 ++n;
4167 w = W_NEXT(w);
4168 }
4169
4170 return n;
4171}
4172
4173 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004174WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004175{
4176 win_T *w;
4177
Bram Moolenaard6e39182013-05-21 18:30:34 +02004178 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004179 return NULL;
4180
4181 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004182 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004183 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004184
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004185 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 return NULL;
4187}
4188
4189/* Convert a Python string into a Vim line.
4190 *
4191 * The result is in allocated memory. All internal nulls are replaced by
4192 * newline characters. It is an error for the string to contain newline
4193 * characters.
4194 *
4195 * On errors, the Python exception data is set, and NULL is returned.
4196 */
4197 static char *
4198StringToLine(PyObject *obj)
4199{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004200 char *str;
4201 char *save;
4202 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004203 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004204 PyInt i;
4205 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004206
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004207 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004208 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004209 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4210 || str == NULL)
4211 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004213 else if (PyUnicode_Check(obj))
4214 {
4215 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4216 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004217
Bram Moolenaardaa27022013-06-24 22:33:30 +02004218 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004219 || str == NULL)
4220 {
4221 Py_DECREF(bytes);
4222 return NULL;
4223 }
4224 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004225 else
4226 {
4227#if PY_MAJOR_VERSION < 3
4228 PyErr_FORMAT(PyExc_TypeError,
4229 N_("expected str() or unicode() instance, but got %s"),
4230 Py_TYPE_NAME(obj));
4231#else
4232 PyErr_FORMAT(PyExc_TypeError,
4233 N_("expected bytes() or str() instance, but got %s"),
4234 Py_TYPE_NAME(obj));
4235#endif
4236 return NULL;
4237 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004238
4239 /*
4240 * Error checking: String must not contain newlines, as we
4241 * are replacing a single line, and we must replace it with
4242 * a single line.
4243 * A trailing newline is removed, so that append(f.readlines()) works.
4244 */
4245 p = memchr(str, '\n', len);
4246 if (p != NULL)
4247 {
4248 if (p == str + len - 1)
4249 --len;
4250 else
4251 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004252 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004253 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004254 return NULL;
4255 }
4256 }
4257
4258 /* Create a copy of the string, with internal nulls replaced by
4259 * newline characters, as is the vim convention.
4260 */
4261 save = (char *)alloc((unsigned)(len+1));
4262 if (save == NULL)
4263 {
4264 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004265 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004266 return NULL;
4267 }
4268
4269 for (i = 0; i < len; ++i)
4270 {
4271 if (str[i] == '\0')
4272 save[i] = '\n';
4273 else
4274 save[i] = str[i];
4275 }
4276
4277 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004278 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004279
4280 return save;
4281}
4282
4283/* Get a line from the specified buffer. The line number is
4284 * in Vim format (1-based). The line is returned as a Python
4285 * string object.
4286 */
4287 static PyObject *
4288GetBufferLine(buf_T *buf, PyInt n)
4289{
4290 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4291}
4292
4293
4294/* Get a list of lines from the specified buffer. The line numbers
4295 * are in Vim format (1-based). The range is from lo up to, but not
4296 * including, hi. The list is returned as a Python list of string objects.
4297 */
4298 static PyObject *
4299GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4300{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004301 PyInt i;
4302 PyInt n = hi - lo;
4303 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004304
4305 if (list == NULL)
4306 return NULL;
4307
4308 for (i = 0; i < n; ++i)
4309 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004310 PyObject *string = LineToString(
4311 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004312
4313 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004314 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004315 {
4316 Py_DECREF(list);
4317 return NULL;
4318 }
4319
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004320 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321 }
4322
4323 /* The ownership of the Python list is passed to the caller (ie,
4324 * the caller should Py_DECREF() the object when it is finished
4325 * with it).
4326 */
4327
4328 return list;
4329}
4330
4331/*
4332 * Check if deleting lines made the cursor position invalid.
4333 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4334 * deleted).
4335 */
4336 static void
4337py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4338{
4339 if (curwin->w_cursor.lnum >= lo)
4340 {
4341 /* Adjust the cursor position if it's in/after the changed
4342 * lines. */
4343 if (curwin->w_cursor.lnum >= hi)
4344 {
4345 curwin->w_cursor.lnum += extra;
4346 check_cursor_col();
4347 }
4348 else if (extra < 0)
4349 {
4350 curwin->w_cursor.lnum = lo;
4351 check_cursor();
4352 }
4353 else
4354 check_cursor_col();
4355 changed_cline_bef_curs();
4356 }
4357 invalidate_botline();
4358}
4359
Bram Moolenaar19e60942011-06-19 00:27:51 +02004360/*
4361 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004362 * in Vim format (1-based). The replacement line is given as
4363 * a Python string object. The object is checked for validity
4364 * and correct format. Errors are returned as a value of FAIL.
4365 * The return value is OK on success.
4366 * If OK is returned and len_change is not NULL, *len_change
4367 * is set to the change in the buffer length.
4368 */
4369 static int
4370SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4371{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004372 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004373 win_T *save_curwin = NULL;
4374 tabpage_T *save_curtab = NULL;
4375
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004376 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004377 * There are three cases:
4378 * 1. NULL, or None - this is a deletion.
4379 * 2. A string - this is a replacement.
4380 * 3. Anything else - this is an error.
4381 */
4382 if (line == Py_None || line == NULL)
4383 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004384 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004385 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004386
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004387 VimTryStart();
4388
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004389 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004390 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004391 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004392 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 else
4394 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004395 if (buf == curbuf && (save_curwin != NULL
4396 || save_curbuf.br_buf == NULL))
4397 // Using an existing window for the buffer, adjust the cursor
4398 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004399 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004400 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004401 /* Only adjust marks if we managed to switch to a window that
4402 * holds the buffer, otherwise line numbers will be invalid. */
4403 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004404 }
4405
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004406 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004408 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004409 return FAIL;
4410
4411 if (len_change)
4412 *len_change = -1;
4413
4414 return OK;
4415 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004416 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004417 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004418 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004419
4420 if (save == NULL)
4421 return FAIL;
4422
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004423 VimTryStart();
4424
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425 /* We do not need to free "save" if ml_replace() consumes it. */
4426 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004427 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428
4429 if (u_savesub((linenr_T)n) == FAIL)
4430 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004431 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 vim_free(save);
4433 }
4434 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4435 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004436 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004437 vim_free(save);
4438 }
4439 else
4440 changed_bytes((linenr_T)n, 0);
4441
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004442 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443
4444 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004445 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446 check_cursor_col();
4447
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004448 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004449 return FAIL;
4450
4451 if (len_change)
4452 *len_change = 0;
4453
4454 return OK;
4455 }
4456 else
4457 {
4458 PyErr_BadArgument();
4459 return FAIL;
4460 }
4461}
4462
Bram Moolenaar19e60942011-06-19 00:27:51 +02004463/* Replace a range of lines in the specified buffer. The line numbers are in
4464 * Vim format (1-based). The range is from lo up to, but not including, hi.
4465 * The replacement lines are given as a Python list of string objects. The
4466 * list is checked for validity and correct format. Errors are returned as a
4467 * value of FAIL. The return value is OK on success.
4468 * If OK is returned and len_change is not NULL, *len_change
4469 * is set to the change in the buffer length.
4470 */
4471 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004472SetBufferLineList(
4473 buf_T *buf,
4474 PyInt lo,
4475 PyInt hi,
4476 PyObject *list,
4477 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004478{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004479 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004480 win_T *save_curwin = NULL;
4481 tabpage_T *save_curtab = NULL;
4482
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004483 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004484 * There are three cases:
4485 * 1. NULL, or None - this is a deletion.
4486 * 2. A list - this is a replacement.
4487 * 3. Anything else - this is an error.
4488 */
4489 if (list == Py_None || list == NULL)
4490 {
4491 PyInt i;
4492 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004493
4494 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004495 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004496 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004497
4498 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004499 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004500 else
4501 {
4502 for (i = 0; i < n; ++i)
4503 {
4504 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4505 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004506 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004507 break;
4508 }
4509 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004510 if (buf == curbuf && (save_curwin != NULL
4511 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004512 /* Using an existing window for the buffer, adjust the cursor
4513 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004514 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004515 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004516 /* Only adjust marks if we managed to switch to a window that
4517 * holds the buffer, otherwise line numbers will be invalid. */
4518 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004519 }
4520
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004521 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004522
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004523 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004524 return FAIL;
4525
4526 if (len_change)
4527 *len_change = -n;
4528
4529 return OK;
4530 }
4531 else if (PyList_Check(list))
4532 {
4533 PyInt i;
4534 PyInt new_len = PyList_Size(list);
4535 PyInt old_len = hi - lo;
4536 PyInt extra = 0; /* lines added to text, can be negative */
4537 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004538
4539 if (new_len == 0) /* avoid allocating zero bytes */
4540 array = NULL;
4541 else
4542 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004543 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004544 if (array == NULL)
4545 {
4546 PyErr_NoMemory();
4547 return FAIL;
4548 }
4549 }
4550
4551 for (i = 0; i < new_len; ++i)
4552 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004553 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004554
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004555 if (!(line = PyList_GetItem(list, i)) ||
4556 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004557 {
4558 while (i)
4559 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004560 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004561 return FAIL;
4562 }
4563 }
4564
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004565 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004566 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004567
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004568 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004569 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004570
4571 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004572 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004573
4574 /* If the size of the range is reducing (ie, new_len < old_len) we
4575 * need to delete some old_len. We do this at the start, by
4576 * repeatedly deleting line "lo".
4577 */
4578 if (!PyErr_Occurred())
4579 {
4580 for (i = 0; i < old_len - new_len; ++i)
4581 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4582 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004583 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004584 break;
4585 }
4586 extra -= i;
4587 }
4588
4589 /* For as long as possible, replace the existing old_len with the
4590 * new old_len. This is a more efficient operation, as it requires
4591 * less memory allocation and freeing.
4592 */
4593 if (!PyErr_Occurred())
4594 {
4595 for (i = 0; i < old_len && i < new_len; ++i)
4596 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4597 == FAIL)
4598 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004599 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004600 break;
4601 }
4602 }
4603 else
4604 i = 0;
4605
4606 /* Now we may need to insert the remaining new old_len. If we do, we
4607 * must free the strings as we finish with them (we can't pass the
4608 * responsibility to vim in this case).
4609 */
4610 if (!PyErr_Occurred())
4611 {
4612 while (i < new_len)
4613 {
4614 if (ml_append((linenr_T)(lo + i - 1),
4615 (char_u *)array[i], 0, FALSE) == FAIL)
4616 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004617 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004618 break;
4619 }
4620 vim_free(array[i]);
4621 ++i;
4622 ++extra;
4623 }
4624 }
4625
4626 /* Free any left-over old_len, as a result of an error */
4627 while (i < new_len)
4628 {
4629 vim_free(array[i]);
4630 ++i;
4631 }
4632
4633 /* Free the array of old_len. All of its contents have now
4634 * been dealt with (either freed, or the responsibility passed
4635 * to vim.
4636 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004637 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004638
4639 /* Adjust marks. Invalidate any which lie in the
4640 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004641 * Only adjust marks if we managed to switch to a window that holds
4642 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004643 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004644 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004645 (long)MAXLNUM, (long)extra);
4646 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4647
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004648 if (buf == curbuf && (save_curwin != NULL
4649 || save_curbuf.br_buf == NULL))
4650 // Using an existing window for the buffer, adjust the cursor
4651 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004652 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4653
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004654 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004655 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004656
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004657 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004658 return FAIL;
4659
4660 if (len_change)
4661 *len_change = new_len - old_len;
4662
4663 return OK;
4664 }
4665 else
4666 {
4667 PyErr_BadArgument();
4668 return FAIL;
4669 }
4670}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004671
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004672/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004673 * The line number is in Vim format (1-based). The lines to be inserted are
4674 * given as a Python list of string objects or as a single string. The lines
4675 * to be added are checked for validity and correct format. Errors are
4676 * returned as a value of FAIL. The return value is OK on success.
4677 * If OK is returned and len_change is not NULL, *len_change
4678 * is set to the change in the buffer length.
4679 */
4680 static int
4681InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4682{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004683 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004684 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004685 tabpage_T *save_curtab = NULL;
4686
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004687 /* First of all, we check the type of the supplied Python object.
4688 * It must be a string or a list, or the call is in error.
4689 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004690 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004692 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693
4694 if (str == NULL)
4695 return FAIL;
4696
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004698 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004699 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004700
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004701 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004702 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004703 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004704 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004705 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004706 /* Only adjust marks if we managed to switch to a window that
4707 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708 appended_lines_mark((linenr_T)n, 1L);
4709
4710 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004711 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 update_screen(VALID);
4713
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004714 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004715 return FAIL;
4716
4717 if (len_change)
4718 *len_change = 1;
4719
4720 return OK;
4721 }
4722 else if (PyList_Check(lines))
4723 {
4724 PyInt i;
4725 PyInt size = PyList_Size(lines);
4726 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004728 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004729 if (array == NULL)
4730 {
4731 PyErr_NoMemory();
4732 return FAIL;
4733 }
4734
4735 for (i = 0; i < size; ++i)
4736 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004737 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004739 if (!(line = PyList_GetItem(lines, i)) ||
4740 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741 {
4742 while (i)
4743 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004744 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 return FAIL;
4746 }
4747 }
4748
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004750 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004751 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752
4753 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004754 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004755 else
4756 {
4757 for (i = 0; i < size; ++i)
4758 {
4759 if (ml_append((linenr_T)(n + i),
4760 (char_u *)array[i], 0, FALSE) == FAIL)
4761 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004762 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763
4764 /* Free the rest of the lines */
4765 while (i < size)
4766 vim_free(array[i++]);
4767
4768 break;
4769 }
4770 vim_free(array[i]);
4771 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004772 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004773 /* Only adjust marks if we managed to switch to a window that
4774 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004775 appended_lines_mark((linenr_T)n, (long)i);
4776 }
4777
4778 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004779 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004780 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004781 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004782
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 update_screen(VALID);
4784
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004785 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 return FAIL;
4787
4788 if (len_change)
4789 *len_change = size;
4790
4791 return OK;
4792 }
4793 else
4794 {
4795 PyErr_BadArgument();
4796 return FAIL;
4797 }
4798}
4799
4800/*
4801 * Common routines for buffers and line ranges
4802 * -------------------------------------------
4803 */
4804
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004805typedef struct
4806{
4807 PyObject_HEAD
4808 buf_T *buf;
4809} BufferObject;
4810
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004811 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004812CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004813{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004814 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004815 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004816 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817 return -1;
4818 }
4819
4820 return 0;
4821}
4822
4823 static PyObject *
4824RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4825{
4826 if (CheckBuffer(self))
4827 return NULL;
4828
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004829 if (end == -1)
4830 end = self->buf->b_ml.ml_line_count;
4831
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004832 if (n < 0)
4833 n += end - start + 1;
4834
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004835 if (n < 0 || n > end - start)
4836 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004837 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004838 return NULL;
4839 }
4840
4841 return GetBufferLine(self->buf, n+start);
4842}
4843
4844 static PyObject *
4845RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4846{
4847 PyInt size;
4848
4849 if (CheckBuffer(self))
4850 return NULL;
4851
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004852 if (end == -1)
4853 end = self->buf->b_ml.ml_line_count;
4854
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004855 size = end - start + 1;
4856
4857 if (lo < 0)
4858 lo = 0;
4859 else if (lo > size)
4860 lo = size;
4861 if (hi < 0)
4862 hi = 0;
4863 if (hi < lo)
4864 hi = lo;
4865 else if (hi > size)
4866 hi = size;
4867
4868 return GetBufferLineList(self->buf, lo+start, hi+start);
4869}
4870
4871 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004872RBAsItem(
4873 BufferObject *self,
4874 PyInt n,
4875 PyObject *valObject,
4876 PyInt start,
4877 PyInt end,
4878 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004879{
4880 PyInt len_change;
4881
4882 if (CheckBuffer(self))
4883 return -1;
4884
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004885 if (end == -1)
4886 end = self->buf->b_ml.ml_line_count;
4887
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004888 if (n < 0)
4889 n += end - start + 1;
4890
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004891 if (n < 0 || n > end - start)
4892 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004893 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004894 return -1;
4895 }
4896
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004897 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004898 return -1;
4899
4900 if (new_end)
4901 *new_end = end + len_change;
4902
4903 return 0;
4904}
4905
Bram Moolenaar19e60942011-06-19 00:27:51 +02004906 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004907RBAsSlice(
4908 BufferObject *self,
4909 PyInt lo,
4910 PyInt hi,
4911 PyObject *valObject,
4912 PyInt start,
4913 PyInt end,
4914 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004915{
4916 PyInt size;
4917 PyInt len_change;
4918
4919 /* Self must be a valid buffer */
4920 if (CheckBuffer(self))
4921 return -1;
4922
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004923 if (end == -1)
4924 end = self->buf->b_ml.ml_line_count;
4925
Bram Moolenaar19e60942011-06-19 00:27:51 +02004926 /* Sort out the slice range */
4927 size = end - start + 1;
4928
4929 if (lo < 0)
4930 lo = 0;
4931 else if (lo > size)
4932 lo = size;
4933 if (hi < 0)
4934 hi = 0;
4935 if (hi < lo)
4936 hi = lo;
4937 else if (hi > size)
4938 hi = size;
4939
4940 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004941 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004942 return -1;
4943
4944 if (new_end)
4945 *new_end = end + len_change;
4946
4947 return 0;
4948}
4949
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004950
4951 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004952RBAppend(
4953 BufferObject *self,
4954 PyObject *args,
4955 PyInt start,
4956 PyInt end,
4957 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004958{
4959 PyObject *lines;
4960 PyInt len_change;
4961 PyInt max;
4962 PyInt n;
4963
4964 if (CheckBuffer(self))
4965 return NULL;
4966
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004967 if (end == -1)
4968 end = self->buf->b_ml.ml_line_count;
4969
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004970 max = n = end - start + 1;
4971
4972 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4973 return NULL;
4974
4975 if (n < 0 || n > max)
4976 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004977 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004978 return NULL;
4979 }
4980
4981 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4982 return NULL;
4983
4984 if (new_end)
4985 *new_end = end + len_change;
4986
4987 Py_INCREF(Py_None);
4988 return Py_None;
4989}
4990
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004991/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992 */
4993
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004994static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004995static PySequenceMethods RangeAsSeq;
4996static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004997
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004998typedef struct
4999{
5000 PyObject_HEAD
5001 BufferObject *buf;
5002 PyInt start;
5003 PyInt end;
5004} RangeObject;
5005
5006 static PyObject *
5007RangeNew(buf_T *buf, PyInt start, PyInt end)
5008{
5009 BufferObject *bufr;
5010 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005011 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005012 if (self == NULL)
5013 return NULL;
5014
5015 bufr = (BufferObject *)BufferNew(buf);
5016 if (bufr == NULL)
5017 {
5018 Py_DECREF(self);
5019 return NULL;
5020 }
5021 Py_INCREF(bufr);
5022
5023 self->buf = bufr;
5024 self->start = start;
5025 self->end = end;
5026
5027 return (PyObject *)(self);
5028}
5029
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005031RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005032{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005033 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005034 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005035 PyObject_GC_Del((void *)(self));
5036}
5037
5038 static int
5039RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5040{
5041 Py_VISIT(((PyObject *)(self->buf)));
5042 return 0;
5043}
5044
5045 static int
5046RangeClear(RangeObject *self)
5047{
5048 Py_CLEAR(self->buf);
5049 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005050}
5051
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005052 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005053RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005054{
5055 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005056 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005057 return -1; /* ??? */
5058
Bram Moolenaard6e39182013-05-21 18:30:34 +02005059 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060}
5061
5062 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005066}
5067
5068 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005070{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005071 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005072}
5073
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005074static char *RangeAttrs[] = {
5075 "start", "end",
5076 NULL
5077};
5078
5079 static PyObject *
5080RangeDir(PyObject *self)
5081{
5082 return ObjectDir(self, RangeAttrs);
5083}
5084
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005085 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089}
5090
5091 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005095 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5096 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097 else
5098 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005099 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100
5101 if (name == NULL)
5102 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005104 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005105 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106 }
5107}
5108
5109static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005110 /* name, function, calling, documentation */
5111 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005112 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5113 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005114};
5115
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005116static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005117static PySequenceMethods BufferAsSeq;
5118static PyMappingMethods BufferAsMapping;
5119
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005121BufferNew(buf_T *buf)
5122{
5123 /* We need to handle deletion of buffers underneath us.
5124 * If we add a "b_python*_ref" field to the buf_T structure,
5125 * then we can get at it in buf_freeall() in vim. We then
5126 * need to create only ONE Python object per buffer - if
5127 * we try to create a second, just INCREF the existing one
5128 * and return it. The (single) Python object referring to
5129 * the buffer is stored in "b_python*_ref".
5130 * Question: what to do on a buf_freeall(). We'll probably
5131 * have to either delete the Python object (DECREF it to
5132 * zero - a bad idea, as it leaves dangling refs!) or
5133 * set the buf_T * value to an invalid value (-1?), which
5134 * means we need checks in all access functions... Bah.
5135 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005137 * b_python_ref and b_python3_ref fields respectively.
5138 */
5139
5140 BufferObject *self;
5141
5142 if (BUF_PYTHON_REF(buf) != NULL)
5143 {
5144 self = BUF_PYTHON_REF(buf);
5145 Py_INCREF(self);
5146 }
5147 else
5148 {
5149 self = PyObject_NEW(BufferObject, &BufferType);
5150 if (self == NULL)
5151 return NULL;
5152 self->buf = buf;
5153 BUF_PYTHON_REF(buf) = self;
5154 }
5155
5156 return (PyObject *)(self);
5157}
5158
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005159 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005160BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005161{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005162 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5163 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005164
5165 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005166}
5167
Bram Moolenaar971db462013-05-12 18:44:48 +02005168 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005170{
5171 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005173 return -1; /* ??? */
5174
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005176}
5177
5178 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005179BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005180{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005181 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005182}
5183
5184 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005185BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005186{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005187 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005188}
5189
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005190static char *BufferAttrs[] = {
5191 "name", "number", "vars", "options", "valid",
5192 NULL
5193};
5194
5195 static PyObject *
5196BufferDir(PyObject *self)
5197{
5198 return ObjectDir(self, BufferAttrs);
5199}
5200
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005201 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005202BufferAttrValid(BufferObject *self, char *name)
5203{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005204 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005205
5206 if (strcmp(name, "valid") != 0)
5207 return NULL;
5208
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005209 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5210 Py_INCREF(ret);
5211 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005212}
5213
5214 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005216{
5217 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005218 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005219 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005220 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005221 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005222 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005223 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005224 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005225 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5226 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005227 else if (strcmp(name, "__members__") == 0)
5228 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005229 else
5230 return NULL;
5231}
5232
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005233 static int
5234BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5235{
5236 if (CheckBuffer(self))
5237 return -1;
5238
5239 if (strcmp(name, "name") == 0)
5240 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005241 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005242 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005243 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005244 PyObject *todecref;
5245
5246 if (!(val = StringToChars(valObject, &todecref)))
5247 return -1;
5248
5249 VimTryStart();
5250 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5251 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005252 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005253 aucmd_restbuf(&aco);
5254 Py_XDECREF(todecref);
5255 if (VimTryEnd())
5256 return -1;
5257
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005258 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005259 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005260 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005261 return -1;
5262 }
5263 return 0;
5264 }
5265 else
5266 {
5267 PyErr_SetString(PyExc_AttributeError, name);
5268 return -1;
5269 }
5270}
5271
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005272 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005273BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005274{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005275 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005276}
5277
5278 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005279BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005280{
5281 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005282 char_u *pmark;
5283 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005284 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005285 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005286
Bram Moolenaard6e39182013-05-21 18:30:34 +02005287 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005288 return NULL;
5289
Bram Moolenaar389a1792013-06-23 13:00:44 +02005290 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005291 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005292
Bram Moolenaar389a1792013-06-23 13:00:44 +02005293 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005294 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005295 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005296 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005297 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005298 return NULL;
5299 }
5300
5301 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005302
5303 Py_XDECREF(todecref);
5304
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005305 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005306 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005307 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005308 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005309 if (VimTryEnd())
5310 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311
5312 if (posp == NULL)
5313 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005314 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315 return NULL;
5316 }
5317
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005318 if (posp->lnum <= 0)
5319 {
5320 /* Or raise an error? */
5321 Py_INCREF(Py_None);
5322 return Py_None;
5323 }
5324
5325 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5326}
5327
5328 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005329BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005330{
5331 PyInt start;
5332 PyInt end;
5333
Bram Moolenaard6e39182013-05-21 18:30:34 +02005334 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005335 return NULL;
5336
5337 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5338 return NULL;
5339
Bram Moolenaard6e39182013-05-21 18:30:34 +02005340 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005341}
5342
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005343 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005344BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005345{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005346 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005347 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005348 else
5349 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005350 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005351
5352 if (name == NULL)
5353 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005354
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005355 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005356 }
5357}
5358
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005359static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005360 /* name, function, calling, documentation */
5361 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005362 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005363 {"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 +02005364 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5365 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005366};
5367
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005368/*
5369 * Buffer list object - Implementation
5370 */
5371
5372static PyTypeObject BufMapType;
5373
5374typedef struct
5375{
5376 PyObject_HEAD
5377} BufMapObject;
5378
5379 static PyInt
5380BufMapLength(PyObject *self UNUSED)
5381{
5382 buf_T *b = firstbuf;
5383 PyInt n = 0;
5384
5385 while (b)
5386 {
5387 ++n;
5388 b = b->b_next;
5389 }
5390
5391 return n;
5392}
5393
5394 static PyObject *
5395BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5396{
5397 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005398 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005399
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005400 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005401 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005402
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005403 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005404
5405 if (b)
5406 return BufferNew(b);
5407 else
5408 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005409 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005410 return NULL;
5411 }
5412}
5413
5414 static void
5415BufMapIterDestruct(PyObject *buffer)
5416{
5417 /* Iteration was stopped before all buffers were processed */
5418 if (buffer)
5419 {
5420 Py_DECREF(buffer);
5421 }
5422}
5423
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005424 static int
5425BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5426{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005427 if (buffer)
5428 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005429 return 0;
5430}
5431
5432 static int
5433BufMapIterClear(PyObject **buffer)
5434{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005435 if (*buffer)
5436 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005437 return 0;
5438}
5439
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005440 static PyObject *
5441BufMapIterNext(PyObject **buffer)
5442{
5443 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005444 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005445
5446 if (!*buffer)
5447 return NULL;
5448
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005449 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005450
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005451 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005452 {
5453 *buffer = NULL;
5454 return NULL;
5455 }
5456
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005457 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005458 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005459 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005460 return NULL;
5461 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005462 /* Do not increment reference: we no longer hold it (decref), but whoever
5463 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005464 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005465}
5466
5467 static PyObject *
5468BufMapIter(PyObject *self UNUSED)
5469{
5470 PyObject *buffer;
5471
5472 buffer = BufferNew(firstbuf);
5473 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005474 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5475 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005476}
5477
5478static PyMappingMethods BufMapAsMapping = {
5479 (lenfunc) BufMapLength,
5480 (binaryfunc) BufMapItem,
5481 (objobjargproc) 0,
5482};
5483
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005484/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005485 */
5486
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005487static char *CurrentAttrs[] = {
5488 "buffer", "window", "line", "range", "tabpage",
5489 NULL
5490};
5491
5492 static PyObject *
5493CurrentDir(PyObject *self)
5494{
5495 return ObjectDir(self, CurrentAttrs);
5496}
5497
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005498 static PyObject *
5499CurrentGetattr(PyObject *self UNUSED, char *name)
5500{
5501 if (strcmp(name, "buffer") == 0)
5502 return (PyObject *)BufferNew(curbuf);
5503 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005504 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005505 else if (strcmp(name, "tabpage") == 0)
5506 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005507 else if (strcmp(name, "line") == 0)
5508 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5509 else if (strcmp(name, "range") == 0)
5510 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005511 else if (strcmp(name, "__members__") == 0)
5512 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005513 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005514#if PY_MAJOR_VERSION < 3
5515 return Py_FindMethod(WindowMethods, self, name);
5516#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005517 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005518#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005519}
5520
5521 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005522CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005523{
5524 if (strcmp(name, "line") == 0)
5525 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005526 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5527 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005528 return -1;
5529
5530 return 0;
5531 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005532 else if (strcmp(name, "buffer") == 0)
5533 {
5534 int count;
5535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005536 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005537 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005538 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005539 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005540 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005541 return -1;
5542 }
5543
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005544 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005545 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005546 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005547
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005548 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005549 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5550 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005551 if (VimTryEnd())
5552 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005553 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005554 return -1;
5555 }
5556
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005557 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005558 }
5559 else if (strcmp(name, "window") == 0)
5560 {
5561 int count;
5562
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005564 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005565 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005566 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005567 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005568 return -1;
5569 }
5570
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005572 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005574
5575 if (!count)
5576 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005577 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005578 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005579 return -1;
5580 }
5581
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005582 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005583 win_goto(((WindowObject *)(valObject))->win);
5584 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005585 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005586 if (VimTryEnd())
5587 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005588 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005589 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005590 return -1;
5591 }
5592
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005593 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005594 }
5595 else if (strcmp(name, "tabpage") == 0)
5596 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005597 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005598 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005599 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005600 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005601 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005602 return -1;
5603 }
5604
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005606 return -1;
5607
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005608 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005609 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5610 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005611 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005612 if (VimTryEnd())
5613 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005614 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005615 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005616 return -1;
5617 }
5618
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005619 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005621 else
5622 {
5623 PyErr_SetString(PyExc_AttributeError, name);
5624 return -1;
5625 }
5626}
5627
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005628static struct PyMethodDef CurrentMethods[] = {
5629 /* name, function, calling, documentation */
5630 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5631 { NULL, NULL, 0, NULL}
5632};
5633
Bram Moolenaardb913952012-06-29 12:54:53 +02005634 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005635init_range_cmd(exarg_T *eap)
5636{
5637 RangeStart = eap->line1;
5638 RangeEnd = eap->line2;
5639}
5640
5641 static void
5642init_range_eval(typval_T *rettv UNUSED)
5643{
5644 RangeStart = (PyInt) curwin->w_cursor.lnum;
5645 RangeEnd = RangeStart;
5646}
5647
5648 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005649run_cmd(const char *cmd, void *arg UNUSED
5650#ifdef PY_CAN_RECURSE
5651 , PyGILState_STATE *pygilstate UNUSED
5652#endif
5653 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005654{
Bram Moolenaar41009372013-07-01 22:03:04 +02005655 PyObject *run_ret;
5656 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5657 if (run_ret != NULL)
5658 {
5659 Py_DECREF(run_ret);
5660 }
5661 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005663 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005664 PyErr_Clear();
5665 }
5666 else
5667 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005668}
5669
5670static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5671static int code_hdr_len = 30;
5672
5673 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005674run_do(const char *cmd, void *arg UNUSED
5675#ifdef PY_CAN_RECURSE
5676 , PyGILState_STATE *pygilstate
5677#endif
5678 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005679{
5680 PyInt lnum;
5681 size_t len;
5682 char *code;
5683 int status;
5684 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005685 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005686 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005687
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005688 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005690 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691 return;
5692 }
5693
5694 len = code_hdr_len + STRLEN(cmd);
5695 code = PyMem_New(char, len + 1);
5696 memcpy(code, code_hdr, code_hdr_len);
5697 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005698 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5699 status = -1;
5700 if (run_ret != NULL)
5701 {
5702 status = 0;
5703 Py_DECREF(run_ret);
5704 }
5705 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5706 {
5707 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005708 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005709 PyErr_Clear();
5710 return;
5711 }
5712 else
5713 PyErr_PrintEx(1);
5714
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005715 PyMem_Free(code);
5716
5717 if (status)
5718 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005719 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005720 return;
5721 }
5722
5723 status = 0;
5724 pymain = PyImport_AddModule("__main__");
5725 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005726#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005727 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005728#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005729
5730 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5731 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005732 PyObject *line;
5733 PyObject *linenr;
5734 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005735
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005736#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005737 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005738#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005739 /* Check the line number, the command my have deleted lines. */
5740 if (lnum > curbuf->b_ml.ml_line_count
5741 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005742 goto err;
5743 if (!(linenr = PyInt_FromLong((long) lnum)))
5744 {
5745 Py_DECREF(line);
5746 goto err;
5747 }
5748 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5749 Py_DECREF(line);
5750 Py_DECREF(linenr);
5751 if (!ret)
5752 goto err;
5753
Bram Moolenaara58883b2017-01-29 21:31:09 +01005754 /* Check that the command didn't switch to another buffer. */
5755 if (curbuf != was_curbuf)
5756 {
5757 Py_XDECREF(ret);
5758 goto err;
5759 }
5760
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005761 if (ret != Py_None)
5762 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005763 {
5764 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005765 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005766 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005767
5768 Py_XDECREF(ret);
5769 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005770#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005772#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005773 }
5774 goto out;
5775err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005776#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005777 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005778#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005779 PyErr_PrintEx(0);
5780 PythonIO_Flush();
5781 status = 1;
5782out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005783#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005784 if (!status)
5785 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005786#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005787 Py_DECREF(pyfunc);
5788 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5789 if (status)
5790 return;
5791 check_cursor();
5792 update_curbuf(NOT_VALID);
5793}
5794
5795 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005796run_eval(const char *cmd, typval_T *rettv
5797#ifdef PY_CAN_RECURSE
5798 , PyGILState_STATE *pygilstate UNUSED
5799#endif
5800 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005801{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005802 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005803
Bram Moolenaar41009372013-07-01 22:03:04 +02005804 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005805 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005807 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005809 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005810 PyErr_Clear();
5811 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005812 else
5813 {
5814 if (PyErr_Occurred() && !msg_silent)
5815 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005816 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005817 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005818 }
5819 else
5820 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005821 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005822 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005823 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005824 }
5825 PyErr_Clear();
5826}
5827
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005828 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005829set_ref_in_py(const int copyID)
5830{
5831 pylinkedlist_T *cur;
5832 dict_T *dd;
5833 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005834 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005835 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005836 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005837
5838 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005839 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005840 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005841 {
5842 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5843 if (dd->dv_copyID != copyID)
5844 {
5845 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005847 }
5848 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005849 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005850
5851 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005852 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005853 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005854 {
5855 ll = ((ListObject *) (cur->pll_obj))->list;
5856 if (ll->lv_copyID != copyID)
5857 {
5858 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005859 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005860 }
5861 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005862 }
5863
Bram Moolenaar8110a092016-04-14 15:56:09 +02005864 if (lastfunc != NULL)
5865 {
5866 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5867 {
5868 func = (FunctionObject *) cur->pll_obj;
5869 if (func->self != NULL && func->self->dv_copyID != copyID)
5870 {
5871 func->self->dv_copyID = copyID;
5872 abort = abort || set_ref_in_ht(
5873 &func->self->dv_hashtab, copyID, NULL);
5874 }
5875 if (func->argc)
5876 for (i = 0; !abort && i < func->argc; ++i)
5877 abort = abort
5878 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5879 }
5880 }
5881
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005882 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005883}
5884
5885 static int
5886set_string_copy(char_u *str, typval_T *tv)
5887{
5888 tv->vval.v_string = vim_strsave(str);
5889 if (tv->vval.v_string == NULL)
5890 {
5891 PyErr_NoMemory();
5892 return -1;
5893 }
5894 return 0;
5895}
5896
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005897 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005898pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005899{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005900 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901 char_u *key;
5902 dictitem_T *di;
5903 PyObject *keyObject;
5904 PyObject *valObject;
5905 Py_ssize_t iter = 0;
5906
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005907 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005908 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005909
5910 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005911 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005912
5913 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5914 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005915 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916
Bram Moolenaara03e6312013-05-29 22:49:26 +02005917 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005918 {
5919 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005921 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005923 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005924 {
5925 dict_unref(dict);
5926 return -1;
5927 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005928
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005929 if (*key == NUL)
5930 {
5931 dict_unref(dict);
5932 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005933 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005934 return -1;
5935 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936
5937 di = dictitem_alloc(key);
5938
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005939 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005940
5941 if (di == NULL)
5942 {
5943 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005944 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005945 return -1;
5946 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005947
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005948 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005949 {
5950 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005951 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005952 return -1;
5953 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005954
5955 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005957 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005958 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005960 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961 return -1;
5962 }
5963 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964
5965 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return 0;
5967}
5968
5969 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005970pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005972 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 char_u *key;
5974 dictitem_T *di;
5975 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005976 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 PyObject *keyObject;
5978 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005979
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005980 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005982
5983 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005984 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005986 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005987 {
5988 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005990 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005991
5992 if (!(iterator = PyObject_GetIter(list)))
5993 {
5994 dict_unref(dict);
5995 Py_DECREF(list);
5996 return -1;
5997 }
5998 Py_DECREF(list);
5999
6000 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006001 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006002 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006003
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006004 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006005 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006006 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006007 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006008 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006009 return -1;
6010 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006011
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006012 if (*key == NUL)
6013 {
6014 Py_DECREF(keyObject);
6015 Py_DECREF(iterator);
6016 Py_XDECREF(todecref);
6017 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006018 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006019 return -1;
6020 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006021
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006022 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006023 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006024 Py_DECREF(keyObject);
6025 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006026 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006027 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006028 return -1;
6029 }
6030
6031 di = dictitem_alloc(key);
6032
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006033 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006034 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006035
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006036 if (di == NULL)
6037 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006038 Py_DECREF(iterator);
6039 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006040 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006041 PyErr_NoMemory();
6042 return -1;
6043 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006044
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006045 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006046 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006047 Py_DECREF(iterator);
6048 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006049 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006050 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006051 return -1;
6052 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006053
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006054 Py_DECREF(valObject);
6055
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006056 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006057 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006058 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006059 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 dictitem_free(di);
6061 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006062 return -1;
6063 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006065 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006067 return 0;
6068}
6069
6070 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006071pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006072{
6073 list_T *l;
6074
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006075 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006077
6078 tv->v_type = VAR_LIST;
6079 tv->vval.v_list = l;
6080
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006081 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006082 {
6083 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006084 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006085 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006086
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006087 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006088 return 0;
6089}
6090
Bram Moolenaardb913952012-06-29 12:54:53 +02006091typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6092
6093 static int
6094convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006095 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006096{
6097 PyObject *capsule;
6098 char hexBuf[sizeof(void *) * 2 + 3];
6099
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006100 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006101
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006102#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006103 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006104#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006105 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006106#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006107 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006108 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006109#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006110 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006111#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006112 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006113#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006114 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6115 {
6116 Py_DECREF(capsule);
6117 tv->v_type = VAR_UNKNOWN;
6118 return -1;
6119 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006120
6121 Py_DECREF(capsule);
6122
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006123 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006124 {
6125 tv->v_type = VAR_UNKNOWN;
6126 return -1;
6127 }
6128 /* As we are not using copy_tv which increments reference count we must
6129 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006130 if (tv->v_type == VAR_DICT)
6131 ++tv->vval.v_dict->dv_refcount;
6132 else if (tv->v_type == VAR_LIST)
6133 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006134 }
6135 else
6136 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006137 typval_T *v;
6138
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006139#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006140 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006141#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006142 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006143#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006144 copy_tv(v, tv);
6145 }
6146 return 0;
6147}
6148
6149 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006150ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6151{
6152 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006153 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006154
6155 if (!(lookup_dict = PyDict_New()))
6156 return -1;
6157
6158 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6159 {
6160 tv->v_type = VAR_DICT;
6161 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6162 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006163 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006164 }
6165 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006166 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006167 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006168 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006169 else
6170 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006171 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006172 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006173 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006174 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006175 }
6176 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006177 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006178}
6179
6180 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006181ConvertFromPySequence(PyObject *obj, typval_T *tv)
6182{
6183 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006184 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006185
6186 if (!(lookup_dict = PyDict_New()))
6187 return -1;
6188
6189 if (PyType_IsSubtype(obj->ob_type, &ListType))
6190 {
6191 tv->v_type = VAR_LIST;
6192 tv->vval.v_list = (((ListObject *)(obj))->list);
6193 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006194 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006195 }
6196 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006197 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006198 else
6199 {
6200 PyErr_FORMAT(PyExc_TypeError,
6201 N_("unable to convert %s to vim list"),
6202 Py_TYPE_NAME(obj));
6203 ret = -1;
6204 }
6205 Py_DECREF(lookup_dict);
6206 return ret;
6207}
6208
6209 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006210ConvertFromPyObject(PyObject *obj, typval_T *tv)
6211{
6212 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006213 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006214
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006215 if (!(lookup_dict = PyDict_New()))
6216 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006217 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006218 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006219 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006220}
6221
6222 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006223_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006224{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006225 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006226 {
6227 tv->v_type = VAR_DICT;
6228 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6229 ++tv->vval.v_dict->dv_refcount;
6230 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006231 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006232 {
6233 tv->v_type = VAR_LIST;
6234 tv->vval.v_list = (((ListObject *)(obj))->list);
6235 ++tv->vval.v_list->lv_refcount;
6236 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006237 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006239 FunctionObject *func = (FunctionObject *) obj;
6240 if (func->self != NULL || func->argv != NULL)
6241 {
6242 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6243 set_partial(func, pt, TRUE);
6244 tv->vval.v_partial = pt;
6245 tv->v_type = VAR_PARTIAL;
6246 }
6247 else
6248 {
6249 if (set_string_copy(func->name, tv) == -1)
6250 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006251
Bram Moolenaar8110a092016-04-14 15:56:09 +02006252 tv->v_type = VAR_FUNC;
6253 }
6254 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006255 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 else if (PyBytes_Check(obj))
6257 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006258 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006259
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006260 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006261 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006262 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006263 return -1;
6264
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006265 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006266 return -1;
6267
6268 tv->v_type = VAR_STRING;
6269 }
6270 else if (PyUnicode_Check(obj))
6271 {
6272 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006273 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006274
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006275 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 if (bytes == NULL)
6277 return -1;
6278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006279 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006280 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006281 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006282 return -1;
6283
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006284 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 {
6286 Py_XDECREF(bytes);
6287 return -1;
6288 }
6289 Py_XDECREF(bytes);
6290
6291 tv->v_type = VAR_STRING;
6292 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006293#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006294 else if (PyInt_Check(obj))
6295 {
6296 tv->v_type = VAR_NUMBER;
6297 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006298 if (PyErr_Occurred())
6299 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006300 }
6301#endif
6302 else if (PyLong_Check(obj))
6303 {
6304 tv->v_type = VAR_NUMBER;
6305 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006306 if (PyErr_Occurred())
6307 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006308 }
6309 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006310 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006311#ifdef FEAT_FLOAT
6312 else if (PyFloat_Check(obj))
6313 {
6314 tv->v_type = VAR_FLOAT;
6315 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6316 }
6317#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006318 else if (PyObject_HasAttrString(obj, "keys"))
6319 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006320 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006321 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006322 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006323 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006324 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006325 else if (PyNumber_Check(obj))
6326 {
6327 PyObject *num;
6328
6329 if (!(num = PyNumber_Long(obj)))
6330 return -1;
6331
6332 tv->v_type = VAR_NUMBER;
6333 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6334
6335 Py_DECREF(num);
6336 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006337 else if (obj == Py_None)
6338 {
6339 tv->v_type = VAR_SPECIAL;
6340 tv->vval.v_number = VVAL_NONE;
6341 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006342 else
6343 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006344 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006345 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006346 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006347 return -1;
6348 }
6349 return 0;
6350}
6351
6352 static PyObject *
6353ConvertToPyObject(typval_T *tv)
6354{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006355 typval_T *argv;
6356 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006357 if (tv == NULL)
6358 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006359 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006360 return NULL;
6361 }
6362 switch (tv->v_type)
6363 {
6364 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006365 return PyBytes_FromString(tv->vval.v_string == NULL
6366 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006367 case VAR_NUMBER:
6368 return PyLong_FromLong((long) tv->vval.v_number);
6369#ifdef FEAT_FLOAT
6370 case VAR_FLOAT:
6371 return PyFloat_FromDouble((double) tv->vval.v_float);
6372#endif
6373 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006374 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006375 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006376 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006377 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006378 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006379 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006380 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006381 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006382 if (tv->vval.v_partial->pt_argc)
6383 {
6384 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6385 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6386 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6387 }
6388 else
6389 argv = NULL;
6390 if (tv->vval.v_partial->pt_dict != NULL)
6391 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006392 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006393 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006394 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006395 tv->vval.v_partial->pt_dict,
6396 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006397 case VAR_BLOB:
6398 return PyBytes_FromStringAndSize(
6399 (char*) tv->vval.v_blob->bv_ga.ga_data,
6400 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006401 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006402 case VAR_CHANNEL:
6403 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006404 Py_INCREF(Py_None);
6405 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006406 case VAR_SPECIAL:
6407 switch (tv->vval.v_number)
6408 {
6409 case VVAL_FALSE: return AlwaysFalse(NULL);
6410 case VVAL_TRUE: return AlwaysTrue(NULL);
6411 case VVAL_NONE:
6412 case VVAL_NULL: return AlwaysNone(NULL);
6413 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006414 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006415 return NULL;
6416 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006417 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006418}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006419
6420typedef struct
6421{
6422 PyObject_HEAD
6423} CurrentObject;
6424static PyTypeObject CurrentType;
6425
6426 static void
6427init_structs(void)
6428{
6429 vim_memset(&OutputType, 0, sizeof(OutputType));
6430 OutputType.tp_name = "vim.message";
6431 OutputType.tp_basicsize = sizeof(OutputObject);
6432 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6433 OutputType.tp_doc = "vim message object";
6434 OutputType.tp_methods = OutputMethods;
6435#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006436 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6437 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006438 OutputType.tp_alloc = call_PyType_GenericAlloc;
6439 OutputType.tp_new = call_PyType_GenericNew;
6440 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006441 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006442#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006443 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6444 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006445 // Disabled, because this causes a crash in test86
6446 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006447#endif
6448
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006449 vim_memset(&IterType, 0, sizeof(IterType));
6450 IterType.tp_name = "vim.iter";
6451 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006452 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006453 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006454 IterType.tp_iter = (getiterfunc)IterIter;
6455 IterType.tp_iternext = (iternextfunc)IterNext;
6456 IterType.tp_dealloc = (destructor)IterDestructor;
6457 IterType.tp_traverse = (traverseproc)IterTraverse;
6458 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006459
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006460 vim_memset(&BufferType, 0, sizeof(BufferType));
6461 BufferType.tp_name = "vim.buffer";
6462 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006463 BufferType.tp_dealloc = (destructor)BufferDestructor;
6464 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465 BufferType.tp_as_sequence = &BufferAsSeq;
6466 BufferType.tp_as_mapping = &BufferAsMapping;
6467 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6468 BufferType.tp_doc = "vim buffer object";
6469 BufferType.tp_methods = BufferMethods;
6470#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006471 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006472 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006473 BufferType.tp_alloc = call_PyType_GenericAlloc;
6474 BufferType.tp_new = call_PyType_GenericNew;
6475 BufferType.tp_free = call_PyObject_Free;
6476#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006477 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006478 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006479#endif
6480
6481 vim_memset(&WindowType, 0, sizeof(WindowType));
6482 WindowType.tp_name = "vim.window";
6483 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006484 WindowType.tp_dealloc = (destructor)WindowDestructor;
6485 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006486 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006487 WindowType.tp_doc = "vim Window object";
6488 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6490 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006492 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6493 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006494 WindowType.tp_alloc = call_PyType_GenericAlloc;
6495 WindowType.tp_new = call_PyType_GenericNew;
6496 WindowType.tp_free = call_PyObject_Free;
6497#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006498 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6499 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006500#endif
6501
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006502 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6503 TabPageType.tp_name = "vim.tabpage";
6504 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6506 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006507 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6508 TabPageType.tp_doc = "vim tab page object";
6509 TabPageType.tp_methods = TabPageMethods;
6510#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006511 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006512 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6513 TabPageType.tp_new = call_PyType_GenericNew;
6514 TabPageType.tp_free = call_PyObject_Free;
6515#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006516 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006517#endif
6518
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006519 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6520 BufMapType.tp_name = "vim.bufferlist";
6521 BufMapType.tp_basicsize = sizeof(BufMapObject);
6522 BufMapType.tp_as_mapping = &BufMapAsMapping;
6523 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006524 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006525 BufferType.tp_doc = "vim buffer list";
6526
6527 vim_memset(&WinListType, 0, sizeof(WinListType));
6528 WinListType.tp_name = "vim.windowlist";
6529 WinListType.tp_basicsize = sizeof(WinListType);
6530 WinListType.tp_as_sequence = &WinListAsSeq;
6531 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6532 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006533 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006534
6535 vim_memset(&TabListType, 0, sizeof(TabListType));
6536 TabListType.tp_name = "vim.tabpagelist";
6537 TabListType.tp_basicsize = sizeof(TabListType);
6538 TabListType.tp_as_sequence = &TabListAsSeq;
6539 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6540 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006541
6542 vim_memset(&RangeType, 0, sizeof(RangeType));
6543 RangeType.tp_name = "vim.range";
6544 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006545 RangeType.tp_dealloc = (destructor)RangeDestructor;
6546 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006547 RangeType.tp_as_sequence = &RangeAsSeq;
6548 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006549 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006550 RangeType.tp_doc = "vim Range object";
6551 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006552 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6553 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006554#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006555 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006556 RangeType.tp_alloc = call_PyType_GenericAlloc;
6557 RangeType.tp_new = call_PyType_GenericNew;
6558 RangeType.tp_free = call_PyObject_Free;
6559#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006560 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006561#endif
6562
6563 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6564 CurrentType.tp_name = "vim.currentdata";
6565 CurrentType.tp_basicsize = sizeof(CurrentObject);
6566 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6567 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006568 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006569#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006570 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6571 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006573 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6574 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006575#endif
6576
6577 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6578 DictionaryType.tp_name = "vim.dictionary";
6579 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006580 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006581 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006583 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006584 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6585 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006586 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6587 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6588 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006589#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006590 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6591 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006593 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6594 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006595#endif
6596
6597 vim_memset(&ListType, 0, sizeof(ListType));
6598 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006599 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600 ListType.tp_basicsize = sizeof(ListObject);
6601 ListType.tp_as_sequence = &ListAsSeq;
6602 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006603 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006604 ListType.tp_doc = "list pushing modifications to vim structure";
6605 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006606 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006607 ListType.tp_new = (newfunc)ListConstructor;
6608 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006609#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006610 ListType.tp_getattro = (getattrofunc)ListGetattro;
6611 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006612#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006613 ListType.tp_getattr = (getattrfunc)ListGetattr;
6614 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006615#endif
6616
6617 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006618 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006620 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6621 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006622 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623 FunctionType.tp_doc = "object that calls vim function";
6624 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006625 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006626 FunctionType.tp_new = (newfunc)FunctionConstructor;
6627 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006628#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006629 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006630#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006631 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006632#endif
6633
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006634 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6635 OptionsType.tp_name = "vim.options";
6636 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006637 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006638 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006639 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006640 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006641 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006642 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6643 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6644 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006645
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006646#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006647 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6648 LoaderType.tp_name = "vim.Loader";
6649 LoaderType.tp_basicsize = sizeof(LoaderObject);
6650 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6651 LoaderType.tp_doc = "vim message object";
6652 LoaderType.tp_methods = LoaderMethods;
6653 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006654#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006655
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006656#if PY_MAJOR_VERSION >= 3
6657 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6658 vimmodule.m_name = "vim";
6659 vimmodule.m_doc = "Vim Python interface\n";
6660 vimmodule.m_size = -1;
6661 vimmodule.m_methods = VimMethods;
6662#endif
6663}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006664
6665#define PYTYPE_READY(type) \
6666 if (PyType_Ready(&type)) \
6667 return -1;
6668
6669 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006670init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006671{
6672 PYTYPE_READY(IterType);
6673 PYTYPE_READY(BufferType);
6674 PYTYPE_READY(RangeType);
6675 PYTYPE_READY(WindowType);
6676 PYTYPE_READY(TabPageType);
6677 PYTYPE_READY(BufMapType);
6678 PYTYPE_READY(WinListType);
6679 PYTYPE_READY(TabListType);
6680 PYTYPE_READY(CurrentType);
6681 PYTYPE_READY(DictionaryType);
6682 PYTYPE_READY(ListType);
6683 PYTYPE_READY(FunctionType);
6684 PYTYPE_READY(OptionsType);
6685 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006686#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006687 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006688#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006689 return 0;
6690}
6691
6692 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006693init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006694{
6695 PyObject *path;
6696 PyObject *path_hook;
6697 PyObject *path_hooks;
6698
6699 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6700 return -1;
6701
6702 if (!(path_hooks = PySys_GetObject("path_hooks")))
6703 {
6704 PyErr_Clear();
6705 path_hooks = PyList_New(1);
6706 PyList_SET_ITEM(path_hooks, 0, path_hook);
6707 if (PySys_SetObject("path_hooks", path_hooks))
6708 {
6709 Py_DECREF(path_hooks);
6710 return -1;
6711 }
6712 Py_DECREF(path_hooks);
6713 }
6714 else if (PyList_Check(path_hooks))
6715 {
6716 if (PyList_Append(path_hooks, path_hook))
6717 {
6718 Py_DECREF(path_hook);
6719 return -1;
6720 }
6721 Py_DECREF(path_hook);
6722 }
6723 else
6724 {
6725 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006726 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006727 "You should now do the following:\n"
6728 "- append vim.path_hook to sys.path_hooks\n"
6729 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6730 VimTryEnd(); /* Discard the error */
6731 Py_DECREF(path_hook);
6732 return 0;
6733 }
6734
6735 if (!(path = PySys_GetObject("path")))
6736 {
6737 PyErr_Clear();
6738 path = PyList_New(1);
6739 Py_INCREF(vim_special_path_object);
6740 PyList_SET_ITEM(path, 0, vim_special_path_object);
6741 if (PySys_SetObject("path", path))
6742 {
6743 Py_DECREF(path);
6744 return -1;
6745 }
6746 Py_DECREF(path);
6747 }
6748 else if (PyList_Check(path))
6749 {
6750 if (PyList_Append(path, vim_special_path_object))
6751 return -1;
6752 }
6753 else
6754 {
6755 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006756 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006757 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6758 VimTryEnd(); /* Discard the error */
6759 }
6760
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006761 return 0;
6762}
6763
6764static BufMapObject TheBufferMap =
6765{
6766 PyObject_HEAD_INIT(&BufMapType)
6767};
6768
6769static WinListObject TheWindowList =
6770{
6771 PyObject_HEAD_INIT(&WinListType)
6772 NULL
6773};
6774
6775static CurrentObject TheCurrent =
6776{
6777 PyObject_HEAD_INIT(&CurrentType)
6778};
6779
6780static TabListObject TheTabPageList =
6781{
6782 PyObject_HEAD_INIT(&TabListType)
6783};
6784
6785static struct numeric_constant {
6786 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006787 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006788} numeric_constants[] = {
6789 {"VAR_LOCKED", VAR_LOCKED},
6790 {"VAR_FIXED", VAR_FIXED},
6791 {"VAR_SCOPE", VAR_SCOPE},
6792 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6793};
6794
6795static struct object_constant {
6796 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006797 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006798} object_constants[] = {
6799 {"buffers", (PyObject *)(void *)&TheBufferMap},
6800 {"windows", (PyObject *)(void *)&TheWindowList},
6801 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6802 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006803
6804 {"Buffer", (PyObject *)&BufferType},
6805 {"Range", (PyObject *)&RangeType},
6806 {"Window", (PyObject *)&WindowType},
6807 {"TabPage", (PyObject *)&TabPageType},
6808 {"Dictionary", (PyObject *)&DictionaryType},
6809 {"List", (PyObject *)&ListType},
6810 {"Function", (PyObject *)&FunctionType},
6811 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006812#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006813 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006814#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006815};
6816
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006817#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006818 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006819 return -1;
6820
6821#define ADD_CHECKED_OBJECT(m, name, obj) \
6822 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006823 PyObject *valObject = obj; \
6824 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006825 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006826 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006827 }
6828
6829 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006830populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006831{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006832 int i;
6833 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006834 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006835 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006836#if PY_VERSION_HEX >= 0x030700f0
6837 PyObject *dict;
6838 PyObject *cls;
6839#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840
6841 for (i = 0; i < (int)(sizeof(numeric_constants)
6842 / sizeof(struct numeric_constant));
6843 ++i)
6844 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006845 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006846
6847 for (i = 0; i < (int)(sizeof(object_constants)
6848 / sizeof(struct object_constant));
6849 ++i)
6850 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006851 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006852
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006853 valObject = object_constants[i].valObject;
6854 Py_INCREF(valObject);
6855 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006856 }
6857
6858 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6859 return -1;
6860 ADD_OBJECT(m, "error", VimError);
6861
Bram Moolenaara9922d62013-05-30 13:01:18 +02006862 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6863 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006864 ADD_CHECKED_OBJECT(m, "options",
6865 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006866
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006867 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006868 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006869 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006870
Bram Moolenaar22081f42016-06-01 20:38:34 +02006871#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006872 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006873 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006874#else
6875 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6876 return -1;
6877#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006878 ADD_OBJECT(m, "_getcwd", py_getcwd)
6879
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006880 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006881 return -1;
6882 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006883 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006884 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006885 if (PyObject_SetAttrString(other_module, "chdir", attr))
6886 {
6887 Py_DECREF(attr);
6888 return -1;
6889 }
6890 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006892 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006893 {
6894 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006895 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006897 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6898 {
6899 Py_DECREF(attr);
6900 return -1;
6901 }
6902 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006903 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006904 else
6905 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006906
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006907 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6908 return -1;
6909
6910 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6911
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006912#if PY_VERSION_HEX >= 0x030700f0
6913 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6914 return -1;
6915
6916 dict = PyModule_GetDict(imp);
6917
6918 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6919 {
6920 Py_DECREF(imp);
6921 return -1;
6922 }
6923
6924 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6925 {
6926 Py_DECREF(imp);
6927 return -1;
6928 }
6929
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006930 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6931 {
6932 // find_module() is deprecated, this may stop working in some later
6933 // version.
6934 ADD_OBJECT(m, "_find_module", py_find_module);
6935 }
6936
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006937 Py_DECREF(imp);
6938
6939 ADD_OBJECT(m, "_find_spec", py_find_spec);
6940#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006941 if (!(imp = PyImport_ImportModule("imp")))
6942 return -1;
6943
6944 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6945 {
6946 Py_DECREF(imp);
6947 return -1;
6948 }
6949
6950 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6951 {
6952 Py_DECREF(py_find_module);
6953 Py_DECREF(imp);
6954 return -1;
6955 }
6956
6957 Py_DECREF(imp);
6958
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006959 ADD_OBJECT(m, "_find_module", py_find_module);
6960 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006961#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006962
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006963 return 0;
6964}