blob: d816386558b658dde320a2349c2e8299553b28f6 [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 Moolenaar520e1e42016-01-23 19:46:28 +0100847 else if (our_tv->v_type == VAR_SPECIAL)
848 {
849 if (our_tv->vval.v_number == VVAL_FALSE)
850 {
851 ret = Py_False;
852 Py_INCREF(ret);
853 }
854 else if (our_tv->vval.v_number == VVAL_TRUE)
855 {
856 ret = Py_True;
857 Py_INCREF(ret);
858 }
859 else
860 {
861 Py_INCREF(Py_None);
862 ret = Py_None;
863 }
864 return ret;
865 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100866 else if (our_tv->v_type == VAR_BLOB)
867 ret = PyBytes_FromStringAndSize(
868 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
869 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200870 else
871 {
872 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200873 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 }
875
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200876 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200877}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878
879 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200880VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200881{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200882 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200883 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200884 PyObject *string;
885 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200887 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200888
Bram Moolenaar389a1792013-06-23 13:00:44 +0200889 if (!PyArg_ParseTuple(args, "O", &string))
890 return NULL;
891
892 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200893 return NULL;
894
895 Py_BEGIN_ALLOW_THREADS
896 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200897 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200899 Python_Release_Vim();
900 Py_END_ALLOW_THREADS
901
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 Py_XDECREF(todecref);
903
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200904 if (VimTryEnd())
905 return NULL;
906
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200907 if (our_tv == NULL)
908 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200909 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200910 return NULL;
911 }
912
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100913 // Convert the Vim type into a Python type. Create a dictionary that's
914 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200915 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200916 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200917 else
918 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200919 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200920 Py_DECREF(lookup_dict);
921 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200922
923
924 Py_BEGIN_ALLOW_THREADS
925 Python_Lock_Vim();
926 free_tv(our_tv);
927 Python_Release_Vim();
928 Py_END_ALLOW_THREADS
929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200930 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200931}
932
Bram Moolenaardb913952012-06-29 12:54:53 +0200933static PyObject *ConvertToPyObject(typval_T *);
934
935 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200936VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200937{
Bram Moolenaardb913952012-06-29 12:54:53 +0200938 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200939 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940 char_u *expr;
941 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200942
Bram Moolenaar389a1792013-06-23 13:00:44 +0200943 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200944 return NULL;
945
946 Py_BEGIN_ALLOW_THREADS
947 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200948 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200949 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200950 Python_Release_Vim();
951 Py_END_ALLOW_THREADS
952
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953 Py_XDECREF(todecref);
954
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200955 if (VimTryEnd())
956 return NULL;
957
Bram Moolenaardb913952012-06-29 12:54:53 +0200958 if (our_tv == NULL)
959 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200960 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200961 return NULL;
962 }
963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200965 Py_BEGIN_ALLOW_THREADS
966 Python_Lock_Vim();
967 free_tv(our_tv);
968 Python_Release_Vim();
969 Py_END_ALLOW_THREADS
970
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200971 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200972}
973
974 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200975VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200976{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200977 char_u *str;
978 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200979 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200980
Bram Moolenaar389a1792013-06-23 13:00:44 +0200981 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200982 return NULL;
983
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200984 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200985
986 Py_XDECREF(todecref);
987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200988 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200989}
990
Bram Moolenaarf4258302013-06-02 18:20:17 +0200991 static PyObject *
992_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
993{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200994 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200995 PyObject *newwd;
996 PyObject *todecref;
997 char_u *new_dir;
998
Bram Moolenaard4209d22013-06-05 20:34:15 +0200999 if (_chdir == NULL)
1000 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001001 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001002 return NULL;
1003
1004 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1005 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001006 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001007 return NULL;
1008 }
1009
1010 if (!(new_dir = StringToChars(newwd, &todecref)))
1011 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001012 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001013 Py_DECREF(newwd);
1014 return NULL;
1015 }
1016
1017 VimTryStart();
1018
1019 if (vim_chdir(new_dir))
1020 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001021 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001022 Py_DECREF(newwd);
1023 Py_XDECREF(todecref);
1024
1025 if (VimTryEnd())
1026 return NULL;
1027
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001028 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001029 return NULL;
1030 }
1031
1032 Py_DECREF(newwd);
1033 Py_XDECREF(todecref);
1034
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001035 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001036
1037 if (VimTryEnd())
1038 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001039 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001040 return NULL;
1041 }
1042
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001043 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001044}
1045
1046 static PyObject *
1047VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1048{
1049 return _VimChdir(py_chdir, args, kwargs);
1050}
1051
1052 static PyObject *
1053VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1054{
1055 return _VimChdir(py_fchdir, args, kwargs);
1056}
1057
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001058typedef struct {
1059 PyObject *callable;
1060 PyObject *result;
1061} map_rtp_data;
1062
1063 static void
1064map_rtp_callback(char_u *path, void *_data)
1065{
1066 void **data = (void **) _data;
1067 PyObject *pathObject;
1068 map_rtp_data *mr_data = *((map_rtp_data **) data);
1069
Bram Moolenaar41009372013-07-01 22:03:04 +02001070 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001071 {
1072 *data = NULL;
1073 return;
1074 }
1075
1076 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1077 pathObject, NULL);
1078
1079 Py_DECREF(pathObject);
1080
1081 if (!mr_data->result || mr_data->result != Py_None)
1082 *data = NULL;
1083 else
1084 {
1085 Py_DECREF(mr_data->result);
1086 mr_data->result = NULL;
1087 }
1088}
1089
1090 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001091VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001092{
1093 map_rtp_data data;
1094
Bram Moolenaar389a1792013-06-23 13:00:44 +02001095 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001096 data.result = NULL;
1097
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001098 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001099
1100 if (data.result == NULL)
1101 {
1102 if (PyErr_Occurred())
1103 return NULL;
1104 else
1105 {
1106 Py_INCREF(Py_None);
1107 return Py_None;
1108 }
1109 }
1110 return data.result;
1111}
1112
1113/*
1114 * _vim_runtimepath_ special path implementation.
1115 */
1116
1117 static void
1118map_finder_callback(char_u *path, void *_data)
1119{
1120 void **data = (void **) _data;
1121 PyObject *list = *((PyObject **) data);
1122 PyObject *pathObject1, *pathObject2;
1123 char *pathbuf;
1124 size_t pathlen;
1125
1126 pathlen = STRLEN(path);
1127
1128#if PY_MAJOR_VERSION < 3
1129# define PY_MAIN_DIR_STRING "python2"
1130#else
1131# define PY_MAIN_DIR_STRING "python3"
1132#endif
1133#define PY_ALTERNATE_DIR_STRING "pythonx"
1134
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001135#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001136 if (!(pathbuf = PyMem_New(char,
1137 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1138 {
1139 PyErr_NoMemory();
1140 *data = NULL;
1141 return;
1142 }
1143
1144 mch_memmove(pathbuf, path, pathlen + 1);
1145 add_pathsep((char_u *) pathbuf);
1146
1147 pathlen = STRLEN(pathbuf);
1148 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1149 PYTHONX_STRING_LENGTH + 1);
1150
1151 if (!(pathObject1 = PyString_FromString(pathbuf)))
1152 {
1153 *data = NULL;
1154 PyMem_Free(pathbuf);
1155 return;
1156 }
1157
1158 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1159 PYTHONX_STRING_LENGTH + 1);
1160
1161 if (!(pathObject2 = PyString_FromString(pathbuf)))
1162 {
1163 Py_DECREF(pathObject1);
1164 PyMem_Free(pathbuf);
1165 *data = NULL;
1166 return;
1167 }
1168
1169 PyMem_Free(pathbuf);
1170
1171 if (PyList_Append(list, pathObject1)
1172 || PyList_Append(list, pathObject2))
1173 *data = NULL;
1174
1175 Py_DECREF(pathObject1);
1176 Py_DECREF(pathObject2);
1177}
1178
1179 static PyObject *
1180Vim_GetPaths(PyObject *self UNUSED)
1181{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001182 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001183
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001184 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001185 return NULL;
1186
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001187 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001188
1189 if (PyErr_Occurred())
1190 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001191 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001192 return NULL;
1193 }
1194
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001195 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001196}
1197
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001198#if PY_VERSION_HEX >= 0x030700f0
1199 static PyObject *
1200FinderFindSpec(PyObject *self, PyObject *args)
1201{
1202 char *fullname;
1203 PyObject *paths;
1204 PyObject *target = Py_None;
1205 PyObject *spec;
1206
1207 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1208 return NULL;
1209
1210 if (!(paths = Vim_GetPaths(self)))
1211 return NULL;
1212
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001213 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001214
1215 Py_DECREF(paths);
1216
1217 if (!spec)
1218 {
1219 if (PyErr_Occurred())
1220 return NULL;
1221
1222 Py_INCREF(Py_None);
1223 return Py_None;
1224 }
1225
1226 return spec;
1227}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001228
1229 static PyObject *
1230FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1231{
1232 // Apparently returning None works.
1233 Py_INCREF(Py_None);
1234 return Py_None;
1235}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001236#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001237 static PyObject *
1238call_load_module(char *name, int len, PyObject *find_module_result)
1239{
1240 PyObject *fd, *pathname, *description;
1241
Bram Moolenaarc476e522013-06-23 13:46:40 +02001242 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001243 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001244 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001245 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001246 Py_TYPE_NAME(find_module_result));
1247 return NULL;
1248 }
1249 if (PyTuple_GET_SIZE(find_module_result) != 3)
1250 {
1251 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001252 N_("expected 3-tuple as imp.find_module() result, but got "
1253 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001254 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001255 return NULL;
1256 }
1257
1258 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1259 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1260 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1261 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001262 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001263 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001264 return NULL;
1265 }
1266
1267 return PyObject_CallFunction(py_load_module,
1268 "s#OOO", name, len, fd, pathname, description);
1269}
1270
1271 static PyObject *
1272find_module(char *fullname, char *tail, PyObject *new_path)
1273{
1274 PyObject *find_module_result;
1275 PyObject *module;
1276 char *dot;
1277
Bram Moolenaar41009372013-07-01 22:03:04 +02001278 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001279 {
1280 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001281 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001282 * first component
1283 */
1284 PyObject *newest_path;
1285 int partlen = (int) (dot - 1 - tail);
1286
1287 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1288 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001289 {
1290 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1291 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001292 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001293 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001294
1295 if (!(module = call_load_module(
1296 fullname,
1297 ((int) (tail - fullname)) + partlen,
1298 find_module_result)))
1299 {
1300 Py_DECREF(find_module_result);
1301 return NULL;
1302 }
1303
1304 Py_DECREF(find_module_result);
1305
1306 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1307 {
1308 Py_DECREF(module);
1309 return NULL;
1310 }
1311
1312 Py_DECREF(module);
1313
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001314 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001315
1316 Py_DECREF(newest_path);
1317
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001318 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001319 }
1320 else
1321 {
1322 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1323 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001324 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001325 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1326 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001327 return NULL;
1328 }
1329
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001330 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001331 }
1332}
1333
1334 static PyObject *
1335FinderFindModule(PyObject *self, PyObject *args)
1336{
1337 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001338 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001339 PyObject *new_path;
1340 LoaderObject *loader;
1341
1342 if (!PyArg_ParseTuple(args, "s", &fullname))
1343 return NULL;
1344
1345 if (!(new_path = Vim_GetPaths(self)))
1346 return NULL;
1347
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001348 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001349
1350 Py_DECREF(new_path);
1351
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001352 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001353 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001354 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001355 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001356
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001357 Py_INCREF(Py_None);
1358 return Py_None;
1359 }
1360
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001361 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001362 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001363 Py_DECREF(result);
1364 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001365 return NULL;
1366 }
1367
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001368 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1369 {
1370 vim_free(fullname);
1371 Py_DECREF(result);
1372 return NULL;
1373 }
1374
1375 loader->fullname = fullname;
1376 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001377
1378 return (PyObject *) loader;
1379}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001380#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001381
1382 static PyObject *
1383VimPathHook(PyObject *self UNUSED, PyObject *args)
1384{
1385 char *path;
1386
1387 if (PyArg_ParseTuple(args, "s", &path)
1388 && STRCMP(path, vim_special_path) == 0)
1389 {
1390 Py_INCREF(vim_module);
1391 return vim_module;
1392 }
1393
1394 PyErr_Clear();
1395 PyErr_SetNone(PyExc_ImportError);
1396 return NULL;
1397}
1398
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001399/*
1400 * Vim module - Definitions
1401 */
1402
1403static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001404 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001405 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001406 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001407 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1408 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001409 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1410 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001411 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001412#if PY_VERSION_HEX >= 0x030700f0
1413 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001414#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001415 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001416 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1417 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1418 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001419};
1420
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001421/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001422 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001423 */
1424
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001425static PyTypeObject IterType;
1426
1427typedef PyObject *(*nextfun)(void **);
1428typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001429typedef int (*traversefun)(void *, visitproc, void *);
1430typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001431
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001432// Main purpose of this object is removing the need for do python
1433// initialization (i.e. PyType_Ready and setting type attributes) for a big
1434// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001435typedef struct
1436{
1437 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001438 void *cur;
1439 nextfun next;
1440 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001441 traversefun traverse;
1442 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001443} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001444
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001445 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001446IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1447 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001448{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001449 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001450
Bram Moolenaar774267b2013-05-21 20:51:59 +02001451 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001452 self->cur = start;
1453 self->next = next;
1454 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001455 self->traverse = traverse;
1456 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001457
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001458 return (PyObject *)(self);
1459}
1460
1461 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001462IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001463{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001464 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001465 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001466 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001467}
1468
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001469 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001470IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001471{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001472 if (self->traverse != NULL)
1473 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001474 else
1475 return 0;
1476}
1477
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001478// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001479#ifdef clear
1480# undef clear
1481#endif
1482
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001483 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001484IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001485{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001486 if (self->clear != NULL)
1487 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001488 else
1489 return 0;
1490}
1491
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001492 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001493IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001494{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001495 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001496}
1497
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001498 static PyObject *
1499IterIter(PyObject *self)
1500{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001501 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001502 return self;
1503}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001504
Bram Moolenaardb913952012-06-29 12:54:53 +02001505typedef struct pylinkedlist_S {
1506 struct pylinkedlist_S *pll_next;
1507 struct pylinkedlist_S *pll_prev;
1508 PyObject *pll_obj;
1509} pylinkedlist_T;
1510
1511static pylinkedlist_T *lastdict = NULL;
1512static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001513static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001514
1515 static void
1516pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1517{
1518 if (ref->pll_prev == NULL)
1519 {
1520 if (ref->pll_next == NULL)
1521 {
1522 *last = NULL;
1523 return;
1524 }
1525 }
1526 else
1527 ref->pll_prev->pll_next = ref->pll_next;
1528
1529 if (ref->pll_next == NULL)
1530 *last = ref->pll_prev;
1531 else
1532 ref->pll_next->pll_prev = ref->pll_prev;
1533}
1534
1535 static void
1536pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1537{
1538 if (*last == NULL)
1539 ref->pll_prev = NULL;
1540 else
1541 {
1542 (*last)->pll_next = ref;
1543 ref->pll_prev = *last;
1544 }
1545 ref->pll_next = NULL;
1546 ref->pll_obj = self;
1547 *last = ref;
1548}
1549
1550static PyTypeObject DictionaryType;
1551
1552typedef struct
1553{
1554 PyObject_HEAD
1555 dict_T *dict;
1556 pylinkedlist_T ref;
1557} DictionaryObject;
1558
Bram Moolenaara9922d62013-05-30 13:01:18 +02001559static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1560
1561#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1562
Bram Moolenaardb913952012-06-29 12:54:53 +02001563 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001564DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001565{
1566 DictionaryObject *self;
1567
Bram Moolenaara9922d62013-05-30 13:01:18 +02001568 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001569 if (self == NULL)
1570 return NULL;
1571 self->dict = dict;
1572 ++dict->dv_refcount;
1573
1574 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1575
1576 return (PyObject *)(self);
1577}
1578
Bram Moolenaara9922d62013-05-30 13:01:18 +02001579 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001580py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001582 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001583
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001584 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001585 {
1586 PyErr_NoMemory();
1587 return NULL;
1588 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001589 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001591 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592}
1593
1594 static PyObject *
1595DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1596{
1597 DictionaryObject *self;
1598 dict_T *dict;
1599
1600 if (!(dict = py_dict_alloc()))
1601 return NULL;
1602
1603 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1604
1605 --dict->dv_refcount;
1606
1607 if (kwargs || PyTuple_Size(args))
1608 {
1609 PyObject *tmp;
1610 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1611 {
1612 Py_DECREF(self);
1613 return NULL;
1614 }
1615
1616 Py_DECREF(tmp);
1617 }
1618
1619 return (PyObject *)(self);
1620}
1621
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001622 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001623DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001624{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001625 pyll_remove(&self->ref, &lastdict);
1626 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001627
1628 DESTRUCTOR_FINISH(self);
1629}
1630
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001631static char *DictionaryAttrs[] = {
1632 "locked", "scope",
1633 NULL
1634};
1635
1636 static PyObject *
1637DictionaryDir(PyObject *self)
1638{
1639 return ObjectDir(self, DictionaryAttrs);
1640}
1641
Bram Moolenaardb913952012-06-29 12:54:53 +02001642 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001643DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001644{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001645 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001646 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001647 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001648 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001649 return -1;
1650 }
1651
1652 if (strcmp(name, "locked") == 0)
1653 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001654 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001655 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001656 PyErr_SET_STRING(PyExc_TypeError,
1657 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001658 return -1;
1659 }
1660 else
1661 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001662 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001663 if (istrue == -1)
1664 return -1;
1665 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001666 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001667 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001668 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001669 }
1670 return 0;
1671 }
1672 else
1673 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001674 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001675 return -1;
1676 }
1677}
1678
1679 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001680DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001681{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001682 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001683}
1684
Bram Moolenaara9922d62013-05-30 13:01:18 +02001685#define DICT_FLAG_HAS_DEFAULT 0x01
1686#define DICT_FLAG_POP 0x02
1687#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001688#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001689#define DICT_FLAG_RETURN_PAIR 0x10
1690
Bram Moolenaardb913952012-06-29 12:54:53 +02001691 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001693{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001694 PyObject *keyObject;
1695 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001696 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001697 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001698 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001699 dict_T *dict = self->dict;
1700 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001701 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001702
Bram Moolenaara9922d62013-05-30 13:01:18 +02001703 if (flags & DICT_FLAG_HAS_DEFAULT)
1704 {
1705 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1706 return NULL;
1707 }
1708 else
1709 keyObject = args;
1710
1711 if (flags & DICT_FLAG_RETURN_BOOL)
1712 defObject = Py_False;
1713
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001714 if (!(key = StringToChars(keyObject, &todecref)))
1715 return NULL;
1716
1717 if (*key == NUL)
1718 {
1719 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001720 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001721 return NULL;
1722 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001723
Bram Moolenaara9922d62013-05-30 13:01:18 +02001724 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001725
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001726 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001727
Bram Moolenaara9922d62013-05-30 13:01:18 +02001728 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001729 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001730 if (defObject)
1731 {
1732 Py_INCREF(defObject);
1733 return defObject;
1734 }
1735 else
1736 {
1737 PyErr_SetObject(PyExc_KeyError, keyObject);
1738 return NULL;
1739 }
1740 }
1741 else if (flags & DICT_FLAG_RETURN_BOOL)
1742 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001743 ret = Py_True;
1744 Py_INCREF(ret);
1745 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001746 }
1747
1748 di = dict_lookup(hi);
1749
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001750 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001751 return NULL;
1752
1753 if (flags & DICT_FLAG_POP)
1754 {
1755 if (dict->dv_lock)
1756 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001757 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001758 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001759 return NULL;
1760 }
1761
1762 hash_remove(&dict->dv_hashtab, hi);
1763 dictitem_free(di);
1764 }
1765
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001766 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001767}
1768
1769 static PyObject *
1770DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1771{
1772 return _DictionaryItem(self, keyObject, 0);
1773}
1774
1775 static int
1776DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1777{
1778 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001779 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001780
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001781 if (rObj == NULL)
1782 return -1;
1783
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001784 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785
Bram Moolenaardee2e312013-06-23 16:35:47 +02001786 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001787
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001788 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789}
1790
1791typedef struct
1792{
1793 hashitem_T *ht_array;
1794 long_u ht_used;
1795 hashtab_T *ht;
1796 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001797 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798} dictiterinfo_T;
1799
1800 static PyObject *
1801DictionaryIterNext(dictiterinfo_T **dii)
1802{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001803 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001804
1805 if (!(*dii)->todo)
1806 return NULL;
1807
1808 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1809 (*dii)->ht->ht_used != (*dii)->ht_used)
1810 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001811 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001812 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001813 return NULL;
1814 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001815
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1817 ++((*dii)->hi);
1818
1819 --((*dii)->todo);
1820
Bram Moolenaar41009372013-07-01 22:03:04 +02001821 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001822 return NULL;
1823
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001824 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001825}
1826
1827 static PyObject *
1828DictionaryIter(DictionaryObject *self)
1829{
1830 dictiterinfo_T *dii;
1831 hashtab_T *ht;
1832
1833 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1834 {
1835 PyErr_NoMemory();
1836 return NULL;
1837 }
1838
1839 ht = &self->dict->dv_hashtab;
1840 dii->ht_array = ht->ht_array;
1841 dii->ht_used = ht->ht_used;
1842 dii->ht = ht;
1843 dii->hi = dii->ht_array;
1844 dii->todo = dii->ht_used;
1845
1846 return IterNew(dii,
1847 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1848 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001849}
1850
1851 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001852DictionaryAssItem(
1853 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001854{
1855 char_u *key;
1856 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001857 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001858 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001859 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001860
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001861 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001862 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001863 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001864 return -1;
1865 }
1866
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001867 if (!(key = StringToChars(keyObject, &todecref)))
1868 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001869
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001870 if (*key == NUL)
1871 {
1872 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001873 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001874 return -1;
1875 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001876
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001877 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001878
1879 if (valObject == NULL)
1880 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001881 hashitem_T *hi;
1882
Bram Moolenaardb913952012-06-29 12:54:53 +02001883 if (di == NULL)
1884 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001885 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001886 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001887 return -1;
1888 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001889 hi = hash_find(&dict->dv_hashtab, di->di_key);
1890 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001891 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001892 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 return 0;
1894 }
1895
1896 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001897 {
1898 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001900 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001901
1902 if (di == NULL)
1903 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001904 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001905 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001906 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 PyErr_NoMemory();
1908 return -1;
1909 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001910 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001911
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001912 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001913 {
1914 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001915 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001916 RAISE_KEY_ADD_FAIL(key);
1917 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001918 return -1;
1919 }
1920 }
1921 else
1922 clear_tv(&di->di_tv);
1923
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001924 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001925
1926 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001927 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001928 return 0;
1929}
1930
Bram Moolenaara9922d62013-05-30 13:01:18 +02001931typedef PyObject *(*hi_to_py)(hashitem_T *);
1932
Bram Moolenaardb913952012-06-29 12:54:53 +02001933 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001934DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001935{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001936 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001937 long_u todo = dict->dv_hashtab.ht_used;
1938 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001939 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001940 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001941 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001942
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001943 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001944 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1945 {
1946 if (!HASHITEM_EMPTY(hi))
1947 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001948 if (!(newObj = hiconvert(hi)))
1949 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001950 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001951 return NULL;
1952 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001953 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001954 --todo;
1955 ++i;
1956 }
1957 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001958 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001959}
1960
Bram Moolenaara9922d62013-05-30 13:01:18 +02001961 static PyObject *
1962dict_key(hashitem_T *hi)
1963{
1964 return PyBytes_FromString((char *)(hi->hi_key));
1965}
1966
1967 static PyObject *
1968DictionaryListKeys(DictionaryObject *self)
1969{
1970 return DictionaryListObjects(self, dict_key);
1971}
1972
1973 static PyObject *
1974dict_val(hashitem_T *hi)
1975{
1976 dictitem_T *di;
1977
1978 di = dict_lookup(hi);
1979 return ConvertToPyObject(&di->di_tv);
1980}
1981
1982 static PyObject *
1983DictionaryListValues(DictionaryObject *self)
1984{
1985 return DictionaryListObjects(self, dict_val);
1986}
1987
1988 static PyObject *
1989dict_item(hashitem_T *hi)
1990{
1991 PyObject *keyObject;
1992 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001993 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001994
1995 if (!(keyObject = dict_key(hi)))
1996 return NULL;
1997
1998 if (!(valObject = dict_val(hi)))
1999 {
2000 Py_DECREF(keyObject);
2001 return NULL;
2002 }
2003
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002004 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002005
2006 Py_DECREF(keyObject);
2007 Py_DECREF(valObject);
2008
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002009 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010}
2011
2012 static PyObject *
2013DictionaryListItems(DictionaryObject *self)
2014{
2015 return DictionaryListObjects(self, dict_item);
2016}
2017
2018 static PyObject *
2019DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2020{
2021 dict_T *dict = self->dict;
2022
2023 if (dict->dv_lock)
2024 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002025 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002026 return NULL;
2027 }
2028
2029 if (kwargs)
2030 {
2031 typval_T tv;
2032
2033 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2034 return NULL;
2035
2036 VimTryStart();
2037 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2038 clear_tv(&tv);
2039 if (VimTryEnd())
2040 return NULL;
2041 }
2042 else
2043 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002044 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002046 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002047 return NULL;
2048
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002049 if (obj == NULL)
2050 {
2051 Py_INCREF(Py_None);
2052 return Py_None;
2053 }
2054
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002055 if (PyObject_HasAttrString(obj, "keys"))
2056 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002057 else
2058 {
2059 PyObject *iterator;
2060 PyObject *item;
2061
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002062 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002063 return NULL;
2064
2065 while ((item = PyIter_Next(iterator)))
2066 {
2067 PyObject *fast;
2068 PyObject *keyObject;
2069 PyObject *valObject;
2070 PyObject *todecref;
2071 char_u *key;
2072 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002073 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002074
2075 if (!(fast = PySequence_Fast(item, "")))
2076 {
2077 Py_DECREF(iterator);
2078 Py_DECREF(item);
2079 return NULL;
2080 }
2081
2082 Py_DECREF(item);
2083
2084 if (PySequence_Fast_GET_SIZE(fast) != 2)
2085 {
2086 Py_DECREF(iterator);
2087 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002088 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002089 N_("expected sequence element of size 2, "
2090 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002091 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002092 return NULL;
2093 }
2094
2095 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2096
2097 if (!(key = StringToChars(keyObject, &todecref)))
2098 {
2099 Py_DECREF(iterator);
2100 Py_DECREF(fast);
2101 return NULL;
2102 }
2103
2104 di = dictitem_alloc(key);
2105
2106 Py_XDECREF(todecref);
2107
2108 if (di == NULL)
2109 {
2110 Py_DECREF(fast);
2111 Py_DECREF(iterator);
2112 PyErr_NoMemory();
2113 return NULL;
2114 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002115 di->di_tv.v_type = VAR_UNKNOWN;
2116
2117 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2118
2119 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2120 {
2121 Py_DECREF(iterator);
2122 Py_DECREF(fast);
2123 dictitem_free(di);
2124 return NULL;
2125 }
2126
2127 Py_DECREF(fast);
2128
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002129 hi = hash_find(&dict->dv_hashtab, di->di_key);
2130 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002131 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002132 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002133 Py_DECREF(iterator);
2134 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002135 return NULL;
2136 }
2137 }
2138
2139 Py_DECREF(iterator);
2140
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002141 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002142 if (PyErr_Occurred())
2143 return NULL;
2144 }
2145 }
2146 Py_INCREF(Py_None);
2147 return Py_None;
2148}
2149
2150 static PyObject *
2151DictionaryGet(DictionaryObject *self, PyObject *args)
2152{
2153 return _DictionaryItem(self, args,
2154 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2155}
2156
2157 static PyObject *
2158DictionaryPop(DictionaryObject *self, PyObject *args)
2159{
2160 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2161}
2162
2163 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002164DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002165{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002166 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002167 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002168 PyObject *valObject;
2169 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002170
Bram Moolenaarde71b562013-06-02 17:41:54 +02002171 if (self->dict->dv_hashtab.ht_used == 0)
2172 {
2173 PyErr_SetNone(PyExc_KeyError);
2174 return NULL;
2175 }
2176
2177 hi = self->dict->dv_hashtab.ht_array;
2178 while (HASHITEM_EMPTY(hi))
2179 ++hi;
2180
2181 di = dict_lookup(hi);
2182
2183 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002184 return NULL;
2185
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002186 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002187 {
2188 Py_DECREF(valObject);
2189 return NULL;
2190 }
2191
2192 hash_remove(&self->dict->dv_hashtab, hi);
2193 dictitem_free(di);
2194
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002195 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002196}
2197
2198 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002199DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002200{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002201 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2202}
2203
2204static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002205 0, // sq_length
2206 0, // sq_concat
2207 0, // sq_repeat
2208 0, // sq_item
2209 0, // sq_slice
2210 0, // sq_ass_item
2211 0, // sq_ass_slice
2212 (objobjproc) DictionaryContains, // sq_contains
2213 0, // sq_inplace_concat
2214 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002215};
2216
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002217static PyMappingMethods DictionaryAsMapping = {
2218 (lenfunc) DictionaryLength,
2219 (binaryfunc) DictionaryItem,
2220 (objobjargproc) DictionaryAssItem,
2221};
2222
Bram Moolenaardb913952012-06-29 12:54:53 +02002223static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002224 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002225 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2226 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2227 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2228 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2229 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002230 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002231 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002232 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2233 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002234};
2235
2236static PyTypeObject ListType;
2237
2238typedef struct
2239{
2240 PyObject_HEAD
2241 list_T *list;
2242 pylinkedlist_T ref;
2243} ListObject;
2244
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002245#define NEW_LIST(list) ListNew(&ListType, list)
2246
Bram Moolenaardb913952012-06-29 12:54:53 +02002247 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002248ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002249{
2250 ListObject *self;
2251
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002252 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002253 if (self == NULL)
2254 return NULL;
2255 self->list = list;
2256 ++list->lv_refcount;
2257
2258 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2259
2260 return (PyObject *)(self);
2261}
2262
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002263 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002264py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002265{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002266 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002267
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002268 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002269 {
2270 PyErr_NoMemory();
2271 return NULL;
2272 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002273 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002274
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002275 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276}
2277
2278 static int
2279list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2280{
2281 PyObject *iterator;
2282 PyObject *item;
2283 listitem_T *li;
2284
2285 if (!(iterator = PyObject_GetIter(obj)))
2286 return -1;
2287
2288 while ((item = PyIter_Next(iterator)))
2289 {
2290 if (!(li = listitem_alloc()))
2291 {
2292 PyErr_NoMemory();
2293 Py_DECREF(item);
2294 Py_DECREF(iterator);
2295 return -1;
2296 }
2297 li->li_tv.v_lock = 0;
2298 li->li_tv.v_type = VAR_UNKNOWN;
2299
2300 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2301 {
2302 Py_DECREF(item);
2303 Py_DECREF(iterator);
2304 listitem_free(li);
2305 return -1;
2306 }
2307
2308 Py_DECREF(item);
2309
2310 list_append(l, li);
2311 }
2312
2313 Py_DECREF(iterator);
2314
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002315 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002316 if (PyErr_Occurred())
2317 return -1;
2318
2319 return 0;
2320}
2321
2322 static PyObject *
2323ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2324{
2325 list_T *list;
2326 PyObject *obj = NULL;
2327
2328 if (kwargs)
2329 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002330 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002331 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002332 return NULL;
2333 }
2334
2335 if (!PyArg_ParseTuple(args, "|O", &obj))
2336 return NULL;
2337
2338 if (!(list = py_list_alloc()))
2339 return NULL;
2340
2341 if (obj)
2342 {
2343 PyObject *lookup_dict;
2344
2345 if (!(lookup_dict = PyDict_New()))
2346 {
2347 list_unref(list);
2348 return NULL;
2349 }
2350
2351 if (list_py_concat(list, obj, lookup_dict) == -1)
2352 {
2353 Py_DECREF(lookup_dict);
2354 list_unref(list);
2355 return NULL;
2356 }
2357
2358 Py_DECREF(lookup_dict);
2359 }
2360
2361 return ListNew(subtype, list);
2362}
2363
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002364 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002365ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002366{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002367 pyll_remove(&self->ref, &lastlist);
2368 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002369
2370 DESTRUCTOR_FINISH(self);
2371}
2372
Bram Moolenaardb913952012-06-29 12:54:53 +02002373 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002374ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002375{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002377}
2378
2379 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002380ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002381{
2382 listitem_T *li;
2383
Bram Moolenaard6e39182013-05-21 18:30:34 +02002384 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002385 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002386 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002387 return NULL;
2388 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002389 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002390 if (li == NULL)
2391 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002392 // No more suitable format specifications in python-2.3
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002393 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002394 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002395 return NULL;
2396 }
2397 return ConvertToPyObject(&li->li_tv);
2398}
2399
Bram Moolenaardb913952012-06-29 12:54:53 +02002400 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002401ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2402 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002403{
2404 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002405 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002406
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002407 if (step == 0)
2408 {
2409 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2410 return NULL;
2411 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002412
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002413 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002414 if (list == NULL)
2415 return NULL;
2416
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002417 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002418 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002419 PyObject *item;
2420
2421 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002422 if (item == NULL)
2423 {
2424 Py_DECREF(list);
2425 return NULL;
2426 }
2427
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002428 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002429 }
2430
2431 return list;
2432}
2433
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002434 static PyObject *
2435ListItem(ListObject *self, PyObject* idx)
2436{
2437#if PY_MAJOR_VERSION < 3
2438 if (PyInt_Check(idx))
2439 {
2440 long _idx = PyInt_AsLong(idx);
2441 return ListIndex(self, _idx);
2442 }
2443 else
2444#endif
2445 if (PyLong_Check(idx))
2446 {
2447 long _idx = PyLong_AsLong(idx);
2448 return ListIndex(self, _idx);
2449 }
2450 else if (PySlice_Check(idx))
2451 {
2452 Py_ssize_t start, stop, step, slicelen;
2453
Bram Moolenaar922a4662014-03-30 16:11:43 +02002454 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002455 &start, &stop, &step, &slicelen) < 0)
2456 return NULL;
2457 return ListSlice(self, start, step, slicelen);
2458 }
2459 else
2460 {
2461 RAISE_INVALID_INDEX_TYPE(idx);
2462 return NULL;
2463 }
2464}
2465
2466 static void
2467list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2468 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2469{
2470 while (numreplaced--)
2471 {
2472 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2473 listitem_remove(l, lis[slicelen + numreplaced]);
2474 }
2475 while (numadded--)
2476 {
2477 listitem_T *next;
2478
2479 next = lastaddedli->li_prev;
2480 listitem_remove(l, lastaddedli);
2481 lastaddedli = next;
2482 }
2483}
2484
2485 static int
2486ListAssSlice(ListObject *self, Py_ssize_t first,
2487 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2488{
2489 PyObject *iterator;
2490 PyObject *item;
2491 listitem_T *li;
2492 listitem_T *lastaddedli = NULL;
2493 listitem_T *next;
2494 typval_T v;
2495 list_T *l = self->list;
2496 PyInt i;
2497 PyInt j;
2498 PyInt numreplaced = 0;
2499 PyInt numadded = 0;
2500 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002501 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002502
2503 size = ListLength(self);
2504
2505 if (l->lv_lock)
2506 {
2507 RAISE_LOCKED_LIST;
2508 return -1;
2509 }
2510
2511 if (step == 0)
2512 {
2513 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2514 return -1;
2515 }
2516
2517 if (step != 1 && slicelen == 0)
2518 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002519 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002520 int ret = 0;
2521
2522 if (obj == NULL)
2523 return 0;
2524
2525 if (!(iterator = PyObject_GetIter(obj)))
2526 return -1;
2527
2528 if ((item = PyIter_Next(iterator)))
2529 {
2530 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002531 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002532 "to extended slice"), 0);
2533 Py_DECREF(item);
2534 ret = -1;
2535 }
2536 Py_DECREF(iterator);
2537 return ret;
2538 }
2539
2540 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002541 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002542 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2543 {
2544 PyErr_NoMemory();
2545 return -1;
2546 }
2547
2548 if (first == size)
2549 li = NULL;
2550 else
2551 {
2552 li = list_find(l, (long) first);
2553 if (li == NULL)
2554 {
2555 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2556 (int)first);
2557 if (obj != NULL)
2558 PyMem_Free(lis);
2559 return -1;
2560 }
2561 i = slicelen;
2562 while (i-- && li != NULL)
2563 {
2564 j = step;
2565 next = li;
2566 if (step > 0)
2567 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2568 else
2569 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2570
2571 if (obj == NULL)
2572 listitem_remove(l, li);
2573 else
2574 lis[slicelen - i - 1] = li;
2575
2576 li = next;
2577 }
2578 if (li == NULL && i != -1)
2579 {
2580 PyErr_SET_VIM(N_("internal error: not enough list items"));
2581 if (obj != NULL)
2582 PyMem_Free(lis);
2583 return -1;
2584 }
2585 }
2586
2587 if (obj == NULL)
2588 return 0;
2589
2590 if (!(iterator = PyObject_GetIter(obj)))
2591 {
2592 PyMem_Free(lis);
2593 return -1;
2594 }
2595
2596 i = 0;
2597 while ((item = PyIter_Next(iterator)))
2598 {
2599 if (ConvertFromPyObject(item, &v) == -1)
2600 {
2601 Py_DECREF(iterator);
2602 Py_DECREF(item);
2603 PyMem_Free(lis);
2604 return -1;
2605 }
2606 Py_DECREF(item);
2607 if (list_insert_tv(l, &v, numreplaced < slicelen
2608 ? lis[numreplaced]
2609 : li) == FAIL)
2610 {
2611 clear_tv(&v);
2612 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2613 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2614 PyMem_Free(lis);
2615 return -1;
2616 }
2617 if (numreplaced < slicelen)
2618 {
2619 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002620 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002621 numreplaced++;
2622 }
2623 else
2624 {
2625 if (li)
2626 lastaddedli = li->li_prev;
2627 else
2628 lastaddedli = l->lv_last;
2629 numadded++;
2630 }
2631 clear_tv(&v);
2632 if (step != 1 && i >= slicelen)
2633 {
2634 Py_DECREF(iterator);
2635 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002636 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002637 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002638 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2639 PyMem_Free(lis);
2640 return -1;
2641 }
2642 ++i;
2643 }
2644 Py_DECREF(iterator);
2645
2646 if (step != 1 && i != slicelen)
2647 {
2648 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002649 N_("attempt to assign sequence of size %d to extended slice "
2650 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002651 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2652 PyMem_Free(lis);
2653 return -1;
2654 }
2655
2656 if (PyErr_Occurred())
2657 {
2658 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2659 PyMem_Free(lis);
2660 return -1;
2661 }
2662
2663 for (i = 0; i < numreplaced; i++)
2664 listitem_free(lis[i]);
2665 if (step == 1)
2666 for (i = numreplaced; i < slicelen; i++)
2667 listitem_remove(l, lis[i]);
2668
2669 PyMem_Free(lis);
2670
2671 return 0;
2672}
2673
2674 static int
2675ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2676{
2677 typval_T tv;
2678 list_T *l = self->list;
2679 listitem_T *li;
2680 Py_ssize_t length = ListLength(self);
2681
2682 if (l->lv_lock)
2683 {
2684 RAISE_LOCKED_LIST;
2685 return -1;
2686 }
2687 if (index > length || (index == length && obj == NULL))
2688 {
2689 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2690 return -1;
2691 }
2692
2693 if (obj == NULL)
2694 {
2695 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002696 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002697 clear_tv(&li->li_tv);
2698 vim_free(li);
2699 return 0;
2700 }
2701
2702 if (ConvertFromPyObject(obj, &tv) == -1)
2703 return -1;
2704
2705 if (index == length)
2706 {
2707 if (list_append_tv(l, &tv) == FAIL)
2708 {
2709 clear_tv(&tv);
2710 PyErr_SET_VIM(N_("failed to add item to list"));
2711 return -1;
2712 }
2713 }
2714 else
2715 {
2716 li = list_find(l, (long) index);
2717 clear_tv(&li->li_tv);
2718 copy_tv(&tv, &li->li_tv);
2719 clear_tv(&tv);
2720 }
2721 return 0;
2722}
2723
2724 static Py_ssize_t
2725ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2726{
2727#if PY_MAJOR_VERSION < 3
2728 if (PyInt_Check(idx))
2729 {
2730 long _idx = PyInt_AsLong(idx);
2731 return ListAssIndex(self, _idx, obj);
2732 }
2733 else
2734#endif
2735 if (PyLong_Check(idx))
2736 {
2737 long _idx = PyLong_AsLong(idx);
2738 return ListAssIndex(self, _idx, obj);
2739 }
2740 else if (PySlice_Check(idx))
2741 {
2742 Py_ssize_t start, stop, step, slicelen;
2743
Bram Moolenaar922a4662014-03-30 16:11:43 +02002744 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002745 &start, &stop, &step, &slicelen) < 0)
2746 return -1;
2747 return ListAssSlice(self, start, step, slicelen,
2748 obj);
2749 }
2750 else
2751 {
2752 RAISE_INVALID_INDEX_TYPE(idx);
2753 return -1;
2754 }
2755}
2756
2757 static PyObject *
2758ListConcatInPlace(ListObject *self, PyObject *obj)
2759{
2760 list_T *l = self->list;
2761 PyObject *lookup_dict;
2762
2763 if (l->lv_lock)
2764 {
2765 RAISE_LOCKED_LIST;
2766 return NULL;
2767 }
2768
2769 if (!(lookup_dict = PyDict_New()))
2770 return NULL;
2771
2772 if (list_py_concat(l, obj, lookup_dict) == -1)
2773 {
2774 Py_DECREF(lookup_dict);
2775 return NULL;
2776 }
2777 Py_DECREF(lookup_dict);
2778
2779 Py_INCREF(self);
2780 return (PyObject *)(self);
2781}
2782
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002783typedef struct
2784{
2785 listwatch_T lw;
2786 list_T *list;
2787} listiterinfo_T;
2788
2789 static void
2790ListIterDestruct(listiterinfo_T *lii)
2791{
2792 list_rem_watch(lii->list, &lii->lw);
2793 PyMem_Free(lii);
2794}
2795
2796 static PyObject *
2797ListIterNext(listiterinfo_T **lii)
2798{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002799 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002800
2801 if (!((*lii)->lw.lw_item))
2802 return NULL;
2803
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002804 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002805 return NULL;
2806
2807 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2808
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002809 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002810}
2811
2812 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002813ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002814{
2815 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002816 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002817
2818 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2819 {
2820 PyErr_NoMemory();
2821 return NULL;
2822 }
2823
2824 list_add_watch(l, &lii->lw);
2825 lii->lw.lw_item = l->lv_first;
2826 lii->list = l;
2827
2828 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002829 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2830 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002831}
2832
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002833static char *ListAttrs[] = {
2834 "locked",
2835 NULL
2836};
2837
2838 static PyObject *
2839ListDir(PyObject *self)
2840{
2841 return ObjectDir(self, ListAttrs);
2842}
2843
Bram Moolenaar66b79852012-09-21 14:00:35 +02002844 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002845ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002846{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002847 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002848 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002849 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002850 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002851 return -1;
2852 }
2853
2854 if (strcmp(name, "locked") == 0)
2855 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002856 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002857 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002858 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002859 return -1;
2860 }
2861 else
2862 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002863 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002864 if (istrue == -1)
2865 return -1;
2866 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002867 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002868 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002869 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870 }
2871 return 0;
2872 }
2873 else
2874 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002875 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002876 return -1;
2877 }
2878}
2879
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002880static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002881 (lenfunc) ListLength, // sq_length, len(x)
2882 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2883 0, // RangeRepeat, sq_repeat, x*n
2884 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2885 0, // was_sq_slice, x[i:j]
2886 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2887 0, // was_sq_ass_slice, x[i:j]=v
2888 0, // sq_contains
2889 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2890 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002891};
2892
2893static PyMappingMethods ListAsMapping = {
2894 /* mp_length */ (lenfunc) ListLength,
2895 /* mp_subscript */ (binaryfunc) ListItem,
2896 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2897};
2898
Bram Moolenaardb913952012-06-29 12:54:53 +02002899static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002900 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2901 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2902 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002903};
2904
2905typedef struct
2906{
2907 PyObject_HEAD
2908 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002909 int argc;
2910 typval_T *argv;
2911 dict_T *self;
2912 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002913 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002914} FunctionObject;
2915
2916static PyTypeObject FunctionType;
2917
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002918#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2919 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002920
Bram Moolenaardb913952012-06-29 12:54:53 +02002921 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002922FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002923 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002924{
2925 FunctionObject *self;
2926
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002927 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002928 if (self == NULL)
2929 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002930
2931 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002932 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002933 if (!translated_function_exists(name))
2934 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002935 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002936 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002937 return NULL;
2938 }
2939 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002940 }
2941 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002942 {
2943 char_u *p;
2944
2945 if ((p = get_expanded_name(name,
2946 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002947 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002948 PyErr_FORMAT(PyExc_ValueError,
2949 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002950 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002951 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002952
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002953 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2954 {
2955 char_u *np;
2956 size_t len = STRLEN(p) + 1;
2957
Bram Moolenaar51e14382019-05-25 20:21:28 +02002958 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002959 {
2960 vim_free(p);
2961 return NULL;
2962 }
2963 mch_memmove(np, "<SNR>", 5);
2964 mch_memmove(np + 5, p + 3, len - 3);
2965 vim_free(p);
2966 self->name = np;
2967 }
2968 else
2969 self->name = p;
2970 }
2971
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002972 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002973 self->argc = argc;
2974 self->argv = argv;
2975 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002976 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002977
2978 if (self->argv || self->self)
2979 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2980
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002981 return (PyObject *)(self);
2982}
2983
2984 static PyObject *
2985FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2986{
2987 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002988 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002989 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002990 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002991 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002992 typval_T selfdicttv;
2993 typval_T argstv;
2994 list_T *argslist = NULL;
2995 dict_T *selfdict = NULL;
2996 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002997 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002998 typval_T *argv = NULL;
2999 typval_T *curtv;
3000 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003001
Bram Moolenaar8110a092016-04-14 15:56:09 +02003002 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003003 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003004 selfdictObject = PyDict_GetItemString(kwargs, "self");
3005 if (selfdictObject != NULL)
3006 {
3007 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3008 return NULL;
3009 selfdict = selfdicttv.vval.v_dict;
3010 }
3011 argsObject = PyDict_GetItemString(kwargs, "args");
3012 if (argsObject != NULL)
3013 {
3014 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3015 {
3016 dict_unref(selfdict);
3017 return NULL;
3018 }
3019 argslist = argstv.vval.v_list;
3020
3021 argc = argslist->lv_len;
3022 if (argc != 0)
3023 {
3024 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003025 if (argv == NULL)
3026 {
3027 PyErr_NoMemory();
3028 dict_unref(selfdict);
3029 list_unref(argslist);
3030 return NULL;
3031 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003032 curtv = argv;
3033 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3034 copy_tv(&li->li_tv, curtv++);
3035 }
3036 list_unref(argslist);
3037 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003038 if (selfdict != NULL)
3039 {
3040 auto_rebind = FALSE;
3041 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3042 if (autoRebindObject != NULL)
3043 {
3044 auto_rebind = PyObject_IsTrue(autoRebindObject);
3045 if (auto_rebind == -1)
3046 {
3047 dict_unref(selfdict);
3048 list_unref(argslist);
3049 return NULL;
3050 }
3051 }
3052 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003053 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003054
Bram Moolenaar389a1792013-06-23 13:00:44 +02003055 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003056 {
3057 dict_unref(selfdict);
3058 while (argc--)
3059 clear_tv(&argv[argc]);
3060 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003061 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003062 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003063
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003064 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003065
Bram Moolenaar389a1792013-06-23 13:00:44 +02003066 PyMem_Free(name);
3067
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003068 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003069}
3070
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003071 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003072FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003073{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003074 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003075 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003076 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003077 for (i = 0; i < self->argc; ++i)
3078 clear_tv(&self->argv[i]);
3079 PyMem_Free(self->argv);
3080 dict_unref(self->self);
3081 if (self->argv || self->self)
3082 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003083
3084 DESTRUCTOR_FINISH(self);
3085}
3086
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003087static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003088 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003089 NULL
3090};
3091
3092 static PyObject *
3093FunctionDir(PyObject *self)
3094{
3095 return ObjectDir(self, FunctionAttrs);
3096}
3097
Bram Moolenaardb913952012-06-29 12:54:53 +02003098 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003099FunctionAttr(FunctionObject *self, char *name)
3100{
3101 list_T *list;
3102 int i;
3103 if (strcmp(name, "name") == 0)
3104 return PyString_FromString((char *)(self->name));
3105 else if (strcmp(name, "args") == 0)
3106 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003107 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003108 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003109
Bram Moolenaar8110a092016-04-14 15:56:09 +02003110 for (i = 0; i < self->argc; ++i)
3111 list_append_tv(list, &self->argv[i]);
3112 return NEW_LIST(list);
3113 }
3114 else if (strcmp(name, "self") == 0)
3115 return self->self == NULL
3116 ? AlwaysNone(NULL)
3117 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003118 else if (strcmp(name, "auto_rebind") == 0)
3119 return self->auto_rebind
3120 ? AlwaysTrue(NULL)
3121 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003122 else if (strcmp(name, "__members__") == 0)
3123 return ObjectDir(NULL, FunctionAttrs);
3124 return NULL;
3125}
3126
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003127/*
3128 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003129 *
3130 * "exported" should be set to true when it is needed to construct a partial
3131 * that may be stored in a variable (i.e. may be freed by Vim).
3132 */
3133 static void
3134set_partial(FunctionObject *self, partial_T *pt, int exported)
3135{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003136 int i;
3137
3138 pt->pt_name = self->name;
3139 if (self->argv)
3140 {
3141 pt->pt_argc = self->argc;
3142 if (exported)
3143 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003144 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003145 for (i = 0; i < pt->pt_argc; ++i)
3146 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3147 }
3148 else
3149 pt->pt_argv = self->argv;
3150 }
3151 else
3152 {
3153 pt->pt_argc = 0;
3154 pt->pt_argv = NULL;
3155 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003156 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003157 pt->pt_dict = self->self;
3158 if (exported && self->self)
3159 ++pt->pt_dict->dv_refcount;
3160 if (exported)
3161 pt->pt_name = vim_strsave(pt->pt_name);
3162 pt->pt_refcount = 1;
3163}
3164
3165 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003166FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003167{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003168 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003169 typval_T args;
3170 typval_T selfdicttv;
3171 typval_T rettv;
3172 dict_T *selfdict = NULL;
3173 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003174 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003175 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003176 partial_T pt;
3177 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003178
Bram Moolenaar8110a092016-04-14 15:56:09 +02003179 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003180 return NULL;
3181
3182 if (kwargs != NULL)
3183 {
3184 selfdictObject = PyDict_GetItemString(kwargs, "self");
3185 if (selfdictObject != NULL)
3186 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003187 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003188 {
3189 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003190 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003191 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003192 selfdict = selfdicttv.vval.v_dict;
3193 }
3194 }
3195
Bram Moolenaar8110a092016-04-14 15:56:09 +02003196 if (self->argv || self->self)
3197 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003198 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003199 set_partial(self, &pt, FALSE);
3200 pt_ptr = &pt;
3201 }
3202
Bram Moolenaar71700b82013-05-15 17:49:05 +02003203 Py_BEGIN_ALLOW_THREADS
3204 Python_Lock_Vim();
3205
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003206 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003207 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003208
3209 Python_Release_Vim();
3210 Py_END_ALLOW_THREADS
3211
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003212 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003213 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003214 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003215 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003216 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003217 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003218 }
3219 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003220 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003221
Bram Moolenaardb913952012-06-29 12:54:53 +02003222 clear_tv(&args);
3223 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003224 if (selfdict != NULL)
3225 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003226
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003227 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003228}
3229
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003230 static PyObject *
3231FunctionRepr(FunctionObject *self)
3232{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003233 PyObject *ret;
3234 garray_T repr_ga;
3235 int i;
3236 char_u *tofree = NULL;
3237 typval_T tv;
3238 char_u numbuf[NUMBUFLEN];
3239
3240 ga_init2(&repr_ga, (int)sizeof(char), 70);
3241 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3242 if (self->name)
3243 ga_concat(&repr_ga, self->name);
3244 else
3245 ga_concat(&repr_ga, (char_u *)"<NULL>");
3246 ga_append(&repr_ga, '\'');
3247 if (self->argv)
3248 {
3249 ga_concat(&repr_ga, (char_u *)", args=[");
3250 ++emsg_silent;
3251 for (i = 0; i < self->argc; i++)
3252 {
3253 if (i != 0)
3254 ga_concat(&repr_ga, (char_u *)", ");
3255 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3256 get_copyID()));
3257 vim_free(tofree);
3258 }
3259 --emsg_silent;
3260 ga_append(&repr_ga, ']');
3261 }
3262 if (self->self)
3263 {
3264 ga_concat(&repr_ga, (char_u *)", self=");
3265 tv.v_type = VAR_DICT;
3266 tv.vval.v_dict = self->self;
3267 ++emsg_silent;
3268 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3269 --emsg_silent;
3270 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003271 if (self->auto_rebind)
3272 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003273 }
3274 ga_append(&repr_ga, '>');
3275 ret = PyString_FromString((char *)repr_ga.ga_data);
3276 ga_clear(&repr_ga);
3277 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003278}
3279
Bram Moolenaardb913952012-06-29 12:54:53 +02003280static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003281 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3282 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003283};
3284
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003285/*
3286 * Options object
3287 */
3288
3289static PyTypeObject OptionsType;
3290
3291typedef int (*checkfun)(void *);
3292
3293typedef struct
3294{
3295 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003296 int opt_type;
3297 void *from;
3298 checkfun Check;
3299 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003300} OptionsObject;
3301
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003302 static int
3303dummy_check(void *arg UNUSED)
3304{
3305 return 0;
3306}
3307
3308 static PyObject *
3309OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3310{
3311 OptionsObject *self;
3312
Bram Moolenaar774267b2013-05-21 20:51:59 +02003313 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003314 if (self == NULL)
3315 return NULL;
3316
3317 self->opt_type = opt_type;
3318 self->from = from;
3319 self->Check = Check;
3320 self->fromObj = fromObj;
3321 if (fromObj)
3322 Py_INCREF(fromObj);
3323
3324 return (PyObject *)(self);
3325}
3326
3327 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003328OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003329{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003330 PyObject_GC_UnTrack((void *)(self));
3331 Py_XDECREF(self->fromObj);
3332 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003333}
3334
3335 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003337{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003338 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003339 return 0;
3340}
3341
3342 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003343OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003344{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003345 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003346 return 0;
3347}
3348
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003349 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003350OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003351{
3352 char_u *key;
3353 int flags;
3354 long numval;
3355 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003356 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003357
Bram Moolenaard6e39182013-05-21 18:30:34 +02003358 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003359 return NULL;
3360
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003361 if (!(key = StringToChars(keyObject, &todecref)))
3362 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003363
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003364 if (*key == NUL)
3365 {
3366 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003367 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003368 return NULL;
3369 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003370
3371 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003373
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003374 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003375
3376 if (flags == 0)
3377 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003378 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003379 return NULL;
3380 }
3381
3382 if (flags & SOPT_UNSET)
3383 {
3384 Py_INCREF(Py_None);
3385 return Py_None;
3386 }
3387 else if (flags & SOPT_BOOL)
3388 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003389 PyObject *ret;
3390 ret = numval ? Py_True : Py_False;
3391 Py_INCREF(ret);
3392 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003393 }
3394 else if (flags & SOPT_NUM)
3395 return PyInt_FromLong(numval);
3396 else if (flags & SOPT_STRING)
3397 {
3398 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003399 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003400 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003401 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003402 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003403 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003404 else
3405 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003406 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003407 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003408 return NULL;
3409 }
3410 }
3411 else
3412 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003413 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003414 return NULL;
3415 }
3416}
3417
3418 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003419OptionsContains(OptionsObject *self, PyObject *keyObject)
3420{
3421 char_u *key;
3422 PyObject *todecref;
3423
3424 if (!(key = StringToChars(keyObject, &todecref)))
3425 return -1;
3426
3427 if (*key == NUL)
3428 {
3429 Py_XDECREF(todecref);
3430 return 0;
3431 }
3432
3433 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3434 {
3435 Py_XDECREF(todecref);
3436 return 1;
3437 }
3438 else
3439 {
3440 Py_XDECREF(todecref);
3441 return 0;
3442 }
3443}
3444
3445typedef struct
3446{
3447 void *lastoption;
3448 int opt_type;
3449} optiterinfo_T;
3450
3451 static PyObject *
3452OptionsIterNext(optiterinfo_T **oii)
3453{
3454 char_u *name;
3455
3456 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3457 return PyString_FromString((char *)name);
3458
3459 return NULL;
3460}
3461
3462 static PyObject *
3463OptionsIter(OptionsObject *self)
3464{
3465 optiterinfo_T *oii;
3466
3467 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3468 {
3469 PyErr_NoMemory();
3470 return NULL;
3471 }
3472
3473 oii->opt_type = self->opt_type;
3474 oii->lastoption = NULL;
3475
3476 return IterNew(oii,
3477 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3478 NULL, NULL);
3479}
3480
3481 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003482set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003483{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003484 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003485
3486 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3487 {
3488 if (VimTryEnd())
3489 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003490 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003491 return FAIL;
3492 }
3493 return OK;
3494}
3495
3496 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003497set_option_value_for(
3498 char_u *key,
3499 int numval,
3500 char_u *stringval,
3501 int opt_flags,
3502 int opt_type,
3503 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003504{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003505 win_T *save_curwin = NULL;
3506 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003507 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003508 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003509
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003510 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003511 switch (opt_type)
3512 {
3513 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003514 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003515 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003516 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003517 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003518 if (VimTryEnd())
3519 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003520 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003521 return -1;
3522 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003523 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3524 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003525 break;
3526 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003527 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003528 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003529 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003530 break;
3531 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003532 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003533 break;
3534 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003535 if (set_ret == FAIL)
3536 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003537 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003538}
3539
3540 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003541OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003542{
3543 char_u *key;
3544 int flags;
3545 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003546 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003547 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003548
Bram Moolenaard6e39182013-05-21 18:30:34 +02003549 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003550 return -1;
3551
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003552 if (!(key = StringToChars(keyObject, &todecref)))
3553 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003554
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003555 if (*key == NUL)
3556 {
3557 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003558 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003559 return -1;
3560 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003561
3562 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003563 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003564
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003565 if (flags == 0)
3566 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003567 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003568 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003569 return -1;
3570 }
3571
3572 if (valObject == NULL)
3573 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003574 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003575 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003576 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003577 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003578 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003579 return -1;
3580 }
3581 else if (!(flags & SOPT_GLOBAL))
3582 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003583 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003584 N_("unable to unset option %s "
3585 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003586 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003587 return -1;
3588 }
3589 else
3590 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003591 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003592 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003593 return 0;
3594 }
3595 }
3596
Bram Moolenaard6e39182013-05-21 18:30:34 +02003597 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003598
3599 if (flags & SOPT_BOOL)
3600 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003601 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003602
Bram Moolenaarb983f752013-05-15 16:11:50 +02003603 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003604 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003605 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003606 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003607 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003608 }
3609 else if (flags & SOPT_NUM)
3610 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003611 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003612
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003613 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003615 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003616 return -1;
3617 }
3618
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003619 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003620 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003621 }
3622 else
3623 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003624 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003625 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003626
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003627 if ((val = StringToChars(valObject, &todecref2)))
3628 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003629 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003630 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003631 Py_XDECREF(todecref2);
3632 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003633 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003634 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003635 }
3636
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003637 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003638
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003639 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003640}
3641
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003642static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003643 0, // sq_length
3644 0, // sq_concat
3645 0, // sq_repeat
3646 0, // sq_item
3647 0, // sq_slice
3648 0, // sq_ass_item
3649 0, // sq_ass_slice
3650 (objobjproc) OptionsContains, // sq_contains
3651 0, // sq_inplace_concat
3652 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003653};
3654
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003655static PyMappingMethods OptionsAsMapping = {
3656 (lenfunc) NULL,
3657 (binaryfunc) OptionsItem,
3658 (objobjargproc) OptionsAssItem,
3659};
3660
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003661// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003662
3663typedef struct
3664{
3665 PyObject_HEAD
3666 tabpage_T *tab;
3667} TabPageObject;
3668
3669static PyObject *WinListNew(TabPageObject *tabObject);
3670
3671static PyTypeObject TabPageType;
3672
3673 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003674CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003675{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003676 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003677 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003678 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003679 return -1;
3680 }
3681
3682 return 0;
3683}
3684
3685 static PyObject *
3686TabPageNew(tabpage_T *tab)
3687{
3688 TabPageObject *self;
3689
3690 if (TAB_PYTHON_REF(tab))
3691 {
3692 self = TAB_PYTHON_REF(tab);
3693 Py_INCREF(self);
3694 }
3695 else
3696 {
3697 self = PyObject_NEW(TabPageObject, &TabPageType);
3698 if (self == NULL)
3699 return NULL;
3700 self->tab = tab;
3701 TAB_PYTHON_REF(tab) = self;
3702 }
3703
3704 return (PyObject *)(self);
3705}
3706
3707 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003708TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003709{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003710 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3711 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003712
3713 DESTRUCTOR_FINISH(self);
3714}
3715
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003716static char *TabPageAttrs[] = {
3717 "windows", "number", "vars", "window", "valid",
3718 NULL
3719};
3720
3721 static PyObject *
3722TabPageDir(PyObject *self)
3723{
3724 return ObjectDir(self, TabPageAttrs);
3725}
3726
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003727 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003728TabPageAttrValid(TabPageObject *self, char *name)
3729{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003730 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003731
3732 if (strcmp(name, "valid") != 0)
3733 return NULL;
3734
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003735 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3736 Py_INCREF(ret);
3737 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003738}
3739
3740 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003741TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003742{
3743 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003744 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003745 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003746 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003747 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003748 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003749 else if (strcmp(name, "window") == 0)
3750 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003751 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003752 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003753 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003754 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003755 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003756 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003757 else if (strcmp(name, "__members__") == 0)
3758 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003759 return NULL;
3760}
3761
3762 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003763TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003764{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003766 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003767 else
3768 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003769 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003770
3771 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003772 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3773 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003775 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003776 }
3777}
3778
3779static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003780 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003781 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3782 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783};
3784
3785/*
3786 * Window list object
3787 */
3788
3789static PyTypeObject TabListType;
3790static PySequenceMethods TabListAsSeq;
3791
3792typedef struct
3793{
3794 PyObject_HEAD
3795} TabListObject;
3796
3797 static PyInt
3798TabListLength(PyObject *self UNUSED)
3799{
3800 tabpage_T *tp = first_tabpage;
3801 PyInt n = 0;
3802
3803 while (tp != NULL)
3804 {
3805 ++n;
3806 tp = tp->tp_next;
3807 }
3808
3809 return n;
3810}
3811
3812 static PyObject *
3813TabListItem(PyObject *self UNUSED, PyInt n)
3814{
3815 tabpage_T *tp;
3816
3817 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3818 if (n == 0)
3819 return TabPageNew(tp);
3820
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003821 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003822 return NULL;
3823}
3824
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003825/*
3826 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003827 */
3828
3829typedef struct
3830{
3831 PyObject_HEAD
3832 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003833 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003834} WindowObject;
3835
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003836static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003837
3838 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003839CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003840{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003841 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003842 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003843 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844 return -1;
3845 }
3846
3847 return 0;
3848}
3849
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003850 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003851WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003852{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003853 /*
3854 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003855 * If we add a "w_python*_ref" field to the win_T structure,
3856 * then we can get at it in win_free() in vim. We then
3857 * need to create only ONE Python object per window - if
3858 * we try to create a second, just INCREF the existing one
3859 * and return it. The (single) Python object referring to
3860 * the window is stored in "w_python*_ref".
3861 * On a win_free() we set the Python object's win_T* field
3862 * to an invalid value. We trap all uses of a window
3863 * object, and reject them if the win_T* field is invalid.
3864 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003865 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003866 * w_python_ref and w_python3_ref fields respectively.
3867 */
3868
3869 WindowObject *self;
3870
3871 if (WIN_PYTHON_REF(win))
3872 {
3873 self = WIN_PYTHON_REF(win);
3874 Py_INCREF(self);
3875 }
3876 else
3877 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003878 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003879 if (self == NULL)
3880 return NULL;
3881 self->win = win;
3882 WIN_PYTHON_REF(win) = self;
3883 }
3884
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003885 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3886
Bram Moolenaar971db462013-05-12 18:44:48 +02003887 return (PyObject *)(self);
3888}
3889
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003890 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003891WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003892{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003893 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003894 if (self->win && self->win != INVALID_WINDOW_VALUE)
3895 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003896 Py_XDECREF(((PyObject *)(self->tabObject)));
3897 PyObject_GC_Del((void *)(self));
3898}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003899
Bram Moolenaar774267b2013-05-21 20:51:59 +02003900 static int
3901WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3902{
3903 Py_VISIT(((PyObject *)(self->tabObject)));
3904 return 0;
3905}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003906
Bram Moolenaar774267b2013-05-21 20:51:59 +02003907 static int
3908WindowClear(WindowObject *self)
3909{
3910 Py_CLEAR(self->tabObject);
3911 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003912}
3913
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003914 static win_T *
3915get_firstwin(TabPageObject *tabObject)
3916{
3917 if (tabObject)
3918 {
3919 if (CheckTabPage(tabObject))
3920 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003921 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003922 else if (tabObject->tab == curtab)
3923 return firstwin;
3924 else
3925 return tabObject->tab->tp_firstwin;
3926 }
3927 else
3928 return firstwin;
3929}
Bram Moolenaare950f992018-06-10 13:55:55 +02003930
3931// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003932static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003933 "buffer",
3934 "cursor",
3935 "height",
3936 "row",
3937 "width",
3938 "col",
3939 "vars",
3940 "options",
3941 "number",
3942 "tabpage",
3943 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003944 NULL
3945};
3946
3947 static PyObject *
3948WindowDir(PyObject *self)
3949{
3950 return ObjectDir(self, WindowAttrs);
3951}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003952
Bram Moolenaar971db462013-05-12 18:44:48 +02003953 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003954WindowAttrValid(WindowObject *self, char *name)
3955{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003956 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003957
3958 if (strcmp(name, "valid") != 0)
3959 return NULL;
3960
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003961 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3962 Py_INCREF(ret);
3963 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003964}
3965
3966 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003967WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003968{
3969 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003970 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003971 else if (strcmp(name, "cursor") == 0)
3972 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003973 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003974
3975 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3976 }
3977 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003978 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003979 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003981 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003982 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003983 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003984 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003985 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003986 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003987 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3989 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003990 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003991 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003993 return NULL;
3994 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003995 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003996 }
3997 else if (strcmp(name, "tabpage") == 0)
3998 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 Py_INCREF(self->tabObject);
4000 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004001 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004002 else if (strcmp(name, "__members__") == 0)
4003 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004004 else
4005 return NULL;
4006}
4007
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004009WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004011 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004012 return -1;
4013
4014 if (strcmp(name, "buffer") == 0)
4015 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004016 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004017 return -1;
4018 }
4019 else if (strcmp(name, "cursor") == 0)
4020 {
4021 long lnum;
4022 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004023
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004024 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004025 return -1;
4026
Bram Moolenaard6e39182013-05-21 18:30:34 +02004027 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004028 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004029 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030 return -1;
4031 }
4032
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004033 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004034 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 return -1;
4036
Bram Moolenaard6e39182013-05-21 18:30:34 +02004037 self->win->w_cursor.lnum = lnum;
4038 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004039 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004040 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004041 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004042 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004044 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004045 return 0;
4046 }
4047 else if (strcmp(name, "height") == 0)
4048 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004049 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004050 win_T *savewin;
4051
Bram Moolenaardee2e312013-06-23 16:35:47 +02004052 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004053 return -1;
4054
4055#ifdef FEAT_GUI
4056 need_mouse_correct = TRUE;
4057#endif
4058 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004059 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004060
4061 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004062 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004063 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004064 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 return -1;
4066
4067 return 0;
4068 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 else if (strcmp(name, "width") == 0)
4070 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004071 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072 win_T *savewin;
4073
Bram Moolenaardee2e312013-06-23 16:35:47 +02004074 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004075 return -1;
4076
4077#ifdef FEAT_GUI
4078 need_mouse_correct = TRUE;
4079#endif
4080 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004081 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004082
4083 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004084 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004086 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 return -1;
4088
4089 return 0;
4090 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 else
4092 {
4093 PyErr_SetString(PyExc_AttributeError, name);
4094 return -1;
4095 }
4096}
4097
4098 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004099WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004100{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004101 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004102 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103 else
4104 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004105 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106
Bram Moolenaar6d216452013-05-12 19:00:41 +02004107 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004108 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 (self));
4110 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004111 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 }
4113}
4114
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004115static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004116 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004117 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4118 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004119};
4120
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004121/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004122 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004123 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004124
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004125static PyTypeObject WinListType;
4126static PySequenceMethods WinListAsSeq;
4127
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004128typedef struct
4129{
4130 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004131 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004132} WinListObject;
4133
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004134 static PyObject *
4135WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004137 WinListObject *self;
4138
4139 self = PyObject_NEW(WinListObject, &WinListType);
4140 self->tabObject = tabObject;
4141 Py_INCREF(tabObject);
4142
4143 return (PyObject *)(self);
4144}
4145
4146 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004147WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004148{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004149 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004150
4151 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004152 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004153 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004154 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004155
4156 DESTRUCTOR_FINISH(self);
4157}
4158
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004159 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004160WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004161{
4162 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004163 PyInt n = 0;
4164
Bram Moolenaard6e39182013-05-21 18:30:34 +02004165 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004166 return -1;
4167
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004168 while (w != NULL)
4169 {
4170 ++n;
4171 w = W_NEXT(w);
4172 }
4173
4174 return n;
4175}
4176
4177 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004178WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004179{
4180 win_T *w;
4181
Bram Moolenaard6e39182013-05-21 18:30:34 +02004182 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004183 return NULL;
4184
4185 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004187 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004188
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004189 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190 return NULL;
4191}
4192
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004193/*
4194 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004195 *
4196 * The result is in allocated memory. All internal nulls are replaced by
4197 * newline characters. It is an error for the string to contain newline
4198 * characters.
4199 *
4200 * On errors, the Python exception data is set, and NULL is returned.
4201 */
4202 static char *
4203StringToLine(PyObject *obj)
4204{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004205 char *str;
4206 char *save;
4207 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004208 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004209 PyInt i;
4210 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004211
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004212 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004213 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004214 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4215 || str == NULL)
4216 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004217 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004218 else if (PyUnicode_Check(obj))
4219 {
4220 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4221 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004222
Bram Moolenaardaa27022013-06-24 22:33:30 +02004223 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004224 || str == NULL)
4225 {
4226 Py_DECREF(bytes);
4227 return NULL;
4228 }
4229 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004230 else
4231 {
4232#if PY_MAJOR_VERSION < 3
4233 PyErr_FORMAT(PyExc_TypeError,
4234 N_("expected str() or unicode() instance, but got %s"),
4235 Py_TYPE_NAME(obj));
4236#else
4237 PyErr_FORMAT(PyExc_TypeError,
4238 N_("expected bytes() or str() instance, but got %s"),
4239 Py_TYPE_NAME(obj));
4240#endif
4241 return NULL;
4242 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004243
4244 /*
4245 * Error checking: String must not contain newlines, as we
4246 * are replacing a single line, and we must replace it with
4247 * a single line.
4248 * A trailing newline is removed, so that append(f.readlines()) works.
4249 */
4250 p = memchr(str, '\n', len);
4251 if (p != NULL)
4252 {
4253 if (p == str + len - 1)
4254 --len;
4255 else
4256 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004257 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004258 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004259 return NULL;
4260 }
4261 }
4262
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004263 /*
4264 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004265 * newline characters, as is the vim convention.
4266 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004267 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004268 if (save == NULL)
4269 {
4270 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004271 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004272 return NULL;
4273 }
4274
4275 for (i = 0; i < len; ++i)
4276 {
4277 if (str[i] == '\0')
4278 save[i] = '\n';
4279 else
4280 save[i] = str[i];
4281 }
4282
4283 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004284 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004285
4286 return save;
4287}
4288
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004289/*
4290 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004291 * in Vim format (1-based). The line is returned as a Python
4292 * string object.
4293 */
4294 static PyObject *
4295GetBufferLine(buf_T *buf, PyInt n)
4296{
4297 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4298}
4299
4300
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004301/*
4302 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004303 * are in Vim format (1-based). The range is from lo up to, but not
4304 * including, hi. The list is returned as a Python list of string objects.
4305 */
4306 static PyObject *
4307GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4308{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004309 PyInt i;
4310 PyInt n = hi - lo;
4311 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004312
4313 if (list == NULL)
4314 return NULL;
4315
4316 for (i = 0; i < n; ++i)
4317 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004318 PyObject *string = LineToString(
4319 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004320
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004321 // Error check - was the Python string creation OK?
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004322 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004323 {
4324 Py_DECREF(list);
4325 return NULL;
4326 }
4327
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004328 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004329 }
4330
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004331 // The ownership of the Python list is passed to the caller (ie,
4332 // the caller should Py_DECREF() the object when it is finished
4333 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004334
4335 return list;
4336}
4337
4338/*
4339 * Check if deleting lines made the cursor position invalid.
4340 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4341 * deleted).
4342 */
4343 static void
4344py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4345{
4346 if (curwin->w_cursor.lnum >= lo)
4347 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004348 // Adjust the cursor position if it's in/after the changed
4349 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004350 if (curwin->w_cursor.lnum >= hi)
4351 {
4352 curwin->w_cursor.lnum += extra;
4353 check_cursor_col();
4354 }
4355 else if (extra < 0)
4356 {
4357 curwin->w_cursor.lnum = lo;
4358 check_cursor();
4359 }
4360 else
4361 check_cursor_col();
4362 changed_cline_bef_curs();
4363 }
4364 invalidate_botline();
4365}
4366
Bram Moolenaar19e60942011-06-19 00:27:51 +02004367/*
4368 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004369 * in Vim format (1-based). The replacement line is given as
4370 * a Python string object. The object is checked for validity
4371 * and correct format. Errors are returned as a value of FAIL.
4372 * The return value is OK on success.
4373 * If OK is returned and len_change is not NULL, *len_change
4374 * is set to the change in the buffer length.
4375 */
4376 static int
4377SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4378{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004379 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004380 win_T *save_curwin = NULL;
4381 tabpage_T *save_curtab = NULL;
4382
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004383 // First of all, we check the type of the supplied Python object.
4384 // There are three cases:
4385 // 1. NULL, or None - this is a deletion.
4386 // 2. A string - this is a replacement.
4387 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004388 if (line == Py_None || line == NULL)
4389 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004391 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004392
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004393 VimTryStart();
4394
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004395 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004396 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004398 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004399 else
4400 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004401 if (buf == curbuf && (save_curwin != NULL
4402 || save_curbuf.br_buf == NULL))
4403 // Using an existing window for the buffer, adjust the cursor
4404 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004405 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004406 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004407 // Only adjust marks if we managed to switch to a window that
4408 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004409 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004410 }
4411
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004412 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004413
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004414 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004415 return FAIL;
4416
4417 if (len_change)
4418 *len_change = -1;
4419
4420 return OK;
4421 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004422 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004424 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425
4426 if (save == NULL)
4427 return FAIL;
4428
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004429 VimTryStart();
4430
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004431 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004433 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434
4435 if (u_savesub((linenr_T)n) == FAIL)
4436 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004437 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 vim_free(save);
4439 }
4440 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4441 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004442 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 vim_free(save);
4444 }
4445 else
4446 changed_bytes((linenr_T)n, 0);
4447
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004448 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004449
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004450 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004451 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004452 check_cursor_col();
4453
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004454 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004455 return FAIL;
4456
4457 if (len_change)
4458 *len_change = 0;
4459
4460 return OK;
4461 }
4462 else
4463 {
4464 PyErr_BadArgument();
4465 return FAIL;
4466 }
4467}
4468
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004469/*
4470 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004471 * Vim format (1-based). The range is from lo up to, but not including, hi.
4472 * The replacement lines are given as a Python list of string objects. The
4473 * list is checked for validity and correct format. Errors are returned as a
4474 * value of FAIL. The return value is OK on success.
4475 * If OK is returned and len_change is not NULL, *len_change
4476 * is set to the change in the buffer length.
4477 */
4478 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004479SetBufferLineList(
4480 buf_T *buf,
4481 PyInt lo,
4482 PyInt hi,
4483 PyObject *list,
4484 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004485{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004486 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004487 win_T *save_curwin = NULL;
4488 tabpage_T *save_curtab = NULL;
4489
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004490 // First of all, we check the type of the supplied Python object.
4491 // There are three cases:
4492 // 1. NULL, or None - this is a deletion.
4493 // 2. A list - this is a replacement.
4494 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004495 if (list == Py_None || list == NULL)
4496 {
4497 PyInt i;
4498 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004499
4500 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004501 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004502 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004503
4504 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004505 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004506 else
4507 {
4508 for (i = 0; i < n; ++i)
4509 {
4510 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4511 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004512 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004513 break;
4514 }
4515 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004516 if (buf == curbuf && (save_curwin != NULL
4517 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004518 // Using an existing window for the buffer, adjust the cursor
4519 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004520 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004521 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004522 // Only adjust marks if we managed to switch to a window that
4523 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004524 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004525 }
4526
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004527 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004528
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004529 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004530 return FAIL;
4531
4532 if (len_change)
4533 *len_change = -n;
4534
4535 return OK;
4536 }
4537 else if (PyList_Check(list))
4538 {
4539 PyInt i;
4540 PyInt new_len = PyList_Size(list);
4541 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004542 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004543 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004544
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004545 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004546 array = NULL;
4547 else
4548 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004549 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004550 if (array == NULL)
4551 {
4552 PyErr_NoMemory();
4553 return FAIL;
4554 }
4555 }
4556
4557 for (i = 0; i < new_len; ++i)
4558 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004559 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004560
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004561 if (!(line = PyList_GetItem(list, i)) ||
4562 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004563 {
4564 while (i)
4565 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004566 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004567 return FAIL;
4568 }
4569 }
4570
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004571 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004572 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004573
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004574 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004575 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004576
4577 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004578 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004579
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004580 // If the size of the range is reducing (ie, new_len < old_len) we
4581 // need to delete some old_len. We do this at the start, by
4582 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004583 if (!PyErr_Occurred())
4584 {
4585 for (i = 0; i < old_len - new_len; ++i)
4586 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4587 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004588 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004589 break;
4590 }
4591 extra -= i;
4592 }
4593
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004594 // For as long as possible, replace the existing old_len with the
4595 // new old_len. This is a more efficient operation, as it requires
4596 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597 if (!PyErr_Occurred())
4598 {
4599 for (i = 0; i < old_len && i < new_len; ++i)
4600 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4601 == FAIL)
4602 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004603 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004604 break;
4605 }
4606 }
4607 else
4608 i = 0;
4609
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004610 // Now we may need to insert the remaining new old_len. If we do, we
4611 // must free the strings as we finish with them (we can't pass the
4612 // responsibility to vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004613 if (!PyErr_Occurred())
4614 {
4615 while (i < new_len)
4616 {
4617 if (ml_append((linenr_T)(lo + i - 1),
4618 (char_u *)array[i], 0, FALSE) == FAIL)
4619 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004620 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004621 break;
4622 }
4623 vim_free(array[i]);
4624 ++i;
4625 ++extra;
4626 }
4627 }
4628
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004629 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004630 while (i < new_len)
4631 {
4632 vim_free(array[i]);
4633 ++i;
4634 }
4635
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004636 // Free the array of old_len. All of its contents have now
4637 // been dealt with (either freed, or the responsibility passed
4638 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004639 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004640
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004641 // Adjust marks. Invalidate any which lie in the
4642 // changed range, and move any in the remainder of the buffer.
4643 // Only adjust marks if we managed to switch to a window that holds
4644 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004645 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004646 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004647 (long)MAXLNUM, (long)extra);
4648 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4649
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004650 if (buf == curbuf && (save_curwin != NULL
4651 || save_curbuf.br_buf == NULL))
4652 // Using an existing window for the buffer, adjust the cursor
4653 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004654 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4655
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004656 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004657 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004658
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004659 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004660 return FAIL;
4661
4662 if (len_change)
4663 *len_change = new_len - old_len;
4664
4665 return OK;
4666 }
4667 else
4668 {
4669 PyErr_BadArgument();
4670 return FAIL;
4671 }
4672}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004673
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004674/*
4675 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004676 * The line number is in Vim format (1-based). The lines to be inserted are
4677 * given as a Python list of string objects or as a single string. The lines
4678 * to be added are checked for validity and correct format. Errors are
4679 * returned as a value of FAIL. The return value is OK on success.
4680 * If OK is returned and len_change is not NULL, *len_change
4681 * is set to the change in the buffer length.
4682 */
4683 static int
4684InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4685{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004686 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004687 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004688 tabpage_T *save_curtab = NULL;
4689
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004690 // First of all, we check the type of the supplied Python object.
4691 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004692 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004694 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004695
4696 if (str == NULL)
4697 return FAIL;
4698
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004699 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004700 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004701 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004702
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004703 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004704 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004705 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004706 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004707 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004708 // Only adjust marks if we managed to switch to a window that
4709 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004710 appended_lines_mark((linenr_T)n, 1L);
4711
4712 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004713 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004714 update_screen(VALID);
4715
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004716 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004717 return FAIL;
4718
4719 if (len_change)
4720 *len_change = 1;
4721
4722 return OK;
4723 }
4724 else if (PyList_Check(lines))
4725 {
4726 PyInt i;
4727 PyInt size = PyList_Size(lines);
4728 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004729
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004730 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731 if (array == NULL)
4732 {
4733 PyErr_NoMemory();
4734 return FAIL;
4735 }
4736
4737 for (i = 0; i < size; ++i)
4738 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004739 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004740
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004741 if (!(line = PyList_GetItem(lines, i)) ||
4742 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004743 {
4744 while (i)
4745 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004746 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004747 return FAIL;
4748 }
4749 }
4750
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004751 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004752 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004753 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004754
4755 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004756 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004757 else
4758 {
4759 for (i = 0; i < size; ++i)
4760 {
4761 if (ml_append((linenr_T)(n + i),
4762 (char_u *)array[i], 0, FALSE) == FAIL)
4763 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004764 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004765
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004766 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004767 while (i < size)
4768 vim_free(array[i++]);
4769
4770 break;
4771 }
4772 vim_free(array[i]);
4773 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004774 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004775 // Only adjust marks if we managed to switch to a window that
4776 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004777 appended_lines_mark((linenr_T)n, (long)i);
4778 }
4779
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004780 // Free the array of lines. All of its contents have now
4781 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004782 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004783 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004784
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004785 update_screen(VALID);
4786
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004787 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004788 return FAIL;
4789
4790 if (len_change)
4791 *len_change = size;
4792
4793 return OK;
4794 }
4795 else
4796 {
4797 PyErr_BadArgument();
4798 return FAIL;
4799 }
4800}
4801
4802/*
4803 * Common routines for buffers and line ranges
4804 * -------------------------------------------
4805 */
4806
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004807typedef struct
4808{
4809 PyObject_HEAD
4810 buf_T *buf;
4811} BufferObject;
4812
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004813 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004814CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004815{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004816 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004818 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004819 return -1;
4820 }
4821
4822 return 0;
4823}
4824
4825 static PyObject *
4826RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4827{
4828 if (CheckBuffer(self))
4829 return NULL;
4830
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004831 if (end == -1)
4832 end = self->buf->b_ml.ml_line_count;
4833
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004834 if (n < 0)
4835 n += end - start + 1;
4836
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004837 if (n < 0 || n > end - start)
4838 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004839 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004840 return NULL;
4841 }
4842
4843 return GetBufferLine(self->buf, n+start);
4844}
4845
4846 static PyObject *
4847RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4848{
4849 PyInt size;
4850
4851 if (CheckBuffer(self))
4852 return NULL;
4853
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004854 if (end == -1)
4855 end = self->buf->b_ml.ml_line_count;
4856
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004857 size = end - start + 1;
4858
4859 if (lo < 0)
4860 lo = 0;
4861 else if (lo > size)
4862 lo = size;
4863 if (hi < 0)
4864 hi = 0;
4865 if (hi < lo)
4866 hi = lo;
4867 else if (hi > size)
4868 hi = size;
4869
4870 return GetBufferLineList(self->buf, lo+start, hi+start);
4871}
4872
4873 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004874RBAsItem(
4875 BufferObject *self,
4876 PyInt n,
4877 PyObject *valObject,
4878 PyInt start,
4879 PyInt end,
4880 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004881{
4882 PyInt len_change;
4883
4884 if (CheckBuffer(self))
4885 return -1;
4886
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004887 if (end == -1)
4888 end = self->buf->b_ml.ml_line_count;
4889
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004890 if (n < 0)
4891 n += end - start + 1;
4892
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004893 if (n < 0 || n > end - start)
4894 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004895 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004896 return -1;
4897 }
4898
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004899 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004900 return -1;
4901
4902 if (new_end)
4903 *new_end = end + len_change;
4904
4905 return 0;
4906}
4907
Bram Moolenaar19e60942011-06-19 00:27:51 +02004908 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004909RBAsSlice(
4910 BufferObject *self,
4911 PyInt lo,
4912 PyInt hi,
4913 PyObject *valObject,
4914 PyInt start,
4915 PyInt end,
4916 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004917{
4918 PyInt size;
4919 PyInt len_change;
4920
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004921 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004922 if (CheckBuffer(self))
4923 return -1;
4924
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004925 if (end == -1)
4926 end = self->buf->b_ml.ml_line_count;
4927
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004928 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004929 size = end - start + 1;
4930
4931 if (lo < 0)
4932 lo = 0;
4933 else if (lo > size)
4934 lo = size;
4935 if (hi < 0)
4936 hi = 0;
4937 if (hi < lo)
4938 hi = lo;
4939 else if (hi > size)
4940 hi = size;
4941
4942 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004943 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004944 return -1;
4945
4946 if (new_end)
4947 *new_end = end + len_change;
4948
4949 return 0;
4950}
4951
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004952
4953 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004954RBAppend(
4955 BufferObject *self,
4956 PyObject *args,
4957 PyInt start,
4958 PyInt end,
4959 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004960{
4961 PyObject *lines;
4962 PyInt len_change;
4963 PyInt max;
4964 PyInt n;
4965
4966 if (CheckBuffer(self))
4967 return NULL;
4968
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004969 if (end == -1)
4970 end = self->buf->b_ml.ml_line_count;
4971
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004972 max = n = end - start + 1;
4973
4974 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4975 return NULL;
4976
4977 if (n < 0 || n > max)
4978 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004979 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004980 return NULL;
4981 }
4982
4983 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4984 return NULL;
4985
4986 if (new_end)
4987 *new_end = end + len_change;
4988
4989 Py_INCREF(Py_None);
4990 return Py_None;
4991}
4992
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004993// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004994
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004995static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004996static PySequenceMethods RangeAsSeq;
4997static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004998
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004999typedef struct
5000{
5001 PyObject_HEAD
5002 BufferObject *buf;
5003 PyInt start;
5004 PyInt end;
5005} RangeObject;
5006
5007 static PyObject *
5008RangeNew(buf_T *buf, PyInt start, PyInt end)
5009{
5010 BufferObject *bufr;
5011 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005012 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005013 if (self == NULL)
5014 return NULL;
5015
5016 bufr = (BufferObject *)BufferNew(buf);
5017 if (bufr == NULL)
5018 {
5019 Py_DECREF(self);
5020 return NULL;
5021 }
5022 Py_INCREF(bufr);
5023
5024 self->buf = bufr;
5025 self->start = start;
5026 self->end = end;
5027
5028 return (PyObject *)(self);
5029}
5030
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005031 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005032RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005033{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005034 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005035 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005036 PyObject_GC_Del((void *)(self));
5037}
5038
5039 static int
5040RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5041{
5042 Py_VISIT(((PyObject *)(self->buf)));
5043 return 0;
5044}
5045
5046 static int
5047RangeClear(RangeObject *self)
5048{
5049 Py_CLEAR(self->buf);
5050 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005051}
5052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005053 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005054RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005055{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005056 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005057 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005058 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005059
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005061}
5062
5063 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005064RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005065{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005067}
5068
5069 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005070RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005071{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005072 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005073}
5074
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005075static char *RangeAttrs[] = {
5076 "start", "end",
5077 NULL
5078};
5079
5080 static PyObject *
5081RangeDir(PyObject *self)
5082{
5083 return ObjectDir(self, RangeAttrs);
5084}
5085
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005087RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005090}
5091
5092 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005095 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005096 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5097 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005098 else
5099 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005100 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101
5102 if (name == NULL)
5103 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005105 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005106 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107 }
5108}
5109
5110static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005111 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005112 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005113 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5114 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005115};
5116
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005117static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005118static PySequenceMethods BufferAsSeq;
5119static PyMappingMethods BufferAsMapping;
5120
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005121 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005122BufferNew(buf_T *buf)
5123{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005124 /*
5125 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005126 * If we add a "b_python*_ref" field to the buf_T structure,
5127 * then we can get at it in buf_freeall() in vim. We then
5128 * need to create only ONE Python object per buffer - if
5129 * we try to create a second, just INCREF the existing one
5130 * and return it. The (single) Python object referring to
5131 * the buffer is stored in "b_python*_ref".
5132 * Question: what to do on a buf_freeall(). We'll probably
5133 * have to either delete the Python object (DECREF it to
5134 * zero - a bad idea, as it leaves dangling refs!) or
5135 * set the buf_T * value to an invalid value (-1?), which
5136 * means we need checks in all access functions... Bah.
5137 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005138 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005139 * b_python_ref and b_python3_ref fields respectively.
5140 */
5141
5142 BufferObject *self;
5143
5144 if (BUF_PYTHON_REF(buf) != NULL)
5145 {
5146 self = BUF_PYTHON_REF(buf);
5147 Py_INCREF(self);
5148 }
5149 else
5150 {
5151 self = PyObject_NEW(BufferObject, &BufferType);
5152 if (self == NULL)
5153 return NULL;
5154 self->buf = buf;
5155 BUF_PYTHON_REF(buf) = self;
5156 }
5157
5158 return (PyObject *)(self);
5159}
5160
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005161 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005162BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005163{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005164 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5165 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005166
5167 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005168}
5169
Bram Moolenaar971db462013-05-12 18:44:48 +02005170 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005171BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005172{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005173 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005174 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005175 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005176
Bram Moolenaard6e39182013-05-21 18:30:34 +02005177 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005178}
5179
5180 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005181BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005182{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005183 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005184}
5185
5186 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005187BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005188{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005189 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005190}
5191
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005192static char *BufferAttrs[] = {
5193 "name", "number", "vars", "options", "valid",
5194 NULL
5195};
5196
5197 static PyObject *
5198BufferDir(PyObject *self)
5199{
5200 return ObjectDir(self, BufferAttrs);
5201}
5202
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005203 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005204BufferAttrValid(BufferObject *self, char *name)
5205{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005206 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005207
5208 if (strcmp(name, "valid") != 0)
5209 return NULL;
5210
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005211 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5212 Py_INCREF(ret);
5213 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005214}
5215
5216 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005217BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005218{
5219 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005220 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005221 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005222 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005223 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005224 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005225 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005226 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005227 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5228 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005229 else if (strcmp(name, "__members__") == 0)
5230 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005231 else
5232 return NULL;
5233}
5234
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005235 static int
5236BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5237{
5238 if (CheckBuffer(self))
5239 return -1;
5240
5241 if (strcmp(name, "name") == 0)
5242 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005243 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005244 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005245 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005246 PyObject *todecref;
5247
5248 if (!(val = StringToChars(valObject, &todecref)))
5249 return -1;
5250
5251 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005252 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005253 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005254 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005255 aucmd_restbuf(&aco);
5256 Py_XDECREF(todecref);
5257 if (VimTryEnd())
5258 return -1;
5259
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005260 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005261 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005262 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005263 return -1;
5264 }
5265 return 0;
5266 }
5267 else
5268 {
5269 PyErr_SetString(PyExc_AttributeError, name);
5270 return -1;
5271 }
5272}
5273
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005274 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005275BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005276{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005277 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005278}
5279
5280 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005281BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005282{
5283 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005284 char_u *pmark;
5285 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005286 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005287 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005288
Bram Moolenaard6e39182013-05-21 18:30:34 +02005289 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005290 return NULL;
5291
Bram Moolenaar389a1792013-06-23 13:00:44 +02005292 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005293 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005294
Bram Moolenaar389a1792013-06-23 13:00:44 +02005295 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005296 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005297 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005298 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005299 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005300 return NULL;
5301 }
5302
5303 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005304
5305 Py_XDECREF(todecref);
5306
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005307 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005308 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005309 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005310 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005311 if (VimTryEnd())
5312 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005313
5314 if (posp == NULL)
5315 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005316 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005317 return NULL;
5318 }
5319
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005320 if (posp->lnum <= 0)
5321 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005322 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005323 Py_INCREF(Py_None);
5324 return Py_None;
5325 }
5326
5327 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5328}
5329
5330 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005331BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005332{
5333 PyInt start;
5334 PyInt end;
5335
Bram Moolenaard6e39182013-05-21 18:30:34 +02005336 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005337 return NULL;
5338
5339 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5340 return NULL;
5341
Bram Moolenaard6e39182013-05-21 18:30:34 +02005342 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005343}
5344
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005345 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005346BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005347{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005348 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005349 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005350 else
5351 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005352 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005353
5354 if (name == NULL)
5355 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005356
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005357 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005358 }
5359}
5360
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005361static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005362 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005363 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005364 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005365 {"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 +02005366 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5367 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005368};
5369
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005370/*
5371 * Buffer list object - Implementation
5372 */
5373
5374static PyTypeObject BufMapType;
5375
5376typedef struct
5377{
5378 PyObject_HEAD
5379} BufMapObject;
5380
5381 static PyInt
5382BufMapLength(PyObject *self UNUSED)
5383{
5384 buf_T *b = firstbuf;
5385 PyInt n = 0;
5386
5387 while (b)
5388 {
5389 ++n;
5390 b = b->b_next;
5391 }
5392
5393 return n;
5394}
5395
5396 static PyObject *
5397BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5398{
5399 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005400 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005401
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005402 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005403 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005404
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005405 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005406
5407 if (b)
5408 return BufferNew(b);
5409 else
5410 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005411 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005412 return NULL;
5413 }
5414}
5415
5416 static void
5417BufMapIterDestruct(PyObject *buffer)
5418{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005419 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005420 if (buffer)
5421 {
5422 Py_DECREF(buffer);
5423 }
5424}
5425
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005426 static int
5427BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5428{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005429 if (buffer)
5430 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005431 return 0;
5432}
5433
5434 static int
5435BufMapIterClear(PyObject **buffer)
5436{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005437 if (*buffer)
5438 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005439 return 0;
5440}
5441
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005442 static PyObject *
5443BufMapIterNext(PyObject **buffer)
5444{
5445 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005446 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005447
5448 if (!*buffer)
5449 return NULL;
5450
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005451 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005452
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005453 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005454 {
5455 *buffer = NULL;
5456 return NULL;
5457 }
5458
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005459 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005460 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005461 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005462 return NULL;
5463 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005464 // Do not increment reference: we no longer hold it (decref), but whoever
5465 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005466 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005467}
5468
5469 static PyObject *
5470BufMapIter(PyObject *self UNUSED)
5471{
5472 PyObject *buffer;
5473
5474 buffer = BufferNew(firstbuf);
5475 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005476 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5477 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005478}
5479
5480static PyMappingMethods BufMapAsMapping = {
5481 (lenfunc) BufMapLength,
5482 (binaryfunc) BufMapItem,
5483 (objobjargproc) 0,
5484};
5485
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005486// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005487
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005488static char *CurrentAttrs[] = {
5489 "buffer", "window", "line", "range", "tabpage",
5490 NULL
5491};
5492
5493 static PyObject *
5494CurrentDir(PyObject *self)
5495{
5496 return ObjectDir(self, CurrentAttrs);
5497}
5498
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005499 static PyObject *
5500CurrentGetattr(PyObject *self UNUSED, char *name)
5501{
5502 if (strcmp(name, "buffer") == 0)
5503 return (PyObject *)BufferNew(curbuf);
5504 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005505 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005506 else if (strcmp(name, "tabpage") == 0)
5507 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005508 else if (strcmp(name, "line") == 0)
5509 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5510 else if (strcmp(name, "range") == 0)
5511 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005512 else if (strcmp(name, "__members__") == 0)
5513 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005514 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005515#if PY_MAJOR_VERSION < 3
5516 return Py_FindMethod(WindowMethods, self, name);
5517#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005518 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005519#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005520}
5521
5522 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005523CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005524{
5525 if (strcmp(name, "line") == 0)
5526 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005527 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5528 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005529 return -1;
5530
5531 return 0;
5532 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005533 else if (strcmp(name, "buffer") == 0)
5534 {
5535 int count;
5536
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005537 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005538 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005539 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005540 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005541 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005542 return -1;
5543 }
5544
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005545 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005546 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005547 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005548
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005549 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005550 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5551 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005552 if (VimTryEnd())
5553 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005554 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005555 return -1;
5556 }
5557
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005558 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005559 }
5560 else if (strcmp(name, "window") == 0)
5561 {
5562 int count;
5563
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005564 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005565 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005566 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005567 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005568 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005569 return -1;
5570 }
5571
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005572 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005573 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005574 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005575
5576 if (!count)
5577 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005578 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005579 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005580 return -1;
5581 }
5582
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005583 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005584 win_goto(((WindowObject *)(valObject))->win);
5585 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005586 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005587 if (VimTryEnd())
5588 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005589 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005590 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005591 return -1;
5592 }
5593
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005594 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005595 }
5596 else if (strcmp(name, "tabpage") == 0)
5597 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005598 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005599 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005600 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005601 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005602 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005603 return -1;
5604 }
5605
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005606 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005607 return -1;
5608
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005609 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005610 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5611 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005612 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005613 if (VimTryEnd())
5614 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005615 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005616 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005617 return -1;
5618 }
5619
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005620 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005621 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005622 else
5623 {
5624 PyErr_SetString(PyExc_AttributeError, name);
5625 return -1;
5626 }
5627}
5628
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005629static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005630 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005631 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5632 { NULL, NULL, 0, NULL}
5633};
5634
Bram Moolenaardb913952012-06-29 12:54:53 +02005635 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005636init_range_cmd(exarg_T *eap)
5637{
5638 RangeStart = eap->line1;
5639 RangeEnd = eap->line2;
5640}
5641
5642 static void
5643init_range_eval(typval_T *rettv UNUSED)
5644{
5645 RangeStart = (PyInt) curwin->w_cursor.lnum;
5646 RangeEnd = RangeStart;
5647}
5648
5649 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005650run_cmd(const char *cmd, void *arg UNUSED
5651#ifdef PY_CAN_RECURSE
5652 , PyGILState_STATE *pygilstate UNUSED
5653#endif
5654 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005655{
Bram Moolenaar41009372013-07-01 22:03:04 +02005656 PyObject *run_ret;
5657 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5658 if (run_ret != NULL)
5659 {
5660 Py_DECREF(run_ret);
5661 }
5662 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005664 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005665 PyErr_Clear();
5666 }
5667 else
5668 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005669}
5670
5671static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5672static int code_hdr_len = 30;
5673
5674 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005675run_do(const char *cmd, void *arg UNUSED
5676#ifdef PY_CAN_RECURSE
5677 , PyGILState_STATE *pygilstate
5678#endif
5679 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005680{
5681 PyInt lnum;
5682 size_t len;
5683 char *code;
5684 int status;
5685 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005686 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005687 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005688
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005689 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005691 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005692 return;
5693 }
5694
5695 len = code_hdr_len + STRLEN(cmd);
5696 code = PyMem_New(char, len + 1);
5697 memcpy(code, code_hdr, code_hdr_len);
5698 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005699 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5700 status = -1;
5701 if (run_ret != NULL)
5702 {
5703 status = 0;
5704 Py_DECREF(run_ret);
5705 }
5706 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5707 {
5708 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005709 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005710 PyErr_Clear();
5711 return;
5712 }
5713 else
5714 PyErr_PrintEx(1);
5715
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005716 PyMem_Free(code);
5717
5718 if (status)
5719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005720 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005721 return;
5722 }
5723
5724 status = 0;
5725 pymain = PyImport_AddModule("__main__");
5726 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005727#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005728 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005729#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005730
5731 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5732 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005733 PyObject *line;
5734 PyObject *linenr;
5735 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005736
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005737#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005738 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005739#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005740 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005741 if (lnum > curbuf->b_ml.ml_line_count
5742 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005743 goto err;
5744 if (!(linenr = PyInt_FromLong((long) lnum)))
5745 {
5746 Py_DECREF(line);
5747 goto err;
5748 }
5749 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5750 Py_DECREF(line);
5751 Py_DECREF(linenr);
5752 if (!ret)
5753 goto err;
5754
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005755 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005756 if (curbuf != was_curbuf)
5757 {
5758 Py_XDECREF(ret);
5759 goto err;
5760 }
5761
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005762 if (ret != Py_None)
5763 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005764 {
5765 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005766 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005767 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005768
5769 Py_XDECREF(ret);
5770 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005771#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005772 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005773#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005774 }
5775 goto out;
5776err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005777#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005778 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005779#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005780 PyErr_PrintEx(0);
5781 PythonIO_Flush();
5782 status = 1;
5783out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005784#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005785 if (!status)
5786 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005787#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005788 Py_DECREF(pyfunc);
5789 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5790 if (status)
5791 return;
5792 check_cursor();
5793 update_curbuf(NOT_VALID);
5794}
5795
5796 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005797run_eval(const char *cmd, typval_T *rettv
5798#ifdef PY_CAN_RECURSE
5799 , PyGILState_STATE *pygilstate UNUSED
5800#endif
5801 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005802{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005803 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005804
Bram Moolenaar41009372013-07-01 22:03:04 +02005805 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005806 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005807 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005808 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005810 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005811 PyErr_Clear();
5812 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005813 else
5814 {
5815 if (PyErr_Occurred() && !msg_silent)
5816 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005817 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005818 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005819 }
5820 else
5821 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005822 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005823 emsg(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005824 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005825 }
5826 PyErr_Clear();
5827}
5828
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005829 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005830set_ref_in_py(const int copyID)
5831{
5832 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005833 list_T *ll;
5834 int i;
5835 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005836 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005837
5838 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005839 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005840 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005841 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5842 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005843 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005844
5845 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005847 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005848 {
5849 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005850 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005851 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005852 }
5853
Bram Moolenaar8110a092016-04-14 15:56:09 +02005854 if (lastfunc != NULL)
5855 {
5856 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5857 {
5858 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005859 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005860 if (func->argc)
5861 for (i = 0; !abort && i < func->argc; ++i)
5862 abort = abort
5863 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5864 }
5865 }
5866
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005867 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005868}
5869
5870 static int
5871set_string_copy(char_u *str, typval_T *tv)
5872{
5873 tv->vval.v_string = vim_strsave(str);
5874 if (tv->vval.v_string == NULL)
5875 {
5876 PyErr_NoMemory();
5877 return -1;
5878 }
5879 return 0;
5880}
5881
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005882 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005883pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005884{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005885 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005886 char_u *key;
5887 dictitem_T *di;
5888 PyObject *keyObject;
5889 PyObject *valObject;
5890 Py_ssize_t iter = 0;
5891
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005892 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005893 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005894
5895 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005896 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005897
5898 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5899 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005900 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901
Bram Moolenaara03e6312013-05-29 22:49:26 +02005902 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005903 {
5904 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005905 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005906 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005907
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005908 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005909 {
5910 dict_unref(dict);
5911 return -1;
5912 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005913
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005914 if (*key == NUL)
5915 {
5916 dict_unref(dict);
5917 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005918 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005919 return -1;
5920 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005921
5922 di = dictitem_alloc(key);
5923
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005924 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005925
5926 if (di == NULL)
5927 {
5928 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005929 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930 return -1;
5931 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005932
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005933 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934 {
5935 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005936 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937 return -1;
5938 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005939
5940 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005942 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005943 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005944 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005945 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946 return -1;
5947 }
5948 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005949
5950 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951 return 0;
5952}
5953
5954 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005955pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005957 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005958 char_u *key;
5959 dictitem_T *di;
5960 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005961 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005962 PyObject *keyObject;
5963 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005964
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005965 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005967
5968 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005969 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005971 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005972 {
5973 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005974 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005976
5977 if (!(iterator = PyObject_GetIter(list)))
5978 {
5979 dict_unref(dict);
5980 Py_DECREF(list);
5981 return -1;
5982 }
5983 Py_DECREF(list);
5984
5985 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005986 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005987 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005988
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005989 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005990 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005991 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005992 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005993 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005994 return -1;
5995 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005996
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005997 if (*key == NUL)
5998 {
5999 Py_DECREF(keyObject);
6000 Py_DECREF(iterator);
6001 Py_XDECREF(todecref);
6002 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006003 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006004 return -1;
6005 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006006
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006007 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006008 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006009 Py_DECREF(keyObject);
6010 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006011 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006012 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006013 return -1;
6014 }
6015
6016 di = dictitem_alloc(key);
6017
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006018 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006019 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006020
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006021 if (di == NULL)
6022 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006023 Py_DECREF(iterator);
6024 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006025 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006026 PyErr_NoMemory();
6027 return -1;
6028 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006029
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006030 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006031 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006032 Py_DECREF(iterator);
6033 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006034 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006035 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006036 return -1;
6037 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006038
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006039 Py_DECREF(valObject);
6040
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006041 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006043 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006044 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006045 dictitem_free(di);
6046 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006047 return -1;
6048 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006049 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006050 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006051 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006052 return 0;
6053}
6054
6055 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006056pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006057{
6058 list_T *l;
6059
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006062
6063 tv->v_type = VAR_LIST;
6064 tv->vval.v_list = l;
6065
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006066 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006067 {
6068 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006069 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006072 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006073 return 0;
6074}
6075
Bram Moolenaardb913952012-06-29 12:54:53 +02006076typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6077
6078 static int
6079convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006080 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006081{
6082 PyObject *capsule;
6083 char hexBuf[sizeof(void *) * 2 + 3];
6084
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006085 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006086
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006087#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006088 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006089#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006090 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006091#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006092 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006093 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006094#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006095 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006096#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006097 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006098#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006099 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6100 {
6101 Py_DECREF(capsule);
6102 tv->v_type = VAR_UNKNOWN;
6103 return -1;
6104 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006105
6106 Py_DECREF(capsule);
6107
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006108 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006109 {
6110 tv->v_type = VAR_UNKNOWN;
6111 return -1;
6112 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006113 // As we are not using copy_tv which increments reference count we must
6114 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006115 if (tv->v_type == VAR_DICT)
6116 ++tv->vval.v_dict->dv_refcount;
6117 else if (tv->v_type == VAR_LIST)
6118 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006119 }
6120 else
6121 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006122 typval_T *v;
6123
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006124#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006125 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006126#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006127 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006128#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006129 copy_tv(v, tv);
6130 }
6131 return 0;
6132}
6133
6134 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006135ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6136{
6137 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006138 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006139
6140 if (!(lookup_dict = PyDict_New()))
6141 return -1;
6142
6143 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6144 {
6145 tv->v_type = VAR_DICT;
6146 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6147 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006148 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006149 }
6150 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006151 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006152 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006153 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006154 else
6155 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006156 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006157 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006158 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006159 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006160 }
6161 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006162 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006163}
6164
6165 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006166ConvertFromPySequence(PyObject *obj, typval_T *tv)
6167{
6168 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006169 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006170
6171 if (!(lookup_dict = PyDict_New()))
6172 return -1;
6173
6174 if (PyType_IsSubtype(obj->ob_type, &ListType))
6175 {
6176 tv->v_type = VAR_LIST;
6177 tv->vval.v_list = (((ListObject *)(obj))->list);
6178 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006179 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006180 }
6181 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006182 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006183 else
6184 {
6185 PyErr_FORMAT(PyExc_TypeError,
6186 N_("unable to convert %s to vim list"),
6187 Py_TYPE_NAME(obj));
6188 ret = -1;
6189 }
6190 Py_DECREF(lookup_dict);
6191 return ret;
6192}
6193
6194 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006195ConvertFromPyObject(PyObject *obj, typval_T *tv)
6196{
6197 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006198 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006199
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006200 if (!(lookup_dict = PyDict_New()))
6201 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006202 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006203 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006204 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006205}
6206
6207 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006208_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006209{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006210 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006211 {
6212 tv->v_type = VAR_DICT;
6213 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6214 ++tv->vval.v_dict->dv_refcount;
6215 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006216 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006217 {
6218 tv->v_type = VAR_LIST;
6219 tv->vval.v_list = (((ListObject *)(obj))->list);
6220 ++tv->vval.v_list->lv_refcount;
6221 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006222 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006223 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006224 FunctionObject *func = (FunctionObject *) obj;
6225 if (func->self != NULL || func->argv != NULL)
6226 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006227 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6228
Bram Moolenaar8110a092016-04-14 15:56:09 +02006229 set_partial(func, pt, TRUE);
6230 tv->vval.v_partial = pt;
6231 tv->v_type = VAR_PARTIAL;
6232 }
6233 else
6234 {
6235 if (set_string_copy(func->name, tv) == -1)
6236 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006237
Bram Moolenaar8110a092016-04-14 15:56:09 +02006238 tv->v_type = VAR_FUNC;
6239 }
6240 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006241 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006242 else if (PyBytes_Check(obj))
6243 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006244 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006245
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006246 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006247 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006248 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006249 return -1;
6250
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006251 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 return -1;
6253
6254 tv->v_type = VAR_STRING;
6255 }
6256 else if (PyUnicode_Check(obj))
6257 {
6258 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006259 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006260
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006261 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006262 if (bytes == NULL)
6263 return -1;
6264
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006265 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006266 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006267 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006268 return -1;
6269
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006270 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006271 {
6272 Py_XDECREF(bytes);
6273 return -1;
6274 }
6275 Py_XDECREF(bytes);
6276
6277 tv->v_type = VAR_STRING;
6278 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006279#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006280 else if (PyInt_Check(obj))
6281 {
6282 tv->v_type = VAR_NUMBER;
6283 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006284 if (PyErr_Occurred())
6285 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006286 }
6287#endif
6288 else if (PyLong_Check(obj))
6289 {
6290 tv->v_type = VAR_NUMBER;
6291 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006292 if (PyErr_Occurred())
6293 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006294 }
6295 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006296 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006297#ifdef FEAT_FLOAT
6298 else if (PyFloat_Check(obj))
6299 {
6300 tv->v_type = VAR_FLOAT;
6301 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6302 }
6303#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006304 else if (PyObject_HasAttrString(obj, "keys"))
6305 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006306 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006307 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006308 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006309 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006310 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006311 else if (PyNumber_Check(obj))
6312 {
6313 PyObject *num;
6314
6315 if (!(num = PyNumber_Long(obj)))
6316 return -1;
6317
6318 tv->v_type = VAR_NUMBER;
6319 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6320
6321 Py_DECREF(num);
6322 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006323 else if (obj == Py_None)
6324 {
6325 tv->v_type = VAR_SPECIAL;
6326 tv->vval.v_number = VVAL_NONE;
6327 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006328 else
6329 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006330 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006331 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006332 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006333 return -1;
6334 }
6335 return 0;
6336}
6337
6338 static PyObject *
6339ConvertToPyObject(typval_T *tv)
6340{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006341 typval_T *argv;
6342 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006343 if (tv == NULL)
6344 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006345 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006346 return NULL;
6347 }
6348 switch (tv->v_type)
6349 {
6350 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006351 return PyBytes_FromString(tv->vval.v_string == NULL
6352 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006353 case VAR_NUMBER:
6354 return PyLong_FromLong((long) tv->vval.v_number);
6355#ifdef FEAT_FLOAT
6356 case VAR_FLOAT:
6357 return PyFloat_FromDouble((double) tv->vval.v_float);
6358#endif
6359 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006360 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006361 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006362 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006363 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006364 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006365 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006366 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006367 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006368 if (tv->vval.v_partial->pt_argc)
6369 {
6370 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6371 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6372 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6373 }
6374 else
6375 argv = NULL;
6376 if (tv->vval.v_partial->pt_dict != NULL)
6377 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006378 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006379 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006380 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006381 tv->vval.v_partial->pt_dict,
6382 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006383 case VAR_BLOB:
6384 return PyBytes_FromStringAndSize(
6385 (char*) tv->vval.v_blob->bv_ga.ga_data,
6386 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006387 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006388 case VAR_CHANNEL:
6389 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006390 Py_INCREF(Py_None);
6391 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006392 case VAR_SPECIAL:
6393 switch (tv->vval.v_number)
6394 {
6395 case VVAL_FALSE: return AlwaysFalse(NULL);
6396 case VVAL_TRUE: return AlwaysTrue(NULL);
6397 case VVAL_NONE:
6398 case VVAL_NULL: return AlwaysNone(NULL);
6399 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006400 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006401 return NULL;
6402 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006403 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006404}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006405
6406typedef struct
6407{
6408 PyObject_HEAD
6409} CurrentObject;
6410static PyTypeObject CurrentType;
6411
6412 static void
6413init_structs(void)
6414{
6415 vim_memset(&OutputType, 0, sizeof(OutputType));
6416 OutputType.tp_name = "vim.message";
6417 OutputType.tp_basicsize = sizeof(OutputObject);
6418 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6419 OutputType.tp_doc = "vim message object";
6420 OutputType.tp_methods = OutputMethods;
6421#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006422 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6423 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006424 OutputType.tp_alloc = call_PyType_GenericAlloc;
6425 OutputType.tp_new = call_PyType_GenericNew;
6426 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006427 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006428#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006429 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6430 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006431 // Disabled, because this causes a crash in test86
6432 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006433#endif
6434
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006435 vim_memset(&IterType, 0, sizeof(IterType));
6436 IterType.tp_name = "vim.iter";
6437 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006438 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006439 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006440 IterType.tp_iter = (getiterfunc)IterIter;
6441 IterType.tp_iternext = (iternextfunc)IterNext;
6442 IterType.tp_dealloc = (destructor)IterDestructor;
6443 IterType.tp_traverse = (traverseproc)IterTraverse;
6444 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006445
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006446 vim_memset(&BufferType, 0, sizeof(BufferType));
6447 BufferType.tp_name = "vim.buffer";
6448 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006449 BufferType.tp_dealloc = (destructor)BufferDestructor;
6450 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006451 BufferType.tp_as_sequence = &BufferAsSeq;
6452 BufferType.tp_as_mapping = &BufferAsMapping;
6453 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6454 BufferType.tp_doc = "vim buffer object";
6455 BufferType.tp_methods = BufferMethods;
6456#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006457 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006458 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006459 BufferType.tp_alloc = call_PyType_GenericAlloc;
6460 BufferType.tp_new = call_PyType_GenericNew;
6461 BufferType.tp_free = call_PyObject_Free;
6462#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006463 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006464 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465#endif
6466
6467 vim_memset(&WindowType, 0, sizeof(WindowType));
6468 WindowType.tp_name = "vim.window";
6469 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006470 WindowType.tp_dealloc = (destructor)WindowDestructor;
6471 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006472 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006473 WindowType.tp_doc = "vim Window object";
6474 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006475 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6476 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006477#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006478 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6479 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006480 WindowType.tp_alloc = call_PyType_GenericAlloc;
6481 WindowType.tp_new = call_PyType_GenericNew;
6482 WindowType.tp_free = call_PyObject_Free;
6483#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006484 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6485 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006486#endif
6487
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006488 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6489 TabPageType.tp_name = "vim.tabpage";
6490 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006491 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6492 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006493 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6494 TabPageType.tp_doc = "vim tab page object";
6495 TabPageType.tp_methods = TabPageMethods;
6496#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006497 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006498 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6499 TabPageType.tp_new = call_PyType_GenericNew;
6500 TabPageType.tp_free = call_PyObject_Free;
6501#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006503#endif
6504
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006505 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6506 BufMapType.tp_name = "vim.bufferlist";
6507 BufMapType.tp_basicsize = sizeof(BufMapObject);
6508 BufMapType.tp_as_mapping = &BufMapAsMapping;
6509 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006510 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006511 BufferType.tp_doc = "vim buffer list";
6512
6513 vim_memset(&WinListType, 0, sizeof(WinListType));
6514 WinListType.tp_name = "vim.windowlist";
6515 WinListType.tp_basicsize = sizeof(WinListType);
6516 WinListType.tp_as_sequence = &WinListAsSeq;
6517 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6518 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006519 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006520
6521 vim_memset(&TabListType, 0, sizeof(TabListType));
6522 TabListType.tp_name = "vim.tabpagelist";
6523 TabListType.tp_basicsize = sizeof(TabListType);
6524 TabListType.tp_as_sequence = &TabListAsSeq;
6525 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6526 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006527
6528 vim_memset(&RangeType, 0, sizeof(RangeType));
6529 RangeType.tp_name = "vim.range";
6530 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006531 RangeType.tp_dealloc = (destructor)RangeDestructor;
6532 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006533 RangeType.tp_as_sequence = &RangeAsSeq;
6534 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006535 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006536 RangeType.tp_doc = "vim Range object";
6537 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006538 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6539 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006540#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006541 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006542 RangeType.tp_alloc = call_PyType_GenericAlloc;
6543 RangeType.tp_new = call_PyType_GenericNew;
6544 RangeType.tp_free = call_PyObject_Free;
6545#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006546 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006547#endif
6548
6549 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6550 CurrentType.tp_name = "vim.currentdata";
6551 CurrentType.tp_basicsize = sizeof(CurrentObject);
6552 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6553 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006554 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006555#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006556 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6557 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006558#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006559 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6560 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006561#endif
6562
6563 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6564 DictionaryType.tp_name = "vim.dictionary";
6565 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006566 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006567 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006569 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006570 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6571 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006572 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6573 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6574 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006575#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006576 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6577 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006578#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006579 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6580 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006581#endif
6582
6583 vim_memset(&ListType, 0, sizeof(ListType));
6584 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006585 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006586 ListType.tp_basicsize = sizeof(ListObject);
6587 ListType.tp_as_sequence = &ListAsSeq;
6588 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006589 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006590 ListType.tp_doc = "list pushing modifications to vim structure";
6591 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006592 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006593 ListType.tp_new = (newfunc)ListConstructor;
6594 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006595#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006596 ListType.tp_getattro = (getattrofunc)ListGetattro;
6597 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006598#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006599 ListType.tp_getattr = (getattrfunc)ListGetattr;
6600 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006601#endif
6602
6603 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006604 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006605 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006606 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6607 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006608 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006609 FunctionType.tp_doc = "object that calls vim function";
6610 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006611 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006612 FunctionType.tp_new = (newfunc)FunctionConstructor;
6613 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006614#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006615 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006618#endif
6619
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006620 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6621 OptionsType.tp_name = "vim.options";
6622 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006623 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006624 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006625 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006626 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006627 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006628 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6629 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6630 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006631
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006632#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006633 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6634 LoaderType.tp_name = "vim.Loader";
6635 LoaderType.tp_basicsize = sizeof(LoaderObject);
6636 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6637 LoaderType.tp_doc = "vim message object";
6638 LoaderType.tp_methods = LoaderMethods;
6639 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006640#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006641
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006642#if PY_MAJOR_VERSION >= 3
6643 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6644 vimmodule.m_name = "vim";
6645 vimmodule.m_doc = "Vim Python interface\n";
6646 vimmodule.m_size = -1;
6647 vimmodule.m_methods = VimMethods;
6648#endif
6649}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006650
6651#define PYTYPE_READY(type) \
6652 if (PyType_Ready(&type)) \
6653 return -1;
6654
6655 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006656init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006657{
6658 PYTYPE_READY(IterType);
6659 PYTYPE_READY(BufferType);
6660 PYTYPE_READY(RangeType);
6661 PYTYPE_READY(WindowType);
6662 PYTYPE_READY(TabPageType);
6663 PYTYPE_READY(BufMapType);
6664 PYTYPE_READY(WinListType);
6665 PYTYPE_READY(TabListType);
6666 PYTYPE_READY(CurrentType);
6667 PYTYPE_READY(DictionaryType);
6668 PYTYPE_READY(ListType);
6669 PYTYPE_READY(FunctionType);
6670 PYTYPE_READY(OptionsType);
6671 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006672#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006673 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006674#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006675 return 0;
6676}
6677
6678 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006679init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006680{
6681 PyObject *path;
6682 PyObject *path_hook;
6683 PyObject *path_hooks;
6684
6685 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6686 return -1;
6687
6688 if (!(path_hooks = PySys_GetObject("path_hooks")))
6689 {
6690 PyErr_Clear();
6691 path_hooks = PyList_New(1);
6692 PyList_SET_ITEM(path_hooks, 0, path_hook);
6693 if (PySys_SetObject("path_hooks", path_hooks))
6694 {
6695 Py_DECREF(path_hooks);
6696 return -1;
6697 }
6698 Py_DECREF(path_hooks);
6699 }
6700 else if (PyList_Check(path_hooks))
6701 {
6702 if (PyList_Append(path_hooks, path_hook))
6703 {
6704 Py_DECREF(path_hook);
6705 return -1;
6706 }
6707 Py_DECREF(path_hook);
6708 }
6709 else
6710 {
6711 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006712 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006713 "You should now do the following:\n"
6714 "- append vim.path_hook to sys.path_hooks\n"
6715 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006716 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006717 Py_DECREF(path_hook);
6718 return 0;
6719 }
6720
6721 if (!(path = PySys_GetObject("path")))
6722 {
6723 PyErr_Clear();
6724 path = PyList_New(1);
6725 Py_INCREF(vim_special_path_object);
6726 PyList_SET_ITEM(path, 0, vim_special_path_object);
6727 if (PySys_SetObject("path", path))
6728 {
6729 Py_DECREF(path);
6730 return -1;
6731 }
6732 Py_DECREF(path);
6733 }
6734 else if (PyList_Check(path))
6735 {
6736 if (PyList_Append(path, vim_special_path_object))
6737 return -1;
6738 }
6739 else
6740 {
6741 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006742 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006743 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006744 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006745 }
6746
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006747 return 0;
6748}
6749
6750static BufMapObject TheBufferMap =
6751{
6752 PyObject_HEAD_INIT(&BufMapType)
6753};
6754
6755static WinListObject TheWindowList =
6756{
6757 PyObject_HEAD_INIT(&WinListType)
6758 NULL
6759};
6760
6761static CurrentObject TheCurrent =
6762{
6763 PyObject_HEAD_INIT(&CurrentType)
6764};
6765
6766static TabListObject TheTabPageList =
6767{
6768 PyObject_HEAD_INIT(&TabListType)
6769};
6770
6771static struct numeric_constant {
6772 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006773 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006774} numeric_constants[] = {
6775 {"VAR_LOCKED", VAR_LOCKED},
6776 {"VAR_FIXED", VAR_FIXED},
6777 {"VAR_SCOPE", VAR_SCOPE},
6778 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6779};
6780
6781static struct object_constant {
6782 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006783 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006784} object_constants[] = {
6785 {"buffers", (PyObject *)(void *)&TheBufferMap},
6786 {"windows", (PyObject *)(void *)&TheWindowList},
6787 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6788 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006789
6790 {"Buffer", (PyObject *)&BufferType},
6791 {"Range", (PyObject *)&RangeType},
6792 {"Window", (PyObject *)&WindowType},
6793 {"TabPage", (PyObject *)&TabPageType},
6794 {"Dictionary", (PyObject *)&DictionaryType},
6795 {"List", (PyObject *)&ListType},
6796 {"Function", (PyObject *)&FunctionType},
6797 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006798#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006799 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006800#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006801};
6802
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006803#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006804 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006805 return -1;
6806
6807#define ADD_CHECKED_OBJECT(m, name, obj) \
6808 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006809 PyObject *valObject = obj; \
6810 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006811 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006812 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006813 }
6814
6815 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006816populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006817{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006818 int i;
6819 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006820 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006821 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006822#if PY_VERSION_HEX >= 0x030700f0
6823 PyObject *dict;
6824 PyObject *cls;
6825#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006826
6827 for (i = 0; i < (int)(sizeof(numeric_constants)
6828 / sizeof(struct numeric_constant));
6829 ++i)
6830 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006831 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006832
6833 for (i = 0; i < (int)(sizeof(object_constants)
6834 / sizeof(struct object_constant));
6835 ++i)
6836 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006837 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006838
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006839 valObject = object_constants[i].valObject;
6840 Py_INCREF(valObject);
6841 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006842 }
6843
6844 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6845 return -1;
6846 ADD_OBJECT(m, "error", VimError);
6847
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006848 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6849 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850 ADD_CHECKED_OBJECT(m, "options",
6851 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006852
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006853 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006854 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006855 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006856
Bram Moolenaar22081f42016-06-01 20:38:34 +02006857#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006858 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006859 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006860#else
6861 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6862 return -1;
6863#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006864 ADD_OBJECT(m, "_getcwd", py_getcwd)
6865
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006866 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006867 return -1;
6868 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006869 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006870 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006871 if (PyObject_SetAttrString(other_module, "chdir", attr))
6872 {
6873 Py_DECREF(attr);
6874 return -1;
6875 }
6876 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006877
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006878 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006879 {
6880 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006881 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006882 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006883 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6884 {
6885 Py_DECREF(attr);
6886 return -1;
6887 }
6888 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006890 else
6891 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006892
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006893 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6894 return -1;
6895
6896 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6897
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006898#if PY_VERSION_HEX >= 0x030700f0
6899 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6900 return -1;
6901
6902 dict = PyModule_GetDict(imp);
6903
6904 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6905 {
6906 Py_DECREF(imp);
6907 return -1;
6908 }
6909
6910 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6911 {
6912 Py_DECREF(imp);
6913 return -1;
6914 }
6915
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006916 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6917 {
6918 // find_module() is deprecated, this may stop working in some later
6919 // version.
6920 ADD_OBJECT(m, "_find_module", py_find_module);
6921 }
6922
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006923 Py_DECREF(imp);
6924
6925 ADD_OBJECT(m, "_find_spec", py_find_spec);
6926#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006927 if (!(imp = PyImport_ImportModule("imp")))
6928 return -1;
6929
6930 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6931 {
6932 Py_DECREF(imp);
6933 return -1;
6934 }
6935
6936 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6937 {
6938 Py_DECREF(py_find_module);
6939 Py_DECREF(imp);
6940 return -1;
6941 }
6942
6943 Py_DECREF(imp);
6944
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006945 ADD_OBJECT(m, "_find_module", py_find_module);
6946 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006947#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006948
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006949 return 0;
6950}