blob: a0c16637841e92e872f21051ea67395d0b3053e5 [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_find_module;
91static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020092#endif
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 }
762# ifdef FEAT_FLOAT
763 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 }
770# endif
771 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 Moolenaaraf003f62013-07-24 17:11:46 +02004395 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004397 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004398 /* Only adjust marks if we managed to switch to a window that
4399 * holds the buffer, otherwise line numbers will be invalid. */
4400 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 }
4402
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004403 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004404
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004405 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406 return FAIL;
4407
4408 if (len_change)
4409 *len_change = -1;
4410
4411 return OK;
4412 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004413 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004414 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004415 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416
4417 if (save == NULL)
4418 return FAIL;
4419
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004420 VimTryStart();
4421
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422 /* We do not need to free "save" if ml_replace() consumes it. */
4423 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004424 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425
4426 if (u_savesub((linenr_T)n) == FAIL)
4427 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004428 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 vim_free(save);
4430 }
4431 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4432 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004433 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434 vim_free(save);
4435 }
4436 else
4437 changed_bytes((linenr_T)n, 0);
4438
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004439 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440
4441 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004442 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 check_cursor_col();
4444
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004445 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446 return FAIL;
4447
4448 if (len_change)
4449 *len_change = 0;
4450
4451 return OK;
4452 }
4453 else
4454 {
4455 PyErr_BadArgument();
4456 return FAIL;
4457 }
4458}
4459
Bram Moolenaar19e60942011-06-19 00:27:51 +02004460/* Replace a range of lines in the specified buffer. The line numbers are in
4461 * Vim format (1-based). The range is from lo up to, but not including, hi.
4462 * The replacement lines are given as a Python list of string objects. The
4463 * list is checked for validity and correct format. Errors are returned as a
4464 * value of FAIL. The return value is OK on success.
4465 * If OK is returned and len_change is not NULL, *len_change
4466 * is set to the change in the buffer length.
4467 */
4468 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004469SetBufferLineList(
4470 buf_T *buf,
4471 PyInt lo,
4472 PyInt hi,
4473 PyObject *list,
4474 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004475{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004476 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004477 win_T *save_curwin = NULL;
4478 tabpage_T *save_curtab = NULL;
4479
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004480 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004481 * There are three cases:
4482 * 1. NULL, or None - this is a deletion.
4483 * 2. A list - this is a replacement.
4484 * 3. Anything else - this is an error.
4485 */
4486 if (list == Py_None || list == NULL)
4487 {
4488 PyInt i;
4489 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004490
4491 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004492 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004493 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004494
4495 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004496 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004497 else
4498 {
4499 for (i = 0; i < n; ++i)
4500 {
4501 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4502 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004503 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504 break;
4505 }
4506 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004507 if (buf == curbuf && (save_curwin != NULL
4508 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004509 /* Using an existing window for the buffer, adjust the cursor
4510 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004511 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004512 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004513 /* Only adjust marks if we managed to switch to a window that
4514 * holds the buffer, otherwise line numbers will be invalid. */
4515 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004516 }
4517
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004518 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004519
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004520 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004521 return FAIL;
4522
4523 if (len_change)
4524 *len_change = -n;
4525
4526 return OK;
4527 }
4528 else if (PyList_Check(list))
4529 {
4530 PyInt i;
4531 PyInt new_len = PyList_Size(list);
4532 PyInt old_len = hi - lo;
4533 PyInt extra = 0; /* lines added to text, can be negative */
4534 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004535
4536 if (new_len == 0) /* avoid allocating zero bytes */
4537 array = NULL;
4538 else
4539 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004540 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541 if (array == NULL)
4542 {
4543 PyErr_NoMemory();
4544 return FAIL;
4545 }
4546 }
4547
4548 for (i = 0; i < new_len; ++i)
4549 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004550 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004552 if (!(line = PyList_GetItem(list, i)) ||
4553 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004554 {
4555 while (i)
4556 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004557 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558 return FAIL;
4559 }
4560 }
4561
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004562 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004563 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004564
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004565 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004566 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004567
4568 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004569 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004570
4571 /* If the size of the range is reducing (ie, new_len < old_len) we
4572 * need to delete some old_len. We do this at the start, by
4573 * repeatedly deleting line "lo".
4574 */
4575 if (!PyErr_Occurred())
4576 {
4577 for (i = 0; i < old_len - new_len; ++i)
4578 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4579 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004580 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004581 break;
4582 }
4583 extra -= i;
4584 }
4585
4586 /* For as long as possible, replace the existing old_len with the
4587 * new old_len. This is a more efficient operation, as it requires
4588 * less memory allocation and freeing.
4589 */
4590 if (!PyErr_Occurred())
4591 {
4592 for (i = 0; i < old_len && i < new_len; ++i)
4593 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4594 == FAIL)
4595 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004596 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597 break;
4598 }
4599 }
4600 else
4601 i = 0;
4602
4603 /* Now we may need to insert the remaining new old_len. If we do, we
4604 * must free the strings as we finish with them (we can't pass the
4605 * responsibility to vim in this case).
4606 */
4607 if (!PyErr_Occurred())
4608 {
4609 while (i < new_len)
4610 {
4611 if (ml_append((linenr_T)(lo + i - 1),
4612 (char_u *)array[i], 0, FALSE) == FAIL)
4613 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004614 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004615 break;
4616 }
4617 vim_free(array[i]);
4618 ++i;
4619 ++extra;
4620 }
4621 }
4622
4623 /* Free any left-over old_len, as a result of an error */
4624 while (i < new_len)
4625 {
4626 vim_free(array[i]);
4627 ++i;
4628 }
4629
4630 /* Free the array of old_len. All of its contents have now
4631 * been dealt with (either freed, or the responsibility passed
4632 * to vim.
4633 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004634 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004635
4636 /* Adjust marks. Invalidate any which lie in the
4637 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004638 * Only adjust marks if we managed to switch to a window that holds
4639 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004640 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004641 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004642 (long)MAXLNUM, (long)extra);
4643 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4644
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004645 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004646 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4647
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004648 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004649 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004650
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004651 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004652 return FAIL;
4653
4654 if (len_change)
4655 *len_change = new_len - old_len;
4656
4657 return OK;
4658 }
4659 else
4660 {
4661 PyErr_BadArgument();
4662 return FAIL;
4663 }
4664}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004665
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004666/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004667 * The line number is in Vim format (1-based). The lines to be inserted are
4668 * given as a Python list of string objects or as a single string. The lines
4669 * to be added are checked for validity and correct format. Errors are
4670 * returned as a value of FAIL. The return value is OK on success.
4671 * If OK is returned and len_change is not NULL, *len_change
4672 * is set to the change in the buffer length.
4673 */
4674 static int
4675InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4676{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004677 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004678 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004679 tabpage_T *save_curtab = NULL;
4680
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004681 /* First of all, we check the type of the supplied Python object.
4682 * It must be a string or a list, or the call is in error.
4683 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004684 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004685 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004686 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004687
4688 if (str == NULL)
4689 return FAIL;
4690
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004692 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004693 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004695 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004696 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004698 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004699 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004700 /* Only adjust marks if we managed to switch to a window that
4701 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004702 appended_lines_mark((linenr_T)n, 1L);
4703
4704 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004705 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706 update_screen(VALID);
4707
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004708 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004709 return FAIL;
4710
4711 if (len_change)
4712 *len_change = 1;
4713
4714 return OK;
4715 }
4716 else if (PyList_Check(lines))
4717 {
4718 PyInt i;
4719 PyInt size = PyList_Size(lines);
4720 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004721
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004722 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723 if (array == NULL)
4724 {
4725 PyErr_NoMemory();
4726 return FAIL;
4727 }
4728
4729 for (i = 0; i < size; ++i)
4730 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004731 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004733 if (!(line = PyList_GetItem(lines, i)) ||
4734 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735 {
4736 while (i)
4737 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004738 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004739 return FAIL;
4740 }
4741 }
4742
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004743 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004744 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004745 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004746
4747 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004748 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 else
4750 {
4751 for (i = 0; i < size; ++i)
4752 {
4753 if (ml_append((linenr_T)(n + i),
4754 (char_u *)array[i], 0, FALSE) == FAIL)
4755 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004756 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004757
4758 /* Free the rest of the lines */
4759 while (i < size)
4760 vim_free(array[i++]);
4761
4762 break;
4763 }
4764 vim_free(array[i]);
4765 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004766 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004767 /* Only adjust marks if we managed to switch to a window that
4768 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004769 appended_lines_mark((linenr_T)n, (long)i);
4770 }
4771
4772 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004773 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004774 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004775 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004776
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004777 update_screen(VALID);
4778
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004779 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004780 return FAIL;
4781
4782 if (len_change)
4783 *len_change = size;
4784
4785 return OK;
4786 }
4787 else
4788 {
4789 PyErr_BadArgument();
4790 return FAIL;
4791 }
4792}
4793
4794/*
4795 * Common routines for buffers and line ranges
4796 * -------------------------------------------
4797 */
4798
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004799typedef struct
4800{
4801 PyObject_HEAD
4802 buf_T *buf;
4803} BufferObject;
4804
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004805 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004806CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004807{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004808 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004809 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004810 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004811 return -1;
4812 }
4813
4814 return 0;
4815}
4816
4817 static PyObject *
4818RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4819{
4820 if (CheckBuffer(self))
4821 return NULL;
4822
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004823 if (end == -1)
4824 end = self->buf->b_ml.ml_line_count;
4825
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004826 if (n < 0)
4827 n += end - start + 1;
4828
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004829 if (n < 0 || n > end - start)
4830 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004831 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004832 return NULL;
4833 }
4834
4835 return GetBufferLine(self->buf, n+start);
4836}
4837
4838 static PyObject *
4839RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4840{
4841 PyInt size;
4842
4843 if (CheckBuffer(self))
4844 return NULL;
4845
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004846 if (end == -1)
4847 end = self->buf->b_ml.ml_line_count;
4848
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004849 size = end - start + 1;
4850
4851 if (lo < 0)
4852 lo = 0;
4853 else if (lo > size)
4854 lo = size;
4855 if (hi < 0)
4856 hi = 0;
4857 if (hi < lo)
4858 hi = lo;
4859 else if (hi > size)
4860 hi = size;
4861
4862 return GetBufferLineList(self->buf, lo+start, hi+start);
4863}
4864
4865 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004866RBAsItem(
4867 BufferObject *self,
4868 PyInt n,
4869 PyObject *valObject,
4870 PyInt start,
4871 PyInt end,
4872 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004873{
4874 PyInt len_change;
4875
4876 if (CheckBuffer(self))
4877 return -1;
4878
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004879 if (end == -1)
4880 end = self->buf->b_ml.ml_line_count;
4881
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004882 if (n < 0)
4883 n += end - start + 1;
4884
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004885 if (n < 0 || n > end - start)
4886 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004887 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004888 return -1;
4889 }
4890
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004891 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004892 return -1;
4893
4894 if (new_end)
4895 *new_end = end + len_change;
4896
4897 return 0;
4898}
4899
Bram Moolenaar19e60942011-06-19 00:27:51 +02004900 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004901RBAsSlice(
4902 BufferObject *self,
4903 PyInt lo,
4904 PyInt hi,
4905 PyObject *valObject,
4906 PyInt start,
4907 PyInt end,
4908 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004909{
4910 PyInt size;
4911 PyInt len_change;
4912
4913 /* Self must be a valid buffer */
4914 if (CheckBuffer(self))
4915 return -1;
4916
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004917 if (end == -1)
4918 end = self->buf->b_ml.ml_line_count;
4919
Bram Moolenaar19e60942011-06-19 00:27:51 +02004920 /* Sort out the slice range */
4921 size = end - start + 1;
4922
4923 if (lo < 0)
4924 lo = 0;
4925 else if (lo > size)
4926 lo = size;
4927 if (hi < 0)
4928 hi = 0;
4929 if (hi < lo)
4930 hi = lo;
4931 else if (hi > size)
4932 hi = size;
4933
4934 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004935 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004936 return -1;
4937
4938 if (new_end)
4939 *new_end = end + len_change;
4940
4941 return 0;
4942}
4943
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004944
4945 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004946RBAppend(
4947 BufferObject *self,
4948 PyObject *args,
4949 PyInt start,
4950 PyInt end,
4951 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004952{
4953 PyObject *lines;
4954 PyInt len_change;
4955 PyInt max;
4956 PyInt n;
4957
4958 if (CheckBuffer(self))
4959 return NULL;
4960
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004961 if (end == -1)
4962 end = self->buf->b_ml.ml_line_count;
4963
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004964 max = n = end - start + 1;
4965
4966 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4967 return NULL;
4968
4969 if (n < 0 || n > max)
4970 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004971 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004972 return NULL;
4973 }
4974
4975 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4976 return NULL;
4977
4978 if (new_end)
4979 *new_end = end + len_change;
4980
4981 Py_INCREF(Py_None);
4982 return Py_None;
4983}
4984
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004985/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004986 */
4987
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004988static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004989static PySequenceMethods RangeAsSeq;
4990static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004991
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992typedef struct
4993{
4994 PyObject_HEAD
4995 BufferObject *buf;
4996 PyInt start;
4997 PyInt end;
4998} RangeObject;
4999
5000 static PyObject *
5001RangeNew(buf_T *buf, PyInt start, PyInt end)
5002{
5003 BufferObject *bufr;
5004 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005005 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005006 if (self == NULL)
5007 return NULL;
5008
5009 bufr = (BufferObject *)BufferNew(buf);
5010 if (bufr == NULL)
5011 {
5012 Py_DECREF(self);
5013 return NULL;
5014 }
5015 Py_INCREF(bufr);
5016
5017 self->buf = bufr;
5018 self->start = start;
5019 self->end = end;
5020
5021 return (PyObject *)(self);
5022}
5023
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005024 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005025RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005026{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005027 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005028 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005029 PyObject_GC_Del((void *)(self));
5030}
5031
5032 static int
5033RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5034{
5035 Py_VISIT(((PyObject *)(self->buf)));
5036 return 0;
5037}
5038
5039 static int
5040RangeClear(RangeObject *self)
5041{
5042 Py_CLEAR(self->buf);
5043 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005044}
5045
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005046 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005047RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005048{
5049 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005050 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005051 return -1; /* ??? */
5052
Bram Moolenaard6e39182013-05-21 18:30:34 +02005053 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005054}
5055
5056 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005057RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005058{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005059 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060}
5061
5062 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005066}
5067
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005068static char *RangeAttrs[] = {
5069 "start", "end",
5070 NULL
5071};
5072
5073 static PyObject *
5074RangeDir(PyObject *self)
5075{
5076 return ObjectDir(self, RangeAttrs);
5077}
5078
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005079 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005080RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005081{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005082 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005083}
5084
5085 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005089 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5090 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091 else
5092 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094
5095 if (name == NULL)
5096 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005098 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005099 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100 }
5101}
5102
5103static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005104 /* name, function, calling, documentation */
5105 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005106 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5107 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005108};
5109
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005110static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005111static PySequenceMethods BufferAsSeq;
5112static PyMappingMethods BufferAsMapping;
5113
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005114 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005115BufferNew(buf_T *buf)
5116{
5117 /* We need to handle deletion of buffers underneath us.
5118 * If we add a "b_python*_ref" field to the buf_T structure,
5119 * then we can get at it in buf_freeall() in vim. We then
5120 * need to create only ONE Python object per buffer - if
5121 * we try to create a second, just INCREF the existing one
5122 * and return it. The (single) Python object referring to
5123 * the buffer is stored in "b_python*_ref".
5124 * Question: what to do on a buf_freeall(). We'll probably
5125 * have to either delete the Python object (DECREF it to
5126 * zero - a bad idea, as it leaves dangling refs!) or
5127 * set the buf_T * value to an invalid value (-1?), which
5128 * means we need checks in all access functions... Bah.
5129 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005130 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005131 * b_python_ref and b_python3_ref fields respectively.
5132 */
5133
5134 BufferObject *self;
5135
5136 if (BUF_PYTHON_REF(buf) != NULL)
5137 {
5138 self = BUF_PYTHON_REF(buf);
5139 Py_INCREF(self);
5140 }
5141 else
5142 {
5143 self = PyObject_NEW(BufferObject, &BufferType);
5144 if (self == NULL)
5145 return NULL;
5146 self->buf = buf;
5147 BUF_PYTHON_REF(buf) = self;
5148 }
5149
5150 return (PyObject *)(self);
5151}
5152
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005153 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005154BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005155{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005156 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5157 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005158
5159 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005160}
5161
Bram Moolenaar971db462013-05-12 18:44:48 +02005162 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005163BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005164{
5165 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005166 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005167 return -1; /* ??? */
5168
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005170}
5171
5172 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005173BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005174{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005176}
5177
5178 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005179BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005180{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005181 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005182}
5183
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005184static char *BufferAttrs[] = {
5185 "name", "number", "vars", "options", "valid",
5186 NULL
5187};
5188
5189 static PyObject *
5190BufferDir(PyObject *self)
5191{
5192 return ObjectDir(self, BufferAttrs);
5193}
5194
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005195 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005196BufferAttrValid(BufferObject *self, char *name)
5197{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005198 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005199
5200 if (strcmp(name, "valid") != 0)
5201 return NULL;
5202
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005203 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5204 Py_INCREF(ret);
5205 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005206}
5207
5208 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005209BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005210{
5211 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005212 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005213 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005214 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005216 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005217 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005218 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005219 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5220 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005221 else if (strcmp(name, "__members__") == 0)
5222 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005223 else
5224 return NULL;
5225}
5226
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005227 static int
5228BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5229{
5230 if (CheckBuffer(self))
5231 return -1;
5232
5233 if (strcmp(name, "name") == 0)
5234 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005235 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005236 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005237 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005238 PyObject *todecref;
5239
5240 if (!(val = StringToChars(valObject, &todecref)))
5241 return -1;
5242
5243 VimTryStart();
5244 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5245 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005246 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005247 aucmd_restbuf(&aco);
5248 Py_XDECREF(todecref);
5249 if (VimTryEnd())
5250 return -1;
5251
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005252 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005253 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005254 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005255 return -1;
5256 }
5257 return 0;
5258 }
5259 else
5260 {
5261 PyErr_SetString(PyExc_AttributeError, name);
5262 return -1;
5263 }
5264}
5265
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005266 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005267BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005268{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005269 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005270}
5271
5272 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005273BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005274{
5275 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005276 char_u *pmark;
5277 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005278 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005279 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005280
Bram Moolenaard6e39182013-05-21 18:30:34 +02005281 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005282 return NULL;
5283
Bram Moolenaar389a1792013-06-23 13:00:44 +02005284 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005285 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005286
Bram Moolenaar389a1792013-06-23 13:00:44 +02005287 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005288 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005289 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005290 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005291 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005292 return NULL;
5293 }
5294
5295 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005296
5297 Py_XDECREF(todecref);
5298
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005299 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005300 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005301 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005302 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005303 if (VimTryEnd())
5304 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005305
5306 if (posp == NULL)
5307 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005308 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005309 return NULL;
5310 }
5311
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005312 if (posp->lnum <= 0)
5313 {
5314 /* Or raise an error? */
5315 Py_INCREF(Py_None);
5316 return Py_None;
5317 }
5318
5319 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5320}
5321
5322 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005323BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005324{
5325 PyInt start;
5326 PyInt end;
5327
Bram Moolenaard6e39182013-05-21 18:30:34 +02005328 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005329 return NULL;
5330
5331 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5332 return NULL;
5333
Bram Moolenaard6e39182013-05-21 18:30:34 +02005334 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005335}
5336
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005337 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005338BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005339{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005340 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005341 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005342 else
5343 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005344 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005345
5346 if (name == NULL)
5347 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005348
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005349 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005350 }
5351}
5352
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005353static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005354 /* name, function, calling, documentation */
5355 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005356 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005357 {"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 +02005358 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5359 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005360};
5361
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005362/*
5363 * Buffer list object - Implementation
5364 */
5365
5366static PyTypeObject BufMapType;
5367
5368typedef struct
5369{
5370 PyObject_HEAD
5371} BufMapObject;
5372
5373 static PyInt
5374BufMapLength(PyObject *self UNUSED)
5375{
5376 buf_T *b = firstbuf;
5377 PyInt n = 0;
5378
5379 while (b)
5380 {
5381 ++n;
5382 b = b->b_next;
5383 }
5384
5385 return n;
5386}
5387
5388 static PyObject *
5389BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5390{
5391 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005392 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005393
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005394 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005395 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005396
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005397 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005398
5399 if (b)
5400 return BufferNew(b);
5401 else
5402 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005403 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005404 return NULL;
5405 }
5406}
5407
5408 static void
5409BufMapIterDestruct(PyObject *buffer)
5410{
5411 /* Iteration was stopped before all buffers were processed */
5412 if (buffer)
5413 {
5414 Py_DECREF(buffer);
5415 }
5416}
5417
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005418 static int
5419BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5420{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005421 if (buffer)
5422 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005423 return 0;
5424}
5425
5426 static int
5427BufMapIterClear(PyObject **buffer)
5428{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005429 if (*buffer)
5430 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005431 return 0;
5432}
5433
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005434 static PyObject *
5435BufMapIterNext(PyObject **buffer)
5436{
5437 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005438 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005439
5440 if (!*buffer)
5441 return NULL;
5442
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005443 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005444
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005445 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005446 {
5447 *buffer = NULL;
5448 return NULL;
5449 }
5450
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005451 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005452 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005453 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005454 return NULL;
5455 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005456 /* Do not increment reference: we no longer hold it (decref), but whoever
5457 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005458 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005459}
5460
5461 static PyObject *
5462BufMapIter(PyObject *self UNUSED)
5463{
5464 PyObject *buffer;
5465
5466 buffer = BufferNew(firstbuf);
5467 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005468 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5469 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005470}
5471
5472static PyMappingMethods BufMapAsMapping = {
5473 (lenfunc) BufMapLength,
5474 (binaryfunc) BufMapItem,
5475 (objobjargproc) 0,
5476};
5477
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005478/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005479 */
5480
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005481static char *CurrentAttrs[] = {
5482 "buffer", "window", "line", "range", "tabpage",
5483 NULL
5484};
5485
5486 static PyObject *
5487CurrentDir(PyObject *self)
5488{
5489 return ObjectDir(self, CurrentAttrs);
5490}
5491
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005492 static PyObject *
5493CurrentGetattr(PyObject *self UNUSED, char *name)
5494{
5495 if (strcmp(name, "buffer") == 0)
5496 return (PyObject *)BufferNew(curbuf);
5497 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005498 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005499 else if (strcmp(name, "tabpage") == 0)
5500 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005501 else if (strcmp(name, "line") == 0)
5502 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5503 else if (strcmp(name, "range") == 0)
5504 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005505 else if (strcmp(name, "__members__") == 0)
5506 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005507 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005508#if PY_MAJOR_VERSION < 3
5509 return Py_FindMethod(WindowMethods, self, name);
5510#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005511 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005512#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005513}
5514
5515 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005516CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005517{
5518 if (strcmp(name, "line") == 0)
5519 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005520 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5521 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005522 return -1;
5523
5524 return 0;
5525 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005526 else if (strcmp(name, "buffer") == 0)
5527 {
5528 int count;
5529
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005530 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005531 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005532 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005533 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005534 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005535 return -1;
5536 }
5537
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005538 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005539 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005540 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005541
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005542 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005543 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5544 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005545 if (VimTryEnd())
5546 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005547 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005548 return -1;
5549 }
5550
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005551 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005552 }
5553 else if (strcmp(name, "window") == 0)
5554 {
5555 int count;
5556
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005557 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005558 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005559 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005560 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005561 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005562 return -1;
5563 }
5564
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005565 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005566 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005567 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005568
5569 if (!count)
5570 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005571 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005572 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005573 return -1;
5574 }
5575
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005576 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 win_goto(((WindowObject *)(valObject))->win);
5578 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005579 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005580 if (VimTryEnd())
5581 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005582 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005583 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005584 return -1;
5585 }
5586
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005587 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005588 }
5589 else if (strcmp(name, "tabpage") == 0)
5590 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005591 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005592 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005593 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005594 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005595 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005596 return -1;
5597 }
5598
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005599 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005600 return -1;
5601
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005602 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005603 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5604 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005605 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005606 if (VimTryEnd())
5607 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005608 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005609 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005610 return -1;
5611 }
5612
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005613 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005614 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005615 else
5616 {
5617 PyErr_SetString(PyExc_AttributeError, name);
5618 return -1;
5619 }
5620}
5621
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005622static struct PyMethodDef CurrentMethods[] = {
5623 /* name, function, calling, documentation */
5624 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5625 { NULL, NULL, 0, NULL}
5626};
5627
Bram Moolenaardb913952012-06-29 12:54:53 +02005628 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005629init_range_cmd(exarg_T *eap)
5630{
5631 RangeStart = eap->line1;
5632 RangeEnd = eap->line2;
5633}
5634
5635 static void
5636init_range_eval(typval_T *rettv UNUSED)
5637{
5638 RangeStart = (PyInt) curwin->w_cursor.lnum;
5639 RangeEnd = RangeStart;
5640}
5641
5642 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005643run_cmd(const char *cmd, void *arg UNUSED
5644#ifdef PY_CAN_RECURSE
5645 , PyGILState_STATE *pygilstate UNUSED
5646#endif
5647 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005648{
Bram Moolenaar41009372013-07-01 22:03:04 +02005649 PyObject *run_ret;
5650 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5651 if (run_ret != NULL)
5652 {
5653 Py_DECREF(run_ret);
5654 }
5655 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005657 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005658 PyErr_Clear();
5659 }
5660 else
5661 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005662}
5663
5664static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5665static int code_hdr_len = 30;
5666
5667 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005668run_do(const char *cmd, void *arg UNUSED
5669#ifdef PY_CAN_RECURSE
5670 , PyGILState_STATE *pygilstate
5671#endif
5672 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005673{
5674 PyInt lnum;
5675 size_t len;
5676 char *code;
5677 int status;
5678 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005679 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005680 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005681
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005682 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005683 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005684 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005685 return;
5686 }
5687
5688 len = code_hdr_len + STRLEN(cmd);
5689 code = PyMem_New(char, len + 1);
5690 memcpy(code, code_hdr, code_hdr_len);
5691 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005692 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5693 status = -1;
5694 if (run_ret != NULL)
5695 {
5696 status = 0;
5697 Py_DECREF(run_ret);
5698 }
5699 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5700 {
5701 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005702 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005703 PyErr_Clear();
5704 return;
5705 }
5706 else
5707 PyErr_PrintEx(1);
5708
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005709 PyMem_Free(code);
5710
5711 if (status)
5712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005713 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005714 return;
5715 }
5716
5717 status = 0;
5718 pymain = PyImport_AddModule("__main__");
5719 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005720#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005721 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005722#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005723
5724 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5725 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005726 PyObject *line;
5727 PyObject *linenr;
5728 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005729
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005730#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005731 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005732#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005733 /* Check the line number, the command my have deleted lines. */
5734 if (lnum > curbuf->b_ml.ml_line_count
5735 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005736 goto err;
5737 if (!(linenr = PyInt_FromLong((long) lnum)))
5738 {
5739 Py_DECREF(line);
5740 goto err;
5741 }
5742 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5743 Py_DECREF(line);
5744 Py_DECREF(linenr);
5745 if (!ret)
5746 goto err;
5747
Bram Moolenaara58883b2017-01-29 21:31:09 +01005748 /* Check that the command didn't switch to another buffer. */
5749 if (curbuf != was_curbuf)
5750 {
5751 Py_XDECREF(ret);
5752 goto err;
5753 }
5754
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005755 if (ret != Py_None)
5756 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005757 {
5758 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005759 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005760 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005761
5762 Py_XDECREF(ret);
5763 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005764#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005765 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005766#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005767 }
5768 goto out;
5769err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005770#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005772#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005773 PyErr_PrintEx(0);
5774 PythonIO_Flush();
5775 status = 1;
5776out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005777#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005778 if (!status)
5779 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005780#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781 Py_DECREF(pyfunc);
5782 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5783 if (status)
5784 return;
5785 check_cursor();
5786 update_curbuf(NOT_VALID);
5787}
5788
5789 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005790run_eval(const char *cmd, typval_T *rettv
5791#ifdef PY_CAN_RECURSE
5792 , PyGILState_STATE *pygilstate UNUSED
5793#endif
5794 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005795{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005796 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005797
Bram Moolenaar41009372013-07-01 22:03:04 +02005798 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005799 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005800 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005801 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005803 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005804 PyErr_Clear();
5805 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005806 else
5807 {
5808 if (PyErr_Occurred() && !msg_silent)
5809 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005810 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005811 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005812 }
5813 else
5814 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005815 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005816 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005817 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005818 }
5819 PyErr_Clear();
5820}
5821
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005822 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005823set_ref_in_py(const int copyID)
5824{
5825 pylinkedlist_T *cur;
5826 dict_T *dd;
5827 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005828 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005829 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005830 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005831
5832 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005833 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005834 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005835 {
5836 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5837 if (dd->dv_copyID != copyID)
5838 {
5839 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005840 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005841 }
5842 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005843 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005844
5845 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005847 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005848 {
5849 ll = ((ListObject *) (cur->pll_obj))->list;
5850 if (ll->lv_copyID != copyID)
5851 {
5852 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005853 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005854 }
5855 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 }
5857
Bram Moolenaar8110a092016-04-14 15:56:09 +02005858 if (lastfunc != NULL)
5859 {
5860 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5861 {
5862 func = (FunctionObject *) cur->pll_obj;
5863 if (func->self != NULL && func->self->dv_copyID != copyID)
5864 {
5865 func->self->dv_copyID = copyID;
5866 abort = abort || set_ref_in_ht(
5867 &func->self->dv_hashtab, copyID, NULL);
5868 }
5869 if (func->argc)
5870 for (i = 0; !abort && i < func->argc; ++i)
5871 abort = abort
5872 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5873 }
5874 }
5875
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005876 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005877}
5878
5879 static int
5880set_string_copy(char_u *str, typval_T *tv)
5881{
5882 tv->vval.v_string = vim_strsave(str);
5883 if (tv->vval.v_string == NULL)
5884 {
5885 PyErr_NoMemory();
5886 return -1;
5887 }
5888 return 0;
5889}
5890
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005891 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005892pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005893{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005894 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005895 char_u *key;
5896 dictitem_T *di;
5897 PyObject *keyObject;
5898 PyObject *valObject;
5899 Py_ssize_t iter = 0;
5900
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005901 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005902 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005903
5904 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005905 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005906
5907 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5908 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005909 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005910
Bram Moolenaara03e6312013-05-29 22:49:26 +02005911 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005912 {
5913 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005914 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005915 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005917 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005918 {
5919 dict_unref(dict);
5920 return -1;
5921 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005922
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005923 if (*key == NUL)
5924 {
5925 dict_unref(dict);
5926 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005927 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005928 return -1;
5929 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930
5931 di = dictitem_alloc(key);
5932
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005933 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934
5935 if (di == NULL)
5936 {
5937 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005938 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939 return -1;
5940 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005942 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005943 {
5944 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005945 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946 return -1;
5947 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005948
5949 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005950 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005951 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005952 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005954 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005955 return -1;
5956 }
5957 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005958
5959 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005960 return 0;
5961}
5962
5963 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005964pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005966 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005967 char_u *key;
5968 dictitem_T *di;
5969 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005970 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971 PyObject *keyObject;
5972 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005974 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976
5977 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005978 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005979
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005980 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981 {
5982 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005984 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005985
5986 if (!(iterator = PyObject_GetIter(list)))
5987 {
5988 dict_unref(dict);
5989 Py_DECREF(list);
5990 return -1;
5991 }
5992 Py_DECREF(list);
5993
5994 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005995 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005996 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005997
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005998 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005999 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006000 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006001 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006002 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006003 return -1;
6004 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006005
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006006 if (*key == NUL)
6007 {
6008 Py_DECREF(keyObject);
6009 Py_DECREF(iterator);
6010 Py_XDECREF(todecref);
6011 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006012 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006013 return -1;
6014 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006015
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006016 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006017 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006018 Py_DECREF(keyObject);
6019 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006020 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006021 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006022 return -1;
6023 }
6024
6025 di = dictitem_alloc(key);
6026
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006027 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006028 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006029
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006030 if (di == NULL)
6031 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006032 Py_DECREF(iterator);
6033 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006034 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006035 PyErr_NoMemory();
6036 return -1;
6037 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006038
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006039 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006040 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006041 Py_DECREF(iterator);
6042 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006043 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006044 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006045 return -1;
6046 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006047
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006048 Py_DECREF(valObject);
6049
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006050 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006051 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006052 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006053 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006054 dictitem_free(di);
6055 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006056 return -1;
6057 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006058 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006059 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 return 0;
6062}
6063
6064 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006065pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006066{
6067 list_T *l;
6068
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006069 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006070 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071
6072 tv->v_type = VAR_LIST;
6073 tv->vval.v_list = l;
6074
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006075 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006076 {
6077 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006078 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006080
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006081 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006082 return 0;
6083}
6084
Bram Moolenaardb913952012-06-29 12:54:53 +02006085typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6086
6087 static int
6088convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006089 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006090{
6091 PyObject *capsule;
6092 char hexBuf[sizeof(void *) * 2 + 3];
6093
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006094 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006095
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006096# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006097 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006098# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006099 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006100# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006101 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006102 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006103# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006104 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006105# else
6106 capsule = PyCObject_FromVoidPtr(tv, NULL);
6107# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006108 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6109 {
6110 Py_DECREF(capsule);
6111 tv->v_type = VAR_UNKNOWN;
6112 return -1;
6113 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006114
6115 Py_DECREF(capsule);
6116
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006117 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006118 {
6119 tv->v_type = VAR_UNKNOWN;
6120 return -1;
6121 }
6122 /* As we are not using copy_tv which increments reference count we must
6123 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006124 if (tv->v_type == VAR_DICT)
6125 ++tv->vval.v_dict->dv_refcount;
6126 else if (tv->v_type == VAR_LIST)
6127 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006128 }
6129 else
6130 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006131 typval_T *v;
6132
6133# ifdef PY_USE_CAPSULE
6134 v = PyCapsule_GetPointer(capsule, NULL);
6135# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006136 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006137# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006138 copy_tv(v, tv);
6139 }
6140 return 0;
6141}
6142
6143 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006144ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6145{
6146 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006147 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006148
6149 if (!(lookup_dict = PyDict_New()))
6150 return -1;
6151
6152 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6153 {
6154 tv->v_type = VAR_DICT;
6155 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6156 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006157 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006158 }
6159 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006160 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006161 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006162 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006163 else
6164 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006165 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006166 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006167 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006168 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006169 }
6170 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006171 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006172}
6173
6174 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006175ConvertFromPySequence(PyObject *obj, typval_T *tv)
6176{
6177 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006178 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006179
6180 if (!(lookup_dict = PyDict_New()))
6181 return -1;
6182
6183 if (PyType_IsSubtype(obj->ob_type, &ListType))
6184 {
6185 tv->v_type = VAR_LIST;
6186 tv->vval.v_list = (((ListObject *)(obj))->list);
6187 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006188 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006189 }
6190 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006191 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006192 else
6193 {
6194 PyErr_FORMAT(PyExc_TypeError,
6195 N_("unable to convert %s to vim list"),
6196 Py_TYPE_NAME(obj));
6197 ret = -1;
6198 }
6199 Py_DECREF(lookup_dict);
6200 return ret;
6201}
6202
6203 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006204ConvertFromPyObject(PyObject *obj, typval_T *tv)
6205{
6206 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006207 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006208
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006209 if (!(lookup_dict = PyDict_New()))
6210 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006211 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006213 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006214}
6215
6216 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006217_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006218{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006219 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006220 {
6221 tv->v_type = VAR_DICT;
6222 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6223 ++tv->vval.v_dict->dv_refcount;
6224 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006225 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006226 {
6227 tv->v_type = VAR_LIST;
6228 tv->vval.v_list = (((ListObject *)(obj))->list);
6229 ++tv->vval.v_list->lv_refcount;
6230 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006231 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006232 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006233 FunctionObject *func = (FunctionObject *) obj;
6234 if (func->self != NULL || func->argv != NULL)
6235 {
6236 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6237 set_partial(func, pt, TRUE);
6238 tv->vval.v_partial = pt;
6239 tv->v_type = VAR_PARTIAL;
6240 }
6241 else
6242 {
6243 if (set_string_copy(func->name, tv) == -1)
6244 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006245
Bram Moolenaar8110a092016-04-14 15:56:09 +02006246 tv->v_type = VAR_FUNC;
6247 }
6248 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006249 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006250 else if (PyBytes_Check(obj))
6251 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006252 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006253
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006254 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006255 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006256 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 return -1;
6258
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006259 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 return -1;
6261
6262 tv->v_type = VAR_STRING;
6263 }
6264 else if (PyUnicode_Check(obj))
6265 {
6266 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006267 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006268
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006269 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006270 if (bytes == NULL)
6271 return -1;
6272
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006273 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006274 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006275 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 return -1;
6277
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006278 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006279 {
6280 Py_XDECREF(bytes);
6281 return -1;
6282 }
6283 Py_XDECREF(bytes);
6284
6285 tv->v_type = VAR_STRING;
6286 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006287#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006288 else if (PyInt_Check(obj))
6289 {
6290 tv->v_type = VAR_NUMBER;
6291 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006292 if (PyErr_Occurred())
6293 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006294 }
6295#endif
6296 else if (PyLong_Check(obj))
6297 {
6298 tv->v_type = VAR_NUMBER;
6299 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006300 if (PyErr_Occurred())
6301 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006302 }
6303 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006304 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006305#ifdef FEAT_FLOAT
6306 else if (PyFloat_Check(obj))
6307 {
6308 tv->v_type = VAR_FLOAT;
6309 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6310 }
6311#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006312 else if (PyObject_HasAttrString(obj, "keys"))
6313 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006314 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006315 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006316 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006317 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006318 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006319 else if (PyNumber_Check(obj))
6320 {
6321 PyObject *num;
6322
6323 if (!(num = PyNumber_Long(obj)))
6324 return -1;
6325
6326 tv->v_type = VAR_NUMBER;
6327 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6328
6329 Py_DECREF(num);
6330 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006331 else if (obj == Py_None)
6332 {
6333 tv->v_type = VAR_SPECIAL;
6334 tv->vval.v_number = VVAL_NONE;
6335 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006336 else
6337 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006338 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006339 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006340 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006341 return -1;
6342 }
6343 return 0;
6344}
6345
6346 static PyObject *
6347ConvertToPyObject(typval_T *tv)
6348{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006349 typval_T *argv;
6350 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006351 if (tv == NULL)
6352 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006353 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006354 return NULL;
6355 }
6356 switch (tv->v_type)
6357 {
6358 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006359 return PyBytes_FromString(tv->vval.v_string == NULL
6360 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006361 case VAR_NUMBER:
6362 return PyLong_FromLong((long) tv->vval.v_number);
6363#ifdef FEAT_FLOAT
6364 case VAR_FLOAT:
6365 return PyFloat_FromDouble((double) tv->vval.v_float);
6366#endif
6367 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006368 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006369 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006370 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006371 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006372 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006373 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006374 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006375 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006376 if (tv->vval.v_partial->pt_argc)
6377 {
6378 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6379 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6380 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6381 }
6382 else
6383 argv = NULL;
6384 if (tv->vval.v_partial->pt_dict != NULL)
6385 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006386 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006387 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006388 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006389 tv->vval.v_partial->pt_dict,
6390 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006391 case VAR_BLOB:
6392 return PyBytes_FromStringAndSize(
6393 (char*) tv->vval.v_blob->bv_ga.ga_data,
6394 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006396 case VAR_CHANNEL:
6397 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 Py_INCREF(Py_None);
6399 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006400 case VAR_SPECIAL:
6401 switch (tv->vval.v_number)
6402 {
6403 case VVAL_FALSE: return AlwaysFalse(NULL);
6404 case VVAL_TRUE: return AlwaysTrue(NULL);
6405 case VVAL_NONE:
6406 case VVAL_NULL: return AlwaysNone(NULL);
6407 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006408 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006409 return NULL;
6410 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006411 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006412}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006413
6414typedef struct
6415{
6416 PyObject_HEAD
6417} CurrentObject;
6418static PyTypeObject CurrentType;
6419
6420 static void
6421init_structs(void)
6422{
6423 vim_memset(&OutputType, 0, sizeof(OutputType));
6424 OutputType.tp_name = "vim.message";
6425 OutputType.tp_basicsize = sizeof(OutputObject);
6426 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6427 OutputType.tp_doc = "vim message object";
6428 OutputType.tp_methods = OutputMethods;
6429#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006430 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6431 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006432 OutputType.tp_alloc = call_PyType_GenericAlloc;
6433 OutputType.tp_new = call_PyType_GenericNew;
6434 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006435 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006436#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006437 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6438 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006439 // Disabled, because this causes a crash in test86
6440 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006441#endif
6442
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006443 vim_memset(&IterType, 0, sizeof(IterType));
6444 IterType.tp_name = "vim.iter";
6445 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006446 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006447 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006448 IterType.tp_iter = (getiterfunc)IterIter;
6449 IterType.tp_iternext = (iternextfunc)IterNext;
6450 IterType.tp_dealloc = (destructor)IterDestructor;
6451 IterType.tp_traverse = (traverseproc)IterTraverse;
6452 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006453
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006454 vim_memset(&BufferType, 0, sizeof(BufferType));
6455 BufferType.tp_name = "vim.buffer";
6456 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006457 BufferType.tp_dealloc = (destructor)BufferDestructor;
6458 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006459 BufferType.tp_as_sequence = &BufferAsSeq;
6460 BufferType.tp_as_mapping = &BufferAsMapping;
6461 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6462 BufferType.tp_doc = "vim buffer object";
6463 BufferType.tp_methods = BufferMethods;
6464#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006465 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006466 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467 BufferType.tp_alloc = call_PyType_GenericAlloc;
6468 BufferType.tp_new = call_PyType_GenericNew;
6469 BufferType.tp_free = call_PyObject_Free;
6470#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006471 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006472 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006473#endif
6474
6475 vim_memset(&WindowType, 0, sizeof(WindowType));
6476 WindowType.tp_name = "vim.window";
6477 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006478 WindowType.tp_dealloc = (destructor)WindowDestructor;
6479 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006480 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006481 WindowType.tp_doc = "vim Window object";
6482 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006483 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6484 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006485#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6487 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488 WindowType.tp_alloc = call_PyType_GenericAlloc;
6489 WindowType.tp_new = call_PyType_GenericNew;
6490 WindowType.tp_free = call_PyObject_Free;
6491#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006492 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6493 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006494#endif
6495
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006496 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6497 TabPageType.tp_name = "vim.tabpage";
6498 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6500 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006501 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6502 TabPageType.tp_doc = "vim tab page object";
6503 TabPageType.tp_methods = TabPageMethods;
6504#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006506 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6507 TabPageType.tp_new = call_PyType_GenericNew;
6508 TabPageType.tp_free = call_PyObject_Free;
6509#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006510 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006511#endif
6512
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006513 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6514 BufMapType.tp_name = "vim.bufferlist";
6515 BufMapType.tp_basicsize = sizeof(BufMapObject);
6516 BufMapType.tp_as_mapping = &BufMapAsMapping;
6517 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006518 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006519 BufferType.tp_doc = "vim buffer list";
6520
6521 vim_memset(&WinListType, 0, sizeof(WinListType));
6522 WinListType.tp_name = "vim.windowlist";
6523 WinListType.tp_basicsize = sizeof(WinListType);
6524 WinListType.tp_as_sequence = &WinListAsSeq;
6525 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6526 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006527 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006528
6529 vim_memset(&TabListType, 0, sizeof(TabListType));
6530 TabListType.tp_name = "vim.tabpagelist";
6531 TabListType.tp_basicsize = sizeof(TabListType);
6532 TabListType.tp_as_sequence = &TabListAsSeq;
6533 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6534 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006535
6536 vim_memset(&RangeType, 0, sizeof(RangeType));
6537 RangeType.tp_name = "vim.range";
6538 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006539 RangeType.tp_dealloc = (destructor)RangeDestructor;
6540 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006541 RangeType.tp_as_sequence = &RangeAsSeq;
6542 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006543 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006544 RangeType.tp_doc = "vim Range object";
6545 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006546 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6547 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006548#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006549 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006550 RangeType.tp_alloc = call_PyType_GenericAlloc;
6551 RangeType.tp_new = call_PyType_GenericNew;
6552 RangeType.tp_free = call_PyObject_Free;
6553#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006554 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006555#endif
6556
6557 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6558 CurrentType.tp_name = "vim.currentdata";
6559 CurrentType.tp_basicsize = sizeof(CurrentObject);
6560 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6561 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006562 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006563#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006564 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6565 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006566#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006567 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6568 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006569#endif
6570
6571 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6572 DictionaryType.tp_name = "vim.dictionary";
6573 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006574 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006575 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006576 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006577 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006578 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6579 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006580 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6581 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6582 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006583#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006584 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6585 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006586#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006587 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6588 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006589#endif
6590
6591 vim_memset(&ListType, 0, sizeof(ListType));
6592 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006593 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006594 ListType.tp_basicsize = sizeof(ListObject);
6595 ListType.tp_as_sequence = &ListAsSeq;
6596 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006597 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006598 ListType.tp_doc = "list pushing modifications to vim structure";
6599 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006600 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006601 ListType.tp_new = (newfunc)ListConstructor;
6602 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006603#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006604 ListType.tp_getattro = (getattrofunc)ListGetattro;
6605 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006606#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006607 ListType.tp_getattr = (getattrfunc)ListGetattr;
6608 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006609#endif
6610
6611 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006612 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006613 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006614 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6615 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006616 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006617 FunctionType.tp_doc = "object that calls vim function";
6618 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006619 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006620 FunctionType.tp_new = (newfunc)FunctionConstructor;
6621 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006622#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006623 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006624#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006625 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006626#endif
6627
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006628 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6629 OptionsType.tp_name = "vim.options";
6630 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006631 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006632 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006633 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006634 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006635 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006636 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6637 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6638 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006639
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006640#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006641 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6642 LoaderType.tp_name = "vim.Loader";
6643 LoaderType.tp_basicsize = sizeof(LoaderObject);
6644 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6645 LoaderType.tp_doc = "vim message object";
6646 LoaderType.tp_methods = LoaderMethods;
6647 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006648#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006649
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006650#if PY_MAJOR_VERSION >= 3
6651 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6652 vimmodule.m_name = "vim";
6653 vimmodule.m_doc = "Vim Python interface\n";
6654 vimmodule.m_size = -1;
6655 vimmodule.m_methods = VimMethods;
6656#endif
6657}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006658
6659#define PYTYPE_READY(type) \
6660 if (PyType_Ready(&type)) \
6661 return -1;
6662
6663 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006664init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006665{
6666 PYTYPE_READY(IterType);
6667 PYTYPE_READY(BufferType);
6668 PYTYPE_READY(RangeType);
6669 PYTYPE_READY(WindowType);
6670 PYTYPE_READY(TabPageType);
6671 PYTYPE_READY(BufMapType);
6672 PYTYPE_READY(WinListType);
6673 PYTYPE_READY(TabListType);
6674 PYTYPE_READY(CurrentType);
6675 PYTYPE_READY(DictionaryType);
6676 PYTYPE_READY(ListType);
6677 PYTYPE_READY(FunctionType);
6678 PYTYPE_READY(OptionsType);
6679 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006680#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006681 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006682#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006683 return 0;
6684}
6685
6686 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006687init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006688{
6689 PyObject *path;
6690 PyObject *path_hook;
6691 PyObject *path_hooks;
6692
6693 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6694 return -1;
6695
6696 if (!(path_hooks = PySys_GetObject("path_hooks")))
6697 {
6698 PyErr_Clear();
6699 path_hooks = PyList_New(1);
6700 PyList_SET_ITEM(path_hooks, 0, path_hook);
6701 if (PySys_SetObject("path_hooks", path_hooks))
6702 {
6703 Py_DECREF(path_hooks);
6704 return -1;
6705 }
6706 Py_DECREF(path_hooks);
6707 }
6708 else if (PyList_Check(path_hooks))
6709 {
6710 if (PyList_Append(path_hooks, path_hook))
6711 {
6712 Py_DECREF(path_hook);
6713 return -1;
6714 }
6715 Py_DECREF(path_hook);
6716 }
6717 else
6718 {
6719 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006720 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006721 "You should now do the following:\n"
6722 "- append vim.path_hook to sys.path_hooks\n"
6723 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6724 VimTryEnd(); /* Discard the error */
6725 Py_DECREF(path_hook);
6726 return 0;
6727 }
6728
6729 if (!(path = PySys_GetObject("path")))
6730 {
6731 PyErr_Clear();
6732 path = PyList_New(1);
6733 Py_INCREF(vim_special_path_object);
6734 PyList_SET_ITEM(path, 0, vim_special_path_object);
6735 if (PySys_SetObject("path", path))
6736 {
6737 Py_DECREF(path);
6738 return -1;
6739 }
6740 Py_DECREF(path);
6741 }
6742 else if (PyList_Check(path))
6743 {
6744 if (PyList_Append(path, vim_special_path_object))
6745 return -1;
6746 }
6747 else
6748 {
6749 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006750 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006751 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6752 VimTryEnd(); /* Discard the error */
6753 }
6754
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006755 return 0;
6756}
6757
6758static BufMapObject TheBufferMap =
6759{
6760 PyObject_HEAD_INIT(&BufMapType)
6761};
6762
6763static WinListObject TheWindowList =
6764{
6765 PyObject_HEAD_INIT(&WinListType)
6766 NULL
6767};
6768
6769static CurrentObject TheCurrent =
6770{
6771 PyObject_HEAD_INIT(&CurrentType)
6772};
6773
6774static TabListObject TheTabPageList =
6775{
6776 PyObject_HEAD_INIT(&TabListType)
6777};
6778
6779static struct numeric_constant {
6780 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006781 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006782} numeric_constants[] = {
6783 {"VAR_LOCKED", VAR_LOCKED},
6784 {"VAR_FIXED", VAR_FIXED},
6785 {"VAR_SCOPE", VAR_SCOPE},
6786 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6787};
6788
6789static struct object_constant {
6790 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006791 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006792} object_constants[] = {
6793 {"buffers", (PyObject *)(void *)&TheBufferMap},
6794 {"windows", (PyObject *)(void *)&TheWindowList},
6795 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6796 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006797
6798 {"Buffer", (PyObject *)&BufferType},
6799 {"Range", (PyObject *)&RangeType},
6800 {"Window", (PyObject *)&WindowType},
6801 {"TabPage", (PyObject *)&TabPageType},
6802 {"Dictionary", (PyObject *)&DictionaryType},
6803 {"List", (PyObject *)&ListType},
6804 {"Function", (PyObject *)&FunctionType},
6805 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006806#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006807 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006808#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006809};
6810
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006811#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006812 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006813 return -1;
6814
6815#define ADD_CHECKED_OBJECT(m, name, obj) \
6816 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006817 PyObject *valObject = obj; \
6818 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006819 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006820 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006821 }
6822
6823 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006824populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006825{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006826 int i;
6827 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006828 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006829 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006830#if PY_VERSION_HEX >= 0x030700f0
6831 PyObject *dict;
6832 PyObject *cls;
6833#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006834
6835 for (i = 0; i < (int)(sizeof(numeric_constants)
6836 / sizeof(struct numeric_constant));
6837 ++i)
6838 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006839 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840
6841 for (i = 0; i < (int)(sizeof(object_constants)
6842 / sizeof(struct object_constant));
6843 ++i)
6844 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006845 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006846
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006847 valObject = object_constants[i].valObject;
6848 Py_INCREF(valObject);
6849 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850 }
6851
6852 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6853 return -1;
6854 ADD_OBJECT(m, "error", VimError);
6855
Bram Moolenaara9922d62013-05-30 13:01:18 +02006856 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6857 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006858 ADD_CHECKED_OBJECT(m, "options",
6859 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006860
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006861 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006862 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006863 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006864
Bram Moolenaar22081f42016-06-01 20:38:34 +02006865#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006866 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006867 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006868#else
6869 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6870 return -1;
6871#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006872 ADD_OBJECT(m, "_getcwd", py_getcwd)
6873
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006874 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006875 return -1;
6876 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006877 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006878 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006879 if (PyObject_SetAttrString(other_module, "chdir", attr))
6880 {
6881 Py_DECREF(attr);
6882 return -1;
6883 }
6884 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006885
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006886 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006887 {
6888 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006889 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006890 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006891 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6892 {
6893 Py_DECREF(attr);
6894 return -1;
6895 }
6896 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006897 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006898 else
6899 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006900
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006901 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6902 return -1;
6903
6904 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6905
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006906#if PY_VERSION_HEX >= 0x030700f0
6907 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6908 return -1;
6909
6910 dict = PyModule_GetDict(imp);
6911
6912 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6913 {
6914 Py_DECREF(imp);
6915 return -1;
6916 }
6917
6918 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6919 {
6920 Py_DECREF(imp);
6921 return -1;
6922 }
6923
6924 Py_DECREF(imp);
6925
6926 ADD_OBJECT(m, "_find_spec", py_find_spec);
6927#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006928 if (!(imp = PyImport_ImportModule("imp")))
6929 return -1;
6930
6931 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6932 {
6933 Py_DECREF(imp);
6934 return -1;
6935 }
6936
6937 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6938 {
6939 Py_DECREF(py_find_module);
6940 Py_DECREF(imp);
6941 return -1;
6942 }
6943
6944 Py_DECREF(imp);
6945
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006946 ADD_OBJECT(m, "_find_module", py_find_module);
6947 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006948#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006949
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006950 return 0;
6951}