blob: af4b98dd06f50d87604f1c799f6479bdc62e6776 [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 Moolenaar41009372013-07-01 22:03:04 +020016static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
17
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020018#if PY_VERSION_HEX < 0x02050000
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010019typedef int Py_ssize_t; // Python 2.4 and earlier don't have this type.
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020020#endif
21
Bram Moolenaard518f952020-01-01 15:04:17 +010022// Use values that are known to work, others may make Vim crash.
23#define ENC_OPT (enc_utf8 ? "utf-8" : enc_dbcs ? "euc-jp" : (char *)p_enc)
Bram Moolenaard620aa92013-05-17 16:40:06 +020024#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020025
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020026static const char *vim_special_path = "_vim_path_";
27
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020028#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020029#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020030#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010031#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
32#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
33#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020034
35#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020039#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020040 N_("empty keys are not allowed"))
41#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
42#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
43#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
44#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
45#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
46#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020047#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020049#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020050 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020051 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020053#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
54#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020055#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020056
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020058typedef void (*runner)(const char *, void *
59#ifdef PY_CAN_RECURSE
60 , PyGILState_STATE *
61#endif
62 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064static int ConvertFromPyObject(PyObject *, typval_T *);
65static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020066static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020067static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020068static PyObject *WindowNew(win_T *, tabpage_T *);
69static PyObject *BufferNew (buf_T *);
70static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020071
72static PyInt RangeStart;
73static PyInt RangeEnd;
74
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020075static PyObject *globals;
76
Bram Moolenaarf4258302013-06-02 18:20:17 +020077static PyObject *py_chdir;
78static PyObject *py_fchdir;
79static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020080static PyObject *vim_module;
81static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020082
Bram Moolenaar79a494d2018-07-22 04:30:21 +020083#if PY_VERSION_HEX >= 0x030700f0
84static PyObject *py_find_spec;
85#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020086static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020087#endif
Bram Moolenaarb999ba22019-02-14 13:28:45 +010088static PyObject *py_find_module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +020089
90static PyObject *VimError;
91
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020092/*
93 * obtain a lock on the Vim data structures
94 */
95 static void
96Python_Lock_Vim(void)
97{
98}
99
100/*
101 * release a lock on the Vim data structures
102 */
103 static void
104Python_Release_Vim(void)
105{
106}
107
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200108/*
109 * The "todecref" argument holds a pointer to PyObject * that must be
110 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
111 * was needed to generate returned value is object.
112 *
113 * Use Py_XDECREF to decrement reference count.
114 */
115 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200116StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200117{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200118 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200119
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121 {
122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
124 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 return NULL;
126
127 *todecref = NULL;
128 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200129 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200130 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200131 PyObject *bytes;
132
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 return NULL;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
137 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200138 {
139 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200140 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200142
143 *todecref = bytes;
144 }
145 else
146 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200147#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200148 PyErr_FORMAT(PyExc_TypeError,
149 N_("expected str() or unicode() instance, but got %s"),
150 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected bytes() or str() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200156 return NULL;
157 }
158
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200159 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160}
161
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200162#define NUMBER_LONG 1
163#define NUMBER_INT 2
164#define NUMBER_NATURAL 4
165#define NUMBER_UNSIGNED 8
166
167 static int
168NumberToLong(PyObject *obj, long *result, int flags)
169{
170#if PY_MAJOR_VERSION < 3
171 if (PyInt_Check(obj))
172 {
173 *result = PyInt_AsLong(obj);
174 if (PyErr_Occurred())
175 return -1;
176 }
177 else
178#endif
179 if (PyLong_Check(obj))
180 {
181 *result = PyLong_AsLong(obj);
182 if (PyErr_Occurred())
183 return -1;
184 }
185 else if (PyNumber_Check(obj))
186 {
187 PyObject *num;
188
189 if (!(num = PyNumber_Long(obj)))
190 return -1;
191
192 *result = PyLong_AsLong(num);
193
194 Py_DECREF(num);
195
196 if (PyErr_Occurred())
197 return -1;
198 }
199 else
200 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200201#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200202 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200203 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200204 "coercing to long(), but got %s"),
205 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200206#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200208 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200209 "but got %s"),
210 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200211#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200212 return -1;
213 }
214
215 if (flags & NUMBER_INT)
216 {
217 if (*result > INT_MAX)
218 {
219 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200220 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200221 return -1;
222 }
223 else if (*result < INT_MIN)
224 {
225 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200226 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200227 return -1;
228 }
229 }
230
231 if (flags & NUMBER_NATURAL)
232 {
233 if (*result <= 0)
234 {
235 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100236 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200237 return -1;
238 }
239 }
240 else if (flags & NUMBER_UNSIGNED)
241 {
242 if (*result < 0)
243 {
244 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200245 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200246 return -1;
247 }
248 }
249
250 return 0;
251}
252
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200253 static int
254add_string(PyObject *list, char *s)
255{
256 PyObject *string;
257
258 if (!(string = PyString_FromString(s)))
259 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200260
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200261 if (PyList_Append(list, string))
262 {
263 Py_DECREF(string);
264 return -1;
265 }
266
267 Py_DECREF(string);
268 return 0;
269}
270
271 static PyObject *
272ObjectDir(PyObject *self, char **attributes)
273{
274 PyMethodDef *method;
275 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200276 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200277
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200278 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200279 return NULL;
280
281 if (self)
282 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200283 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200284 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200285 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200286 return NULL;
287 }
288
289 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200290 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200291 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200292 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200293 return NULL;
294 }
295
296#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200297 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200298 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200299 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200300 return NULL;
301 }
302#endif
303
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200304 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200305}
306
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100307// Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100309// Function to write a line, points to either msg() or emsg().
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200310typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311
312static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313
314typedef struct
315{
316 PyObject_HEAD
317 long softspace;
318 long error;
319} OutputObject;
320
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200321static char *OutputAttrs[] = {
322 "softspace",
323 NULL
324};
325
326 static PyObject *
327OutputDir(PyObject *self)
328{
329 return ObjectDir(self, OutputAttrs);
330}
331
Bram Moolenaar77045652012-09-21 13:46:06 +0200332 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200333OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200334{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200335 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200337 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200338 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200339 return -1;
340 }
341
342 if (strcmp(name, "softspace") == 0)
343 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200344 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200345 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200346 return 0;
347 }
348
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200349 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
351}
352
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100353// Buffer IO, we write one whole line at a time.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200354static garray_T io_ga = {0, 0, 1, 80, NULL};
355static writefn old_fn = NULL;
356
357 static void
358PythonIO_Flush(void)
359{
360 if (old_fn != NULL && io_ga.ga_len > 0)
361 {
362 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
363 old_fn((char_u *)io_ga.ga_data);
364 }
365 io_ga.ga_len = 0;
366}
367
368 static void
369writer(writefn fn, char_u *str, PyInt n)
370{
371 char_u *ptr;
372
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100373 // Flush when switching output function.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200374 if (fn != old_fn)
375 PythonIO_Flush();
376 old_fn = fn;
377
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200378 // Write each NL separated line. Text after the last NL is kept for
379 // writing later.
380 // For normal messages: Do not output when "got_int" was set. This avoids
381 // a loop gone crazy flooding the terminal with messages. Also for when
382 // "q" is pressed at the more-prompt.
383 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
384 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200399 // Put the remaining text into io_ga for later printing.
400 if (n > 0 && (fn == (writefn)emsg || !got_int)
401 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200402 {
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 Moolenaar7f3a2842019-05-18 15:02:25 +0200420 if (error)
421 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100422 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423 Python_Release_Vim();
424 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200425 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200426
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200427 return 0;
428}
429
430 static PyObject *
431OutputWrite(OutputObject *self, PyObject *string)
432{
433 if (write_output(self, string))
434 return NULL;
435
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200436 Py_INCREF(Py_None);
437 return Py_None;
438}
439
440 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200441OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 PyObject *iterator;
444 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200449 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200451 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200452 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200453 Py_DECREF(iterator);
454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 return NULL;
456 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200458 }
459
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200460 Py_DECREF(iterator);
461
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100462 // Iterator may have finished due to an exception
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200463 if (PyErr_Occurred())
464 return NULL;
465
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200466 Py_INCREF(Py_None);
467 return Py_None;
468}
469
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100471AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100472{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100473 // do nothing
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100474 Py_INCREF(Py_None);
475 return Py_None;
476}
477
Bram Moolenaard4247472015-11-02 13:28:59 +0100478 static PyObject *
479AlwaysFalse(PyObject *self UNUSED)
480{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100481 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100482 PyObject *ret = Py_False;
483 Py_INCREF(ret);
484 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100485}
486
487 static PyObject *
488AlwaysTrue(PyObject *self UNUSED)
489{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100490 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100491 PyObject *ret = Py_True;
492 Py_INCREF(ret);
493 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100494}
495
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200496/***************/
497
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200498static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100499 // name, function, calling, doc
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200500 {"write", (PyCFunction)OutputWrite, METH_O, ""},
501 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100502 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
503 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
504 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
505 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
506 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
507 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200508 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200509 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200510 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200511};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200512
513static OutputObject Output =
514{
515 PyObject_HEAD_INIT(&OutputType)
516 0,
517 0
518};
519
520static OutputObject Error =
521{
522 PyObject_HEAD_INIT(&OutputType)
523 0,
524 1
525};
526
527 static int
528PythonIO_Init_io(void)
529{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200530 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
531 return -1;
532 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
533 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200534
535 if (PyErr_Occurred())
536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100537 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200538 return -1;
539 }
540
541 return 0;
542}
543
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200544#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200545static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
546
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200547typedef struct
548{
549 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200550 char *fullname;
551 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200552} LoaderObject;
553static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200554
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200555 static void
556LoaderDestructor(LoaderObject *self)
557{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200558 vim_free(self->fullname);
559 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200560 DESTRUCTOR_FINISH(self);
561}
562
563 static PyObject *
564LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
565{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200566 char *fullname = self->fullname;
567 PyObject *result = self->result;
568 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200569
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200570 if (!fullname)
571 {
572 module = result ? result : Py_None;
573 Py_INCREF(module);
574 return module;
575 }
576
577 module = call_load_module(fullname, (int)STRLEN(fullname), result);
578
579 self->fullname = NULL;
580 self->result = module;
581
582 vim_free(fullname);
583 Py_DECREF(result);
584
585 if (!module)
586 {
587 if (PyErr_Occurred())
588 return NULL;
589
590 Py_INCREF(Py_None);
591 return Py_None;
592 }
593
594 Py_INCREF(module);
595 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200596}
597
598static struct PyMethodDef LoaderMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100599 // name, function, calling, doc
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200600 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
601 { NULL, NULL, 0, NULL}
602};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200603#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200604
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100605/*
606 * Check to see whether a Vim error has been reported, or a keyboard
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200607 * interrupt has been detected.
608 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 static void
610VimTryStart(void)
611{
612 ++trylevel;
613}
614
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200615 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200616VimTryEnd(void)
617{
618 --trylevel;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100619 // Without this it stops processing all subsequent Vim script commands and
620 // generates strange error messages if I e.g. try calling Test() in a cycle
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200621 did_emsg = FALSE;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100622 // Keyboard interrupt should be preferred over anything else
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200623 if (got_int)
624 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100625 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100626 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100627 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200628 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200629 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200630 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100631 else if (msg_list != NULL && *msg_list != NULL)
632 {
633 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100634 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100635
636 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
637
638 if (msg == NULL)
639 {
640 PyErr_NoMemory();
641 return -1;
642 }
643
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100644 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100645
646 free_global_msglist();
647
648 if (should_free)
649 vim_free(msg);
650
651 return -1;
652 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200653 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200654 return (PyErr_Occurred() ? -1 : 0);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100655 // Python exception is preferred over vim one; unlikely to occur though
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200656 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200657 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100658 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200659 return -1;
660 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100661 // Finally transform Vim script exception to python one
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200662 else
663 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200664 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200666 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200667 }
668}
669
670 static int
671VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200672{
673 if (got_int)
674 {
675 PyErr_SetNone(PyExc_KeyboardInterrupt);
676 return 1;
677 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200678 return 0;
679}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200680
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100681// Vim module - Implementation
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
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100727 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 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 Moolenaar9bf703d2019-11-30 19:44:38 +0100735 // Check if we run into a recursive loop. The item must be in lookup_dict
736 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 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
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100758 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200759 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200760 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100762#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 else if (our_tv->v_type == VAR_FLOAT)
764 {
765 char buf[NUMBUFLEN];
766
767 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200768 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100770#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 else if (our_tv->v_type == VAR_LIST)
772 {
773 list_T *list = our_tv->vval.v_list;
774 listitem_T *curr;
775
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 if (list == NULL)
777 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200779 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 return NULL;
781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200783 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 return NULL;
786 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200788 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
789 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200790 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200792 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200793 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 {
797 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200798 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200799 return NULL;
800 }
801 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200802 }
803 }
804 else if (our_tv->v_type == VAR_DICT)
805 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200806
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100807 hashtab_T *ht;
808 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200809 hashitem_T *hi;
810 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100811
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200812 if (our_tv->vval.v_dict == NULL)
813 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100814 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200815
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200816 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200817 return NULL;
818
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200819 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200820 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200821 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200822 return NULL;
823 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100825 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200826 for (hi = ht->ht_array; todo > 0; ++hi)
827 {
828 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200829 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200830 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200831
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200832 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200833 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200834 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200835 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200836 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200837 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200839 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200840 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200841 Py_DECREF(newObj);
842 return NULL;
843 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200844 }
845 }
846 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100847 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100848 {
849 if (our_tv->vval.v_number == VVAL_FALSE)
850 {
851 ret = Py_False;
852 Py_INCREF(ret);
853 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100854 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100855 {
856 ret = Py_True;
857 Py_INCREF(ret);
858 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100859 return ret;
860 }
861 else if (our_tv->v_type == VAR_SPECIAL)
862 {
863 Py_INCREF(Py_None);
864 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100865 return ret;
866 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100867 else if (our_tv->v_type == VAR_BLOB)
868 ret = PyBytes_FromStringAndSize(
869 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
870 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200871 else
872 {
873 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200874 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200875 }
876
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200877 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200879
880 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200881VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200882{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200883 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200884 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200885 PyObject *string;
886 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200887 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200888 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200889
Bram Moolenaar389a1792013-06-23 13:00:44 +0200890 if (!PyArg_ParseTuple(args, "O", &string))
891 return NULL;
892
893 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200894 return NULL;
895
896 Py_BEGIN_ALLOW_THREADS
897 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200898 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200899 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200900 Python_Release_Vim();
901 Py_END_ALLOW_THREADS
902
Bram Moolenaar389a1792013-06-23 13:00:44 +0200903 Py_XDECREF(todecref);
904
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200905 if (VimTryEnd())
906 return NULL;
907
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200908 if (our_tv == NULL)
909 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200910 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200911 return NULL;
912 }
913
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100914 // Convert the Vim type into a Python type. Create a dictionary that's
915 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200916 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200917 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200918 else
919 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200920 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200921 Py_DECREF(lookup_dict);
922 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200923
924
925 Py_BEGIN_ALLOW_THREADS
926 Python_Lock_Vim();
927 free_tv(our_tv);
928 Python_Release_Vim();
929 Py_END_ALLOW_THREADS
930
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200931 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200932}
933
Bram Moolenaardb913952012-06-29 12:54:53 +0200934static PyObject *ConvertToPyObject(typval_T *);
935
936 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200937VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200938{
Bram Moolenaardb913952012-06-29 12:54:53 +0200939 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200940 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200941 char_u *expr;
942 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200943
Bram Moolenaar389a1792013-06-23 13:00:44 +0200944 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200945 return NULL;
946
947 Py_BEGIN_ALLOW_THREADS
948 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200949 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200950 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200951 Python_Release_Vim();
952 Py_END_ALLOW_THREADS
953
Bram Moolenaar389a1792013-06-23 13:00:44 +0200954 Py_XDECREF(todecref);
955
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200956 if (VimTryEnd())
957 return NULL;
958
Bram Moolenaardb913952012-06-29 12:54:53 +0200959 if (our_tv == NULL)
960 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200961 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 return NULL;
963 }
964
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200965 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200966 Py_BEGIN_ALLOW_THREADS
967 Python_Lock_Vim();
968 free_tv(our_tv);
969 Python_Release_Vim();
970 Py_END_ALLOW_THREADS
971
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200972 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200973}
974
975 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200976VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200977{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200978 char_u *str;
979 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200980 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200981
Bram Moolenaar389a1792013-06-23 13:00:44 +0200982 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200983 return NULL;
984
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200985 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200986
987 Py_XDECREF(todecref);
988
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200989 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200990}
991
Bram Moolenaarf4258302013-06-02 18:20:17 +0200992 static PyObject *
993_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
994{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200995 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200996 PyObject *newwd;
997 PyObject *todecref;
998 char_u *new_dir;
999
Bram Moolenaard4209d22013-06-05 20:34:15 +02001000 if (_chdir == NULL)
1001 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001002 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001003 return NULL;
1004
1005 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1006 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001007 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001008 return NULL;
1009 }
1010
1011 if (!(new_dir = StringToChars(newwd, &todecref)))
1012 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001013 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001014 Py_DECREF(newwd);
1015 return NULL;
1016 }
1017
1018 VimTryStart();
1019
1020 if (vim_chdir(new_dir))
1021 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001022 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001023 Py_DECREF(newwd);
1024 Py_XDECREF(todecref);
1025
1026 if (VimTryEnd())
1027 return NULL;
1028
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001029 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001030 return NULL;
1031 }
1032
1033 Py_DECREF(newwd);
1034 Py_XDECREF(todecref);
1035
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001036 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001037
1038 if (VimTryEnd())
1039 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001040 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001041 return NULL;
1042 }
1043
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001044 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001045}
1046
1047 static PyObject *
1048VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1049{
1050 return _VimChdir(py_chdir, args, kwargs);
1051}
1052
1053 static PyObject *
1054VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1055{
1056 return _VimChdir(py_fchdir, args, kwargs);
1057}
1058
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001059typedef struct {
1060 PyObject *callable;
1061 PyObject *result;
1062} map_rtp_data;
1063
1064 static void
1065map_rtp_callback(char_u *path, void *_data)
1066{
1067 void **data = (void **) _data;
1068 PyObject *pathObject;
1069 map_rtp_data *mr_data = *((map_rtp_data **) data);
1070
Bram Moolenaar41009372013-07-01 22:03:04 +02001071 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001072 {
1073 *data = NULL;
1074 return;
1075 }
1076
1077 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1078 pathObject, NULL);
1079
1080 Py_DECREF(pathObject);
1081
1082 if (!mr_data->result || mr_data->result != Py_None)
1083 *data = NULL;
1084 else
1085 {
1086 Py_DECREF(mr_data->result);
1087 mr_data->result = NULL;
1088 }
1089}
1090
1091 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001092VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001093{
1094 map_rtp_data data;
1095
Bram Moolenaar389a1792013-06-23 13:00:44 +02001096 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001097 data.result = NULL;
1098
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001099 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001100
1101 if (data.result == NULL)
1102 {
1103 if (PyErr_Occurred())
1104 return NULL;
1105 else
1106 {
1107 Py_INCREF(Py_None);
1108 return Py_None;
1109 }
1110 }
1111 return data.result;
1112}
1113
1114/*
1115 * _vim_runtimepath_ special path implementation.
1116 */
1117
1118 static void
1119map_finder_callback(char_u *path, void *_data)
1120{
1121 void **data = (void **) _data;
1122 PyObject *list = *((PyObject **) data);
1123 PyObject *pathObject1, *pathObject2;
1124 char *pathbuf;
1125 size_t pathlen;
1126
1127 pathlen = STRLEN(path);
1128
1129#if PY_MAJOR_VERSION < 3
1130# define PY_MAIN_DIR_STRING "python2"
1131#else
1132# define PY_MAIN_DIR_STRING "python3"
1133#endif
1134#define PY_ALTERNATE_DIR_STRING "pythonx"
1135
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001136#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001137 if (!(pathbuf = PyMem_New(char,
1138 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1139 {
1140 PyErr_NoMemory();
1141 *data = NULL;
1142 return;
1143 }
1144
1145 mch_memmove(pathbuf, path, pathlen + 1);
1146 add_pathsep((char_u *) pathbuf);
1147
1148 pathlen = STRLEN(pathbuf);
1149 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1150 PYTHONX_STRING_LENGTH + 1);
1151
1152 if (!(pathObject1 = PyString_FromString(pathbuf)))
1153 {
1154 *data = NULL;
1155 PyMem_Free(pathbuf);
1156 return;
1157 }
1158
1159 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1160 PYTHONX_STRING_LENGTH + 1);
1161
1162 if (!(pathObject2 = PyString_FromString(pathbuf)))
1163 {
1164 Py_DECREF(pathObject1);
1165 PyMem_Free(pathbuf);
1166 *data = NULL;
1167 return;
1168 }
1169
1170 PyMem_Free(pathbuf);
1171
1172 if (PyList_Append(list, pathObject1)
1173 || PyList_Append(list, pathObject2))
1174 *data = NULL;
1175
1176 Py_DECREF(pathObject1);
1177 Py_DECREF(pathObject2);
1178}
1179
1180 static PyObject *
1181Vim_GetPaths(PyObject *self UNUSED)
1182{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001183 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001184
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001185 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001186 return NULL;
1187
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001188 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001189
1190 if (PyErr_Occurred())
1191 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001192 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001193 return NULL;
1194 }
1195
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001196 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001197}
1198
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001199#if PY_VERSION_HEX >= 0x030700f0
1200 static PyObject *
1201FinderFindSpec(PyObject *self, PyObject *args)
1202{
1203 char *fullname;
1204 PyObject *paths;
1205 PyObject *target = Py_None;
1206 PyObject *spec;
1207
1208 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1209 return NULL;
1210
1211 if (!(paths = Vim_GetPaths(self)))
1212 return NULL;
1213
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001214 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001215
1216 Py_DECREF(paths);
1217
1218 if (!spec)
1219 {
1220 if (PyErr_Occurred())
1221 return NULL;
1222
1223 Py_INCREF(Py_None);
1224 return Py_None;
1225 }
1226
1227 return spec;
1228}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001229
1230 static PyObject *
1231FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1232{
1233 // Apparently returning None works.
1234 Py_INCREF(Py_None);
1235 return Py_None;
1236}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001237#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001238 static PyObject *
1239call_load_module(char *name, int len, PyObject *find_module_result)
1240{
1241 PyObject *fd, *pathname, *description;
1242
Bram Moolenaarc476e522013-06-23 13:46:40 +02001243 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001244 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001245 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001246 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001247 Py_TYPE_NAME(find_module_result));
1248 return NULL;
1249 }
1250 if (PyTuple_GET_SIZE(find_module_result) != 3)
1251 {
1252 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001253 N_("expected 3-tuple as imp.find_module() result, but got "
1254 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001255 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001256 return NULL;
1257 }
1258
1259 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1260 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1261 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1262 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001263 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001264 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001265 return NULL;
1266 }
1267
1268 return PyObject_CallFunction(py_load_module,
1269 "s#OOO", name, len, fd, pathname, description);
1270}
1271
1272 static PyObject *
1273find_module(char *fullname, char *tail, PyObject *new_path)
1274{
1275 PyObject *find_module_result;
1276 PyObject *module;
1277 char *dot;
1278
Bram Moolenaar41009372013-07-01 22:03:04 +02001279 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001280 {
1281 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001282 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001283 * first component
1284 */
1285 PyObject *newest_path;
1286 int partlen = (int) (dot - 1 - tail);
1287
1288 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1289 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001290 {
1291 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1292 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001293 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001294 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001295
1296 if (!(module = call_load_module(
1297 fullname,
1298 ((int) (tail - fullname)) + partlen,
1299 find_module_result)))
1300 {
1301 Py_DECREF(find_module_result);
1302 return NULL;
1303 }
1304
1305 Py_DECREF(find_module_result);
1306
1307 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1308 {
1309 Py_DECREF(module);
1310 return NULL;
1311 }
1312
1313 Py_DECREF(module);
1314
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001315 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001316
1317 Py_DECREF(newest_path);
1318
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001319 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001320 }
1321 else
1322 {
1323 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1324 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001325 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001326 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1327 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001328 return NULL;
1329 }
1330
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001331 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001332 }
1333}
1334
1335 static PyObject *
1336FinderFindModule(PyObject *self, PyObject *args)
1337{
1338 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001339 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001340 PyObject *new_path;
1341 LoaderObject *loader;
1342
1343 if (!PyArg_ParseTuple(args, "s", &fullname))
1344 return NULL;
1345
1346 if (!(new_path = Vim_GetPaths(self)))
1347 return NULL;
1348
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001349 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001350
1351 Py_DECREF(new_path);
1352
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001353 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001354 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001355 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001356 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001357
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001358 Py_INCREF(Py_None);
1359 return Py_None;
1360 }
1361
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001362 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001363 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001364 Py_DECREF(result);
1365 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001366 return NULL;
1367 }
1368
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001369 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1370 {
1371 vim_free(fullname);
1372 Py_DECREF(result);
1373 return NULL;
1374 }
1375
1376 loader->fullname = fullname;
1377 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001378
1379 return (PyObject *) loader;
1380}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001381#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001382
1383 static PyObject *
1384VimPathHook(PyObject *self UNUSED, PyObject *args)
1385{
1386 char *path;
1387
1388 if (PyArg_ParseTuple(args, "s", &path)
1389 && STRCMP(path, vim_special_path) == 0)
1390 {
1391 Py_INCREF(vim_module);
1392 return vim_module;
1393 }
1394
1395 PyErr_Clear();
1396 PyErr_SetNone(PyExc_ImportError);
1397 return NULL;
1398}
1399
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001400/*
1401 * Vim module - Definitions
1402 */
1403
1404static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001405 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001406 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001407 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001408 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1409 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001410 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1411 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001412 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001413#if PY_VERSION_HEX >= 0x030700f0
1414 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001415#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001416 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001417 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1418 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1419 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001420};
1421
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001422/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001423 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001424 */
1425
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001426static PyTypeObject IterType;
1427
1428typedef PyObject *(*nextfun)(void **);
1429typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001430typedef int (*traversefun)(void *, visitproc, void *);
1431typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001432
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001433// Main purpose of this object is removing the need for do python
1434// initialization (i.e. PyType_Ready and setting type attributes) for a big
1435// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001436typedef struct
1437{
1438 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001439 void *cur;
1440 nextfun next;
1441 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001442 traversefun traverse;
1443 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001444} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001445
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001446 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001447IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1448 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001449{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001450 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001451
Bram Moolenaar774267b2013-05-21 20:51:59 +02001452 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001453 self->cur = start;
1454 self->next = next;
1455 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001456 self->traverse = traverse;
1457 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001458
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001459 return (PyObject *)(self);
1460}
1461
1462 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001463IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001464{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001465 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001466 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001467 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001468}
1469
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001470 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001471IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001472{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001473 if (self->traverse != NULL)
1474 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001475 else
1476 return 0;
1477}
1478
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001479// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001480#ifdef clear
1481# undef clear
1482#endif
1483
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001484 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001485IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001486{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001487 if (self->clear != NULL)
1488 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001489 else
1490 return 0;
1491}
1492
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001493 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001494IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001495{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001496 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001497}
1498
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001499 static PyObject *
1500IterIter(PyObject *self)
1501{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001502 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001503 return self;
1504}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001505
Bram Moolenaardb913952012-06-29 12:54:53 +02001506typedef struct pylinkedlist_S {
1507 struct pylinkedlist_S *pll_next;
1508 struct pylinkedlist_S *pll_prev;
1509 PyObject *pll_obj;
1510} pylinkedlist_T;
1511
1512static pylinkedlist_T *lastdict = NULL;
1513static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001514static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001515
1516 static void
1517pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1518{
1519 if (ref->pll_prev == NULL)
1520 {
1521 if (ref->pll_next == NULL)
1522 {
1523 *last = NULL;
1524 return;
1525 }
1526 }
1527 else
1528 ref->pll_prev->pll_next = ref->pll_next;
1529
1530 if (ref->pll_next == NULL)
1531 *last = ref->pll_prev;
1532 else
1533 ref->pll_next->pll_prev = ref->pll_prev;
1534}
1535
1536 static void
1537pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1538{
1539 if (*last == NULL)
1540 ref->pll_prev = NULL;
1541 else
1542 {
1543 (*last)->pll_next = ref;
1544 ref->pll_prev = *last;
1545 }
1546 ref->pll_next = NULL;
1547 ref->pll_obj = self;
1548 *last = ref;
1549}
1550
1551static PyTypeObject DictionaryType;
1552
1553typedef struct
1554{
1555 PyObject_HEAD
1556 dict_T *dict;
1557 pylinkedlist_T ref;
1558} DictionaryObject;
1559
Bram Moolenaara9922d62013-05-30 13:01:18 +02001560static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1561
1562#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1563
Bram Moolenaardb913952012-06-29 12:54:53 +02001564 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001565DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001566{
1567 DictionaryObject *self;
1568
Bram Moolenaara9922d62013-05-30 13:01:18 +02001569 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001570 if (self == NULL)
1571 return NULL;
1572 self->dict = dict;
1573 ++dict->dv_refcount;
1574
1575 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1576
1577 return (PyObject *)(self);
1578}
1579
Bram Moolenaara9922d62013-05-30 13:01:18 +02001580 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001581py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001582{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001583 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001584
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001585 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001586 {
1587 PyErr_NoMemory();
1588 return NULL;
1589 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001590 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001591
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001592 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001593}
1594
1595 static PyObject *
1596DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1597{
1598 DictionaryObject *self;
1599 dict_T *dict;
1600
1601 if (!(dict = py_dict_alloc()))
1602 return NULL;
1603
1604 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1605
1606 --dict->dv_refcount;
1607
1608 if (kwargs || PyTuple_Size(args))
1609 {
1610 PyObject *tmp;
1611 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1612 {
1613 Py_DECREF(self);
1614 return NULL;
1615 }
1616
1617 Py_DECREF(tmp);
1618 }
1619
1620 return (PyObject *)(self);
1621}
1622
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001623 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001624DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001625{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001626 pyll_remove(&self->ref, &lastdict);
1627 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001628
1629 DESTRUCTOR_FINISH(self);
1630}
1631
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001632static char *DictionaryAttrs[] = {
1633 "locked", "scope",
1634 NULL
1635};
1636
1637 static PyObject *
1638DictionaryDir(PyObject *self)
1639{
1640 return ObjectDir(self, DictionaryAttrs);
1641}
1642
Bram Moolenaardb913952012-06-29 12:54:53 +02001643 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001644DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001645{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001646 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001647 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001648 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001649 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001650 return -1;
1651 }
1652
1653 if (strcmp(name, "locked") == 0)
1654 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001655 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001656 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001657 PyErr_SET_STRING(PyExc_TypeError,
1658 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001659 return -1;
1660 }
1661 else
1662 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001663 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001664 if (istrue == -1)
1665 return -1;
1666 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001667 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001668 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001669 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001670 }
1671 return 0;
1672 }
1673 else
1674 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001675 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001676 return -1;
1677 }
1678}
1679
1680 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001681DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001682{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001683 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001684}
1685
Bram Moolenaara9922d62013-05-30 13:01:18 +02001686#define DICT_FLAG_HAS_DEFAULT 0x01
1687#define DICT_FLAG_POP 0x02
1688#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001689#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001690#define DICT_FLAG_RETURN_PAIR 0x10
1691
Bram Moolenaardb913952012-06-29 12:54:53 +02001692 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001693_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001694{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001695 PyObject *keyObject;
1696 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001697 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001698 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001699 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001700 dict_T *dict = self->dict;
1701 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001702 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001703
Bram Moolenaara9922d62013-05-30 13:01:18 +02001704 if (flags & DICT_FLAG_HAS_DEFAULT)
1705 {
1706 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1707 return NULL;
1708 }
1709 else
1710 keyObject = args;
1711
1712 if (flags & DICT_FLAG_RETURN_BOOL)
1713 defObject = Py_False;
1714
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001715 if (!(key = StringToChars(keyObject, &todecref)))
1716 return NULL;
1717
1718 if (*key == NUL)
1719 {
1720 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001721 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001722 return NULL;
1723 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001724
Bram Moolenaara9922d62013-05-30 13:01:18 +02001725 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001726
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001727 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001728
Bram Moolenaara9922d62013-05-30 13:01:18 +02001729 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001730 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001731 if (defObject)
1732 {
1733 Py_INCREF(defObject);
1734 return defObject;
1735 }
1736 else
1737 {
1738 PyErr_SetObject(PyExc_KeyError, keyObject);
1739 return NULL;
1740 }
1741 }
1742 else if (flags & DICT_FLAG_RETURN_BOOL)
1743 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001744 ret = Py_True;
1745 Py_INCREF(ret);
1746 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747 }
1748
1749 di = dict_lookup(hi);
1750
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001751 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001752 return NULL;
1753
1754 if (flags & DICT_FLAG_POP)
1755 {
1756 if (dict->dv_lock)
1757 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001758 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001759 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001760 return NULL;
1761 }
1762
1763 hash_remove(&dict->dv_hashtab, hi);
1764 dictitem_free(di);
1765 }
1766
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001767 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001768}
1769
1770 static PyObject *
1771DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1772{
1773 return _DictionaryItem(self, keyObject, 0);
1774}
1775
1776 static int
1777DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1778{
1779 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001780 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001781
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001782 if (rObj == NULL)
1783 return -1;
1784
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001785 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001786
Bram Moolenaardee2e312013-06-23 16:35:47 +02001787 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001789 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001790}
1791
1792typedef struct
1793{
1794 hashitem_T *ht_array;
1795 long_u ht_used;
1796 hashtab_T *ht;
1797 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001798 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001799} dictiterinfo_T;
1800
1801 static PyObject *
1802DictionaryIterNext(dictiterinfo_T **dii)
1803{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001804 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001805
1806 if (!(*dii)->todo)
1807 return NULL;
1808
1809 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1810 (*dii)->ht->ht_used != (*dii)->ht_used)
1811 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001812 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001813 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001814 return NULL;
1815 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001816
Bram Moolenaara9922d62013-05-30 13:01:18 +02001817 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1818 ++((*dii)->hi);
1819
1820 --((*dii)->todo);
1821
Bram Moolenaar41009372013-07-01 22:03:04 +02001822 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001823 return NULL;
1824
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001825 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001826}
1827
1828 static PyObject *
1829DictionaryIter(DictionaryObject *self)
1830{
1831 dictiterinfo_T *dii;
1832 hashtab_T *ht;
1833
1834 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1835 {
1836 PyErr_NoMemory();
1837 return NULL;
1838 }
1839
1840 ht = &self->dict->dv_hashtab;
1841 dii->ht_array = ht->ht_array;
1842 dii->ht_used = ht->ht_used;
1843 dii->ht = ht;
1844 dii->hi = dii->ht_array;
1845 dii->todo = dii->ht_used;
1846
1847 return IterNew(dii,
1848 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1849 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001850}
1851
1852 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001853DictionaryAssItem(
1854 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001855{
1856 char_u *key;
1857 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001858 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001859 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001860 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001861
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001862 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001863 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001864 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001865 return -1;
1866 }
1867
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001868 if (!(key = StringToChars(keyObject, &todecref)))
1869 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001870
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001871 if (*key == NUL)
1872 {
1873 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001874 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001875 return -1;
1876 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001877
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001878 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001879
1880 if (valObject == NULL)
1881 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001882 hashitem_T *hi;
1883
Bram Moolenaardb913952012-06-29 12:54:53 +02001884 if (di == NULL)
1885 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001886 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001887 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001888 return -1;
1889 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001890 hi = hash_find(&dict->dv_hashtab, di->di_key);
1891 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001892 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001893 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001894 return 0;
1895 }
1896
1897 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001898 {
1899 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001900 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001901 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001902
1903 if (di == NULL)
1904 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001905 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001906 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001907 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001908 PyErr_NoMemory();
1909 return -1;
1910 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001911 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001912
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001913 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001914 {
1915 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001916 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001917 RAISE_KEY_ADD_FAIL(key);
1918 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001919 return -1;
1920 }
1921 }
1922 else
1923 clear_tv(&di->di_tv);
1924
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001925 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001926
1927 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001928 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001929 return 0;
1930}
1931
Bram Moolenaara9922d62013-05-30 13:01:18 +02001932typedef PyObject *(*hi_to_py)(hashitem_T *);
1933
Bram Moolenaardb913952012-06-29 12:54:53 +02001934 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001935DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001936{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001937 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001938 long_u todo = dict->dv_hashtab.ht_used;
1939 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001940 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001941 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001942 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001943
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001944 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001945 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1946 {
1947 if (!HASHITEM_EMPTY(hi))
1948 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 if (!(newObj = hiconvert(hi)))
1950 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001951 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001952 return NULL;
1953 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001954 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001955 --todo;
1956 ++i;
1957 }
1958 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001959 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001960}
1961
Bram Moolenaara9922d62013-05-30 13:01:18 +02001962 static PyObject *
1963dict_key(hashitem_T *hi)
1964{
1965 return PyBytes_FromString((char *)(hi->hi_key));
1966}
1967
1968 static PyObject *
1969DictionaryListKeys(DictionaryObject *self)
1970{
1971 return DictionaryListObjects(self, dict_key);
1972}
1973
1974 static PyObject *
1975dict_val(hashitem_T *hi)
1976{
1977 dictitem_T *di;
1978
1979 di = dict_lookup(hi);
1980 return ConvertToPyObject(&di->di_tv);
1981}
1982
1983 static PyObject *
1984DictionaryListValues(DictionaryObject *self)
1985{
1986 return DictionaryListObjects(self, dict_val);
1987}
1988
1989 static PyObject *
1990dict_item(hashitem_T *hi)
1991{
1992 PyObject *keyObject;
1993 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001994 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001995
1996 if (!(keyObject = dict_key(hi)))
1997 return NULL;
1998
1999 if (!(valObject = dict_val(hi)))
2000 {
2001 Py_DECREF(keyObject);
2002 return NULL;
2003 }
2004
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002005 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002006
2007 Py_DECREF(keyObject);
2008 Py_DECREF(valObject);
2009
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002010 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002011}
2012
2013 static PyObject *
2014DictionaryListItems(DictionaryObject *self)
2015{
2016 return DictionaryListObjects(self, dict_item);
2017}
2018
2019 static PyObject *
2020DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2021{
2022 dict_T *dict = self->dict;
2023
2024 if (dict->dv_lock)
2025 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002026 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002027 return NULL;
2028 }
2029
2030 if (kwargs)
2031 {
2032 typval_T tv;
2033
2034 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2035 return NULL;
2036
2037 VimTryStart();
2038 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2039 clear_tv(&tv);
2040 if (VimTryEnd())
2041 return NULL;
2042 }
2043 else
2044 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002045 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002046
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002047 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002048 return NULL;
2049
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002050 if (obj == NULL)
2051 {
2052 Py_INCREF(Py_None);
2053 return Py_None;
2054 }
2055
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002056 if (PyObject_HasAttrString(obj, "keys"))
2057 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002058 else
2059 {
2060 PyObject *iterator;
2061 PyObject *item;
2062
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002063 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002064 return NULL;
2065
2066 while ((item = PyIter_Next(iterator)))
2067 {
2068 PyObject *fast;
2069 PyObject *keyObject;
2070 PyObject *valObject;
2071 PyObject *todecref;
2072 char_u *key;
2073 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002074 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002075
2076 if (!(fast = PySequence_Fast(item, "")))
2077 {
2078 Py_DECREF(iterator);
2079 Py_DECREF(item);
2080 return NULL;
2081 }
2082
2083 Py_DECREF(item);
2084
2085 if (PySequence_Fast_GET_SIZE(fast) != 2)
2086 {
2087 Py_DECREF(iterator);
2088 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002089 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002090 N_("expected sequence element of size 2, "
2091 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002092 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002093 return NULL;
2094 }
2095
2096 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2097
2098 if (!(key = StringToChars(keyObject, &todecref)))
2099 {
2100 Py_DECREF(iterator);
2101 Py_DECREF(fast);
2102 return NULL;
2103 }
2104
2105 di = dictitem_alloc(key);
2106
2107 Py_XDECREF(todecref);
2108
2109 if (di == NULL)
2110 {
2111 Py_DECREF(fast);
2112 Py_DECREF(iterator);
2113 PyErr_NoMemory();
2114 return NULL;
2115 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002116 di->di_tv.v_type = VAR_UNKNOWN;
2117
2118 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2119
2120 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2121 {
2122 Py_DECREF(iterator);
2123 Py_DECREF(fast);
2124 dictitem_free(di);
2125 return NULL;
2126 }
2127
2128 Py_DECREF(fast);
2129
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002130 hi = hash_find(&dict->dv_hashtab, di->di_key);
2131 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002132 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002133 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002134 Py_DECREF(iterator);
2135 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002136 return NULL;
2137 }
2138 }
2139
2140 Py_DECREF(iterator);
2141
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002142 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002143 if (PyErr_Occurred())
2144 return NULL;
2145 }
2146 }
2147 Py_INCREF(Py_None);
2148 return Py_None;
2149}
2150
2151 static PyObject *
2152DictionaryGet(DictionaryObject *self, PyObject *args)
2153{
2154 return _DictionaryItem(self, args,
2155 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2156}
2157
2158 static PyObject *
2159DictionaryPop(DictionaryObject *self, PyObject *args)
2160{
2161 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2162}
2163
2164 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002165DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002166{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002167 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002168 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002169 PyObject *valObject;
2170 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002171
Bram Moolenaarde71b562013-06-02 17:41:54 +02002172 if (self->dict->dv_hashtab.ht_used == 0)
2173 {
2174 PyErr_SetNone(PyExc_KeyError);
2175 return NULL;
2176 }
2177
2178 hi = self->dict->dv_hashtab.ht_array;
2179 while (HASHITEM_EMPTY(hi))
2180 ++hi;
2181
2182 di = dict_lookup(hi);
2183
2184 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002185 return NULL;
2186
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002187 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002188 {
2189 Py_DECREF(valObject);
2190 return NULL;
2191 }
2192
2193 hash_remove(&self->dict->dv_hashtab, hi);
2194 dictitem_free(di);
2195
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002196 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002197}
2198
2199 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002200DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002201{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002202 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2203}
2204
2205static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002206 0, // sq_length
2207 0, // sq_concat
2208 0, // sq_repeat
2209 0, // sq_item
2210 0, // sq_slice
2211 0, // sq_ass_item
2212 0, // sq_ass_slice
2213 (objobjproc) DictionaryContains, // sq_contains
2214 0, // sq_inplace_concat
2215 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002216};
2217
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002218static PyMappingMethods DictionaryAsMapping = {
2219 (lenfunc) DictionaryLength,
2220 (binaryfunc) DictionaryItem,
2221 (objobjargproc) DictionaryAssItem,
2222};
2223
Bram Moolenaardb913952012-06-29 12:54:53 +02002224static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002225 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002226 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2227 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2228 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2229 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2230 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002231 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002232 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002233 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2234 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002235};
2236
2237static PyTypeObject ListType;
2238
2239typedef struct
2240{
2241 PyObject_HEAD
2242 list_T *list;
2243 pylinkedlist_T ref;
2244} ListObject;
2245
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002246#define NEW_LIST(list) ListNew(&ListType, list)
2247
Bram Moolenaardb913952012-06-29 12:54:53 +02002248 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002249ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002250{
2251 ListObject *self;
2252
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002253 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002254 if (self == NULL)
2255 return NULL;
2256 self->list = list;
2257 ++list->lv_refcount;
2258
2259 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2260
2261 return (PyObject *)(self);
2262}
2263
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002264 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002265py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002266{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002267 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002268
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002269 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002270 {
2271 PyErr_NoMemory();
2272 return NULL;
2273 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002274 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002275
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002276 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002277}
2278
2279 static int
2280list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2281{
2282 PyObject *iterator;
2283 PyObject *item;
2284 listitem_T *li;
2285
2286 if (!(iterator = PyObject_GetIter(obj)))
2287 return -1;
2288
2289 while ((item = PyIter_Next(iterator)))
2290 {
2291 if (!(li = listitem_alloc()))
2292 {
2293 PyErr_NoMemory();
2294 Py_DECREF(item);
2295 Py_DECREF(iterator);
2296 return -1;
2297 }
2298 li->li_tv.v_lock = 0;
2299 li->li_tv.v_type = VAR_UNKNOWN;
2300
2301 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2302 {
2303 Py_DECREF(item);
2304 Py_DECREF(iterator);
2305 listitem_free(li);
2306 return -1;
2307 }
2308
2309 Py_DECREF(item);
2310
2311 list_append(l, li);
2312 }
2313
2314 Py_DECREF(iterator);
2315
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002316 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002317 if (PyErr_Occurred())
2318 return -1;
2319
2320 return 0;
2321}
2322
2323 static PyObject *
2324ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2325{
2326 list_T *list;
2327 PyObject *obj = NULL;
2328
2329 if (kwargs)
2330 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002331 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002332 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002333 return NULL;
2334 }
2335
2336 if (!PyArg_ParseTuple(args, "|O", &obj))
2337 return NULL;
2338
2339 if (!(list = py_list_alloc()))
2340 return NULL;
2341
2342 if (obj)
2343 {
2344 PyObject *lookup_dict;
2345
2346 if (!(lookup_dict = PyDict_New()))
2347 {
2348 list_unref(list);
2349 return NULL;
2350 }
2351
2352 if (list_py_concat(list, obj, lookup_dict) == -1)
2353 {
2354 Py_DECREF(lookup_dict);
2355 list_unref(list);
2356 return NULL;
2357 }
2358
2359 Py_DECREF(lookup_dict);
2360 }
2361
2362 return ListNew(subtype, list);
2363}
2364
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002365 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002366ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002367{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002368 pyll_remove(&self->ref, &lastlist);
2369 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002370
2371 DESTRUCTOR_FINISH(self);
2372}
2373
Bram Moolenaardb913952012-06-29 12:54:53 +02002374 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002375ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002376{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002377 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002378}
2379
2380 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002381ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002382{
2383 listitem_T *li;
2384
Bram Moolenaard6e39182013-05-21 18:30:34 +02002385 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002386 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002387 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002388 return NULL;
2389 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002390 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002391 if (li == NULL)
2392 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002393 // No more suitable format specifications in python-2.3
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002394 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002395 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002396 return NULL;
2397 }
2398 return ConvertToPyObject(&li->li_tv);
2399}
2400
Bram Moolenaardb913952012-06-29 12:54:53 +02002401 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002402ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2403 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002404{
2405 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002406 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002407
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002408 if (step == 0)
2409 {
2410 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2411 return NULL;
2412 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002413
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002414 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002415 if (list == NULL)
2416 return NULL;
2417
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002418 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002419 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002420 PyObject *item;
2421
2422 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002423 if (item == NULL)
2424 {
2425 Py_DECREF(list);
2426 return NULL;
2427 }
2428
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002429 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002430 }
2431
2432 return list;
2433}
2434
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002435 static PyObject *
2436ListItem(ListObject *self, PyObject* idx)
2437{
2438#if PY_MAJOR_VERSION < 3
2439 if (PyInt_Check(idx))
2440 {
2441 long _idx = PyInt_AsLong(idx);
2442 return ListIndex(self, _idx);
2443 }
2444 else
2445#endif
2446 if (PyLong_Check(idx))
2447 {
2448 long _idx = PyLong_AsLong(idx);
2449 return ListIndex(self, _idx);
2450 }
2451 else if (PySlice_Check(idx))
2452 {
2453 Py_ssize_t start, stop, step, slicelen;
2454
Bram Moolenaar922a4662014-03-30 16:11:43 +02002455 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002456 &start, &stop, &step, &slicelen) < 0)
2457 return NULL;
2458 return ListSlice(self, start, step, slicelen);
2459 }
2460 else
2461 {
2462 RAISE_INVALID_INDEX_TYPE(idx);
2463 return NULL;
2464 }
2465}
2466
2467 static void
2468list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2469 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2470{
2471 while (numreplaced--)
2472 {
2473 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2474 listitem_remove(l, lis[slicelen + numreplaced]);
2475 }
2476 while (numadded--)
2477 {
2478 listitem_T *next;
2479
2480 next = lastaddedli->li_prev;
2481 listitem_remove(l, lastaddedli);
2482 lastaddedli = next;
2483 }
2484}
2485
2486 static int
2487ListAssSlice(ListObject *self, Py_ssize_t first,
2488 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2489{
2490 PyObject *iterator;
2491 PyObject *item;
2492 listitem_T *li;
2493 listitem_T *lastaddedli = NULL;
2494 listitem_T *next;
2495 typval_T v;
2496 list_T *l = self->list;
2497 PyInt i;
2498 PyInt j;
2499 PyInt numreplaced = 0;
2500 PyInt numadded = 0;
2501 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002502 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002503
2504 size = ListLength(self);
2505
2506 if (l->lv_lock)
2507 {
2508 RAISE_LOCKED_LIST;
2509 return -1;
2510 }
2511
2512 if (step == 0)
2513 {
2514 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2515 return -1;
2516 }
2517
2518 if (step != 1 && slicelen == 0)
2519 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002520 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002521 int ret = 0;
2522
2523 if (obj == NULL)
2524 return 0;
2525
2526 if (!(iterator = PyObject_GetIter(obj)))
2527 return -1;
2528
2529 if ((item = PyIter_Next(iterator)))
2530 {
2531 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002532 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002533 "to extended slice"), 0);
2534 Py_DECREF(item);
2535 ret = -1;
2536 }
2537 Py_DECREF(iterator);
2538 return ret;
2539 }
2540
2541 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002542 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002543 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2544 {
2545 PyErr_NoMemory();
2546 return -1;
2547 }
2548
2549 if (first == size)
2550 li = NULL;
2551 else
2552 {
2553 li = list_find(l, (long) first);
2554 if (li == NULL)
2555 {
2556 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2557 (int)first);
2558 if (obj != NULL)
2559 PyMem_Free(lis);
2560 return -1;
2561 }
2562 i = slicelen;
2563 while (i-- && li != NULL)
2564 {
2565 j = step;
2566 next = li;
2567 if (step > 0)
2568 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2569 else
2570 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2571
2572 if (obj == NULL)
2573 listitem_remove(l, li);
2574 else
2575 lis[slicelen - i - 1] = li;
2576
2577 li = next;
2578 }
2579 if (li == NULL && i != -1)
2580 {
2581 PyErr_SET_VIM(N_("internal error: not enough list items"));
2582 if (obj != NULL)
2583 PyMem_Free(lis);
2584 return -1;
2585 }
2586 }
2587
2588 if (obj == NULL)
2589 return 0;
2590
2591 if (!(iterator = PyObject_GetIter(obj)))
2592 {
2593 PyMem_Free(lis);
2594 return -1;
2595 }
2596
2597 i = 0;
2598 while ((item = PyIter_Next(iterator)))
2599 {
2600 if (ConvertFromPyObject(item, &v) == -1)
2601 {
2602 Py_DECREF(iterator);
2603 Py_DECREF(item);
2604 PyMem_Free(lis);
2605 return -1;
2606 }
2607 Py_DECREF(item);
2608 if (list_insert_tv(l, &v, numreplaced < slicelen
2609 ? lis[numreplaced]
2610 : li) == FAIL)
2611 {
2612 clear_tv(&v);
2613 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2614 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2615 PyMem_Free(lis);
2616 return -1;
2617 }
2618 if (numreplaced < slicelen)
2619 {
2620 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002621 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002622 numreplaced++;
2623 }
2624 else
2625 {
2626 if (li)
2627 lastaddedli = li->li_prev;
2628 else
2629 lastaddedli = l->lv_last;
2630 numadded++;
2631 }
2632 clear_tv(&v);
2633 if (step != 1 && i >= slicelen)
2634 {
2635 Py_DECREF(iterator);
2636 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002637 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002638 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002639 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2640 PyMem_Free(lis);
2641 return -1;
2642 }
2643 ++i;
2644 }
2645 Py_DECREF(iterator);
2646
2647 if (step != 1 && i != slicelen)
2648 {
2649 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002650 N_("attempt to assign sequence of size %d to extended slice "
2651 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002652 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2653 PyMem_Free(lis);
2654 return -1;
2655 }
2656
2657 if (PyErr_Occurred())
2658 {
2659 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2660 PyMem_Free(lis);
2661 return -1;
2662 }
2663
2664 for (i = 0; i < numreplaced; i++)
2665 listitem_free(lis[i]);
2666 if (step == 1)
2667 for (i = numreplaced; i < slicelen; i++)
2668 listitem_remove(l, lis[i]);
2669
2670 PyMem_Free(lis);
2671
2672 return 0;
2673}
2674
2675 static int
2676ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2677{
2678 typval_T tv;
2679 list_T *l = self->list;
2680 listitem_T *li;
2681 Py_ssize_t length = ListLength(self);
2682
2683 if (l->lv_lock)
2684 {
2685 RAISE_LOCKED_LIST;
2686 return -1;
2687 }
2688 if (index > length || (index == length && obj == NULL))
2689 {
2690 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2691 return -1;
2692 }
2693
2694 if (obj == NULL)
2695 {
2696 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002697 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002698 clear_tv(&li->li_tv);
2699 vim_free(li);
2700 return 0;
2701 }
2702
2703 if (ConvertFromPyObject(obj, &tv) == -1)
2704 return -1;
2705
2706 if (index == length)
2707 {
2708 if (list_append_tv(l, &tv) == FAIL)
2709 {
2710 clear_tv(&tv);
2711 PyErr_SET_VIM(N_("failed to add item to list"));
2712 return -1;
2713 }
2714 }
2715 else
2716 {
2717 li = list_find(l, (long) index);
2718 clear_tv(&li->li_tv);
2719 copy_tv(&tv, &li->li_tv);
2720 clear_tv(&tv);
2721 }
2722 return 0;
2723}
2724
2725 static Py_ssize_t
2726ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2727{
2728#if PY_MAJOR_VERSION < 3
2729 if (PyInt_Check(idx))
2730 {
2731 long _idx = PyInt_AsLong(idx);
2732 return ListAssIndex(self, _idx, obj);
2733 }
2734 else
2735#endif
2736 if (PyLong_Check(idx))
2737 {
2738 long _idx = PyLong_AsLong(idx);
2739 return ListAssIndex(self, _idx, obj);
2740 }
2741 else if (PySlice_Check(idx))
2742 {
2743 Py_ssize_t start, stop, step, slicelen;
2744
Bram Moolenaar922a4662014-03-30 16:11:43 +02002745 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002746 &start, &stop, &step, &slicelen) < 0)
2747 return -1;
2748 return ListAssSlice(self, start, step, slicelen,
2749 obj);
2750 }
2751 else
2752 {
2753 RAISE_INVALID_INDEX_TYPE(idx);
2754 return -1;
2755 }
2756}
2757
2758 static PyObject *
2759ListConcatInPlace(ListObject *self, PyObject *obj)
2760{
2761 list_T *l = self->list;
2762 PyObject *lookup_dict;
2763
2764 if (l->lv_lock)
2765 {
2766 RAISE_LOCKED_LIST;
2767 return NULL;
2768 }
2769
2770 if (!(lookup_dict = PyDict_New()))
2771 return NULL;
2772
2773 if (list_py_concat(l, obj, lookup_dict) == -1)
2774 {
2775 Py_DECREF(lookup_dict);
2776 return NULL;
2777 }
2778 Py_DECREF(lookup_dict);
2779
2780 Py_INCREF(self);
2781 return (PyObject *)(self);
2782}
2783
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002784typedef struct
2785{
2786 listwatch_T lw;
2787 list_T *list;
2788} listiterinfo_T;
2789
2790 static void
2791ListIterDestruct(listiterinfo_T *lii)
2792{
2793 list_rem_watch(lii->list, &lii->lw);
2794 PyMem_Free(lii);
2795}
2796
2797 static PyObject *
2798ListIterNext(listiterinfo_T **lii)
2799{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002800 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002801
2802 if (!((*lii)->lw.lw_item))
2803 return NULL;
2804
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002805 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002806 return NULL;
2807
2808 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2809
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002810 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002811}
2812
2813 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002814ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002815{
2816 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002817 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002818
2819 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2820 {
2821 PyErr_NoMemory();
2822 return NULL;
2823 }
2824
2825 list_add_watch(l, &lii->lw);
2826 lii->lw.lw_item = l->lv_first;
2827 lii->list = l;
2828
2829 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002830 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2831 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002832}
2833
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002834static char *ListAttrs[] = {
2835 "locked",
2836 NULL
2837};
2838
2839 static PyObject *
2840ListDir(PyObject *self)
2841{
2842 return ObjectDir(self, ListAttrs);
2843}
2844
Bram Moolenaar66b79852012-09-21 14:00:35 +02002845 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002846ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002847{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002848 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002849 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002850 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002851 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002852 return -1;
2853 }
2854
2855 if (strcmp(name, "locked") == 0)
2856 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002857 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002858 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002859 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002860 return -1;
2861 }
2862 else
2863 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002864 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002865 if (istrue == -1)
2866 return -1;
2867 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002868 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002869 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002870 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002871 }
2872 return 0;
2873 }
2874 else
2875 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002876 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002877 return -1;
2878 }
2879}
2880
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002881static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002882 (lenfunc) ListLength, // sq_length, len(x)
2883 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2884 0, // RangeRepeat, sq_repeat, x*n
2885 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2886 0, // was_sq_slice, x[i:j]
2887 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2888 0, // was_sq_ass_slice, x[i:j]=v
2889 0, // sq_contains
2890 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2891 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002892};
2893
2894static PyMappingMethods ListAsMapping = {
2895 /* mp_length */ (lenfunc) ListLength,
2896 /* mp_subscript */ (binaryfunc) ListItem,
2897 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2898};
2899
Bram Moolenaardb913952012-06-29 12:54:53 +02002900static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002901 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2902 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2903 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002904};
2905
2906typedef struct
2907{
2908 PyObject_HEAD
2909 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002910 int argc;
2911 typval_T *argv;
2912 dict_T *self;
2913 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002914 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002915} FunctionObject;
2916
2917static PyTypeObject FunctionType;
2918
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002919#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2920 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002921
Bram Moolenaardb913952012-06-29 12:54:53 +02002922 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002923FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002924 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002925{
2926 FunctionObject *self;
2927
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002928 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002929 if (self == NULL)
2930 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002931
2932 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002933 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002934 if (!translated_function_exists(name))
2935 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002936 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002937 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002938 return NULL;
2939 }
2940 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002941 }
2942 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002943 {
2944 char_u *p;
2945
2946 if ((p = get_expanded_name(name,
2947 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002948 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002949 PyErr_FORMAT(PyExc_ValueError,
2950 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002951 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002952 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002953
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002954 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2955 {
2956 char_u *np;
2957 size_t len = STRLEN(p) + 1;
2958
Bram Moolenaar51e14382019-05-25 20:21:28 +02002959 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002960 {
2961 vim_free(p);
2962 return NULL;
2963 }
2964 mch_memmove(np, "<SNR>", 5);
2965 mch_memmove(np + 5, p + 3, len - 3);
2966 vim_free(p);
2967 self->name = np;
2968 }
2969 else
2970 self->name = p;
2971 }
2972
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002973 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002974 self->argc = argc;
2975 self->argv = argv;
2976 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002977 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002978
2979 if (self->argv || self->self)
2980 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2981
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002982 return (PyObject *)(self);
2983}
2984
2985 static PyObject *
2986FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2987{
2988 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002989 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002990 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002991 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002992 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002993 typval_T selfdicttv;
2994 typval_T argstv;
2995 list_T *argslist = NULL;
2996 dict_T *selfdict = NULL;
2997 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002998 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002999 typval_T *argv = NULL;
3000 typval_T *curtv;
3001 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003002
Bram Moolenaar8110a092016-04-14 15:56:09 +02003003 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003004 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003005 selfdictObject = PyDict_GetItemString(kwargs, "self");
3006 if (selfdictObject != NULL)
3007 {
3008 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3009 return NULL;
3010 selfdict = selfdicttv.vval.v_dict;
3011 }
3012 argsObject = PyDict_GetItemString(kwargs, "args");
3013 if (argsObject != NULL)
3014 {
3015 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3016 {
3017 dict_unref(selfdict);
3018 return NULL;
3019 }
3020 argslist = argstv.vval.v_list;
3021
3022 argc = argslist->lv_len;
3023 if (argc != 0)
3024 {
3025 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003026 if (argv == NULL)
3027 {
3028 PyErr_NoMemory();
3029 dict_unref(selfdict);
3030 list_unref(argslist);
3031 return NULL;
3032 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003033 curtv = argv;
3034 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3035 copy_tv(&li->li_tv, curtv++);
3036 }
3037 list_unref(argslist);
3038 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003039 if (selfdict != NULL)
3040 {
3041 auto_rebind = FALSE;
3042 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3043 if (autoRebindObject != NULL)
3044 {
3045 auto_rebind = PyObject_IsTrue(autoRebindObject);
3046 if (auto_rebind == -1)
3047 {
3048 dict_unref(selfdict);
3049 list_unref(argslist);
3050 return NULL;
3051 }
3052 }
3053 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003054 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003055
Bram Moolenaar389a1792013-06-23 13:00:44 +02003056 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003057 {
3058 dict_unref(selfdict);
3059 while (argc--)
3060 clear_tv(&argv[argc]);
3061 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003062 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003063 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003064
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003065 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003066
Bram Moolenaar389a1792013-06-23 13:00:44 +02003067 PyMem_Free(name);
3068
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003069 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003070}
3071
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003072 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003073FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003074{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003075 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003076 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003077 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003078 for (i = 0; i < self->argc; ++i)
3079 clear_tv(&self->argv[i]);
3080 PyMem_Free(self->argv);
3081 dict_unref(self->self);
3082 if (self->argv || self->self)
3083 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003084
3085 DESTRUCTOR_FINISH(self);
3086}
3087
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003088static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003089 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003090 NULL
3091};
3092
3093 static PyObject *
3094FunctionDir(PyObject *self)
3095{
3096 return ObjectDir(self, FunctionAttrs);
3097}
3098
Bram Moolenaardb913952012-06-29 12:54:53 +02003099 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003100FunctionAttr(FunctionObject *self, char *name)
3101{
3102 list_T *list;
3103 int i;
3104 if (strcmp(name, "name") == 0)
3105 return PyString_FromString((char *)(self->name));
3106 else if (strcmp(name, "args") == 0)
3107 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003108 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003109 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003110
Bram Moolenaar8110a092016-04-14 15:56:09 +02003111 for (i = 0; i < self->argc; ++i)
3112 list_append_tv(list, &self->argv[i]);
3113 return NEW_LIST(list);
3114 }
3115 else if (strcmp(name, "self") == 0)
3116 return self->self == NULL
3117 ? AlwaysNone(NULL)
3118 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003119 else if (strcmp(name, "auto_rebind") == 0)
3120 return self->auto_rebind
3121 ? AlwaysTrue(NULL)
3122 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003123 else if (strcmp(name, "__members__") == 0)
3124 return ObjectDir(NULL, FunctionAttrs);
3125 return NULL;
3126}
3127
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003128/*
3129 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003130 *
3131 * "exported" should be set to true when it is needed to construct a partial
3132 * that may be stored in a variable (i.e. may be freed by Vim).
3133 */
3134 static void
3135set_partial(FunctionObject *self, partial_T *pt, int exported)
3136{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003137 int i;
3138
3139 pt->pt_name = self->name;
3140 if (self->argv)
3141 {
3142 pt->pt_argc = self->argc;
3143 if (exported)
3144 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003145 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003146 for (i = 0; i < pt->pt_argc; ++i)
3147 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3148 }
3149 else
3150 pt->pt_argv = self->argv;
3151 }
3152 else
3153 {
3154 pt->pt_argc = 0;
3155 pt->pt_argv = NULL;
3156 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003157 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003158 pt->pt_dict = self->self;
3159 if (exported && self->self)
3160 ++pt->pt_dict->dv_refcount;
3161 if (exported)
3162 pt->pt_name = vim_strsave(pt->pt_name);
3163 pt->pt_refcount = 1;
3164}
3165
3166 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003167FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003168{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003169 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003170 typval_T args;
3171 typval_T selfdicttv;
3172 typval_T rettv;
3173 dict_T *selfdict = NULL;
3174 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003175 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003176 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003177 partial_T pt;
3178 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003179
Bram Moolenaar8110a092016-04-14 15:56:09 +02003180 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003181 return NULL;
3182
3183 if (kwargs != NULL)
3184 {
3185 selfdictObject = PyDict_GetItemString(kwargs, "self");
3186 if (selfdictObject != NULL)
3187 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003188 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003189 {
3190 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003191 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003192 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003193 selfdict = selfdicttv.vval.v_dict;
3194 }
3195 }
3196
Bram Moolenaar8110a092016-04-14 15:56:09 +02003197 if (self->argv || self->self)
3198 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003199 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003200 set_partial(self, &pt, FALSE);
3201 pt_ptr = &pt;
3202 }
3203
Bram Moolenaar71700b82013-05-15 17:49:05 +02003204 Py_BEGIN_ALLOW_THREADS
3205 Python_Lock_Vim();
3206
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003207 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003208 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003209
3210 Python_Release_Vim();
3211 Py_END_ALLOW_THREADS
3212
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003213 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003214 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003215 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003216 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003217 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003218 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003219 }
3220 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003221 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003222
Bram Moolenaardb913952012-06-29 12:54:53 +02003223 clear_tv(&args);
3224 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003225 if (selfdict != NULL)
3226 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003227
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003228 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003229}
3230
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003231 static PyObject *
3232FunctionRepr(FunctionObject *self)
3233{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003234 PyObject *ret;
3235 garray_T repr_ga;
3236 int i;
3237 char_u *tofree = NULL;
3238 typval_T tv;
3239 char_u numbuf[NUMBUFLEN];
3240
3241 ga_init2(&repr_ga, (int)sizeof(char), 70);
3242 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3243 if (self->name)
3244 ga_concat(&repr_ga, self->name);
3245 else
3246 ga_concat(&repr_ga, (char_u *)"<NULL>");
3247 ga_append(&repr_ga, '\'');
3248 if (self->argv)
3249 {
3250 ga_concat(&repr_ga, (char_u *)", args=[");
3251 ++emsg_silent;
3252 for (i = 0; i < self->argc; i++)
3253 {
3254 if (i != 0)
3255 ga_concat(&repr_ga, (char_u *)", ");
3256 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3257 get_copyID()));
3258 vim_free(tofree);
3259 }
3260 --emsg_silent;
3261 ga_append(&repr_ga, ']');
3262 }
3263 if (self->self)
3264 {
3265 ga_concat(&repr_ga, (char_u *)", self=");
3266 tv.v_type = VAR_DICT;
3267 tv.vval.v_dict = self->self;
3268 ++emsg_silent;
3269 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3270 --emsg_silent;
3271 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003272 if (self->auto_rebind)
3273 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003274 }
3275 ga_append(&repr_ga, '>');
3276 ret = PyString_FromString((char *)repr_ga.ga_data);
3277 ga_clear(&repr_ga);
3278 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003279}
3280
Bram Moolenaardb913952012-06-29 12:54:53 +02003281static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003282 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3283 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003284};
3285
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003286/*
3287 * Options object
3288 */
3289
3290static PyTypeObject OptionsType;
3291
3292typedef int (*checkfun)(void *);
3293
3294typedef struct
3295{
3296 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003297 int opt_type;
3298 void *from;
3299 checkfun Check;
3300 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003301} OptionsObject;
3302
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003303 static int
3304dummy_check(void *arg UNUSED)
3305{
3306 return 0;
3307}
3308
3309 static PyObject *
3310OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3311{
3312 OptionsObject *self;
3313
Bram Moolenaar774267b2013-05-21 20:51:59 +02003314 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003315 if (self == NULL)
3316 return NULL;
3317
3318 self->opt_type = opt_type;
3319 self->from = from;
3320 self->Check = Check;
3321 self->fromObj = fromObj;
3322 if (fromObj)
3323 Py_INCREF(fromObj);
3324
3325 return (PyObject *)(self);
3326}
3327
3328 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003329OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003330{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003331 PyObject_GC_UnTrack((void *)(self));
3332 Py_XDECREF(self->fromObj);
3333 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003334}
3335
3336 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003337OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003338{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003339 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003340 return 0;
3341}
3342
3343 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003345{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003346 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003347 return 0;
3348}
3349
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003350 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003351OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003352{
3353 char_u *key;
3354 int flags;
3355 long numval;
3356 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003357 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003358
Bram Moolenaard6e39182013-05-21 18:30:34 +02003359 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003360 return NULL;
3361
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003362 if (!(key = StringToChars(keyObject, &todecref)))
3363 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003364
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003365 if (*key == NUL)
3366 {
3367 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003368 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003369 return NULL;
3370 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003371
3372 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003374
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003375 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003376
3377 if (flags == 0)
3378 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003379 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003380 return NULL;
3381 }
3382
3383 if (flags & SOPT_UNSET)
3384 {
3385 Py_INCREF(Py_None);
3386 return Py_None;
3387 }
3388 else if (flags & SOPT_BOOL)
3389 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003390 PyObject *ret;
3391 ret = numval ? Py_True : Py_False;
3392 Py_INCREF(ret);
3393 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003394 }
3395 else if (flags & SOPT_NUM)
3396 return PyInt_FromLong(numval);
3397 else if (flags & SOPT_STRING)
3398 {
3399 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003400 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003401 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003402 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003403 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003404 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003405 else
3406 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003407 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003408 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003409 return NULL;
3410 }
3411 }
3412 else
3413 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003414 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003415 return NULL;
3416 }
3417}
3418
3419 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003420OptionsContains(OptionsObject *self, PyObject *keyObject)
3421{
3422 char_u *key;
3423 PyObject *todecref;
3424
3425 if (!(key = StringToChars(keyObject, &todecref)))
3426 return -1;
3427
3428 if (*key == NUL)
3429 {
3430 Py_XDECREF(todecref);
3431 return 0;
3432 }
3433
3434 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3435 {
3436 Py_XDECREF(todecref);
3437 return 1;
3438 }
3439 else
3440 {
3441 Py_XDECREF(todecref);
3442 return 0;
3443 }
3444}
3445
3446typedef struct
3447{
3448 void *lastoption;
3449 int opt_type;
3450} optiterinfo_T;
3451
3452 static PyObject *
3453OptionsIterNext(optiterinfo_T **oii)
3454{
3455 char_u *name;
3456
3457 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3458 return PyString_FromString((char *)name);
3459
3460 return NULL;
3461}
3462
3463 static PyObject *
3464OptionsIter(OptionsObject *self)
3465{
3466 optiterinfo_T *oii;
3467
3468 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3469 {
3470 PyErr_NoMemory();
3471 return NULL;
3472 }
3473
3474 oii->opt_type = self->opt_type;
3475 oii->lastoption = NULL;
3476
3477 return IterNew(oii,
3478 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3479 NULL, NULL);
3480}
3481
3482 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003483set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003484{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003485 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003486
3487 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3488 {
3489 if (VimTryEnd())
3490 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003491 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003492 return FAIL;
3493 }
3494 return OK;
3495}
3496
3497 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003498set_option_value_for(
3499 char_u *key,
3500 int numval,
3501 char_u *stringval,
3502 int opt_flags,
3503 int opt_type,
3504 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003505{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003506 win_T *save_curwin = NULL;
3507 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003508 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003509 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003510
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003511 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003512 switch (opt_type)
3513 {
3514 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003515 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003516 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003517 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003518 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003519 if (VimTryEnd())
3520 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003521 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003522 return -1;
3523 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003524 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3525 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003526 break;
3527 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003528 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003529 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003530 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003531 break;
3532 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003533 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003534 break;
3535 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003536 if (set_ret == FAIL)
3537 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003538 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003539}
3540
3541 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003542OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003543{
3544 char_u *key;
3545 int flags;
3546 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003547 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003548 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003549
Bram Moolenaard6e39182013-05-21 18:30:34 +02003550 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003551 return -1;
3552
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003553 if (!(key = StringToChars(keyObject, &todecref)))
3554 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003555
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003556 if (*key == NUL)
3557 {
3558 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003559 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003560 return -1;
3561 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003562
3563 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003564 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003565
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003566 if (flags == 0)
3567 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003568 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003569 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003570 return -1;
3571 }
3572
3573 if (valObject == NULL)
3574 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003575 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003576 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003577 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003578 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003579 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003580 return -1;
3581 }
3582 else if (!(flags & SOPT_GLOBAL))
3583 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003584 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003585 N_("unable to unset option %s "
3586 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003587 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003588 return -1;
3589 }
3590 else
3591 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003592 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003593 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003594 return 0;
3595 }
3596 }
3597
Bram Moolenaard6e39182013-05-21 18:30:34 +02003598 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003599
3600 if (flags & SOPT_BOOL)
3601 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003602 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003603
Bram Moolenaarb983f752013-05-15 16:11:50 +02003604 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003605 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003606 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003607 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003608 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003609 }
3610 else if (flags & SOPT_NUM)
3611 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003612 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003613
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003614 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003615 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003616 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003617 return -1;
3618 }
3619
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003620 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003621 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003622 }
3623 else
3624 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003625 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003626 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003627
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003628 if ((val = StringToChars(valObject, &todecref2)))
3629 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003630 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003631 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003632 Py_XDECREF(todecref2);
3633 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003634 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003635 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003636 }
3637
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003638 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003639
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003640 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003641}
3642
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003643static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003644 0, // sq_length
3645 0, // sq_concat
3646 0, // sq_repeat
3647 0, // sq_item
3648 0, // sq_slice
3649 0, // sq_ass_item
3650 0, // sq_ass_slice
3651 (objobjproc) OptionsContains, // sq_contains
3652 0, // sq_inplace_concat
3653 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003654};
3655
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003656static PyMappingMethods OptionsAsMapping = {
3657 (lenfunc) NULL,
3658 (binaryfunc) OptionsItem,
3659 (objobjargproc) OptionsAssItem,
3660};
3661
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003662// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003663
3664typedef struct
3665{
3666 PyObject_HEAD
3667 tabpage_T *tab;
3668} TabPageObject;
3669
3670static PyObject *WinListNew(TabPageObject *tabObject);
3671
3672static PyTypeObject TabPageType;
3673
3674 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003675CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003676{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003677 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003678 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003679 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003680 return -1;
3681 }
3682
3683 return 0;
3684}
3685
3686 static PyObject *
3687TabPageNew(tabpage_T *tab)
3688{
3689 TabPageObject *self;
3690
3691 if (TAB_PYTHON_REF(tab))
3692 {
3693 self = TAB_PYTHON_REF(tab);
3694 Py_INCREF(self);
3695 }
3696 else
3697 {
3698 self = PyObject_NEW(TabPageObject, &TabPageType);
3699 if (self == NULL)
3700 return NULL;
3701 self->tab = tab;
3702 TAB_PYTHON_REF(tab) = self;
3703 }
3704
3705 return (PyObject *)(self);
3706}
3707
3708 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003709TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003710{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003711 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3712 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003713
3714 DESTRUCTOR_FINISH(self);
3715}
3716
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003717static char *TabPageAttrs[] = {
3718 "windows", "number", "vars", "window", "valid",
3719 NULL
3720};
3721
3722 static PyObject *
3723TabPageDir(PyObject *self)
3724{
3725 return ObjectDir(self, TabPageAttrs);
3726}
3727
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003728 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003729TabPageAttrValid(TabPageObject *self, char *name)
3730{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003731 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003732
3733 if (strcmp(name, "valid") != 0)
3734 return NULL;
3735
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003736 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3737 Py_INCREF(ret);
3738 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003739}
3740
3741 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003742TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003743{
3744 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003745 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003746 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003747 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003748 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003749 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003750 else if (strcmp(name, "window") == 0)
3751 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003752 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003753 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003754 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003755 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003756 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003757 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003758 else if (strcmp(name, "__members__") == 0)
3759 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003760 return NULL;
3761}
3762
3763 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003764TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003765{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003766 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003767 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003768 else
3769 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003770 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771
3772 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003773 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3774 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003775 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003776 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003777 }
3778}
3779
3780static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003781 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003782 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3783 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003784};
3785
3786/*
3787 * Window list object
3788 */
3789
3790static PyTypeObject TabListType;
3791static PySequenceMethods TabListAsSeq;
3792
3793typedef struct
3794{
3795 PyObject_HEAD
3796} TabListObject;
3797
3798 static PyInt
3799TabListLength(PyObject *self UNUSED)
3800{
3801 tabpage_T *tp = first_tabpage;
3802 PyInt n = 0;
3803
3804 while (tp != NULL)
3805 {
3806 ++n;
3807 tp = tp->tp_next;
3808 }
3809
3810 return n;
3811}
3812
3813 static PyObject *
3814TabListItem(PyObject *self UNUSED, PyInt n)
3815{
3816 tabpage_T *tp;
3817
3818 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3819 if (n == 0)
3820 return TabPageNew(tp);
3821
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003822 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003823 return NULL;
3824}
3825
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003826/*
3827 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003828 */
3829
3830typedef struct
3831{
3832 PyObject_HEAD
3833 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003834 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003835} WindowObject;
3836
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003837static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003838
3839 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003840CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003842 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003843 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003844 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003845 return -1;
3846 }
3847
3848 return 0;
3849}
3850
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003851 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003852WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003853{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003854 /*
3855 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003856 * If we add a "w_python*_ref" field to the win_T structure,
3857 * then we can get at it in win_free() in vim. We then
3858 * need to create only ONE Python object per window - if
3859 * we try to create a second, just INCREF the existing one
3860 * and return it. The (single) Python object referring to
3861 * the window is stored in "w_python*_ref".
3862 * On a win_free() we set the Python object's win_T* field
3863 * to an invalid value. We trap all uses of a window
3864 * object, and reject them if the win_T* field is invalid.
3865 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003866 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003867 * w_python_ref and w_python3_ref fields respectively.
3868 */
3869
3870 WindowObject *self;
3871
3872 if (WIN_PYTHON_REF(win))
3873 {
3874 self = WIN_PYTHON_REF(win);
3875 Py_INCREF(self);
3876 }
3877 else
3878 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003879 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003880 if (self == NULL)
3881 return NULL;
3882 self->win = win;
3883 WIN_PYTHON_REF(win) = self;
3884 }
3885
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003886 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3887
Bram Moolenaar971db462013-05-12 18:44:48 +02003888 return (PyObject *)(self);
3889}
3890
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003891 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003892WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003893{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003894 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003895 if (self->win && self->win != INVALID_WINDOW_VALUE)
3896 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003897 Py_XDECREF(((PyObject *)(self->tabObject)));
3898 PyObject_GC_Del((void *)(self));
3899}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003900
Bram Moolenaar774267b2013-05-21 20:51:59 +02003901 static int
3902WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3903{
3904 Py_VISIT(((PyObject *)(self->tabObject)));
3905 return 0;
3906}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003907
Bram Moolenaar774267b2013-05-21 20:51:59 +02003908 static int
3909WindowClear(WindowObject *self)
3910{
3911 Py_CLEAR(self->tabObject);
3912 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003913}
3914
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003915 static win_T *
3916get_firstwin(TabPageObject *tabObject)
3917{
3918 if (tabObject)
3919 {
3920 if (CheckTabPage(tabObject))
3921 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003922 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003923 else if (tabObject->tab == curtab)
3924 return firstwin;
3925 else
3926 return tabObject->tab->tp_firstwin;
3927 }
3928 else
3929 return firstwin;
3930}
Bram Moolenaare950f992018-06-10 13:55:55 +02003931
3932// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003933static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003934 "buffer",
3935 "cursor",
3936 "height",
3937 "row",
3938 "width",
3939 "col",
3940 "vars",
3941 "options",
3942 "number",
3943 "tabpage",
3944 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003945 NULL
3946};
3947
3948 static PyObject *
3949WindowDir(PyObject *self)
3950{
3951 return ObjectDir(self, WindowAttrs);
3952}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003953
Bram Moolenaar971db462013-05-12 18:44:48 +02003954 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003955WindowAttrValid(WindowObject *self, char *name)
3956{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003957 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003958
3959 if (strcmp(name, "valid") != 0)
3960 return NULL;
3961
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003962 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3963 Py_INCREF(ret);
3964 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003965}
3966
3967 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003968WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003969{
3970 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003971 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003972 else if (strcmp(name, "cursor") == 0)
3973 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003974 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003975
3976 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3977 }
3978 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003979 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003980 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003981 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003982 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003983 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003984 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003985 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003986 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003987 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003988 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003989 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3990 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003991 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003992 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003993 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003994 return NULL;
3995 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003997 }
3998 else if (strcmp(name, "tabpage") == 0)
3999 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004000 Py_INCREF(self->tabObject);
4001 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004002 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004003 else if (strcmp(name, "__members__") == 0)
4004 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004005 else
4006 return NULL;
4007}
4008
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004009 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004010WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004012 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004013 return -1;
4014
4015 if (strcmp(name, "buffer") == 0)
4016 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004017 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018 return -1;
4019 }
4020 else if (strcmp(name, "cursor") == 0)
4021 {
4022 long lnum;
4023 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004024
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004025 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026 return -1;
4027
Bram Moolenaard6e39182013-05-21 18:30:34 +02004028 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004029 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004030 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004031 return -1;
4032 }
4033
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004034 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004035 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036 return -1;
4037
Bram Moolenaard6e39182013-05-21 18:30:34 +02004038 self->win->w_cursor.lnum = lnum;
4039 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004040 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004042 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004043 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004045 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046 return 0;
4047 }
4048 else if (strcmp(name, "height") == 0)
4049 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004050 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 win_T *savewin;
4052
Bram Moolenaardee2e312013-06-23 16:35:47 +02004053 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 return -1;
4055
4056#ifdef FEAT_GUI
4057 need_mouse_correct = TRUE;
4058#endif
4059 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004060 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004061
4062 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004063 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004064 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004065 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066 return -1;
4067
4068 return 0;
4069 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070 else if (strcmp(name, "width") == 0)
4071 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004072 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073 win_T *savewin;
4074
Bram Moolenaardee2e312013-06-23 16:35:47 +02004075 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 return -1;
4077
4078#ifdef FEAT_GUI
4079 need_mouse_correct = TRUE;
4080#endif
4081 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004082 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004083
4084 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004085 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004087 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 return -1;
4089
4090 return 0;
4091 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 else
4093 {
4094 PyErr_SetString(PyExc_AttributeError, name);
4095 return -1;
4096 }
4097}
4098
4099 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004100WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004102 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004103 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004104 else
4105 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004106 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107
Bram Moolenaar6d216452013-05-12 19:00:41 +02004108 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004109 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004110 (self));
4111 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004112 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 }
4114}
4115
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004116static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004117 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004118 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4119 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004120};
4121
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004123 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004125
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004126static PyTypeObject WinListType;
4127static PySequenceMethods WinListAsSeq;
4128
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004129typedef struct
4130{
4131 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004132 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004133} WinListObject;
4134
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004135 static PyObject *
4136WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004138 WinListObject *self;
4139
4140 self = PyObject_NEW(WinListObject, &WinListType);
4141 self->tabObject = tabObject;
4142 Py_INCREF(tabObject);
4143
4144 return (PyObject *)(self);
4145}
4146
4147 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004148WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004149{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004150 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004151
4152 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004153 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004154 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004155 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004156
4157 DESTRUCTOR_FINISH(self);
4158}
4159
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004160 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004161WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004162{
4163 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164 PyInt n = 0;
4165
Bram Moolenaard6e39182013-05-21 18:30:34 +02004166 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004167 return -1;
4168
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004169 while (w != NULL)
4170 {
4171 ++n;
4172 w = W_NEXT(w);
4173 }
4174
4175 return n;
4176}
4177
4178 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004179WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004180{
4181 win_T *w;
4182
Bram Moolenaard6e39182013-05-21 18:30:34 +02004183 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004184 return NULL;
4185
4186 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004187 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004188 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004189
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004190 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004191 return NULL;
4192}
4193
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004194/*
4195 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004196 *
4197 * The result is in allocated memory. All internal nulls are replaced by
4198 * newline characters. It is an error for the string to contain newline
4199 * characters.
4200 *
4201 * On errors, the Python exception data is set, and NULL is returned.
4202 */
4203 static char *
4204StringToLine(PyObject *obj)
4205{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004206 char *str;
4207 char *save;
4208 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004209 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004210 PyInt i;
4211 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004213 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004215 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4216 || str == NULL)
4217 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004219 else if (PyUnicode_Check(obj))
4220 {
4221 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4222 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004223
Bram Moolenaardaa27022013-06-24 22:33:30 +02004224 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004225 || str == NULL)
4226 {
4227 Py_DECREF(bytes);
4228 return NULL;
4229 }
4230 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004231 else
4232 {
4233#if PY_MAJOR_VERSION < 3
4234 PyErr_FORMAT(PyExc_TypeError,
4235 N_("expected str() or unicode() instance, but got %s"),
4236 Py_TYPE_NAME(obj));
4237#else
4238 PyErr_FORMAT(PyExc_TypeError,
4239 N_("expected bytes() or str() instance, but got %s"),
4240 Py_TYPE_NAME(obj));
4241#endif
4242 return NULL;
4243 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004244
4245 /*
4246 * Error checking: String must not contain newlines, as we
4247 * are replacing a single line, and we must replace it with
4248 * a single line.
4249 * A trailing newline is removed, so that append(f.readlines()) works.
4250 */
4251 p = memchr(str, '\n', len);
4252 if (p != NULL)
4253 {
4254 if (p == str + len - 1)
4255 --len;
4256 else
4257 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004258 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004259 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004260 return NULL;
4261 }
4262 }
4263
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004264 /*
4265 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004266 * newline characters, as is the vim convention.
4267 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004268 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269 if (save == NULL)
4270 {
4271 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004272 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004273 return NULL;
4274 }
4275
4276 for (i = 0; i < len; ++i)
4277 {
4278 if (str[i] == '\0')
4279 save[i] = '\n';
4280 else
4281 save[i] = str[i];
4282 }
4283
4284 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004285 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004286
4287 return save;
4288}
4289
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004290/*
4291 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 * in Vim format (1-based). The line is returned as a Python
4293 * string object.
4294 */
4295 static PyObject *
4296GetBufferLine(buf_T *buf, PyInt n)
4297{
4298 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4299}
4300
4301
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004302/*
4303 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004304 * are in Vim format (1-based). The range is from lo up to, but not
4305 * including, hi. The list is returned as a Python list of string objects.
4306 */
4307 static PyObject *
4308GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4309{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004310 PyInt i;
4311 PyInt n = hi - lo;
4312 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004313
4314 if (list == NULL)
4315 return NULL;
4316
4317 for (i = 0; i < n; ++i)
4318 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004319 PyObject *string = LineToString(
4320 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004322 // Error check - was the Python string creation OK?
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004323 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004324 {
4325 Py_DECREF(list);
4326 return NULL;
4327 }
4328
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004329 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004330 }
4331
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004332 // The ownership of the Python list is passed to the caller (ie,
4333 // the caller should Py_DECREF() the object when it is finished
4334 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004335
4336 return list;
4337}
4338
4339/*
4340 * Check if deleting lines made the cursor position invalid.
4341 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4342 * deleted).
4343 */
4344 static void
4345py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4346{
4347 if (curwin->w_cursor.lnum >= lo)
4348 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004349 // Adjust the cursor position if it's in/after the changed
4350 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004351 if (curwin->w_cursor.lnum >= hi)
4352 {
4353 curwin->w_cursor.lnum += extra;
4354 check_cursor_col();
4355 }
4356 else if (extra < 0)
4357 {
4358 curwin->w_cursor.lnum = lo;
4359 check_cursor();
4360 }
4361 else
4362 check_cursor_col();
4363 changed_cline_bef_curs();
4364 }
4365 invalidate_botline();
4366}
4367
Bram Moolenaar19e60942011-06-19 00:27:51 +02004368/*
4369 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004370 * in Vim format (1-based). The replacement line is given as
4371 * a Python string object. The object is checked for validity
4372 * and correct format. Errors are returned as a value of FAIL.
4373 * The return value is OK on success.
4374 * If OK is returned and len_change is not NULL, *len_change
4375 * is set to the change in the buffer length.
4376 */
4377 static int
4378SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4379{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004380 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004381 win_T *save_curwin = NULL;
4382 tabpage_T *save_curtab = NULL;
4383
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004384 // First of all, we check the type of the supplied Python object.
4385 // There are three cases:
4386 // 1. NULL, or None - this is a deletion.
4387 // 2. A string - this is a replacement.
4388 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004389 if (line == Py_None || line == NULL)
4390 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004391 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004392 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004394 VimTryStart();
4395
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004397 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004398 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004399 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004400 else
4401 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004402 if (buf == curbuf && (save_curwin != NULL
4403 || save_curbuf.br_buf == NULL))
4404 // Using an existing window for the buffer, adjust the cursor
4405 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004407 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004408 // Only adjust marks if we managed to switch to a window that
4409 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004410 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004411 }
4412
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004413 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004414
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004415 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416 return FAIL;
4417
4418 if (len_change)
4419 *len_change = -1;
4420
4421 return OK;
4422 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004423 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004424 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004425 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426
4427 if (save == NULL)
4428 return FAIL;
4429
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004430 VimTryStart();
4431
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004432 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004433 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004434 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435
4436 if (u_savesub((linenr_T)n) == FAIL)
4437 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004438 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004439 vim_free(save);
4440 }
4441 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4442 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004443 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004444 vim_free(save);
4445 }
4446 else
4447 changed_bytes((linenr_T)n, 0);
4448
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004449 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004450
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004451 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004452 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453 check_cursor_col();
4454
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004455 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456 return FAIL;
4457
4458 if (len_change)
4459 *len_change = 0;
4460
4461 return OK;
4462 }
4463 else
4464 {
4465 PyErr_BadArgument();
4466 return FAIL;
4467 }
4468}
4469
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004470/*
4471 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004472 * Vim format (1-based). The range is from lo up to, but not including, hi.
4473 * The replacement lines are given as a Python list of string objects. The
4474 * list is checked for validity and correct format. Errors are returned as a
4475 * value of FAIL. The return value is OK on success.
4476 * If OK is returned and len_change is not NULL, *len_change
4477 * is set to the change in the buffer length.
4478 */
4479 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004480SetBufferLineList(
4481 buf_T *buf,
4482 PyInt lo,
4483 PyInt hi,
4484 PyObject *list,
4485 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004486{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004487 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004488 win_T *save_curwin = NULL;
4489 tabpage_T *save_curtab = NULL;
4490
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004491 // First of all, we check the type of the supplied Python object.
4492 // There are three cases:
4493 // 1. NULL, or None - this is a deletion.
4494 // 2. A list - this is a replacement.
4495 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004496 if (list == Py_None || list == NULL)
4497 {
4498 PyInt i;
4499 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004500
4501 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004502 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004503 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504
4505 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004506 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004507 else
4508 {
4509 for (i = 0; i < n; ++i)
4510 {
4511 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4512 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004513 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004514 break;
4515 }
4516 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004517 if (buf == curbuf && (save_curwin != NULL
4518 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004519 // Using an existing window for the buffer, adjust the cursor
4520 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004521 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004522 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004523 // Only adjust marks if we managed to switch to a window that
4524 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004525 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004526 }
4527
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004528 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004529
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004530 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004531 return FAIL;
4532
4533 if (len_change)
4534 *len_change = -n;
4535
4536 return OK;
4537 }
4538 else if (PyList_Check(list))
4539 {
4540 PyInt i;
4541 PyInt new_len = PyList_Size(list);
4542 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004543 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004544 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004545
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004546 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004547 array = NULL;
4548 else
4549 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004550 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551 if (array == NULL)
4552 {
4553 PyErr_NoMemory();
4554 return FAIL;
4555 }
4556 }
4557
4558 for (i = 0; i < new_len; ++i)
4559 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004560 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004561
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004562 if (!(line = PyList_GetItem(list, i)) ||
4563 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004564 {
4565 while (i)
4566 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004567 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004568 return FAIL;
4569 }
4570 }
4571
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004572 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004573 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004574
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004575 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004576 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004577
4578 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004579 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004580
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004581 // If the size of the range is reducing (ie, new_len < old_len) we
4582 // need to delete some old_len. We do this at the start, by
4583 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004584 if (!PyErr_Occurred())
4585 {
4586 for (i = 0; i < old_len - new_len; ++i)
4587 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4588 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004589 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004590 break;
4591 }
4592 extra -= i;
4593 }
4594
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004595 // For as long as possible, replace the existing old_len with the
4596 // new old_len. This is a more efficient operation, as it requires
4597 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004598 if (!PyErr_Occurred())
4599 {
4600 for (i = 0; i < old_len && i < new_len; ++i)
4601 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4602 == FAIL)
4603 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004604 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004605 break;
4606 }
4607 }
4608 else
4609 i = 0;
4610
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004611 // Now we may need to insert the remaining new old_len. If we do, we
4612 // must free the strings as we finish with them (we can't pass the
4613 // responsibility to vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004614 if (!PyErr_Occurred())
4615 {
4616 while (i < new_len)
4617 {
4618 if (ml_append((linenr_T)(lo + i - 1),
4619 (char_u *)array[i], 0, FALSE) == FAIL)
4620 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004621 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 break;
4623 }
4624 vim_free(array[i]);
4625 ++i;
4626 ++extra;
4627 }
4628 }
4629
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004630 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004631 while (i < new_len)
4632 {
4633 vim_free(array[i]);
4634 ++i;
4635 }
4636
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004637 // Free the array of old_len. All of its contents have now
4638 // been dealt with (either freed, or the responsibility passed
4639 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004640 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004641
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004642 // Adjust marks. Invalidate any which lie in the
4643 // changed range, and move any in the remainder of the buffer.
4644 // Only adjust marks if we managed to switch to a window that holds
4645 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004646 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004647 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004648 (long)MAXLNUM, (long)extra);
4649 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4650
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004651 if (buf == curbuf && (save_curwin != NULL
4652 || save_curbuf.br_buf == NULL))
4653 // Using an existing window for the buffer, adjust the cursor
4654 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004655 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4656
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004657 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004658 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004659
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004660 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004661 return FAIL;
4662
4663 if (len_change)
4664 *len_change = new_len - old_len;
4665
4666 return OK;
4667 }
4668 else
4669 {
4670 PyErr_BadArgument();
4671 return FAIL;
4672 }
4673}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004674
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004675/*
4676 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677 * The line number is in Vim format (1-based). The lines to be inserted are
4678 * given as a Python list of string objects or as a single string. The lines
4679 * to be added are checked for validity and correct format. Errors are
4680 * returned as a value of FAIL. The return value is OK on success.
4681 * If OK is returned and len_change is not NULL, *len_change
4682 * is set to the change in the buffer length.
4683 */
4684 static int
4685InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4686{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004687 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004688 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004689 tabpage_T *save_curtab = NULL;
4690
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004691 // First of all, we check the type of the supplied Python object.
4692 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004693 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004695 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004696
4697 if (str == NULL)
4698 return FAIL;
4699
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004700 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004701 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004702 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004703
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004704 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004705 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004707 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004708 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004709 // Only adjust marks if we managed to switch to a window that
4710 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711 appended_lines_mark((linenr_T)n, 1L);
4712
4713 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004714 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004715 update_screen(VALID);
4716
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004717 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004718 return FAIL;
4719
4720 if (len_change)
4721 *len_change = 1;
4722
4723 return OK;
4724 }
4725 else if (PyList_Check(lines))
4726 {
4727 PyInt i;
4728 PyInt size = PyList_Size(lines);
4729 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004731 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732 if (array == NULL)
4733 {
4734 PyErr_NoMemory();
4735 return FAIL;
4736 }
4737
4738 for (i = 0; i < size; ++i)
4739 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004740 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004742 if (!(line = PyList_GetItem(lines, i)) ||
4743 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004744 {
4745 while (i)
4746 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004747 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004748 return FAIL;
4749 }
4750 }
4751
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004753 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004754 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004755
4756 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004757 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004758 else
4759 {
4760 for (i = 0; i < size; ++i)
4761 {
4762 if (ml_append((linenr_T)(n + i),
4763 (char_u *)array[i], 0, FALSE) == FAIL)
4764 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004765 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004766
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004767 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004768 while (i < size)
4769 vim_free(array[i++]);
4770
4771 break;
4772 }
4773 vim_free(array[i]);
4774 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004775 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004776 // Only adjust marks if we managed to switch to a window that
4777 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004778 appended_lines_mark((linenr_T)n, (long)i);
4779 }
4780
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004781 // Free the array of lines. All of its contents have now
4782 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004783 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004784 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004785
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 update_screen(VALID);
4787
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004788 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004789 return FAIL;
4790
4791 if (len_change)
4792 *len_change = size;
4793
4794 return OK;
4795 }
4796 else
4797 {
4798 PyErr_BadArgument();
4799 return FAIL;
4800 }
4801}
4802
4803/*
4804 * Common routines for buffers and line ranges
4805 * -------------------------------------------
4806 */
4807
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004808typedef struct
4809{
4810 PyObject_HEAD
4811 buf_T *buf;
4812} BufferObject;
4813
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004814 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004815CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004816{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004817 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004818 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004819 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004820 return -1;
4821 }
4822
4823 return 0;
4824}
4825
4826 static PyObject *
4827RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4828{
4829 if (CheckBuffer(self))
4830 return NULL;
4831
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004832 if (end == -1)
4833 end = self->buf->b_ml.ml_line_count;
4834
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004835 if (n < 0)
4836 n += end - start + 1;
4837
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004838 if (n < 0 || n > end - start)
4839 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004840 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004841 return NULL;
4842 }
4843
4844 return GetBufferLine(self->buf, n+start);
4845}
4846
4847 static PyObject *
4848RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4849{
4850 PyInt size;
4851
4852 if (CheckBuffer(self))
4853 return NULL;
4854
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004855 if (end == -1)
4856 end = self->buf->b_ml.ml_line_count;
4857
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004858 size = end - start + 1;
4859
4860 if (lo < 0)
4861 lo = 0;
4862 else if (lo > size)
4863 lo = size;
4864 if (hi < 0)
4865 hi = 0;
4866 if (hi < lo)
4867 hi = lo;
4868 else if (hi > size)
4869 hi = size;
4870
4871 return GetBufferLineList(self->buf, lo+start, hi+start);
4872}
4873
4874 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004875RBAsItem(
4876 BufferObject *self,
4877 PyInt n,
4878 PyObject *valObject,
4879 PyInt start,
4880 PyInt end,
4881 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004882{
4883 PyInt len_change;
4884
4885 if (CheckBuffer(self))
4886 return -1;
4887
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004888 if (end == -1)
4889 end = self->buf->b_ml.ml_line_count;
4890
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004891 if (n < 0)
4892 n += end - start + 1;
4893
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004894 if (n < 0 || n > end - start)
4895 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004896 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004897 return -1;
4898 }
4899
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004900 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004901 return -1;
4902
4903 if (new_end)
4904 *new_end = end + len_change;
4905
4906 return 0;
4907}
4908
Bram Moolenaar19e60942011-06-19 00:27:51 +02004909 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004910RBAsSlice(
4911 BufferObject *self,
4912 PyInt lo,
4913 PyInt hi,
4914 PyObject *valObject,
4915 PyInt start,
4916 PyInt end,
4917 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004918{
4919 PyInt size;
4920 PyInt len_change;
4921
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004922 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004923 if (CheckBuffer(self))
4924 return -1;
4925
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004926 if (end == -1)
4927 end = self->buf->b_ml.ml_line_count;
4928
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004929 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004930 size = end - start + 1;
4931
4932 if (lo < 0)
4933 lo = 0;
4934 else if (lo > size)
4935 lo = size;
4936 if (hi < 0)
4937 hi = 0;
4938 if (hi < lo)
4939 hi = lo;
4940 else if (hi > size)
4941 hi = size;
4942
4943 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004944 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004945 return -1;
4946
4947 if (new_end)
4948 *new_end = end + len_change;
4949
4950 return 0;
4951}
4952
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004953
4954 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004955RBAppend(
4956 BufferObject *self,
4957 PyObject *args,
4958 PyInt start,
4959 PyInt end,
4960 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004961{
4962 PyObject *lines;
4963 PyInt len_change;
4964 PyInt max;
4965 PyInt n;
4966
4967 if (CheckBuffer(self))
4968 return NULL;
4969
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004970 if (end == -1)
4971 end = self->buf->b_ml.ml_line_count;
4972
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004973 max = n = end - start + 1;
4974
4975 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4976 return NULL;
4977
4978 if (n < 0 || n > max)
4979 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004980 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004981 return NULL;
4982 }
4983
4984 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4985 return NULL;
4986
4987 if (new_end)
4988 *new_end = end + len_change;
4989
4990 Py_INCREF(Py_None);
4991 return Py_None;
4992}
4993
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004994// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004995
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004996static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004997static PySequenceMethods RangeAsSeq;
4998static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004999
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005000typedef struct
5001{
5002 PyObject_HEAD
5003 BufferObject *buf;
5004 PyInt start;
5005 PyInt end;
5006} RangeObject;
5007
5008 static PyObject *
5009RangeNew(buf_T *buf, PyInt start, PyInt end)
5010{
5011 BufferObject *bufr;
5012 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005013 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005014 if (self == NULL)
5015 return NULL;
5016
5017 bufr = (BufferObject *)BufferNew(buf);
5018 if (bufr == NULL)
5019 {
5020 Py_DECREF(self);
5021 return NULL;
5022 }
5023 Py_INCREF(bufr);
5024
5025 self->buf = bufr;
5026 self->start = start;
5027 self->end = end;
5028
5029 return (PyObject *)(self);
5030}
5031
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005032 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005033RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005034{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005035 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005036 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005037 PyObject_GC_Del((void *)(self));
5038}
5039
5040 static int
5041RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5042{
5043 Py_VISIT(((PyObject *)(self->buf)));
5044 return 0;
5045}
5046
5047 static int
5048RangeClear(RangeObject *self)
5049{
5050 Py_CLEAR(self->buf);
5051 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005052}
5053
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005054 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005055RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005056{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005057 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005058 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005059 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060
Bram Moolenaard6e39182013-05-21 18:30:34 +02005061 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005062}
5063
5064 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005066{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005068}
5069
5070 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005071RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005072{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005073 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005074}
5075
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005076static char *RangeAttrs[] = {
5077 "start", "end",
5078 NULL
5079};
5080
5081 static PyObject *
5082RangeDir(PyObject *self)
5083{
5084 return ObjectDir(self, RangeAttrs);
5085}
5086
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091}
5092
5093 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005097 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5098 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005099 else
5100 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005101 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005102
5103 if (name == NULL)
5104 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005105
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005106 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005107 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005108 }
5109}
5110
5111static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005112 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005113 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005114 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5115 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116};
5117
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005118static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005119static PySequenceMethods BufferAsSeq;
5120static PyMappingMethods BufferAsMapping;
5121
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005122 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005123BufferNew(buf_T *buf)
5124{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005125 /*
5126 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005127 * If we add a "b_python*_ref" field to the buf_T structure,
5128 * then we can get at it in buf_freeall() in vim. We then
5129 * need to create only ONE Python object per buffer - if
5130 * we try to create a second, just INCREF the existing one
5131 * and return it. The (single) Python object referring to
5132 * the buffer is stored in "b_python*_ref".
5133 * Question: what to do on a buf_freeall(). We'll probably
5134 * have to either delete the Python object (DECREF it to
5135 * zero - a bad idea, as it leaves dangling refs!) or
5136 * set the buf_T * value to an invalid value (-1?), which
5137 * means we need checks in all access functions... Bah.
5138 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005140 * b_python_ref and b_python3_ref fields respectively.
5141 */
5142
5143 BufferObject *self;
5144
5145 if (BUF_PYTHON_REF(buf) != NULL)
5146 {
5147 self = BUF_PYTHON_REF(buf);
5148 Py_INCREF(self);
5149 }
5150 else
5151 {
5152 self = PyObject_NEW(BufferObject, &BufferType);
5153 if (self == NULL)
5154 return NULL;
5155 self->buf = buf;
5156 BUF_PYTHON_REF(buf) = self;
5157 }
5158
5159 return (PyObject *)(self);
5160}
5161
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005162 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005163BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005164{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005165 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5166 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005167
5168 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005169}
5170
Bram Moolenaar971db462013-05-12 18:44:48 +02005171 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005173{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005174 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005176 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005177
Bram Moolenaard6e39182013-05-21 18:30:34 +02005178 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005179}
5180
5181 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005182BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005183{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005184 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005185}
5186
5187 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005188BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005189{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005190 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005191}
5192
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005193static char *BufferAttrs[] = {
5194 "name", "number", "vars", "options", "valid",
5195 NULL
5196};
5197
5198 static PyObject *
5199BufferDir(PyObject *self)
5200{
5201 return ObjectDir(self, BufferAttrs);
5202}
5203
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005204 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005205BufferAttrValid(BufferObject *self, char *name)
5206{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005207 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005208
5209 if (strcmp(name, "valid") != 0)
5210 return NULL;
5211
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005212 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5213 Py_INCREF(ret);
5214 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005215}
5216
5217 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005218BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005219{
5220 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005221 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005222 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005223 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005224 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005225 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005226 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005227 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005228 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5229 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005230 else if (strcmp(name, "__members__") == 0)
5231 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 else
5233 return NULL;
5234}
5235
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005236 static int
5237BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5238{
5239 if (CheckBuffer(self))
5240 return -1;
5241
5242 if (strcmp(name, "name") == 0)
5243 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005244 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005245 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005246 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005247 PyObject *todecref;
5248
5249 if (!(val = StringToChars(valObject, &todecref)))
5250 return -1;
5251
5252 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005253 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005254 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005255 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005256 aucmd_restbuf(&aco);
5257 Py_XDECREF(todecref);
5258 if (VimTryEnd())
5259 return -1;
5260
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005261 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005262 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005263 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005264 return -1;
5265 }
5266 return 0;
5267 }
5268 else
5269 {
5270 PyErr_SetString(PyExc_AttributeError, name);
5271 return -1;
5272 }
5273}
5274
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005275 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005276BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005277{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005278 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005279}
5280
5281 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005282BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005283{
5284 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005285 char_u *pmark;
5286 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005287 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005288 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005289
Bram Moolenaard6e39182013-05-21 18:30:34 +02005290 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005291 return NULL;
5292
Bram Moolenaar389a1792013-06-23 13:00:44 +02005293 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005294 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005295
Bram Moolenaar389a1792013-06-23 13:00:44 +02005296 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005297 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005298 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005299 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005300 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005301 return NULL;
5302 }
5303
5304 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005305
5306 Py_XDECREF(todecref);
5307
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005308 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005309 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005310 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005311 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005312 if (VimTryEnd())
5313 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005314
5315 if (posp == NULL)
5316 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005317 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005318 return NULL;
5319 }
5320
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321 if (posp->lnum <= 0)
5322 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005323 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005324 Py_INCREF(Py_None);
5325 return Py_None;
5326 }
5327
5328 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5329}
5330
5331 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005332BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005333{
5334 PyInt start;
5335 PyInt end;
5336
Bram Moolenaard6e39182013-05-21 18:30:34 +02005337 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005338 return NULL;
5339
5340 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5341 return NULL;
5342
Bram Moolenaard6e39182013-05-21 18:30:34 +02005343 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005344}
5345
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005346 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005347BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005348{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005349 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005350 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005351 else
5352 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005353 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005354
5355 if (name == NULL)
5356 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005357
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005358 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005359 }
5360}
5361
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005362static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005363 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005364 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005365 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005366 {"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 +02005367 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5368 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005369};
5370
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005371/*
5372 * Buffer list object - Implementation
5373 */
5374
5375static PyTypeObject BufMapType;
5376
5377typedef struct
5378{
5379 PyObject_HEAD
5380} BufMapObject;
5381
5382 static PyInt
5383BufMapLength(PyObject *self UNUSED)
5384{
5385 buf_T *b = firstbuf;
5386 PyInt n = 0;
5387
5388 while (b)
5389 {
5390 ++n;
5391 b = b->b_next;
5392 }
5393
5394 return n;
5395}
5396
5397 static PyObject *
5398BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5399{
5400 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005401 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005402
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005403 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005404 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005405
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005406 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005407
5408 if (b)
5409 return BufferNew(b);
5410 else
5411 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005412 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005413 return NULL;
5414 }
5415}
5416
5417 static void
5418BufMapIterDestruct(PyObject *buffer)
5419{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005420 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005421 if (buffer)
5422 {
5423 Py_DECREF(buffer);
5424 }
5425}
5426
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005427 static int
5428BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5429{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005430 if (buffer)
5431 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005432 return 0;
5433}
5434
5435 static int
5436BufMapIterClear(PyObject **buffer)
5437{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005438 if (*buffer)
5439 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005440 return 0;
5441}
5442
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005443 static PyObject *
5444BufMapIterNext(PyObject **buffer)
5445{
5446 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005447 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005448
5449 if (!*buffer)
5450 return NULL;
5451
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005452 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005453
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005454 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005455 {
5456 *buffer = NULL;
5457 return NULL;
5458 }
5459
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005460 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005461 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005462 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005463 return NULL;
5464 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005465 // Do not increment reference: we no longer hold it (decref), but whoever
5466 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005467 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005468}
5469
5470 static PyObject *
5471BufMapIter(PyObject *self UNUSED)
5472{
5473 PyObject *buffer;
5474
5475 buffer = BufferNew(firstbuf);
5476 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005477 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5478 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005479}
5480
5481static PyMappingMethods BufMapAsMapping = {
5482 (lenfunc) BufMapLength,
5483 (binaryfunc) BufMapItem,
5484 (objobjargproc) 0,
5485};
5486
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005487// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005488
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005489static char *CurrentAttrs[] = {
5490 "buffer", "window", "line", "range", "tabpage",
5491 NULL
5492};
5493
5494 static PyObject *
5495CurrentDir(PyObject *self)
5496{
5497 return ObjectDir(self, CurrentAttrs);
5498}
5499
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005500 static PyObject *
5501CurrentGetattr(PyObject *self UNUSED, char *name)
5502{
5503 if (strcmp(name, "buffer") == 0)
5504 return (PyObject *)BufferNew(curbuf);
5505 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005506 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005507 else if (strcmp(name, "tabpage") == 0)
5508 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005509 else if (strcmp(name, "line") == 0)
5510 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5511 else if (strcmp(name, "range") == 0)
5512 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005513 else if (strcmp(name, "__members__") == 0)
5514 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005515 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005516#if PY_MAJOR_VERSION < 3
5517 return Py_FindMethod(WindowMethods, self, name);
5518#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005519 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005520#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005521}
5522
5523 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005524CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005525{
5526 if (strcmp(name, "line") == 0)
5527 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005528 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5529 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005530 return -1;
5531
5532 return 0;
5533 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005534 else if (strcmp(name, "buffer") == 0)
5535 {
5536 int count;
5537
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005538 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005539 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005540 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005541 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005542 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005543 return -1;
5544 }
5545
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005546 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005547 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005548 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005549
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005550 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005551 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5552 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005553 if (VimTryEnd())
5554 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005555 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005556 return -1;
5557 }
5558
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005559 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005560 }
5561 else if (strcmp(name, "window") == 0)
5562 {
5563 int count;
5564
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005565 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005566 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005567 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005568 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005569 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005570 return -1;
5571 }
5572
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005574 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005575 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005576
5577 if (!count)
5578 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005579 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005580 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005581 return -1;
5582 }
5583
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005584 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005585 win_goto(((WindowObject *)(valObject))->win);
5586 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005587 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005588 if (VimTryEnd())
5589 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005590 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005591 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005592 return -1;
5593 }
5594
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005595 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005596 }
5597 else if (strcmp(name, "tabpage") == 0)
5598 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005599 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005600 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005601 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005602 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005603 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005604 return -1;
5605 }
5606
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005607 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005608 return -1;
5609
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005610 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005611 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5612 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005613 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005614 if (VimTryEnd())
5615 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005616 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005617 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005618 return -1;
5619 }
5620
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005621 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005622 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005623 else
5624 {
5625 PyErr_SetString(PyExc_AttributeError, name);
5626 return -1;
5627 }
5628}
5629
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005630static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005631 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005632 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5633 { NULL, NULL, 0, NULL}
5634};
5635
Bram Moolenaardb913952012-06-29 12:54:53 +02005636 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005637init_range_cmd(exarg_T *eap)
5638{
5639 RangeStart = eap->line1;
5640 RangeEnd = eap->line2;
5641}
5642
5643 static void
5644init_range_eval(typval_T *rettv UNUSED)
5645{
5646 RangeStart = (PyInt) curwin->w_cursor.lnum;
5647 RangeEnd = RangeStart;
5648}
5649
5650 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005651run_cmd(const char *cmd, void *arg UNUSED
5652#ifdef PY_CAN_RECURSE
5653 , PyGILState_STATE *pygilstate UNUSED
5654#endif
5655 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005656{
Bram Moolenaar41009372013-07-01 22:03:04 +02005657 PyObject *run_ret;
5658 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5659 if (run_ret != NULL)
5660 {
5661 Py_DECREF(run_ret);
5662 }
5663 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005665 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005666 PyErr_Clear();
5667 }
5668 else
5669 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005670}
5671
5672static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5673static int code_hdr_len = 30;
5674
5675 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005676run_do(const char *cmd, void *arg UNUSED
5677#ifdef PY_CAN_RECURSE
5678 , PyGILState_STATE *pygilstate
5679#endif
5680 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005681{
5682 PyInt lnum;
5683 size_t len;
5684 char *code;
5685 int status;
5686 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005687 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005688 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005689
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005690 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005692 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005693 return;
5694 }
5695
5696 len = code_hdr_len + STRLEN(cmd);
5697 code = PyMem_New(char, len + 1);
5698 memcpy(code, code_hdr, code_hdr_len);
5699 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005700 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5701 status = -1;
5702 if (run_ret != NULL)
5703 {
5704 status = 0;
5705 Py_DECREF(run_ret);
5706 }
5707 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5708 {
5709 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005710 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005711 PyErr_Clear();
5712 return;
5713 }
5714 else
5715 PyErr_PrintEx(1);
5716
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005717 PyMem_Free(code);
5718
5719 if (status)
5720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005721 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005722 return;
5723 }
5724
5725 status = 0;
5726 pymain = PyImport_AddModule("__main__");
5727 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005728#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005729 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005730#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005731
5732 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5733 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005734 PyObject *line;
5735 PyObject *linenr;
5736 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005737
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005738#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005739 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005740#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005741 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005742 if (lnum > curbuf->b_ml.ml_line_count
5743 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005744 goto err;
5745 if (!(linenr = PyInt_FromLong((long) lnum)))
5746 {
5747 Py_DECREF(line);
5748 goto err;
5749 }
5750 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5751 Py_DECREF(line);
5752 Py_DECREF(linenr);
5753 if (!ret)
5754 goto err;
5755
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005756 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005757 if (curbuf != was_curbuf)
5758 {
5759 Py_XDECREF(ret);
5760 goto err;
5761 }
5762
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005763 if (ret != Py_None)
5764 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005765 {
5766 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005767 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005768 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005769
5770 Py_XDECREF(ret);
5771 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005772#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005773 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005774#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005775 }
5776 goto out;
5777err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005778#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005779 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005780#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781 PyErr_PrintEx(0);
5782 PythonIO_Flush();
5783 status = 1;
5784out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005785#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005786 if (!status)
5787 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005788#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005789 Py_DECREF(pyfunc);
5790 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5791 if (status)
5792 return;
5793 check_cursor();
5794 update_curbuf(NOT_VALID);
5795}
5796
5797 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005798run_eval(const char *cmd, typval_T *rettv
5799#ifdef PY_CAN_RECURSE
5800 , PyGILState_STATE *pygilstate UNUSED
5801#endif
5802 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005803{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005804 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005805
Bram Moolenaar41009372013-07-01 22:03:04 +02005806 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005807 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005808 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005809 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005811 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005812 PyErr_Clear();
5813 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005814 else
5815 {
5816 if (PyErr_Occurred() && !msg_silent)
5817 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005818 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005819 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005820 }
5821 else
5822 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005823 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005824 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005825 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005826 }
5827 PyErr_Clear();
5828}
5829
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005830 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005831set_ref_in_py(const int copyID)
5832{
5833 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005834 list_T *ll;
5835 int i;
5836 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005837 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005838
5839 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005840 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005841 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005842 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5843 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005844 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005845
5846 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005847 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005848 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005849 {
5850 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005851 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005852 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005853 }
5854
Bram Moolenaar8110a092016-04-14 15:56:09 +02005855 if (lastfunc != NULL)
5856 {
5857 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5858 {
5859 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005860 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005861 if (func->argc)
5862 for (i = 0; !abort && i < func->argc; ++i)
5863 abort = abort
5864 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5865 }
5866 }
5867
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005868 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005869}
5870
5871 static int
5872set_string_copy(char_u *str, typval_T *tv)
5873{
5874 tv->vval.v_string = vim_strsave(str);
5875 if (tv->vval.v_string == NULL)
5876 {
5877 PyErr_NoMemory();
5878 return -1;
5879 }
5880 return 0;
5881}
5882
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005883 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005884pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005885{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005886 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005887 char_u *key;
5888 dictitem_T *di;
5889 PyObject *keyObject;
5890 PyObject *valObject;
5891 Py_ssize_t iter = 0;
5892
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005893 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005894 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005895
5896 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005897 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005898
5899 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5900 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005901 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005902
Bram Moolenaara03e6312013-05-29 22:49:26 +02005903 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005904 {
5905 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005906 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005907 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005908
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005909 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005910 {
5911 dict_unref(dict);
5912 return -1;
5913 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005914
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005915 if (*key == NUL)
5916 {
5917 dict_unref(dict);
5918 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005919 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005920 return -1;
5921 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922
5923 di = dictitem_alloc(key);
5924
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005925 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926
5927 if (di == NULL)
5928 {
5929 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005930 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005931 return -1;
5932 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005933
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005934 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005935 {
5936 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005937 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005938 return -1;
5939 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005940
5941 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005942 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005943 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005944 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005945 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005946 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005947 return -1;
5948 }
5949 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005950
5951 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005952 return 0;
5953}
5954
5955 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005956pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005957{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005958 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959 char_u *key;
5960 dictitem_T *di;
5961 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005962 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 PyObject *keyObject;
5964 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005966 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005967 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968
5969 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005970 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005972 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005973 {
5974 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005976 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005977
5978 if (!(iterator = PyObject_GetIter(list)))
5979 {
5980 dict_unref(dict);
5981 Py_DECREF(list);
5982 return -1;
5983 }
5984 Py_DECREF(list);
5985
5986 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005987 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005988 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005990 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005991 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005992 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005993 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005994 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005995 return -1;
5996 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005997
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005998 if (*key == NUL)
5999 {
6000 Py_DECREF(keyObject);
6001 Py_DECREF(iterator);
6002 Py_XDECREF(todecref);
6003 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006004 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006005 return -1;
6006 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006007
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006008 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006009 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006010 Py_DECREF(keyObject);
6011 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006012 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006013 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006014 return -1;
6015 }
6016
6017 di = dictitem_alloc(key);
6018
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006019 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006020 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006021
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006022 if (di == NULL)
6023 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006024 Py_DECREF(iterator);
6025 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006026 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006027 PyErr_NoMemory();
6028 return -1;
6029 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006030
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006031 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006032 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006033 Py_DECREF(iterator);
6034 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006035 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006036 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006037 return -1;
6038 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006039
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006040 Py_DECREF(valObject);
6041
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006042 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006043 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006044 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006045 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006046 dictitem_free(di);
6047 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006048 return -1;
6049 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006050 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006051 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006052 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006053 return 0;
6054}
6055
6056 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006057pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006058{
6059 list_T *l;
6060
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006061 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006062 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006063
6064 tv->v_type = VAR_LIST;
6065 tv->vval.v_list = l;
6066
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006067 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006068 {
6069 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006070 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006071 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006072
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006073 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006074 return 0;
6075}
6076
Bram Moolenaardb913952012-06-29 12:54:53 +02006077typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6078
6079 static int
6080convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006081 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006082{
6083 PyObject *capsule;
6084 char hexBuf[sizeof(void *) * 2 + 3];
6085
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006086 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006087
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006088#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006089 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006090#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006091 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006092#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006093 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006094 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006095#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006096 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006097#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006098 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006099#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006100 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6101 {
6102 Py_DECREF(capsule);
6103 tv->v_type = VAR_UNKNOWN;
6104 return -1;
6105 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006106
6107 Py_DECREF(capsule);
6108
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006109 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006110 {
6111 tv->v_type = VAR_UNKNOWN;
6112 return -1;
6113 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006114 // As we are not using copy_tv which increments reference count we must
6115 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006116 if (tv->v_type == VAR_DICT)
6117 ++tv->vval.v_dict->dv_refcount;
6118 else if (tv->v_type == VAR_LIST)
6119 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006120 }
6121 else
6122 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006123 typval_T *v;
6124
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006125#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006126 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006127#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006128 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006129#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006130 copy_tv(v, tv);
6131 }
6132 return 0;
6133}
6134
6135 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006136ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6137{
6138 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006139 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006140
6141 if (!(lookup_dict = PyDict_New()))
6142 return -1;
6143
6144 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6145 {
6146 tv->v_type = VAR_DICT;
6147 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6148 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006149 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006150 }
6151 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006152 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006153 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006154 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006155 else
6156 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006157 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006158 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006159 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006160 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006161 }
6162 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006163 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006164}
6165
6166 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006167ConvertFromPySequence(PyObject *obj, typval_T *tv)
6168{
6169 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006170 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006171
6172 if (!(lookup_dict = PyDict_New()))
6173 return -1;
6174
6175 if (PyType_IsSubtype(obj->ob_type, &ListType))
6176 {
6177 tv->v_type = VAR_LIST;
6178 tv->vval.v_list = (((ListObject *)(obj))->list);
6179 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006180 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006181 }
6182 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006183 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006184 else
6185 {
6186 PyErr_FORMAT(PyExc_TypeError,
6187 N_("unable to convert %s to vim list"),
6188 Py_TYPE_NAME(obj));
6189 ret = -1;
6190 }
6191 Py_DECREF(lookup_dict);
6192 return ret;
6193}
6194
6195 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006196ConvertFromPyObject(PyObject *obj, typval_T *tv)
6197{
6198 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006199 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006200
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006201 if (!(lookup_dict = PyDict_New()))
6202 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006203 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006204 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006205 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006206}
6207
6208 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006209_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006210{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006211 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 {
6213 tv->v_type = VAR_DICT;
6214 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6215 ++tv->vval.v_dict->dv_refcount;
6216 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006217 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006218 {
6219 tv->v_type = VAR_LIST;
6220 tv->vval.v_list = (((ListObject *)(obj))->list);
6221 ++tv->vval.v_list->lv_refcount;
6222 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006223 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006224 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006225 FunctionObject *func = (FunctionObject *) obj;
6226 if (func->self != NULL || func->argv != NULL)
6227 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006228 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6229
Bram Moolenaar8110a092016-04-14 15:56:09 +02006230 set_partial(func, pt, TRUE);
6231 tv->vval.v_partial = pt;
6232 tv->v_type = VAR_PARTIAL;
6233 }
6234 else
6235 {
6236 if (set_string_copy(func->name, tv) == -1)
6237 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006238
Bram Moolenaar8110a092016-04-14 15:56:09 +02006239 tv->v_type = VAR_FUNC;
6240 }
6241 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006242 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006243 else if (PyBytes_Check(obj))
6244 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006245 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006246
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006247 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006248 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006249 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006250 return -1;
6251
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006252 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006253 return -1;
6254
6255 tv->v_type = VAR_STRING;
6256 }
6257 else if (PyUnicode_Check(obj))
6258 {
6259 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006260 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006261
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006262 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006263 if (bytes == NULL)
6264 return -1;
6265
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006266 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006267 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006268 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006269 return -1;
6270
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006271 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006272 {
6273 Py_XDECREF(bytes);
6274 return -1;
6275 }
6276 Py_XDECREF(bytes);
6277
6278 tv->v_type = VAR_STRING;
6279 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006280#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006281 else if (PyInt_Check(obj))
6282 {
6283 tv->v_type = VAR_NUMBER;
6284 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006285 if (PyErr_Occurred())
6286 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006287 }
6288#endif
6289 else if (PyLong_Check(obj))
6290 {
6291 tv->v_type = VAR_NUMBER;
6292 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006293 if (PyErr_Occurred())
6294 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006295 }
6296 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006297 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006298#ifdef FEAT_FLOAT
6299 else if (PyFloat_Check(obj))
6300 {
6301 tv->v_type = VAR_FLOAT;
6302 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6303 }
6304#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006305 else if (PyObject_HasAttrString(obj, "keys"))
6306 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006307 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006308 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006309 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006310 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006311 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006312 else if (PyNumber_Check(obj))
6313 {
6314 PyObject *num;
6315
6316 if (!(num = PyNumber_Long(obj)))
6317 return -1;
6318
6319 tv->v_type = VAR_NUMBER;
6320 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6321
6322 Py_DECREF(num);
6323 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006324 else if (obj == Py_None)
6325 {
6326 tv->v_type = VAR_SPECIAL;
6327 tv->vval.v_number = VVAL_NONE;
6328 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006329 else
6330 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006331 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006332 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006333 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006334 return -1;
6335 }
6336 return 0;
6337}
6338
6339 static PyObject *
6340ConvertToPyObject(typval_T *tv)
6341{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006342 typval_T *argv;
6343 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006344 if (tv == NULL)
6345 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006346 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006347 return NULL;
6348 }
6349 switch (tv->v_type)
6350 {
6351 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006352 return PyBytes_FromString(tv->vval.v_string == NULL
6353 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006354 case VAR_NUMBER:
6355 return PyLong_FromLong((long) tv->vval.v_number);
6356#ifdef FEAT_FLOAT
6357 case VAR_FLOAT:
6358 return PyFloat_FromDouble((double) tv->vval.v_float);
6359#endif
6360 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006361 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006362 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006363 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006364 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006365 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006366 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006367 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006368 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006369 if (tv->vval.v_partial->pt_argc)
6370 {
6371 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6372 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6373 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6374 }
6375 else
6376 argv = NULL;
6377 if (tv->vval.v_partial->pt_dict != NULL)
6378 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006379 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006380 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006381 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006382 tv->vval.v_partial->pt_dict,
6383 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006384 case VAR_BLOB:
6385 return PyBytes_FromStringAndSize(
6386 (char*) tv->vval.v_blob->bv_ga.ga_data,
6387 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006389 case VAR_CHANNEL:
6390 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006391 Py_INCREF(Py_None);
6392 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006393 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006394 case VAR_SPECIAL:
6395 switch (tv->vval.v_number)
6396 {
6397 case VVAL_FALSE: return AlwaysFalse(NULL);
6398 case VVAL_TRUE: return AlwaysTrue(NULL);
6399 case VVAL_NONE:
6400 case VVAL_NULL: return AlwaysNone(NULL);
6401 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006402 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006403 return NULL;
6404 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006405 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006406}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006407
6408typedef struct
6409{
6410 PyObject_HEAD
6411} CurrentObject;
6412static PyTypeObject CurrentType;
6413
6414 static void
6415init_structs(void)
6416{
6417 vim_memset(&OutputType, 0, sizeof(OutputType));
6418 OutputType.tp_name = "vim.message";
6419 OutputType.tp_basicsize = sizeof(OutputObject);
6420 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6421 OutputType.tp_doc = "vim message object";
6422 OutputType.tp_methods = OutputMethods;
6423#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006424 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6425 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006426 OutputType.tp_alloc = call_PyType_GenericAlloc;
6427 OutputType.tp_new = call_PyType_GenericNew;
6428 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006429 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006430#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006431 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6432 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006433 // Disabled, because this causes a crash in test86
6434 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006435#endif
6436
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006437 vim_memset(&IterType, 0, sizeof(IterType));
6438 IterType.tp_name = "vim.iter";
6439 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006440 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006441 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006442 IterType.tp_iter = (getiterfunc)IterIter;
6443 IterType.tp_iternext = (iternextfunc)IterNext;
6444 IterType.tp_dealloc = (destructor)IterDestructor;
6445 IterType.tp_traverse = (traverseproc)IterTraverse;
6446 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006447
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448 vim_memset(&BufferType, 0, sizeof(BufferType));
6449 BufferType.tp_name = "vim.buffer";
6450 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006451 BufferType.tp_dealloc = (destructor)BufferDestructor;
6452 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006453 BufferType.tp_as_sequence = &BufferAsSeq;
6454 BufferType.tp_as_mapping = &BufferAsMapping;
6455 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6456 BufferType.tp_doc = "vim buffer object";
6457 BufferType.tp_methods = BufferMethods;
6458#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006459 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006460 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006461 BufferType.tp_alloc = call_PyType_GenericAlloc;
6462 BufferType.tp_new = call_PyType_GenericNew;
6463 BufferType.tp_free = call_PyObject_Free;
6464#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006465 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006466 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467#endif
6468
6469 vim_memset(&WindowType, 0, sizeof(WindowType));
6470 WindowType.tp_name = "vim.window";
6471 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006472 WindowType.tp_dealloc = (destructor)WindowDestructor;
6473 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006474 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006475 WindowType.tp_doc = "vim Window object";
6476 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006477 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6478 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006479#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006480 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6481 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006482 WindowType.tp_alloc = call_PyType_GenericAlloc;
6483 WindowType.tp_new = call_PyType_GenericNew;
6484 WindowType.tp_free = call_PyObject_Free;
6485#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6487 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488#endif
6489
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006490 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6491 TabPageType.tp_name = "vim.tabpage";
6492 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006493 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6494 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006495 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6496 TabPageType.tp_doc = "vim tab page object";
6497 TabPageType.tp_methods = TabPageMethods;
6498#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006500 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6501 TabPageType.tp_new = call_PyType_GenericNew;
6502 TabPageType.tp_free = call_PyObject_Free;
6503#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006504 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006505#endif
6506
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006507 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6508 BufMapType.tp_name = "vim.bufferlist";
6509 BufMapType.tp_basicsize = sizeof(BufMapObject);
6510 BufMapType.tp_as_mapping = &BufMapAsMapping;
6511 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006512 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006513 BufferType.tp_doc = "vim buffer list";
6514
6515 vim_memset(&WinListType, 0, sizeof(WinListType));
6516 WinListType.tp_name = "vim.windowlist";
6517 WinListType.tp_basicsize = sizeof(WinListType);
6518 WinListType.tp_as_sequence = &WinListAsSeq;
6519 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6520 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006522
6523 vim_memset(&TabListType, 0, sizeof(TabListType));
6524 TabListType.tp_name = "vim.tabpagelist";
6525 TabListType.tp_basicsize = sizeof(TabListType);
6526 TabListType.tp_as_sequence = &TabListAsSeq;
6527 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6528 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006529
6530 vim_memset(&RangeType, 0, sizeof(RangeType));
6531 RangeType.tp_name = "vim.range";
6532 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006533 RangeType.tp_dealloc = (destructor)RangeDestructor;
6534 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006535 RangeType.tp_as_sequence = &RangeAsSeq;
6536 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006537 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006538 RangeType.tp_doc = "vim Range object";
6539 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006540 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6541 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006542#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006543 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006544 RangeType.tp_alloc = call_PyType_GenericAlloc;
6545 RangeType.tp_new = call_PyType_GenericNew;
6546 RangeType.tp_free = call_PyObject_Free;
6547#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006548 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006549#endif
6550
6551 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6552 CurrentType.tp_name = "vim.currentdata";
6553 CurrentType.tp_basicsize = sizeof(CurrentObject);
6554 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6555 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006556 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006557#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006558 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6559 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006561 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6562 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006563#endif
6564
6565 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6566 DictionaryType.tp_name = "vim.dictionary";
6567 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006568 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006569 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006570 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006571 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6573 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006574 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6575 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6576 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006577#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006578 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6579 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006580#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006581 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6582 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006583#endif
6584
6585 vim_memset(&ListType, 0, sizeof(ListType));
6586 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006587 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588 ListType.tp_basicsize = sizeof(ListObject);
6589 ListType.tp_as_sequence = &ListAsSeq;
6590 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006591 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592 ListType.tp_doc = "list pushing modifications to vim structure";
6593 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006594 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006595 ListType.tp_new = (newfunc)ListConstructor;
6596 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006597#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006598 ListType.tp_getattro = (getattrofunc)ListGetattro;
6599 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006601 ListType.tp_getattr = (getattrfunc)ListGetattr;
6602 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006603#endif
6604
6605 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006606 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006607 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006608 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6609 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006610 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006611 FunctionType.tp_doc = "object that calls vim function";
6612 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006613 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006614 FunctionType.tp_new = (newfunc)FunctionConstructor;
6615 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006618#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006619 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006620#endif
6621
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006622 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6623 OptionsType.tp_name = "vim.options";
6624 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006625 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006626 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006627 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006628 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006629 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006630 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6631 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6632 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006633
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006634#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006635 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6636 LoaderType.tp_name = "vim.Loader";
6637 LoaderType.tp_basicsize = sizeof(LoaderObject);
6638 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6639 LoaderType.tp_doc = "vim message object";
6640 LoaderType.tp_methods = LoaderMethods;
6641 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006642#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006643
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006644#if PY_MAJOR_VERSION >= 3
6645 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6646 vimmodule.m_name = "vim";
6647 vimmodule.m_doc = "Vim Python interface\n";
6648 vimmodule.m_size = -1;
6649 vimmodule.m_methods = VimMethods;
6650#endif
6651}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006652
6653#define PYTYPE_READY(type) \
6654 if (PyType_Ready(&type)) \
6655 return -1;
6656
6657 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006658init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006659{
6660 PYTYPE_READY(IterType);
6661 PYTYPE_READY(BufferType);
6662 PYTYPE_READY(RangeType);
6663 PYTYPE_READY(WindowType);
6664 PYTYPE_READY(TabPageType);
6665 PYTYPE_READY(BufMapType);
6666 PYTYPE_READY(WinListType);
6667 PYTYPE_READY(TabListType);
6668 PYTYPE_READY(CurrentType);
6669 PYTYPE_READY(DictionaryType);
6670 PYTYPE_READY(ListType);
6671 PYTYPE_READY(FunctionType);
6672 PYTYPE_READY(OptionsType);
6673 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006674#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006675 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006676#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006677 return 0;
6678}
6679
6680 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006681init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006682{
6683 PyObject *path;
6684 PyObject *path_hook;
6685 PyObject *path_hooks;
6686
6687 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6688 return -1;
6689
6690 if (!(path_hooks = PySys_GetObject("path_hooks")))
6691 {
6692 PyErr_Clear();
6693 path_hooks = PyList_New(1);
6694 PyList_SET_ITEM(path_hooks, 0, path_hook);
6695 if (PySys_SetObject("path_hooks", path_hooks))
6696 {
6697 Py_DECREF(path_hooks);
6698 return -1;
6699 }
6700 Py_DECREF(path_hooks);
6701 }
6702 else if (PyList_Check(path_hooks))
6703 {
6704 if (PyList_Append(path_hooks, path_hook))
6705 {
6706 Py_DECREF(path_hook);
6707 return -1;
6708 }
6709 Py_DECREF(path_hook);
6710 }
6711 else
6712 {
6713 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006714 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006715 "You should now do the following:\n"
6716 "- append vim.path_hook to sys.path_hooks\n"
6717 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006718 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006719 Py_DECREF(path_hook);
6720 return 0;
6721 }
6722
6723 if (!(path = PySys_GetObject("path")))
6724 {
6725 PyErr_Clear();
6726 path = PyList_New(1);
6727 Py_INCREF(vim_special_path_object);
6728 PyList_SET_ITEM(path, 0, vim_special_path_object);
6729 if (PySys_SetObject("path", path))
6730 {
6731 Py_DECREF(path);
6732 return -1;
6733 }
6734 Py_DECREF(path);
6735 }
6736 else if (PyList_Check(path))
6737 {
6738 if (PyList_Append(path, vim_special_path_object))
6739 return -1;
6740 }
6741 else
6742 {
6743 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006744 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006745 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006746 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006747 }
6748
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006749 return 0;
6750}
6751
6752static BufMapObject TheBufferMap =
6753{
6754 PyObject_HEAD_INIT(&BufMapType)
6755};
6756
6757static WinListObject TheWindowList =
6758{
6759 PyObject_HEAD_INIT(&WinListType)
6760 NULL
6761};
6762
6763static CurrentObject TheCurrent =
6764{
6765 PyObject_HEAD_INIT(&CurrentType)
6766};
6767
6768static TabListObject TheTabPageList =
6769{
6770 PyObject_HEAD_INIT(&TabListType)
6771};
6772
6773static struct numeric_constant {
6774 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006775 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006776} numeric_constants[] = {
6777 {"VAR_LOCKED", VAR_LOCKED},
6778 {"VAR_FIXED", VAR_FIXED},
6779 {"VAR_SCOPE", VAR_SCOPE},
6780 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6781};
6782
6783static struct object_constant {
6784 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006785 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006786} object_constants[] = {
6787 {"buffers", (PyObject *)(void *)&TheBufferMap},
6788 {"windows", (PyObject *)(void *)&TheWindowList},
6789 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6790 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006791
6792 {"Buffer", (PyObject *)&BufferType},
6793 {"Range", (PyObject *)&RangeType},
6794 {"Window", (PyObject *)&WindowType},
6795 {"TabPage", (PyObject *)&TabPageType},
6796 {"Dictionary", (PyObject *)&DictionaryType},
6797 {"List", (PyObject *)&ListType},
6798 {"Function", (PyObject *)&FunctionType},
6799 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006800#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006801 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006802#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006803};
6804
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006805#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006806 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006807 return -1;
6808
6809#define ADD_CHECKED_OBJECT(m, name, obj) \
6810 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006811 PyObject *valObject = obj; \
6812 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006813 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006814 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006815 }
6816
6817 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006818populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006819{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006820 int i;
6821 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006822 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006823 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006824#if PY_VERSION_HEX >= 0x030700f0
6825 PyObject *dict;
6826 PyObject *cls;
6827#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006828
6829 for (i = 0; i < (int)(sizeof(numeric_constants)
6830 / sizeof(struct numeric_constant));
6831 ++i)
6832 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006833 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006834
6835 for (i = 0; i < (int)(sizeof(object_constants)
6836 / sizeof(struct object_constant));
6837 ++i)
6838 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006839 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006841 valObject = object_constants[i].valObject;
6842 Py_INCREF(valObject);
6843 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006844 }
6845
6846 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6847 return -1;
6848 ADD_OBJECT(m, "error", VimError);
6849
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006850 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6851 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006852 ADD_CHECKED_OBJECT(m, "options",
6853 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006854
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006855 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006856 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006857 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006858
Bram Moolenaar22081f42016-06-01 20:38:34 +02006859#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006860 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006861 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006862#else
6863 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6864 return -1;
6865#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006866 ADD_OBJECT(m, "_getcwd", py_getcwd)
6867
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006868 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006869 return -1;
6870 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006871 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006872 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006873 if (PyObject_SetAttrString(other_module, "chdir", attr))
6874 {
6875 Py_DECREF(attr);
6876 return -1;
6877 }
6878 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006879
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006880 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006881 {
6882 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006883 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006884 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006885 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6886 {
6887 Py_DECREF(attr);
6888 return -1;
6889 }
6890 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006892 else
6893 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006895 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6896 return -1;
6897
6898 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6899
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006900#if PY_VERSION_HEX >= 0x030700f0
6901 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6902 return -1;
6903
6904 dict = PyModule_GetDict(imp);
6905
6906 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6907 {
6908 Py_DECREF(imp);
6909 return -1;
6910 }
6911
6912 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6913 {
6914 Py_DECREF(imp);
6915 return -1;
6916 }
6917
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006918 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6919 {
6920 // find_module() is deprecated, this may stop working in some later
6921 // version.
6922 ADD_OBJECT(m, "_find_module", py_find_module);
6923 }
6924
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006925 Py_DECREF(imp);
6926
6927 ADD_OBJECT(m, "_find_spec", py_find_spec);
6928#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006929 if (!(imp = PyImport_ImportModule("imp")))
6930 return -1;
6931
6932 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6933 {
6934 Py_DECREF(imp);
6935 return -1;
6936 }
6937
6938 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6939 {
6940 Py_DECREF(py_find_module);
6941 Py_DECREF(imp);
6942 return -1;
6943 }
6944
6945 Py_DECREF(imp);
6946
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006947 ADD_OBJECT(m, "_find_module", py_find_module);
6948 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006949#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006950
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006951 return 0;
6952}