blob: 6d71ac1a28bc86a0a23974df70b3a6aaa8b54e21 [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 Moolenaar2e2f52a2020-12-21 16:03:02 +0100133 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
134 ERRORS_ENCODE_ARG)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200135 return NULL;
136
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100137 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200138 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200139 {
140 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200141 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200142 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200143
144 *todecref = bytes;
145 }
146 else
147 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200148#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200149 PyErr_FORMAT(PyExc_TypeError,
150 N_("expected str() or unicode() instance, but got %s"),
151 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200152#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200153 PyErr_FORMAT(PyExc_TypeError,
154 N_("expected bytes() or str() instance, but got %s"),
155 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200156#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200157 return NULL;
158 }
159
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200160 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200161}
162
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200163#define NUMBER_LONG 1
164#define NUMBER_INT 2
165#define NUMBER_NATURAL 4
166#define NUMBER_UNSIGNED 8
167
168 static int
169NumberToLong(PyObject *obj, long *result, int flags)
170{
171#if PY_MAJOR_VERSION < 3
172 if (PyInt_Check(obj))
173 {
174 *result = PyInt_AsLong(obj);
175 if (PyErr_Occurred())
176 return -1;
177 }
178 else
179#endif
180 if (PyLong_Check(obj))
181 {
182 *result = PyLong_AsLong(obj);
183 if (PyErr_Occurred())
184 return -1;
185 }
186 else if (PyNumber_Check(obj))
187 {
188 PyObject *num;
189
190 if (!(num = PyNumber_Long(obj)))
191 return -1;
192
193 *result = PyLong_AsLong(num);
194
195 Py_DECREF(num);
196
197 if (PyErr_Occurred())
198 return -1;
199 }
200 else
201 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200202#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200203 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200204 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200205 "coercing to long(), but got %s"),
206 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200207#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200209 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 "but got %s"),
211 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200212#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200213 return -1;
214 }
215
216 if (flags & NUMBER_INT)
217 {
218 if (*result > INT_MAX)
219 {
220 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200221 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200222 return -1;
223 }
224 else if (*result < INT_MIN)
225 {
226 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200227 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200228 return -1;
229 }
230 }
231
232 if (flags & NUMBER_NATURAL)
233 {
234 if (*result <= 0)
235 {
236 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100237 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200238 return -1;
239 }
240 }
241 else if (flags & NUMBER_UNSIGNED)
242 {
243 if (*result < 0)
244 {
245 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200246 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200247 return -1;
248 }
249 }
250
251 return 0;
252}
253
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200254 static int
255add_string(PyObject *list, char *s)
256{
257 PyObject *string;
258
259 if (!(string = PyString_FromString(s)))
260 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200261
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200262 if (PyList_Append(list, string))
263 {
264 Py_DECREF(string);
265 return -1;
266 }
267
268 Py_DECREF(string);
269 return 0;
270}
271
272 static PyObject *
273ObjectDir(PyObject *self, char **attributes)
274{
275 PyMethodDef *method;
276 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200277 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200279 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200280 return NULL;
281
282 if (self)
283 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200284 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200285 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200286 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 return NULL;
288 }
289
290 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200291 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200292 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 return NULL;
295 }
296
297#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200298 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200299 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 return NULL;
302 }
303#endif
304
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200305 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200306}
307
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100308// Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200309
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100310// Function to write a line, points to either msg() or emsg().
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200311typedef int (*writefn)(char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200312
313static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314
315typedef struct
316{
317 PyObject_HEAD
318 long softspace;
319 long error;
320} OutputObject;
321
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200322static char *OutputAttrs[] = {
323 "softspace",
324 NULL
325};
326
327 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200328OutputDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200329{
330 return ObjectDir(self, OutputAttrs);
331}
332
Bram Moolenaar77045652012-09-21 13:46:06 +0200333 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200334OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200335{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200336 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200338 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200339 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200340 return -1;
341 }
342
343 if (strcmp(name, "softspace") == 0)
344 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200345 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200346 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200347 return 0;
348 }
349
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200350 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200351 return -1;
352}
353
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100354// Buffer IO, we write one whole line at a time.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200355static garray_T io_ga = {0, 0, 1, 80, NULL};
356static writefn old_fn = NULL;
357
358 static void
359PythonIO_Flush(void)
360{
361 if (old_fn != NULL && io_ga.ga_len > 0)
362 {
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200363 ((char *)io_ga.ga_data)[io_ga.ga_len] = NUL;
364 old_fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200365 }
366 io_ga.ga_len = 0;
367}
368
369 static void
370writer(writefn fn, char_u *str, PyInt n)
371{
372 char_u *ptr;
373
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100374 // Flush when switching output function.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200375 if (fn != old_fn)
376 PythonIO_Flush();
377 old_fn = fn;
378
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200379 // Write each NL separated line. Text after the last NL is kept for
380 // writing later.
381 // For normal messages: Do not output when "got_int" was set. This avoids
382 // a loop gone crazy flooding the terminal with messages. Also for when
383 // "q" is pressed at the more-prompt.
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
385 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200386 {
387 PyInt len = ptr - str;
388
389 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
390 break;
391
392 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
393 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200394 fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200395 str = ptr + 1;
396 n -= len + 1;
397 io_ga.ga_len = 0;
398 }
399
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200400 // Put the remaining text into io_ga for later printing.
401 if (n > 0 && (fn == (writefn)emsg || !got_int)
402 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200403 {
404 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
405 io_ga.ga_len += (int)n;
406 }
407}
408
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200409 static int
410write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200411{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200412 Py_ssize_t len = 0;
413 char *str = NULL;
414 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200415
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200416 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
417 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200418
419 Py_BEGIN_ALLOW_THREADS
420 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200421 if (error)
422 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100423 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424 Python_Release_Vim();
425 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200426 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200427
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200428 return 0;
429}
430
431 static PyObject *
432OutputWrite(OutputObject *self, PyObject *string)
433{
434 if (write_output(self, string))
435 return NULL;
436
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200437 Py_INCREF(Py_None);
438 return Py_None;
439}
440
441 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200442OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 PyObject *iterator;
445 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200447 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200450 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200451 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200452 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200453 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 Py_DECREF(iterator);
455 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456 return NULL;
457 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200458 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200459 }
460
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200461 Py_DECREF(iterator);
462
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100463 // Iterator may have finished due to an exception
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200464 if (PyErr_Occurred())
465 return NULL;
466
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200467 Py_INCREF(Py_None);
468 return Py_None;
469}
470
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100471 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200472AlwaysNone(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100473{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100474 // do nothing
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100475 Py_INCREF(Py_None);
476 return Py_None;
477}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200478#define ALWAYS_NONE AlwaysNone(NULL, NULL)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100479
Bram Moolenaard4247472015-11-02 13:28:59 +0100480 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200481AlwaysFalse(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100482{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100483 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100484 PyObject *ret = Py_False;
485 Py_INCREF(ret);
486 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100487}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200488#define ALWAYS_FALSE AlwaysFalse(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100489
490 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200491AlwaysTrue(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100492{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100493 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100494 PyObject *ret = Py_True;
495 Py_INCREF(ret);
496 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100497}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200498#define ALWAYS_TRUE AlwaysTrue(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100499
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200500/***************/
501
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200502static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100503 // name, function, calling, doc
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200504 {"write", (PyCFunction)OutputWrite, METH_O, ""},
505 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100506 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
507 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
508 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
509 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
510 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
511 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200512 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200513 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200514 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200515};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200516
517static OutputObject Output =
518{
519 PyObject_HEAD_INIT(&OutputType)
520 0,
521 0
522};
523
524static OutputObject Error =
525{
526 PyObject_HEAD_INIT(&OutputType)
527 0,
528 1
529};
530
531 static int
532PythonIO_Init_io(void)
533{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200534 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
535 return -1;
536 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
537 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200538
539 if (PyErr_Occurred())
540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100541 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200542 return -1;
543 }
544
545 return 0;
546}
547
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200548#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200549static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
550
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200551typedef struct
552{
553 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200554 char *fullname;
555 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200556} LoaderObject;
557static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200558
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200559 static void
560LoaderDestructor(LoaderObject *self)
561{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200562 vim_free(self->fullname);
563 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200564 DESTRUCTOR_FINISH(self);
565}
566
567 static PyObject *
568LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
569{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200570 char *fullname = self->fullname;
571 PyObject *result = self->result;
572 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200573
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200574 if (!fullname)
575 {
576 module = result ? result : Py_None;
577 Py_INCREF(module);
578 return module;
579 }
580
581 module = call_load_module(fullname, (int)STRLEN(fullname), result);
582
583 self->fullname = NULL;
584 self->result = module;
585
586 vim_free(fullname);
587 Py_DECREF(result);
588
589 if (!module)
590 {
591 if (PyErr_Occurred())
592 return NULL;
593
594 Py_INCREF(Py_None);
595 return Py_None;
596 }
597
598 Py_INCREF(module);
599 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200600}
601
602static struct PyMethodDef LoaderMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100603 // name, function, calling, doc
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200604 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
605 { NULL, NULL, 0, NULL}
606};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200607#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200608
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100609/*
610 * Check to see whether a Vim error has been reported, or a keyboard
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200611 * interrupt has been detected.
612 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200613 static void
614VimTryStart(void)
615{
616 ++trylevel;
617}
618
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200619 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200620VimTryEnd(void)
621{
622 --trylevel;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100623 // Without this it stops processing all subsequent Vim script commands and
624 // generates strange error messages if I e.g. try calling Test() in a cycle
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200625 did_emsg = FALSE;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100626 // Keyboard interrupt should be preferred over anything else
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200627 if (got_int)
628 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100629 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100630 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100631 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200632 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200633 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200634 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100635 else if (msg_list != NULL && *msg_list != NULL)
636 {
637 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100638 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100639
640 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
641
642 if (msg == NULL)
643 {
644 PyErr_NoMemory();
645 return -1;
646 }
647
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100648 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100649
650 free_global_msglist();
651
652 if (should_free)
653 vim_free(msg);
654
655 return -1;
656 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200657 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200658 return (PyErr_Occurred() ? -1 : 0);
Bram Moolenaar86181df2020-05-11 23:14:04 +0200659 // Python exception is preferred over Vim one; unlikely to occur though
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200660 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200661 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100662 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200663 return -1;
664 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100665 // Finally transform Vim script exception to python one
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200666 else
667 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200668 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200669 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200670 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200671 }
672}
673
674 static int
675VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200676{
677 if (got_int)
678 {
679 PyErr_SetNone(PyExc_KeyboardInterrupt);
680 return 1;
681 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200682 return 0;
683}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200684
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100685// Vim module - Implementation
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200686
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200688VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200690 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200691 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200692 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693
Bram Moolenaar389a1792013-06-23 13:00:44 +0200694 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695 return NULL;
696
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 Py_BEGIN_ALLOW_THREADS
698 Python_Lock_Vim();
699
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200700 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200701 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 update_screen(VALID);
703
704 Python_Release_Vim();
705 Py_END_ALLOW_THREADS
706
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200707 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200713 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200714 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715}
716
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717/*
718 * Function to translate a typval_T into a PyObject; this will recursively
719 * translate lists/dictionaries into their Python equivalents.
720 *
721 * The depth parameter is to avoid infinite recursion, set it to 1 when
722 * you call VimToPython.
723 */
724 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200725VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200729 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100731 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200732 if (depth > 100)
733 {
734 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200735 ret = Py_None;
736 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
738
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100739 // Check if we run into a recursive loop. The item must be in lookup_dict
740 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200741 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
742 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
743 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200744 sprintf(ptrBuf, "%p",
745 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
746 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 Py_INCREF(ret);
751 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200752 }
753 }
754
755 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200756 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200757 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 else if (our_tv->v_type == VAR_NUMBER)
759 {
760 char buf[NUMBUFLEN];
761
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100762 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200764 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100766#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200767 else if (our_tv->v_type == VAR_FLOAT)
768 {
769 char buf[NUMBUFLEN];
770
771 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200772 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100774#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200775 else if (our_tv->v_type == VAR_LIST)
776 {
777 list_T *list = our_tv->vval.v_list;
778 listitem_T *curr;
779
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 if (list == NULL)
781 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200782
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200784 return NULL;
785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200786 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200788 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200789 return NULL;
790 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200792 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200793 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200794 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200795 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200796 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200797 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200798 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200799 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200800 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200801 {
802 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200804 return NULL;
805 }
806 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200807 }
808 }
809 else if (our_tv->v_type == VAR_DICT)
810 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200811
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100812 hashtab_T *ht;
813 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200814 hashitem_T *hi;
815 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100816
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200817 if (our_tv->vval.v_dict == NULL)
818 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100819 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200820
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200821 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200822 return NULL;
823
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200824 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200825 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200826 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200827 return NULL;
828 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200829
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100830 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200831 for (hi = ht->ht_array; todo > 0; ++hi)
832 {
833 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200834 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200835 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200836
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200837 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200838 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
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 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200842 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200843 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200844 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200845 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200846 Py_DECREF(newObj);
847 return NULL;
848 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200849 }
850 }
851 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100852 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100853 {
854 if (our_tv->vval.v_number == VVAL_FALSE)
855 {
856 ret = Py_False;
857 Py_INCREF(ret);
858 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100859 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100860 {
861 ret = Py_True;
862 Py_INCREF(ret);
863 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100864 return ret;
865 }
866 else if (our_tv->v_type == VAR_SPECIAL)
867 {
868 Py_INCREF(Py_None);
869 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100870 return ret;
871 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100872 else if (our_tv->v_type == VAR_BLOB)
873 ret = PyBytes_FromStringAndSize(
874 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
875 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200876 else
877 {
878 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200879 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200880 }
881
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200882 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200883}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200884
885 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200886VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200887{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200888 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200889 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200890 PyObject *string;
891 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200892 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200893 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200894
Bram Moolenaar389a1792013-06-23 13:00:44 +0200895 if (!PyArg_ParseTuple(args, "O", &string))
896 return NULL;
897
898 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200899 return NULL;
900
901 Py_BEGIN_ALLOW_THREADS
902 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200903 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200904 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200905 Python_Release_Vim();
906 Py_END_ALLOW_THREADS
907
Bram Moolenaar389a1792013-06-23 13:00:44 +0200908 Py_XDECREF(todecref);
909
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200910 if (VimTryEnd())
911 return NULL;
912
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200913 if (our_tv == NULL)
914 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200915 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200916 return NULL;
917 }
918
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100919 // Convert the Vim type into a Python type. Create a dictionary that's
920 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200921 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200922 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200923 else
924 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200925 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200926 Py_DECREF(lookup_dict);
927 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200928
929
930 Py_BEGIN_ALLOW_THREADS
931 Python_Lock_Vim();
932 free_tv(our_tv);
933 Python_Release_Vim();
934 Py_END_ALLOW_THREADS
935
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200936 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200937}
938
Bram Moolenaardb913952012-06-29 12:54:53 +0200939static PyObject *ConvertToPyObject(typval_T *);
940
941 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200942VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200943{
Bram Moolenaardb913952012-06-29 12:54:53 +0200944 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200945 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200946 char_u *expr;
947 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200948
Bram Moolenaar389a1792013-06-23 13:00:44 +0200949 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200950 return NULL;
951
952 Py_BEGIN_ALLOW_THREADS
953 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200954 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200955 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200956 Python_Release_Vim();
957 Py_END_ALLOW_THREADS
958
Bram Moolenaar389a1792013-06-23 13:00:44 +0200959 Py_XDECREF(todecref);
960
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200961 if (VimTryEnd())
962 return NULL;
963
Bram Moolenaardb913952012-06-29 12:54:53 +0200964 if (our_tv == NULL)
965 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200966 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200967 return NULL;
968 }
969
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200970 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200971 Py_BEGIN_ALLOW_THREADS
972 Python_Lock_Vim();
973 free_tv(our_tv);
974 Python_Release_Vim();
975 Py_END_ALLOW_THREADS
976
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200977 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200978}
979
980 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200981VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200982{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200983 char_u *str;
984 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200985 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200986
Bram Moolenaar389a1792013-06-23 13:00:44 +0200987 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200988 return NULL;
989
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200990 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200991
992 Py_XDECREF(todecref);
993
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200994 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200995}
996
Bram Moolenaarf4258302013-06-02 18:20:17 +0200997 static PyObject *
998_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
999{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001000 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001001 PyObject *newwd;
1002 PyObject *todecref;
1003 char_u *new_dir;
1004
Bram Moolenaard4209d22013-06-05 20:34:15 +02001005 if (_chdir == NULL)
1006 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001007 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001008 return NULL;
1009
1010 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1011 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001012 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001013 return NULL;
1014 }
1015
1016 if (!(new_dir = StringToChars(newwd, &todecref)))
1017 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001018 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001019 Py_DECREF(newwd);
1020 return NULL;
1021 }
1022
1023 VimTryStart();
1024
1025 if (vim_chdir(new_dir))
1026 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001027 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001028 Py_DECREF(newwd);
1029 Py_XDECREF(todecref);
1030
1031 if (VimTryEnd())
1032 return NULL;
1033
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001034 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001035 return NULL;
1036 }
1037
1038 Py_DECREF(newwd);
1039 Py_XDECREF(todecref);
1040
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001041 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001042
1043 if (VimTryEnd())
1044 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001045 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001046 return NULL;
1047 }
1048
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001049 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001050}
1051
1052 static PyObject *
1053VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1054{
1055 return _VimChdir(py_chdir, args, kwargs);
1056}
1057
1058 static PyObject *
1059VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1060{
1061 return _VimChdir(py_fchdir, args, kwargs);
1062}
1063
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001064typedef struct {
1065 PyObject *callable;
1066 PyObject *result;
1067} map_rtp_data;
1068
1069 static void
1070map_rtp_callback(char_u *path, void *_data)
1071{
1072 void **data = (void **) _data;
1073 PyObject *pathObject;
1074 map_rtp_data *mr_data = *((map_rtp_data **) data);
1075
Bram Moolenaar41009372013-07-01 22:03:04 +02001076 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001077 {
1078 *data = NULL;
1079 return;
1080 }
1081
1082 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1083 pathObject, NULL);
1084
1085 Py_DECREF(pathObject);
1086
1087 if (!mr_data->result || mr_data->result != Py_None)
1088 *data = NULL;
1089 else
1090 {
1091 Py_DECREF(mr_data->result);
1092 mr_data->result = NULL;
1093 }
1094}
1095
1096 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001097VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001098{
1099 map_rtp_data data;
1100
Bram Moolenaar389a1792013-06-23 13:00:44 +02001101 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001102 data.result = NULL;
1103
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001104 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001105
1106 if (data.result == NULL)
1107 {
1108 if (PyErr_Occurred())
1109 return NULL;
1110 else
1111 {
1112 Py_INCREF(Py_None);
1113 return Py_None;
1114 }
1115 }
1116 return data.result;
1117}
1118
1119/*
1120 * _vim_runtimepath_ special path implementation.
1121 */
1122
1123 static void
1124map_finder_callback(char_u *path, void *_data)
1125{
1126 void **data = (void **) _data;
1127 PyObject *list = *((PyObject **) data);
1128 PyObject *pathObject1, *pathObject2;
1129 char *pathbuf;
1130 size_t pathlen;
1131
1132 pathlen = STRLEN(path);
1133
1134#if PY_MAJOR_VERSION < 3
1135# define PY_MAIN_DIR_STRING "python2"
1136#else
1137# define PY_MAIN_DIR_STRING "python3"
1138#endif
1139#define PY_ALTERNATE_DIR_STRING "pythonx"
1140
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001141#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001142 if (!(pathbuf = PyMem_New(char,
1143 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1144 {
1145 PyErr_NoMemory();
1146 *data = NULL;
1147 return;
1148 }
1149
1150 mch_memmove(pathbuf, path, pathlen + 1);
1151 add_pathsep((char_u *) pathbuf);
1152
1153 pathlen = STRLEN(pathbuf);
1154 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1155 PYTHONX_STRING_LENGTH + 1);
1156
1157 if (!(pathObject1 = PyString_FromString(pathbuf)))
1158 {
1159 *data = NULL;
1160 PyMem_Free(pathbuf);
1161 return;
1162 }
1163
1164 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1165 PYTHONX_STRING_LENGTH + 1);
1166
1167 if (!(pathObject2 = PyString_FromString(pathbuf)))
1168 {
1169 Py_DECREF(pathObject1);
1170 PyMem_Free(pathbuf);
1171 *data = NULL;
1172 return;
1173 }
1174
1175 PyMem_Free(pathbuf);
1176
1177 if (PyList_Append(list, pathObject1)
1178 || PyList_Append(list, pathObject2))
1179 *data = NULL;
1180
1181 Py_DECREF(pathObject1);
1182 Py_DECREF(pathObject2);
1183}
1184
1185 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001186Vim_GetPaths(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001187{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001188 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001189
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001190 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001191 return NULL;
1192
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001193 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001194
1195 if (PyErr_Occurred())
1196 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001197 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001198 return NULL;
1199 }
1200
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001201 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001202}
1203
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001204#if PY_VERSION_HEX >= 0x030700f0
1205 static PyObject *
1206FinderFindSpec(PyObject *self, PyObject *args)
1207{
1208 char *fullname;
1209 PyObject *paths;
1210 PyObject *target = Py_None;
1211 PyObject *spec;
1212
1213 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1214 return NULL;
1215
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001216 if (!(paths = Vim_GetPaths(self, NULL)))
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001217 return NULL;
1218
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001219 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001220
1221 Py_DECREF(paths);
1222
1223 if (!spec)
1224 {
1225 if (PyErr_Occurred())
1226 return NULL;
1227
1228 Py_INCREF(Py_None);
1229 return Py_None;
1230 }
1231
1232 return spec;
1233}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001234
1235 static PyObject *
1236FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1237{
1238 // Apparently returning None works.
1239 Py_INCREF(Py_None);
1240 return Py_None;
1241}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001242#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001243 static PyObject *
1244call_load_module(char *name, int len, PyObject *find_module_result)
1245{
1246 PyObject *fd, *pathname, *description;
1247
Bram Moolenaarc476e522013-06-23 13:46:40 +02001248 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001249 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001250 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001251 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001252 Py_TYPE_NAME(find_module_result));
1253 return NULL;
1254 }
1255 if (PyTuple_GET_SIZE(find_module_result) != 3)
1256 {
1257 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001258 N_("expected 3-tuple as imp.find_module() result, but got "
1259 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001260 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001261 return NULL;
1262 }
1263
1264 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1265 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1266 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1267 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001268 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001269 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001270 return NULL;
1271 }
1272
1273 return PyObject_CallFunction(py_load_module,
1274 "s#OOO", name, len, fd, pathname, description);
1275}
1276
1277 static PyObject *
1278find_module(char *fullname, char *tail, PyObject *new_path)
1279{
1280 PyObject *find_module_result;
1281 PyObject *module;
1282 char *dot;
1283
Bram Moolenaar41009372013-07-01 22:03:04 +02001284 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001285 {
1286 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001287 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001288 * first component
1289 */
1290 PyObject *newest_path;
1291 int partlen = (int) (dot - 1 - tail);
1292
1293 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1294 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001295 {
1296 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1297 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001298 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001299 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001300
1301 if (!(module = call_load_module(
1302 fullname,
1303 ((int) (tail - fullname)) + partlen,
1304 find_module_result)))
1305 {
1306 Py_DECREF(find_module_result);
1307 return NULL;
1308 }
1309
1310 Py_DECREF(find_module_result);
1311
1312 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1313 {
1314 Py_DECREF(module);
1315 return NULL;
1316 }
1317
1318 Py_DECREF(module);
1319
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001320 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001321
1322 Py_DECREF(newest_path);
1323
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001324 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001325 }
1326 else
1327 {
1328 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1329 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001330 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001331 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1332 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001333 return NULL;
1334 }
1335
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001336 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001337 }
1338}
1339
1340 static PyObject *
1341FinderFindModule(PyObject *self, PyObject *args)
1342{
1343 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001344 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001345 PyObject *new_path;
1346 LoaderObject *loader;
1347
1348 if (!PyArg_ParseTuple(args, "s", &fullname))
1349 return NULL;
1350
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001351 if (!(new_path = Vim_GetPaths(self, NULL)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001352 return NULL;
1353
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001354 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001355
1356 Py_DECREF(new_path);
1357
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001358 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001359 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001360 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001361 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001362
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001363 Py_INCREF(Py_None);
1364 return Py_None;
1365 }
1366
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001367 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001368 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001369 Py_DECREF(result);
1370 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001371 return NULL;
1372 }
1373
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001374 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1375 {
1376 vim_free(fullname);
1377 Py_DECREF(result);
1378 return NULL;
1379 }
1380
1381 loader->fullname = fullname;
1382 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001383
1384 return (PyObject *) loader;
1385}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001386#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001387
1388 static PyObject *
1389VimPathHook(PyObject *self UNUSED, PyObject *args)
1390{
1391 char *path;
1392
1393 if (PyArg_ParseTuple(args, "s", &path)
1394 && STRCMP(path, vim_special_path) == 0)
1395 {
1396 Py_INCREF(vim_module);
1397 return vim_module;
1398 }
1399
1400 PyErr_Clear();
1401 PyErr_SetNone(PyExc_ImportError);
1402 return NULL;
1403}
1404
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001405/*
1406 * Vim module - Definitions
1407 */
1408
1409static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001410 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001411 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001412 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001413 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001414 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001415 {"chdir", (PyCFunction)(void *)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1416 {"fchdir", (PyCFunction)(void *)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001417 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001418#if PY_VERSION_HEX >= 0x030700f0
1419 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001420#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001421 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001422 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1423 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1424 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001425};
1426
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001427/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001428 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001429 */
1430
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001431static PyTypeObject IterType;
1432
1433typedef PyObject *(*nextfun)(void **);
1434typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001435typedef int (*traversefun)(void *, visitproc, void *);
1436typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001437
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001438// Main purpose of this object is removing the need for do python
1439// initialization (i.e. PyType_Ready and setting type attributes) for a big
1440// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001441typedef struct
1442{
1443 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001444 void *cur;
1445 nextfun next;
1446 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001447 traversefun traverse;
1448 clearfun clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001449 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001450} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001451
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001452 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001453IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001454 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001455{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001456 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001457
Bram Moolenaar774267b2013-05-21 20:51:59 +02001458 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001459 self->cur = start;
1460 self->next = next;
1461 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001462 self->traverse = traverse;
1463 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001464 self->iter_object = iter_object;
1465
1466 if (iter_object)
1467 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001468
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001469 return (PyObject *)(self);
1470}
1471
1472 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001473IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001474{
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001475 if (self->iter_object)
1476 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001477 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001478 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001479 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001480}
1481
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001482 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001483IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001484{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001485 if (self->traverse != NULL)
1486 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001487 else
1488 return 0;
1489}
1490
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001491// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001492#ifdef clear
1493# undef clear
1494#endif
1495
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001496 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001497IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001498{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001499 if (self->clear != NULL)
1500 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001501 else
1502 return 0;
1503}
1504
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001505 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001506IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001507{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001508 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001509}
1510
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001511 static PyObject *
1512IterIter(PyObject *self)
1513{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001514 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001515 return self;
1516}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001517
Bram Moolenaardb913952012-06-29 12:54:53 +02001518typedef struct pylinkedlist_S {
1519 struct pylinkedlist_S *pll_next;
1520 struct pylinkedlist_S *pll_prev;
1521 PyObject *pll_obj;
1522} pylinkedlist_T;
1523
1524static pylinkedlist_T *lastdict = NULL;
1525static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001526static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001527
1528 static void
1529pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1530{
1531 if (ref->pll_prev == NULL)
1532 {
1533 if (ref->pll_next == NULL)
1534 {
1535 *last = NULL;
1536 return;
1537 }
1538 }
1539 else
1540 ref->pll_prev->pll_next = ref->pll_next;
1541
1542 if (ref->pll_next == NULL)
1543 *last = ref->pll_prev;
1544 else
1545 ref->pll_next->pll_prev = ref->pll_prev;
1546}
1547
1548 static void
1549pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1550{
1551 if (*last == NULL)
1552 ref->pll_prev = NULL;
1553 else
1554 {
1555 (*last)->pll_next = ref;
1556 ref->pll_prev = *last;
1557 }
1558 ref->pll_next = NULL;
1559 ref->pll_obj = self;
1560 *last = ref;
1561}
1562
1563static PyTypeObject DictionaryType;
1564
1565typedef struct
1566{
1567 PyObject_HEAD
1568 dict_T *dict;
1569 pylinkedlist_T ref;
1570} DictionaryObject;
1571
Bram Moolenaara9922d62013-05-30 13:01:18 +02001572static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1573
1574#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1575
Bram Moolenaardb913952012-06-29 12:54:53 +02001576 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001578{
1579 DictionaryObject *self;
1580
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001582 if (self == NULL)
1583 return NULL;
1584 self->dict = dict;
1585 ++dict->dv_refcount;
1586
1587 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1588
1589 return (PyObject *)(self);
1590}
1591
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001593py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001595 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001596
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001597 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001598 {
1599 PyErr_NoMemory();
1600 return NULL;
1601 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001602 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001603
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001604 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605}
1606
1607 static PyObject *
1608DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1609{
1610 DictionaryObject *self;
1611 dict_T *dict;
1612
1613 if (!(dict = py_dict_alloc()))
1614 return NULL;
1615
1616 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1617
1618 --dict->dv_refcount;
1619
1620 if (kwargs || PyTuple_Size(args))
1621 {
1622 PyObject *tmp;
1623 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1624 {
1625 Py_DECREF(self);
1626 return NULL;
1627 }
1628
1629 Py_DECREF(tmp);
1630 }
1631
1632 return (PyObject *)(self);
1633}
1634
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001635 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001636DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001637{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001638 pyll_remove(&self->ref, &lastdict);
1639 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001640
1641 DESTRUCTOR_FINISH(self);
1642}
1643
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001644static char *DictionaryAttrs[] = {
1645 "locked", "scope",
1646 NULL
1647};
1648
1649 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001650DictionaryDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001651{
1652 return ObjectDir(self, DictionaryAttrs);
1653}
1654
Bram Moolenaardb913952012-06-29 12:54:53 +02001655 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001656DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001657{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001658 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001659 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001660 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001661 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001662 return -1;
1663 }
1664
1665 if (strcmp(name, "locked") == 0)
1666 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001667 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001668 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001669 PyErr_SET_STRING(PyExc_TypeError,
1670 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001671 return -1;
1672 }
1673 else
1674 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001675 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001676 if (istrue == -1)
1677 return -1;
1678 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001679 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001680 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001681 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001682 }
1683 return 0;
1684 }
1685 else
1686 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001687 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001688 return -1;
1689 }
1690}
1691
1692 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001693DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001694{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001695 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001696}
1697
Bram Moolenaara9922d62013-05-30 13:01:18 +02001698#define DICT_FLAG_HAS_DEFAULT 0x01
1699#define DICT_FLAG_POP 0x02
1700#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001701#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702#define DICT_FLAG_RETURN_PAIR 0x10
1703
Bram Moolenaardb913952012-06-29 12:54:53 +02001704 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001706{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001707 PyObject *keyObject;
1708 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001709 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001710 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001711 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001712 dict_T *dict = self->dict;
1713 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001714 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001715
Bram Moolenaara9922d62013-05-30 13:01:18 +02001716 if (flags & DICT_FLAG_HAS_DEFAULT)
1717 {
1718 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1719 return NULL;
1720 }
1721 else
1722 keyObject = args;
1723
1724 if (flags & DICT_FLAG_RETURN_BOOL)
1725 defObject = Py_False;
1726
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001727 if (!(key = StringToChars(keyObject, &todecref)))
1728 return NULL;
1729
1730 if (*key == NUL)
1731 {
1732 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001733 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001734 return NULL;
1735 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001736
Bram Moolenaara9922d62013-05-30 13:01:18 +02001737 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001738
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001739 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001740
Bram Moolenaara9922d62013-05-30 13:01:18 +02001741 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001742 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001743 if (defObject)
1744 {
1745 Py_INCREF(defObject);
1746 return defObject;
1747 }
1748 else
1749 {
1750 PyErr_SetObject(PyExc_KeyError, keyObject);
1751 return NULL;
1752 }
1753 }
1754 else if (flags & DICT_FLAG_RETURN_BOOL)
1755 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001756 ret = Py_True;
1757 Py_INCREF(ret);
1758 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001759 }
1760
1761 di = dict_lookup(hi);
1762
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001763 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001764 return NULL;
1765
1766 if (flags & DICT_FLAG_POP)
1767 {
1768 if (dict->dv_lock)
1769 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001770 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001771 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001772 return NULL;
1773 }
1774
1775 hash_remove(&dict->dv_hashtab, hi);
1776 dictitem_free(di);
1777 }
1778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001779 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001780}
1781
1782 static PyObject *
1783DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1784{
1785 return _DictionaryItem(self, keyObject, 0);
1786}
1787
1788 static int
1789DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1790{
1791 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001792 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001793
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001794 if (rObj == NULL)
1795 return -1;
1796
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001797 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798
Bram Moolenaardee2e312013-06-23 16:35:47 +02001799 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001800
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001801 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001802}
1803
1804typedef struct
1805{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001806 int dii_changed;
1807 hashtab_T *dii_ht;
1808 hashitem_T *dii_hi;
1809 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001810} dictiterinfo_T;
1811
1812 static PyObject *
1813DictionaryIterNext(dictiterinfo_T **dii)
1814{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001815 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001817 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001818 return NULL;
1819
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001820 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001821 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001822 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001823 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001824 return NULL;
1825 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001826
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001827 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
1828 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001830 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001831
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001832 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001833 return NULL;
1834
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001835 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001836}
1837
1838 static PyObject *
1839DictionaryIter(DictionaryObject *self)
1840{
1841 dictiterinfo_T *dii;
1842 hashtab_T *ht;
1843
1844 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1845 {
1846 PyErr_NoMemory();
1847 return NULL;
1848 }
1849
1850 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001851 dii->dii_changed = ht->ht_changed;
1852 dii->dii_ht = ht;
1853 dii->dii_hi = ht->ht_array;
1854 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001855
1856 return IterNew(dii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001857 (destructorfun)(void *) PyMem_Free, (nextfun) DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001858 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001859}
1860
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001861 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001862DictionaryAssItem(
1863 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001864{
1865 char_u *key;
1866 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001867 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001868 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001869 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001870
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001871 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001872 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001873 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001874 return -1;
1875 }
1876
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001877 if (!(key = StringToChars(keyObject, &todecref)))
1878 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001879
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001880 if (*key == NUL)
1881 {
1882 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001883 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001884 return -1;
1885 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001886
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001887 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001888
1889 if (valObject == NULL)
1890 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001891 hashitem_T *hi;
1892
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 if (di == NULL)
1894 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001895 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001896 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001897 return -1;
1898 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001899 hi = hash_find(&dict->dv_hashtab, di->di_key);
1900 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001902 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001903 return 0;
1904 }
1905
1906 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001907 {
1908 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001909 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001910 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001911
1912 if (di == NULL)
1913 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001914 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001915 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001916 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001917 PyErr_NoMemory();
1918 return -1;
1919 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001920 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001921
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001922 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001923 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001924 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001925 RAISE_KEY_ADD_FAIL(key);
1926 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001927 return -1;
1928 }
1929 }
1930 else
1931 clear_tv(&di->di_tv);
1932
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001933 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001934
1935 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001936 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001937 return 0;
1938}
1939
Bram Moolenaara9922d62013-05-30 13:01:18 +02001940typedef PyObject *(*hi_to_py)(hashitem_T *);
1941
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001943DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001944{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001945 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001946 long_u todo = dict->dv_hashtab.ht_used;
1947 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001948 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001949 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001950 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001951
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001952 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001953 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1954 {
1955 if (!HASHITEM_EMPTY(hi))
1956 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001957 if (!(newObj = hiconvert(hi)))
1958 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001959 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001960 return NULL;
1961 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001962 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001963 --todo;
1964 ++i;
1965 }
1966 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001967 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001968}
1969
Bram Moolenaara9922d62013-05-30 13:01:18 +02001970 static PyObject *
1971dict_key(hashitem_T *hi)
1972{
1973 return PyBytes_FromString((char *)(hi->hi_key));
1974}
1975
1976 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001977DictionaryListKeys(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001978{
1979 return DictionaryListObjects(self, dict_key);
1980}
1981
1982 static PyObject *
1983dict_val(hashitem_T *hi)
1984{
1985 dictitem_T *di;
1986
1987 di = dict_lookup(hi);
1988 return ConvertToPyObject(&di->di_tv);
1989}
1990
1991 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001992DictionaryListValues(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001993{
1994 return DictionaryListObjects(self, dict_val);
1995}
1996
1997 static PyObject *
1998dict_item(hashitem_T *hi)
1999{
2000 PyObject *keyObject;
2001 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002002 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002003
2004 if (!(keyObject = dict_key(hi)))
2005 return NULL;
2006
2007 if (!(valObject = dict_val(hi)))
2008 {
2009 Py_DECREF(keyObject);
2010 return NULL;
2011 }
2012
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002013 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002014
2015 Py_DECREF(keyObject);
2016 Py_DECREF(valObject);
2017
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002018 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002019}
2020
2021 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002022DictionaryListItems(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002023{
2024 return DictionaryListObjects(self, dict_item);
2025}
2026
2027 static PyObject *
2028DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2029{
2030 dict_T *dict = self->dict;
2031
2032 if (dict->dv_lock)
2033 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002034 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002035 return NULL;
2036 }
2037
2038 if (kwargs)
2039 {
2040 typval_T tv;
2041
2042 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2043 return NULL;
2044
2045 VimTryStart();
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002046 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force", NULL);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002047 clear_tv(&tv);
2048 if (VimTryEnd())
2049 return NULL;
2050 }
2051 else
2052 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002053 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002054
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002055 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002056 return NULL;
2057
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002058 if (obj == NULL)
2059 {
2060 Py_INCREF(Py_None);
2061 return Py_None;
2062 }
2063
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002064 if (PyObject_HasAttrString(obj, "keys"))
2065 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002066 else
2067 {
2068 PyObject *iterator;
2069 PyObject *item;
2070
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002071 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002072 return NULL;
2073
2074 while ((item = PyIter_Next(iterator)))
2075 {
2076 PyObject *fast;
2077 PyObject *keyObject;
2078 PyObject *valObject;
2079 PyObject *todecref;
2080 char_u *key;
2081 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002082 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002083
2084 if (!(fast = PySequence_Fast(item, "")))
2085 {
2086 Py_DECREF(iterator);
2087 Py_DECREF(item);
2088 return NULL;
2089 }
2090
2091 Py_DECREF(item);
2092
2093 if (PySequence_Fast_GET_SIZE(fast) != 2)
2094 {
2095 Py_DECREF(iterator);
2096 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002097 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002098 N_("expected sequence element of size 2, "
2099 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002100 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002101 return NULL;
2102 }
2103
2104 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2105
2106 if (!(key = StringToChars(keyObject, &todecref)))
2107 {
2108 Py_DECREF(iterator);
2109 Py_DECREF(fast);
2110 return NULL;
2111 }
2112
2113 di = dictitem_alloc(key);
2114
2115 Py_XDECREF(todecref);
2116
2117 if (di == NULL)
2118 {
2119 Py_DECREF(fast);
2120 Py_DECREF(iterator);
2121 PyErr_NoMemory();
2122 return NULL;
2123 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002124 di->di_tv.v_type = VAR_UNKNOWN;
2125
2126 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2127
2128 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2129 {
2130 Py_DECREF(iterator);
2131 Py_DECREF(fast);
2132 dictitem_free(di);
2133 return NULL;
2134 }
2135
2136 Py_DECREF(fast);
2137
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002138 hi = hash_find(&dict->dv_hashtab, di->di_key);
2139 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002140 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002141 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002142 Py_DECREF(iterator);
2143 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002144 return NULL;
2145 }
2146 }
2147
2148 Py_DECREF(iterator);
2149
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002150 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002151 if (PyErr_Occurred())
2152 return NULL;
2153 }
2154 }
2155 Py_INCREF(Py_None);
2156 return Py_None;
2157}
2158
2159 static PyObject *
2160DictionaryGet(DictionaryObject *self, PyObject *args)
2161{
2162 return _DictionaryItem(self, args,
2163 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2164}
2165
2166 static PyObject *
2167DictionaryPop(DictionaryObject *self, PyObject *args)
2168{
2169 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2170}
2171
2172 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002173DictionaryPopItem(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002174{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002175 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002176 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002177 PyObject *valObject;
2178 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002179
Bram Moolenaarde71b562013-06-02 17:41:54 +02002180 if (self->dict->dv_hashtab.ht_used == 0)
2181 {
2182 PyErr_SetNone(PyExc_KeyError);
2183 return NULL;
2184 }
2185
2186 hi = self->dict->dv_hashtab.ht_array;
2187 while (HASHITEM_EMPTY(hi))
2188 ++hi;
2189
2190 di = dict_lookup(hi);
2191
2192 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002193 return NULL;
2194
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002195 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002196 {
2197 Py_DECREF(valObject);
2198 return NULL;
2199 }
2200
2201 hash_remove(&self->dict->dv_hashtab, hi);
2202 dictitem_free(di);
2203
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002204 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002205}
2206
2207 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002208DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002209{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002210 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2211}
2212
2213static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002214 0, // sq_length
2215 0, // sq_concat
2216 0, // sq_repeat
2217 0, // sq_item
2218 0, // sq_slice
2219 0, // sq_ass_item
2220 0, // sq_ass_slice
2221 (objobjproc) DictionaryContains, // sq_contains
2222 0, // sq_inplace_concat
2223 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002224};
2225
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002226static PyMappingMethods DictionaryAsMapping = {
2227 (lenfunc) DictionaryLength,
2228 (binaryfunc) DictionaryItem,
2229 (objobjargproc) DictionaryAssItem,
2230};
2231
Bram Moolenaardb913952012-06-29 12:54:53 +02002232static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002233 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002234 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2235 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002236 {"update", (PyCFunction)(void *)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002237 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2238 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002239 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002240 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002241 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2242 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002243};
2244
2245static PyTypeObject ListType;
2246
2247typedef struct
2248{
2249 PyObject_HEAD
2250 list_T *list;
2251 pylinkedlist_T ref;
2252} ListObject;
2253
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002254#define NEW_LIST(list) ListNew(&ListType, list)
2255
Bram Moolenaardb913952012-06-29 12:54:53 +02002256 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002257ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002258{
2259 ListObject *self;
2260
Bram Moolenaarab589462020-07-06 21:03:06 +02002261 if (list == NULL)
2262 return NULL;
2263
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002264 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002265 if (self == NULL)
2266 return NULL;
2267 self->list = list;
2268 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002269 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002270
2271 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2272
2273 return (PyObject *)(self);
2274}
2275
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002277py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002278{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002279 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002281 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002282 {
2283 PyErr_NoMemory();
2284 return NULL;
2285 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002286 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002287
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002288 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002289}
2290
2291 static int
2292list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2293{
2294 PyObject *iterator;
2295 PyObject *item;
2296 listitem_T *li;
2297
2298 if (!(iterator = PyObject_GetIter(obj)))
2299 return -1;
2300
2301 while ((item = PyIter_Next(iterator)))
2302 {
2303 if (!(li = listitem_alloc()))
2304 {
2305 PyErr_NoMemory();
2306 Py_DECREF(item);
2307 Py_DECREF(iterator);
2308 return -1;
2309 }
2310 li->li_tv.v_lock = 0;
2311 li->li_tv.v_type = VAR_UNKNOWN;
2312
2313 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2314 {
2315 Py_DECREF(item);
2316 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002318 return -1;
2319 }
2320
2321 Py_DECREF(item);
2322
2323 list_append(l, li);
2324 }
2325
2326 Py_DECREF(iterator);
2327
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002328 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002329 if (PyErr_Occurred())
2330 return -1;
2331
2332 return 0;
2333}
2334
2335 static PyObject *
2336ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2337{
2338 list_T *list;
2339 PyObject *obj = NULL;
2340
2341 if (kwargs)
2342 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002343 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002344 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002345 return NULL;
2346 }
2347
2348 if (!PyArg_ParseTuple(args, "|O", &obj))
2349 return NULL;
2350
2351 if (!(list = py_list_alloc()))
2352 return NULL;
2353
2354 if (obj)
2355 {
2356 PyObject *lookup_dict;
2357
2358 if (!(lookup_dict = PyDict_New()))
2359 {
2360 list_unref(list);
2361 return NULL;
2362 }
2363
2364 if (list_py_concat(list, obj, lookup_dict) == -1)
2365 {
2366 Py_DECREF(lookup_dict);
2367 list_unref(list);
2368 return NULL;
2369 }
2370
2371 Py_DECREF(lookup_dict);
2372 }
2373
2374 return ListNew(subtype, list);
2375}
2376
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002377 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002378ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002379{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002380 pyll_remove(&self->ref, &lastlist);
2381 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002382
2383 DESTRUCTOR_FINISH(self);
2384}
2385
Bram Moolenaardb913952012-06-29 12:54:53 +02002386 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002387ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002388{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002389 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002390}
2391
2392 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002393ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002394{
2395 listitem_T *li;
2396
Bram Moolenaard6e39182013-05-21 18:30:34 +02002397 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002399 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002400 return NULL;
2401 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002402 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002403 if (li == NULL)
2404 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002405 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002406 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002407 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 return NULL;
2409 }
2410 return ConvertToPyObject(&li->li_tv);
2411}
2412
Bram Moolenaardb913952012-06-29 12:54:53 +02002413 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002414ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2415 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002416{
2417 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002418 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002419
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002420 if (step == 0)
2421 {
2422 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2423 return NULL;
2424 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002425
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002426 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002427 if (list == NULL)
2428 return NULL;
2429
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002430 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002431 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002432 PyObject *item;
2433
2434 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002435 if (item == NULL)
2436 {
2437 Py_DECREF(list);
2438 return NULL;
2439 }
2440
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002441 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002442 }
2443
2444 return list;
2445}
2446
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002447 static PyObject *
2448ListItem(ListObject *self, PyObject* idx)
2449{
2450#if PY_MAJOR_VERSION < 3
2451 if (PyInt_Check(idx))
2452 {
2453 long _idx = PyInt_AsLong(idx);
2454 return ListIndex(self, _idx);
2455 }
2456 else
2457#endif
2458 if (PyLong_Check(idx))
2459 {
2460 long _idx = PyLong_AsLong(idx);
2461 return ListIndex(self, _idx);
2462 }
2463 else if (PySlice_Check(idx))
2464 {
2465 Py_ssize_t start, stop, step, slicelen;
2466
Bram Moolenaar922a4662014-03-30 16:11:43 +02002467 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002468 &start, &stop, &step, &slicelen) < 0)
2469 return NULL;
2470 return ListSlice(self, start, step, slicelen);
2471 }
2472 else
2473 {
2474 RAISE_INVALID_INDEX_TYPE(idx);
2475 return NULL;
2476 }
2477}
2478
2479 static void
2480list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2481 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2482{
2483 while (numreplaced--)
2484 {
2485 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2486 listitem_remove(l, lis[slicelen + numreplaced]);
2487 }
2488 while (numadded--)
2489 {
2490 listitem_T *next;
2491
2492 next = lastaddedli->li_prev;
2493 listitem_remove(l, lastaddedli);
2494 lastaddedli = next;
2495 }
2496}
2497
2498 static int
2499ListAssSlice(ListObject *self, Py_ssize_t first,
2500 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2501{
2502 PyObject *iterator;
2503 PyObject *item;
2504 listitem_T *li;
2505 listitem_T *lastaddedli = NULL;
2506 listitem_T *next;
2507 typval_T v;
2508 list_T *l = self->list;
2509 PyInt i;
2510 PyInt j;
2511 PyInt numreplaced = 0;
2512 PyInt numadded = 0;
2513 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002514 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002515
2516 size = ListLength(self);
2517
2518 if (l->lv_lock)
2519 {
2520 RAISE_LOCKED_LIST;
2521 return -1;
2522 }
2523
2524 if (step == 0)
2525 {
2526 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2527 return -1;
2528 }
2529
2530 if (step != 1 && slicelen == 0)
2531 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002532 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002533 int ret = 0;
2534
2535 if (obj == NULL)
2536 return 0;
2537
2538 if (!(iterator = PyObject_GetIter(obj)))
2539 return -1;
2540
2541 if ((item = PyIter_Next(iterator)))
2542 {
2543 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002544 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002545 "to extended slice"), 0);
2546 Py_DECREF(item);
2547 ret = -1;
2548 }
2549 Py_DECREF(iterator);
2550 return ret;
2551 }
2552
2553 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002554 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002555 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2556 {
2557 PyErr_NoMemory();
2558 return -1;
2559 }
2560
2561 if (first == size)
2562 li = NULL;
2563 else
2564 {
2565 li = list_find(l, (long) first);
2566 if (li == NULL)
2567 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002568 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002569 (int)first);
2570 if (obj != NULL)
2571 PyMem_Free(lis);
2572 return -1;
2573 }
2574 i = slicelen;
2575 while (i-- && li != NULL)
2576 {
2577 j = step;
2578 next = li;
2579 if (step > 0)
2580 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2581 else
2582 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2583
2584 if (obj == NULL)
2585 listitem_remove(l, li);
2586 else
2587 lis[slicelen - i - 1] = li;
2588
2589 li = next;
2590 }
2591 if (li == NULL && i != -1)
2592 {
2593 PyErr_SET_VIM(N_("internal error: not enough list items"));
2594 if (obj != NULL)
2595 PyMem_Free(lis);
2596 return -1;
2597 }
2598 }
2599
2600 if (obj == NULL)
2601 return 0;
2602
2603 if (!(iterator = PyObject_GetIter(obj)))
2604 {
2605 PyMem_Free(lis);
2606 return -1;
2607 }
2608
2609 i = 0;
2610 while ((item = PyIter_Next(iterator)))
2611 {
2612 if (ConvertFromPyObject(item, &v) == -1)
2613 {
2614 Py_DECREF(iterator);
2615 Py_DECREF(item);
2616 PyMem_Free(lis);
2617 return -1;
2618 }
2619 Py_DECREF(item);
2620 if (list_insert_tv(l, &v, numreplaced < slicelen
2621 ? lis[numreplaced]
2622 : li) == FAIL)
2623 {
2624 clear_tv(&v);
2625 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2626 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2627 PyMem_Free(lis);
2628 return -1;
2629 }
2630 if (numreplaced < slicelen)
2631 {
2632 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002633 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002634 numreplaced++;
2635 }
2636 else
2637 {
2638 if (li)
2639 lastaddedli = li->li_prev;
2640 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002641 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002642 numadded++;
2643 }
2644 clear_tv(&v);
2645 if (step != 1 && i >= slicelen)
2646 {
2647 Py_DECREF(iterator);
2648 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002649 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002650 "to extended slice"), (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 ++i;
2656 }
2657 Py_DECREF(iterator);
2658
2659 if (step != 1 && i != slicelen)
2660 {
2661 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002662 N_("attempt to assign sequence of size %d to extended slice "
2663 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002664 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2665 PyMem_Free(lis);
2666 return -1;
2667 }
2668
2669 if (PyErr_Occurred())
2670 {
2671 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2672 PyMem_Free(lis);
2673 return -1;
2674 }
2675
2676 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002678 if (step == 1)
2679 for (i = numreplaced; i < slicelen; i++)
2680 listitem_remove(l, lis[i]);
2681
2682 PyMem_Free(lis);
2683
2684 return 0;
2685}
2686
2687 static int
2688ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2689{
2690 typval_T tv;
2691 list_T *l = self->list;
2692 listitem_T *li;
2693 Py_ssize_t length = ListLength(self);
2694
2695 if (l->lv_lock)
2696 {
2697 RAISE_LOCKED_LIST;
2698 return -1;
2699 }
2700 if (index > length || (index == length && obj == NULL))
2701 {
2702 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2703 return -1;
2704 }
2705
2706 if (obj == NULL)
2707 {
2708 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002709 if (li == NULL)
2710 {
2711 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2712 "list item %d"), (int) index);
2713 return -1;
2714 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002715 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002716 clear_tv(&li->li_tv);
2717 vim_free(li);
2718 return 0;
2719 }
2720
2721 if (ConvertFromPyObject(obj, &tv) == -1)
2722 return -1;
2723
2724 if (index == length)
2725 {
2726 if (list_append_tv(l, &tv) == FAIL)
2727 {
2728 clear_tv(&tv);
2729 PyErr_SET_VIM(N_("failed to add item to list"));
2730 return -1;
2731 }
2732 }
2733 else
2734 {
2735 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002736 if (li == NULL)
2737 {
2738 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2739 "list item %d"), (int) index);
2740 return -1;
2741 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002742 clear_tv(&li->li_tv);
2743 copy_tv(&tv, &li->li_tv);
2744 clear_tv(&tv);
2745 }
2746 return 0;
2747}
2748
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002749 static int
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002750ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2751{
2752#if PY_MAJOR_VERSION < 3
2753 if (PyInt_Check(idx))
2754 {
2755 long _idx = PyInt_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002756 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002757 }
2758 else
2759#endif
2760 if (PyLong_Check(idx))
2761 {
2762 long _idx = PyLong_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002763 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002764 }
2765 else if (PySlice_Check(idx))
2766 {
2767 Py_ssize_t start, stop, step, slicelen;
2768
Bram Moolenaar922a4662014-03-30 16:11:43 +02002769 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002770 &start, &stop, &step, &slicelen) < 0)
2771 return -1;
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002772 return (int)ListAssSlice(self, start, step, slicelen,
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002773 obj);
2774 }
2775 else
2776 {
2777 RAISE_INVALID_INDEX_TYPE(idx);
2778 return -1;
2779 }
2780}
2781
2782 static PyObject *
2783ListConcatInPlace(ListObject *self, PyObject *obj)
2784{
2785 list_T *l = self->list;
2786 PyObject *lookup_dict;
2787
2788 if (l->lv_lock)
2789 {
2790 RAISE_LOCKED_LIST;
2791 return NULL;
2792 }
2793
2794 if (!(lookup_dict = PyDict_New()))
2795 return NULL;
2796
2797 if (list_py_concat(l, obj, lookup_dict) == -1)
2798 {
2799 Py_DECREF(lookup_dict);
2800 return NULL;
2801 }
2802 Py_DECREF(lookup_dict);
2803
2804 Py_INCREF(self);
2805 return (PyObject *)(self);
2806}
2807
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002808typedef struct
2809{
2810 listwatch_T lw;
2811 list_T *list;
2812} listiterinfo_T;
2813
2814 static void
2815ListIterDestruct(listiterinfo_T *lii)
2816{
2817 list_rem_watch(lii->list, &lii->lw);
Bram Moolenaar21578272021-02-21 19:12:47 +01002818 list_unref(lii->list);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002819 PyMem_Free(lii);
2820}
2821
2822 static PyObject *
2823ListIterNext(listiterinfo_T **lii)
2824{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002825 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002826
2827 if (!((*lii)->lw.lw_item))
2828 return NULL;
2829
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002830 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002831 return NULL;
2832
2833 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2834
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002835 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002836}
2837
2838 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002839ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002840{
2841 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002842 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002843
2844 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2845 {
2846 PyErr_NoMemory();
2847 return NULL;
2848 }
2849
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002850 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002851 list_add_watch(l, &lii->lw);
2852 lii->lw.lw_item = l->lv_first;
2853 lii->list = l;
Bram Moolenaar21578272021-02-21 19:12:47 +01002854 ++l->lv_refcount;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002855
2856 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002857 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002858 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002859}
2860
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002861static char *ListAttrs[] = {
2862 "locked",
2863 NULL
2864};
2865
2866 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002867ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002868{
2869 return ObjectDir(self, ListAttrs);
2870}
2871
Bram Moolenaar66b79852012-09-21 14:00:35 +02002872 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002873ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002874{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002875 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002876 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002877 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002878 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002879 return -1;
2880 }
2881
2882 if (strcmp(name, "locked") == 0)
2883 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002884 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002885 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002886 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002887 return -1;
2888 }
2889 else
2890 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002891 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002892 if (istrue == -1)
2893 return -1;
2894 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002896 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002897 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002898 }
2899 return 0;
2900 }
2901 else
2902 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002903 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002904 return -1;
2905 }
2906}
2907
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002908static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002909 (lenfunc) ListLength, // sq_length, len(x)
2910 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2911 0, // RangeRepeat, sq_repeat, x*n
2912 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2913 0, // was_sq_slice, x[i:j]
2914 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2915 0, // was_sq_ass_slice, x[i:j]=v
2916 0, // sq_contains
2917 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2918 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002919};
2920
2921static PyMappingMethods ListAsMapping = {
2922 /* mp_length */ (lenfunc) ListLength,
2923 /* mp_subscript */ (binaryfunc) ListItem,
2924 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2925};
2926
Bram Moolenaardb913952012-06-29 12:54:53 +02002927static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002928 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2929 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2930 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002931};
2932
2933typedef struct
2934{
2935 PyObject_HEAD
2936 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002937 int argc;
2938 typval_T *argv;
2939 dict_T *self;
2940 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002941 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002942} FunctionObject;
2943
2944static PyTypeObject FunctionType;
2945
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002946#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2947 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002948
Bram Moolenaardb913952012-06-29 12:54:53 +02002949 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002950FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002951 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002952{
2953 FunctionObject *self;
2954
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002955 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002956 if (self == NULL)
2957 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002958
2959 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002960 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002961 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002962 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002963 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002964 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002965 return NULL;
2966 }
2967 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002968 }
2969 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002970 {
2971 char_u *p;
2972
2973 if ((p = get_expanded_name(name,
2974 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002975 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002976 PyErr_FORMAT(PyExc_ValueError,
2977 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002978 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002979 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002980
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002981 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2982 {
2983 char_u *np;
2984 size_t len = STRLEN(p) + 1;
2985
Bram Moolenaar51e14382019-05-25 20:21:28 +02002986 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002987 {
2988 vim_free(p);
2989 return NULL;
2990 }
2991 mch_memmove(np, "<SNR>", 5);
2992 mch_memmove(np + 5, p + 3, len - 3);
2993 vim_free(p);
2994 self->name = np;
2995 }
2996 else
2997 self->name = p;
2998 }
2999
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02003000 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003001 self->argc = argc;
3002 self->argv = argv;
3003 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003004 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003005
3006 if (self->argv || self->self)
3007 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3008
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003009 return (PyObject *)(self);
3010}
3011
3012 static PyObject *
3013FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3014{
3015 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003016 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003017 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003018 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003019 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003020 typval_T selfdicttv;
3021 typval_T argstv;
3022 list_T *argslist = NULL;
3023 dict_T *selfdict = NULL;
3024 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003025 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003026 typval_T *argv = NULL;
3027 typval_T *curtv;
3028 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003029
Bram Moolenaar8110a092016-04-14 15:56:09 +02003030 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003031 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003032 selfdictObject = PyDict_GetItemString(kwargs, "self");
3033 if (selfdictObject != NULL)
3034 {
3035 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3036 return NULL;
3037 selfdict = selfdicttv.vval.v_dict;
3038 }
3039 argsObject = PyDict_GetItemString(kwargs, "args");
3040 if (argsObject != NULL)
3041 {
3042 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3043 {
3044 dict_unref(selfdict);
3045 return NULL;
3046 }
3047 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003048 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003049
3050 argc = argslist->lv_len;
3051 if (argc != 0)
3052 {
3053 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003054 if (argv == NULL)
3055 {
3056 PyErr_NoMemory();
3057 dict_unref(selfdict);
3058 list_unref(argslist);
3059 return NULL;
3060 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003061 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003062 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003063 copy_tv(&li->li_tv, curtv++);
3064 }
3065 list_unref(argslist);
3066 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003067 if (selfdict != NULL)
3068 {
3069 auto_rebind = FALSE;
3070 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3071 if (autoRebindObject != NULL)
3072 {
3073 auto_rebind = PyObject_IsTrue(autoRebindObject);
3074 if (auto_rebind == -1)
3075 {
3076 dict_unref(selfdict);
3077 list_unref(argslist);
3078 return NULL;
3079 }
3080 }
3081 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003082 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003083
Bram Moolenaar389a1792013-06-23 13:00:44 +02003084 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003085 {
3086 dict_unref(selfdict);
3087 while (argc--)
3088 clear_tv(&argv[argc]);
3089 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003090 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003091 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003092
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003093 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003094
Bram Moolenaar389a1792013-06-23 13:00:44 +02003095 PyMem_Free(name);
3096
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003097 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003098}
3099
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003100 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003101FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003102{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003103 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003104 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003105 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003106 for (i = 0; i < self->argc; ++i)
3107 clear_tv(&self->argv[i]);
3108 PyMem_Free(self->argv);
3109 dict_unref(self->self);
3110 if (self->argv || self->self)
3111 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003112
3113 DESTRUCTOR_FINISH(self);
3114}
3115
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003116static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003117 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003118 NULL
3119};
3120
3121 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003122FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003123{
3124 return ObjectDir(self, FunctionAttrs);
3125}
3126
Bram Moolenaardb913952012-06-29 12:54:53 +02003127 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003128FunctionAttr(FunctionObject *self, char *name)
3129{
3130 list_T *list;
3131 int i;
3132 if (strcmp(name, "name") == 0)
3133 return PyString_FromString((char *)(self->name));
3134 else if (strcmp(name, "args") == 0)
3135 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003136 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003137 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003138
Bram Moolenaar8110a092016-04-14 15:56:09 +02003139 for (i = 0; i < self->argc; ++i)
3140 list_append_tv(list, &self->argv[i]);
3141 return NEW_LIST(list);
3142 }
3143 else if (strcmp(name, "self") == 0)
3144 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003145 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003146 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003147 else if (strcmp(name, "auto_rebind") == 0)
3148 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003149 ? ALWAYS_TRUE
3150 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003151 else if (strcmp(name, "__members__") == 0)
3152 return ObjectDir(NULL, FunctionAttrs);
3153 return NULL;
3154}
3155
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003156/*
3157 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003158 *
3159 * "exported" should be set to true when it is needed to construct a partial
3160 * that may be stored in a variable (i.e. may be freed by Vim).
3161 */
3162 static void
3163set_partial(FunctionObject *self, partial_T *pt, int exported)
3164{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003165 int i;
3166
3167 pt->pt_name = self->name;
3168 if (self->argv)
3169 {
3170 pt->pt_argc = self->argc;
3171 if (exported)
3172 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003173 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003174 for (i = 0; i < pt->pt_argc; ++i)
3175 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3176 }
3177 else
3178 pt->pt_argv = self->argv;
3179 }
3180 else
3181 {
3182 pt->pt_argc = 0;
3183 pt->pt_argv = NULL;
3184 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003185 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003186 pt->pt_dict = self->self;
3187 if (exported && self->self)
3188 ++pt->pt_dict->dv_refcount;
3189 if (exported)
3190 pt->pt_name = vim_strsave(pt->pt_name);
3191 pt->pt_refcount = 1;
3192}
3193
3194 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003195FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003196{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003197 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003198 typval_T args;
3199 typval_T selfdicttv;
3200 typval_T rettv;
3201 dict_T *selfdict = NULL;
3202 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003203 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003204 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003205 partial_T pt;
3206 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003207
Bram Moolenaar8110a092016-04-14 15:56:09 +02003208 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003209 return NULL;
3210
3211 if (kwargs != NULL)
3212 {
3213 selfdictObject = PyDict_GetItemString(kwargs, "self");
3214 if (selfdictObject != NULL)
3215 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003216 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003217 {
3218 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003219 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003220 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003221 selfdict = selfdicttv.vval.v_dict;
3222 }
3223 }
3224
Bram Moolenaar8110a092016-04-14 15:56:09 +02003225 if (self->argv || self->self)
3226 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003227 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003228 set_partial(self, &pt, FALSE);
3229 pt_ptr = &pt;
3230 }
3231
Bram Moolenaar71700b82013-05-15 17:49:05 +02003232 Py_BEGIN_ALLOW_THREADS
3233 Python_Lock_Vim();
3234
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003235 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003236 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003237
3238 Python_Release_Vim();
3239 Py_END_ALLOW_THREADS
3240
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003241 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003242 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003243 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003244 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003245 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003246 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003247 }
3248 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003249 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003250
Bram Moolenaardb913952012-06-29 12:54:53 +02003251 clear_tv(&args);
3252 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003253 if (selfdict != NULL)
3254 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003255
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003256 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003257}
3258
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003259 static PyObject *
3260FunctionRepr(FunctionObject *self)
3261{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003262 PyObject *ret;
3263 garray_T repr_ga;
3264 int i;
3265 char_u *tofree = NULL;
3266 typval_T tv;
3267 char_u numbuf[NUMBUFLEN];
3268
3269 ga_init2(&repr_ga, (int)sizeof(char), 70);
3270 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3271 if (self->name)
3272 ga_concat(&repr_ga, self->name);
3273 else
3274 ga_concat(&repr_ga, (char_u *)"<NULL>");
3275 ga_append(&repr_ga, '\'');
3276 if (self->argv)
3277 {
3278 ga_concat(&repr_ga, (char_u *)", args=[");
3279 ++emsg_silent;
3280 for (i = 0; i < self->argc; i++)
3281 {
3282 if (i != 0)
3283 ga_concat(&repr_ga, (char_u *)", ");
3284 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3285 get_copyID()));
3286 vim_free(tofree);
3287 }
3288 --emsg_silent;
3289 ga_append(&repr_ga, ']');
3290 }
3291 if (self->self)
3292 {
3293 ga_concat(&repr_ga, (char_u *)", self=");
3294 tv.v_type = VAR_DICT;
3295 tv.vval.v_dict = self->self;
3296 ++emsg_silent;
3297 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3298 --emsg_silent;
3299 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003300 if (self->auto_rebind)
3301 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003302 }
3303 ga_append(&repr_ga, '>');
3304 ret = PyString_FromString((char *)repr_ga.ga_data);
3305 ga_clear(&repr_ga);
3306 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003307}
3308
Bram Moolenaardb913952012-06-29 12:54:53 +02003309static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003310 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3311 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003312};
3313
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003314/*
3315 * Options object
3316 */
3317
3318static PyTypeObject OptionsType;
3319
3320typedef int (*checkfun)(void *);
3321
3322typedef struct
3323{
3324 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003325 int opt_type;
3326 void *from;
3327 checkfun Check;
3328 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003329} OptionsObject;
3330
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003331 static int
3332dummy_check(void *arg UNUSED)
3333{
3334 return 0;
3335}
3336
3337 static PyObject *
3338OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3339{
3340 OptionsObject *self;
3341
Bram Moolenaar774267b2013-05-21 20:51:59 +02003342 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003343 if (self == NULL)
3344 return NULL;
3345
3346 self->opt_type = opt_type;
3347 self->from = from;
3348 self->Check = Check;
3349 self->fromObj = fromObj;
3350 if (fromObj)
3351 Py_INCREF(fromObj);
3352
3353 return (PyObject *)(self);
3354}
3355
3356 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003357OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003358{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003359 PyObject_GC_UnTrack((void *)(self));
3360 Py_XDECREF(self->fromObj);
3361 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003362}
3363
3364 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003365OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003366{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003367 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003368 return 0;
3369}
3370
3371 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003373{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003374 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003375 return 0;
3376}
3377
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003378 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003379OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003380{
3381 char_u *key;
3382 int flags;
3383 long numval;
3384 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003385 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003387 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003388 return NULL;
3389
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003390 if (!(key = StringToChars(keyObject, &todecref)))
3391 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003392
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003393 if (*key == NUL)
3394 {
3395 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003396 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003397 return NULL;
3398 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003399
3400 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003401 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003403 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003404
3405 if (flags == 0)
3406 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003407 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003408 return NULL;
3409 }
3410
3411 if (flags & SOPT_UNSET)
3412 {
3413 Py_INCREF(Py_None);
3414 return Py_None;
3415 }
3416 else if (flags & SOPT_BOOL)
3417 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003418 PyObject *ret;
3419 ret = numval ? Py_True : Py_False;
3420 Py_INCREF(ret);
3421 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003422 }
3423 else if (flags & SOPT_NUM)
3424 return PyInt_FromLong(numval);
3425 else if (flags & SOPT_STRING)
3426 {
3427 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003428 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003429 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003430 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003431 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003432 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003433 else
3434 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003435 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003436 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003437 return NULL;
3438 }
3439 }
3440 else
3441 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003442 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003443 return NULL;
3444 }
3445}
3446
3447 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003448OptionsContains(OptionsObject *self, PyObject *keyObject)
3449{
3450 char_u *key;
3451 PyObject *todecref;
3452
3453 if (!(key = StringToChars(keyObject, &todecref)))
3454 return -1;
3455
3456 if (*key == NUL)
3457 {
3458 Py_XDECREF(todecref);
3459 return 0;
3460 }
3461
3462 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3463 {
3464 Py_XDECREF(todecref);
3465 return 1;
3466 }
3467 else
3468 {
3469 Py_XDECREF(todecref);
3470 return 0;
3471 }
3472}
3473
3474typedef struct
3475{
3476 void *lastoption;
3477 int opt_type;
3478} optiterinfo_T;
3479
3480 static PyObject *
3481OptionsIterNext(optiterinfo_T **oii)
3482{
3483 char_u *name;
3484
3485 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3486 return PyString_FromString((char *)name);
3487
3488 return NULL;
3489}
3490
3491 static PyObject *
3492OptionsIter(OptionsObject *self)
3493{
3494 optiterinfo_T *oii;
3495
3496 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3497 {
3498 PyErr_NoMemory();
3499 return NULL;
3500 }
3501
3502 oii->opt_type = self->opt_type;
3503 oii->lastoption = NULL;
3504
3505 return IterNew(oii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003506 (destructorfun)(void *) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003507 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003508}
3509
3510 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003511set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003512{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003513 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003514
3515 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3516 {
3517 if (VimTryEnd())
3518 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003519 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003520 return FAIL;
3521 }
3522 return OK;
3523}
3524
3525 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003526set_option_value_for(
3527 char_u *key,
3528 int numval,
3529 char_u *stringval,
3530 int opt_flags,
3531 int opt_type,
3532 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003533{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003534 win_T *save_curwin = NULL;
3535 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003536 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003537 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003538
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003539 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540 switch (opt_type)
3541 {
3542 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003543 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003544 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003545 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003546 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003547 if (VimTryEnd())
3548 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003549 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003550 return -1;
3551 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003552 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3553 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003554 break;
3555 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003556 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003557 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003558 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003559 break;
3560 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003561 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003562 break;
3563 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003564 if (set_ret == FAIL)
3565 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003566 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003567}
3568
3569 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003570OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003571{
3572 char_u *key;
3573 int flags;
3574 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003575 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003576 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003578 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003579 return -1;
3580
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003581 if (!(key = StringToChars(keyObject, &todecref)))
3582 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003583
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003584 if (*key == NUL)
3585 {
3586 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003587 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003588 return -1;
3589 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003590
3591 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003592 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003593
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003594 if (flags == 0)
3595 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003596 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003597 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003598 return -1;
3599 }
3600
3601 if (valObject == NULL)
3602 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003603 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003604 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003605 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003606 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003607 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003608 return -1;
3609 }
3610 else if (!(flags & SOPT_GLOBAL))
3611 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003612 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003613 N_("unable to unset option %s "
3614 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003615 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003616 return -1;
3617 }
3618 else
3619 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003620 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003621 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003622 return 0;
3623 }
3624 }
3625
Bram Moolenaard6e39182013-05-21 18:30:34 +02003626 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003627
3628 if (flags & SOPT_BOOL)
3629 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003630 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003631
Bram Moolenaarb983f752013-05-15 16:11:50 +02003632 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003633 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003634 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003635 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003636 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003637 }
3638 else if (flags & SOPT_NUM)
3639 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003640 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003641
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003642 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003643 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003644 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003645 return -1;
3646 }
3647
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003648 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003649 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003650 }
3651 else
3652 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003653 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003654 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003655
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003656 if ((val = StringToChars(valObject, &todecref2)))
3657 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003658 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003659 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003660 Py_XDECREF(todecref2);
3661 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003662 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003663 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003664 }
3665
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003666 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003667
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003668 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003669}
3670
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003671static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003672 0, // sq_length
3673 0, // sq_concat
3674 0, // sq_repeat
3675 0, // sq_item
3676 0, // sq_slice
3677 0, // sq_ass_item
3678 0, // sq_ass_slice
3679 (objobjproc) OptionsContains, // sq_contains
3680 0, // sq_inplace_concat
3681 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003682};
3683
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003684static PyMappingMethods OptionsAsMapping = {
3685 (lenfunc) NULL,
3686 (binaryfunc) OptionsItem,
3687 (objobjargproc) OptionsAssItem,
3688};
3689
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003690// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003691
3692typedef struct
3693{
3694 PyObject_HEAD
3695 tabpage_T *tab;
3696} TabPageObject;
3697
3698static PyObject *WinListNew(TabPageObject *tabObject);
3699
3700static PyTypeObject TabPageType;
3701
3702 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003703CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003704{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003705 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003706 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003707 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003708 return -1;
3709 }
3710
3711 return 0;
3712}
3713
3714 static PyObject *
3715TabPageNew(tabpage_T *tab)
3716{
3717 TabPageObject *self;
3718
3719 if (TAB_PYTHON_REF(tab))
3720 {
3721 self = TAB_PYTHON_REF(tab);
3722 Py_INCREF(self);
3723 }
3724 else
3725 {
3726 self = PyObject_NEW(TabPageObject, &TabPageType);
3727 if (self == NULL)
3728 return NULL;
3729 self->tab = tab;
3730 TAB_PYTHON_REF(tab) = self;
3731 }
3732
3733 return (PyObject *)(self);
3734}
3735
3736 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003737TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003738{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003739 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3740 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003741
3742 DESTRUCTOR_FINISH(self);
3743}
3744
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003745static char *TabPageAttrs[] = {
3746 "windows", "number", "vars", "window", "valid",
3747 NULL
3748};
3749
3750 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003751TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003752{
3753 return ObjectDir(self, TabPageAttrs);
3754}
3755
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003756 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003757TabPageAttrValid(TabPageObject *self, char *name)
3758{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003759 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003760
3761 if (strcmp(name, "valid") != 0)
3762 return NULL;
3763
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003764 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3765 Py_INCREF(ret);
3766 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003767}
3768
3769 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003770TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771{
3772 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003773 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003775 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003776 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003777 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003778 else if (strcmp(name, "window") == 0)
3779 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003780 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003781 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003782 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003784 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003785 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003786 else if (strcmp(name, "__members__") == 0)
3787 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003788 return NULL;
3789}
3790
3791 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003792TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003793{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003794 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003795 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003796 else
3797 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003798 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003799
3800 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003801 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3802 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003803 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003804 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003805 }
3806}
3807
3808static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003809 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003810 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3811 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003812};
3813
3814/*
3815 * Window list object
3816 */
3817
3818static PyTypeObject TabListType;
3819static PySequenceMethods TabListAsSeq;
3820
3821typedef struct
3822{
3823 PyObject_HEAD
3824} TabListObject;
3825
3826 static PyInt
3827TabListLength(PyObject *self UNUSED)
3828{
3829 tabpage_T *tp = first_tabpage;
3830 PyInt n = 0;
3831
3832 while (tp != NULL)
3833 {
3834 ++n;
3835 tp = tp->tp_next;
3836 }
3837
3838 return n;
3839}
3840
3841 static PyObject *
3842TabListItem(PyObject *self UNUSED, PyInt n)
3843{
3844 tabpage_T *tp;
3845
3846 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3847 if (n == 0)
3848 return TabPageNew(tp);
3849
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003850 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003851 return NULL;
3852}
3853
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003854/*
3855 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003856 */
3857
3858typedef struct
3859{
3860 PyObject_HEAD
3861 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003862 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003863} WindowObject;
3864
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003865static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003866
3867 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003868CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003869{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003870 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003871 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003872 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003873 return -1;
3874 }
3875
3876 return 0;
3877}
3878
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003879 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003880WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003881{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003882 /*
3883 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003884 * If we add a "w_python*_ref" field to the win_T structure,
3885 * then we can get at it in win_free() in vim. We then
3886 * need to create only ONE Python object per window - if
3887 * we try to create a second, just INCREF the existing one
3888 * and return it. The (single) Python object referring to
3889 * the window is stored in "w_python*_ref".
3890 * On a win_free() we set the Python object's win_T* field
3891 * to an invalid value. We trap all uses of a window
3892 * object, and reject them if the win_T* field is invalid.
3893 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003894 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003895 * w_python_ref and w_python3_ref fields respectively.
3896 */
3897
3898 WindowObject *self;
3899
3900 if (WIN_PYTHON_REF(win))
3901 {
3902 self = WIN_PYTHON_REF(win);
3903 Py_INCREF(self);
3904 }
3905 else
3906 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003907 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003908 if (self == NULL)
3909 return NULL;
3910 self->win = win;
3911 WIN_PYTHON_REF(win) = self;
3912 }
3913
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003914 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3915
Bram Moolenaar971db462013-05-12 18:44:48 +02003916 return (PyObject *)(self);
3917}
3918
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003919 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003920WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003921{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003922 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003923 if (self->win && self->win != INVALID_WINDOW_VALUE)
3924 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003925 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003926 PyObject_GC_Del((void *)(self));
3927}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003928
Bram Moolenaar774267b2013-05-21 20:51:59 +02003929 static int
3930WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3931{
3932 Py_VISIT(((PyObject *)(self->tabObject)));
3933 return 0;
3934}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003935
Bram Moolenaar774267b2013-05-21 20:51:59 +02003936 static int
3937WindowClear(WindowObject *self)
3938{
3939 Py_CLEAR(self->tabObject);
3940 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003941}
3942
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003943 static win_T *
3944get_firstwin(TabPageObject *tabObject)
3945{
3946 if (tabObject)
3947 {
3948 if (CheckTabPage(tabObject))
3949 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003950 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003951 else if (tabObject->tab == curtab)
3952 return firstwin;
3953 else
3954 return tabObject->tab->tp_firstwin;
3955 }
3956 else
3957 return firstwin;
3958}
Bram Moolenaare950f992018-06-10 13:55:55 +02003959
3960// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003961static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003962 "buffer",
3963 "cursor",
3964 "height",
3965 "row",
3966 "width",
3967 "col",
3968 "vars",
3969 "options",
3970 "number",
3971 "tabpage",
3972 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003973 NULL
3974};
3975
3976 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003977WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003978{
3979 return ObjectDir(self, WindowAttrs);
3980}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003981
Bram Moolenaar971db462013-05-12 18:44:48 +02003982 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003983WindowAttrValid(WindowObject *self, char *name)
3984{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003985 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003986
3987 if (strcmp(name, "valid") != 0)
3988 return NULL;
3989
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003990 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3991 Py_INCREF(ret);
3992 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003993}
3994
3995 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003997{
3998 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004000 else if (strcmp(name, "cursor") == 0)
4001 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004002 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004003
4004 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4005 }
4006 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004007 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004008 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004009 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004010 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004011 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004012 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004013 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004014 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004015 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004016 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004017 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4018 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004019 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004020 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004021 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004022 return NULL;
4023 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004024 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004025 }
4026 else if (strcmp(name, "tabpage") == 0)
4027 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004028 Py_INCREF(self->tabObject);
4029 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004030 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004031 else if (strcmp(name, "__members__") == 0)
4032 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004033 else
4034 return NULL;
4035}
4036
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004038WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004040 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041 return -1;
4042
4043 if (strcmp(name, "buffer") == 0)
4044 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004045 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046 return -1;
4047 }
4048 else if (strcmp(name, "cursor") == 0)
4049 {
4050 long lnum;
4051 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004053 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 return -1;
4055
Bram Moolenaard6e39182013-05-21 18:30:34 +02004056 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004058 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 return -1;
4060 }
4061
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004062 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004063 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004064 return -1;
4065
Bram Moolenaard6e39182013-05-21 18:30:34 +02004066 self->win->w_cursor.lnum = lnum;
4067 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004068 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004069 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004070 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004071 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004073 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 return 0;
4075 }
4076 else if (strcmp(name, "height") == 0)
4077 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004078 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 win_T *savewin;
4080
Bram Moolenaardee2e312013-06-23 16:35:47 +02004081 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004082 return -1;
4083
4084#ifdef FEAT_GUI
4085 need_mouse_correct = TRUE;
4086#endif
4087 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004088 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004089
4090 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004091 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004093 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094 return -1;
4095
4096 return 0;
4097 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 else if (strcmp(name, "width") == 0)
4099 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004100 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 win_T *savewin;
4102
Bram Moolenaardee2e312013-06-23 16:35:47 +02004103 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004104 return -1;
4105
4106#ifdef FEAT_GUI
4107 need_mouse_correct = TRUE;
4108#endif
4109 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004110 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004111
4112 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004113 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004115 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 return -1;
4117
4118 return 0;
4119 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004120 else
4121 {
4122 PyErr_SetString(PyExc_AttributeError, name);
4123 return -1;
4124 }
4125}
4126
4127 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004128WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004130 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004131 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004132 else
4133 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004134 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135
Bram Moolenaar6d216452013-05-12 19:00:41 +02004136 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004137 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138 (self));
4139 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004140 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004141 }
4142}
4143
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004144static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004145 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004146 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4147 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004148};
4149
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004151 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004152 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004153
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004154static PyTypeObject WinListType;
4155static PySequenceMethods WinListAsSeq;
4156
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004157typedef struct
4158{
4159 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004160 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004161} WinListObject;
4162
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004163 static PyObject *
4164WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004165{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004166 WinListObject *self;
4167
4168 self = PyObject_NEW(WinListObject, &WinListType);
4169 self->tabObject = tabObject;
4170 Py_INCREF(tabObject);
4171
4172 return (PyObject *)(self);
4173}
4174
4175 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004176WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004177{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004178 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004179
4180 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004181 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004182 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004183 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004184
4185 DESTRUCTOR_FINISH(self);
4186}
4187
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004188 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004189WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004190{
4191 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192 PyInt n = 0;
4193
Bram Moolenaard6e39182013-05-21 18:30:34 +02004194 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004195 return -1;
4196
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004197 while (w != NULL)
4198 {
4199 ++n;
4200 w = W_NEXT(w);
4201 }
4202
4203 return n;
4204}
4205
4206 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004207WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004208{
4209 win_T *w;
4210
Bram Moolenaard6e39182013-05-21 18:30:34 +02004211 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004212 return NULL;
4213
4214 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004215 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004216 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004217
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004218 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004219 return NULL;
4220}
4221
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004222/*
4223 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004224 *
4225 * The result is in allocated memory. All internal nulls are replaced by
4226 * newline characters. It is an error for the string to contain newline
4227 * characters.
4228 *
4229 * On errors, the Python exception data is set, and NULL is returned.
4230 */
4231 static char *
4232StringToLine(PyObject *obj)
4233{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004234 char *str;
4235 char *save;
4236 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004237 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004238 PyInt i;
4239 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004240
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004241 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004242 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004243 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4244 || str == NULL)
4245 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004246 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004247 else if (PyUnicode_Check(obj))
4248 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004249 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4250 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004251 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004252
Bram Moolenaardaa27022013-06-24 22:33:30 +02004253 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004254 || str == NULL)
4255 {
4256 Py_DECREF(bytes);
4257 return NULL;
4258 }
4259 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004260 else
4261 {
4262#if PY_MAJOR_VERSION < 3
4263 PyErr_FORMAT(PyExc_TypeError,
4264 N_("expected str() or unicode() instance, but got %s"),
4265 Py_TYPE_NAME(obj));
4266#else
4267 PyErr_FORMAT(PyExc_TypeError,
4268 N_("expected bytes() or str() instance, but got %s"),
4269 Py_TYPE_NAME(obj));
4270#endif
4271 return NULL;
4272 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004273
4274 /*
4275 * Error checking: String must not contain newlines, as we
4276 * are replacing a single line, and we must replace it with
4277 * a single line.
4278 * A trailing newline is removed, so that append(f.readlines()) works.
4279 */
4280 p = memchr(str, '\n', len);
4281 if (p != NULL)
4282 {
4283 if (p == str + len - 1)
4284 --len;
4285 else
4286 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004287 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004288 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004289 return NULL;
4290 }
4291 }
4292
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004293 /*
4294 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004295 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004296 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004297 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004298 if (save == NULL)
4299 {
4300 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004301 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004302 return NULL;
4303 }
4304
4305 for (i = 0; i < len; ++i)
4306 {
4307 if (str[i] == '\0')
4308 save[i] = '\n';
4309 else
4310 save[i] = str[i];
4311 }
4312
4313 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004314 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004315
4316 return save;
4317}
4318
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004319/*
4320 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321 * in Vim format (1-based). The line is returned as a Python
4322 * string object.
4323 */
4324 static PyObject *
4325GetBufferLine(buf_T *buf, PyInt n)
4326{
4327 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4328}
4329
4330
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004331/*
4332 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004333 * are in Vim format (1-based). The range is from lo up to, but not
4334 * including, hi. The list is returned as a Python list of string objects.
4335 */
4336 static PyObject *
4337GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004339 PyInt i;
4340 PyInt n = hi - lo;
4341 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004342
4343 if (list == NULL)
4344 return NULL;
4345
4346 for (i = 0; i < n; ++i)
4347 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004348 linenr_T lnum = (linenr_T)(lo + i);
4349 char *text;
4350 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004351
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004352 if (lnum > buf->b_ml.ml_line_count)
4353 text = "";
4354 else
4355 text = (char *)ml_get_buf(buf, lnum, FALSE);
4356 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004357 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004358 {
4359 Py_DECREF(list);
4360 return NULL;
4361 }
4362
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004363 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004364 }
4365
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004366 // The ownership of the Python list is passed to the caller (ie,
4367 // the caller should Py_DECREF() the object when it is finished
4368 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004369
4370 return list;
4371}
4372
4373/*
4374 * Check if deleting lines made the cursor position invalid.
4375 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4376 * deleted).
4377 */
4378 static void
4379py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4380{
4381 if (curwin->w_cursor.lnum >= lo)
4382 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004383 // Adjust the cursor position if it's in/after the changed
4384 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004385 if (curwin->w_cursor.lnum >= hi)
4386 {
4387 curwin->w_cursor.lnum += extra;
4388 check_cursor_col();
4389 }
4390 else if (extra < 0)
4391 {
4392 curwin->w_cursor.lnum = lo;
4393 check_cursor();
4394 }
4395 else
4396 check_cursor_col();
4397 changed_cline_bef_curs();
4398 }
4399 invalidate_botline();
4400}
4401
Bram Moolenaar19e60942011-06-19 00:27:51 +02004402/*
4403 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004404 * in Vim format (1-based). The replacement line is given as
4405 * a Python string object. The object is checked for validity
4406 * and correct format. Errors are returned as a value of FAIL.
4407 * The return value is OK on success.
4408 * If OK is returned and len_change is not NULL, *len_change
4409 * is set to the change in the buffer length.
4410 */
4411 static int
4412SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4413{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004414 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004415 win_T *save_curwin = NULL;
4416 tabpage_T *save_curtab = NULL;
4417
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004418 // First of all, we check the type of the supplied Python object.
4419 // There are three cases:
4420 // 1. NULL, or None - this is a deletion.
4421 // 2. A string - this is a replacement.
4422 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423 if (line == Py_None || line == NULL)
4424 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004426 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004427
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004428 VimTryStart();
4429
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004430 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004431 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004432 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004433 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434 else
4435 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004436 if (buf == curbuf && (save_curwin != NULL
4437 || save_curbuf.br_buf == NULL))
4438 // Using an existing window for the buffer, adjust the cursor
4439 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004441 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004442 // Only adjust marks if we managed to switch to a window that
4443 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004444 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 }
4446
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004447 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004449 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004450 return FAIL;
4451
4452 if (len_change)
4453 *len_change = -1;
4454
4455 return OK;
4456 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004457 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004458 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004459 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004460
4461 if (save == NULL)
4462 return FAIL;
4463
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004464 VimTryStart();
4465
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004466 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004467 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004468 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004469
4470 if (u_savesub((linenr_T)n) == FAIL)
4471 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004472 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004473 vim_free(save);
4474 }
4475 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4476 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004477 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004478 vim_free(save);
4479 }
4480 else
4481 changed_bytes((linenr_T)n, 0);
4482
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004483 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004484
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004485 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004486 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004487 check_cursor_col();
4488
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004489 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004490 return FAIL;
4491
4492 if (len_change)
4493 *len_change = 0;
4494
4495 return OK;
4496 }
4497 else
4498 {
4499 PyErr_BadArgument();
4500 return FAIL;
4501 }
4502}
4503
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004504/*
4505 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004506 * Vim format (1-based). The range is from lo up to, but not including, hi.
4507 * The replacement lines are given as a Python list of string objects. The
4508 * list is checked for validity and correct format. Errors are returned as a
4509 * value of FAIL. The return value is OK on success.
4510 * If OK is returned and len_change is not NULL, *len_change
4511 * is set to the change in the buffer length.
4512 */
4513 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004514SetBufferLineList(
4515 buf_T *buf,
4516 PyInt lo,
4517 PyInt hi,
4518 PyObject *list,
4519 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004520{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004521 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004522 win_T *save_curwin = NULL;
4523 tabpage_T *save_curtab = NULL;
4524
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004525 // First of all, we check the type of the supplied Python object.
4526 // There are three cases:
4527 // 1. NULL, or None - this is a deletion.
4528 // 2. A list - this is a replacement.
4529 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004530 if (list == Py_None || list == NULL)
4531 {
4532 PyInt i;
4533 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004534
4535 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004536 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004537 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004538
4539 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004540 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541 else
4542 {
4543 for (i = 0; i < n; ++i)
4544 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004545 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004546 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004547 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004548 break;
4549 }
4550 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004551 if (buf == curbuf && (save_curwin != NULL
4552 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004553 // Using an existing window for the buffer, adjust the cursor
4554 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004555 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004556 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004557 // Only adjust marks if we managed to switch to a window that
4558 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004559 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004560 }
4561
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004562 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004563
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004564 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004565 return FAIL;
4566
4567 if (len_change)
4568 *len_change = -n;
4569
4570 return OK;
4571 }
4572 else if (PyList_Check(list))
4573 {
4574 PyInt i;
4575 PyInt new_len = PyList_Size(list);
4576 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004577 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004578 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004579
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004580 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004581 array = NULL;
4582 else
4583 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004584 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004585 if (array == NULL)
4586 {
4587 PyErr_NoMemory();
4588 return FAIL;
4589 }
4590 }
4591
4592 for (i = 0; i < new_len; ++i)
4593 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004594 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004595
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004596 if (!(line = PyList_GetItem(list, i)) ||
4597 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004598 {
4599 while (i)
4600 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004601 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004602 return FAIL;
4603 }
4604 }
4605
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004606 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004607 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004608
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004609 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004610 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004611
4612 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004613 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004614
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004615 // If the size of the range is reducing (ie, new_len < old_len) we
4616 // need to delete some old_len. We do this at the start, by
4617 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004618 if (!PyErr_Occurred())
4619 {
4620 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004621 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004623 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004624 break;
4625 }
4626 extra -= i;
4627 }
4628
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004629 // For as long as possible, replace the existing old_len with the
4630 // new old_len. This is a more efficient operation, as it requires
4631 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004632 if (!PyErr_Occurred())
4633 {
4634 for (i = 0; i < old_len && i < new_len; ++i)
4635 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4636 == FAIL)
4637 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004638 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004639 break;
4640 }
4641 }
4642 else
4643 i = 0;
4644
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004645 // Now we may need to insert the remaining new old_len. If we do, we
4646 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004647 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004648 if (!PyErr_Occurred())
4649 {
4650 while (i < new_len)
4651 {
4652 if (ml_append((linenr_T)(lo + i - 1),
4653 (char_u *)array[i], 0, FALSE) == FAIL)
4654 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004655 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004656 break;
4657 }
4658 vim_free(array[i]);
4659 ++i;
4660 ++extra;
4661 }
4662 }
4663
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004664 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004665 while (i < new_len)
4666 {
4667 vim_free(array[i]);
4668 ++i;
4669 }
4670
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004671 // Free the array of old_len. All of its contents have now
4672 // been dealt with (either freed, or the responsibility passed
4673 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004674 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004675
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004676 // Adjust marks. Invalidate any which lie in the
4677 // changed range, and move any in the remainder of the buffer.
4678 // Only adjust marks if we managed to switch to a window that holds
4679 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004680 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004681 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004682 (long)MAXLNUM, (long)extra);
4683 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4684
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004685 if (buf == curbuf && (save_curwin != NULL
4686 || save_curbuf.br_buf == NULL))
4687 // Using an existing window for the buffer, adjust the cursor
4688 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004689 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4690
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004691 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004692 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004693
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004694 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004695 return FAIL;
4696
4697 if (len_change)
4698 *len_change = new_len - old_len;
4699
4700 return OK;
4701 }
4702 else
4703 {
4704 PyErr_BadArgument();
4705 return FAIL;
4706 }
4707}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004709/*
4710 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711 * The line number is in Vim format (1-based). The lines to be inserted are
4712 * given as a Python list of string objects or as a single string. The lines
4713 * to be added are checked for validity and correct format. Errors are
4714 * returned as a value of FAIL. The return value is OK on success.
4715 * If OK is returned and len_change is not NULL, *len_change
4716 * is set to the change in the buffer length.
4717 */
4718 static int
4719InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4720{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004721 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004722 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004723 tabpage_T *save_curtab = NULL;
4724
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004725 // First of all, we check the type of the supplied Python object.
4726 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004727 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004728 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004729 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730
4731 if (str == NULL)
4732 return FAIL;
4733
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004734 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004735 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004736 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004737
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004738 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004739 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004740 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004741 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004742 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004743 // Only adjust marks if we managed to switch to a window that
4744 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 appended_lines_mark((linenr_T)n, 1L);
4746
4747 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004748 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 update_screen(VALID);
4750
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004751 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752 return FAIL;
4753
4754 if (len_change)
4755 *len_change = 1;
4756
4757 return OK;
4758 }
4759 else if (PyList_Check(lines))
4760 {
4761 PyInt i;
4762 PyInt size = PyList_Size(lines);
4763 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004764
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004765 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004766 if (array == NULL)
4767 {
4768 PyErr_NoMemory();
4769 return FAIL;
4770 }
4771
4772 for (i = 0; i < size; ++i)
4773 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004774 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004775
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004776 if (!(line = PyList_GetItem(lines, i)) ||
4777 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004778 {
4779 while (i)
4780 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004781 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004782 return FAIL;
4783 }
4784 }
4785
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004787 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004788 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004789
4790 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004791 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004792 else
4793 {
4794 for (i = 0; i < size; ++i)
4795 {
4796 if (ml_append((linenr_T)(n + i),
4797 (char_u *)array[i], 0, FALSE) == FAIL)
4798 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004799 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004800
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004801 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004802 while (i < size)
4803 vim_free(array[i++]);
4804
4805 break;
4806 }
4807 vim_free(array[i]);
4808 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004809 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004810 // Only adjust marks if we managed to switch to a window that
4811 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004812 appended_lines_mark((linenr_T)n, (long)i);
4813 }
4814
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004815 // Free the array of lines. All of its contents have now
4816 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004817 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004818 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004819
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004820 update_screen(VALID);
4821
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004822 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004823 return FAIL;
4824
4825 if (len_change)
4826 *len_change = size;
4827
4828 return OK;
4829 }
4830 else
4831 {
4832 PyErr_BadArgument();
4833 return FAIL;
4834 }
4835}
4836
4837/*
4838 * Common routines for buffers and line ranges
4839 * -------------------------------------------
4840 */
4841
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004842typedef struct
4843{
4844 PyObject_HEAD
4845 buf_T *buf;
4846} BufferObject;
4847
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004848 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004849CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004850{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004851 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004852 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004853 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004854 return -1;
4855 }
4856
4857 return 0;
4858}
4859
4860 static PyObject *
4861RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4862{
4863 if (CheckBuffer(self))
4864 return NULL;
4865
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004866 if (end == -1)
4867 end = self->buf->b_ml.ml_line_count;
4868
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004869 if (n < 0)
4870 n += end - start + 1;
4871
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004872 if (n < 0 || n > end - start)
4873 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004874 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004875 return NULL;
4876 }
4877
4878 return GetBufferLine(self->buf, n+start);
4879}
4880
4881 static PyObject *
4882RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4883{
4884 PyInt size;
4885
4886 if (CheckBuffer(self))
4887 return NULL;
4888
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004889 if (end == -1)
4890 end = self->buf->b_ml.ml_line_count;
4891
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004892 size = end - start + 1;
4893
4894 if (lo < 0)
4895 lo = 0;
4896 else if (lo > size)
4897 lo = size;
4898 if (hi < 0)
4899 hi = 0;
4900 if (hi < lo)
4901 hi = lo;
4902 else if (hi > size)
4903 hi = size;
4904
4905 return GetBufferLineList(self->buf, lo+start, hi+start);
4906}
4907
4908 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004909RBAsItem(
4910 BufferObject *self,
4911 PyInt n,
4912 PyObject *valObject,
4913 PyInt start,
4914 PyInt end,
4915 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004916{
4917 PyInt len_change;
4918
4919 if (CheckBuffer(self))
4920 return -1;
4921
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004922 if (end == -1)
4923 end = self->buf->b_ml.ml_line_count;
4924
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004925 if (n < 0)
4926 n += end - start + 1;
4927
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004928 if (n < 0 || n > end - start)
4929 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004930 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004931 return -1;
4932 }
4933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004934 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004935 return -1;
4936
4937 if (new_end)
4938 *new_end = end + len_change;
4939
4940 return 0;
4941}
4942
Bram Moolenaar19e60942011-06-19 00:27:51 +02004943 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004944RBAsSlice(
4945 BufferObject *self,
4946 PyInt lo,
4947 PyInt hi,
4948 PyObject *valObject,
4949 PyInt start,
4950 PyInt end,
4951 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004952{
4953 PyInt size;
4954 PyInt len_change;
4955
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004956 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004957 if (CheckBuffer(self))
4958 return -1;
4959
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004960 if (end == -1)
4961 end = self->buf->b_ml.ml_line_count;
4962
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004963 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004964 size = end - start + 1;
4965
4966 if (lo < 0)
4967 lo = 0;
4968 else if (lo > size)
4969 lo = size;
4970 if (hi < 0)
4971 hi = 0;
4972 if (hi < lo)
4973 hi = lo;
4974 else if (hi > size)
4975 hi = size;
4976
4977 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004978 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004979 return -1;
4980
4981 if (new_end)
4982 *new_end = end + len_change;
4983
4984 return 0;
4985}
4986
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004987
4988 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004989RBAppend(
4990 BufferObject *self,
4991 PyObject *args,
4992 PyInt start,
4993 PyInt end,
4994 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004995{
4996 PyObject *lines;
4997 PyInt len_change;
4998 PyInt max;
4999 PyInt n;
5000
5001 if (CheckBuffer(self))
5002 return NULL;
5003
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005004 if (end == -1)
5005 end = self->buf->b_ml.ml_line_count;
5006
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005007 max = n = end - start + 1;
5008
5009 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5010 return NULL;
5011
5012 if (n < 0 || n > max)
5013 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005014 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005015 return NULL;
5016 }
5017
5018 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5019 return NULL;
5020
5021 if (new_end)
5022 *new_end = end + len_change;
5023
5024 Py_INCREF(Py_None);
5025 return Py_None;
5026}
5027
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005028// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005029
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005031static PySequenceMethods RangeAsSeq;
5032static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005034typedef struct
5035{
5036 PyObject_HEAD
5037 BufferObject *buf;
5038 PyInt start;
5039 PyInt end;
5040} RangeObject;
5041
5042 static PyObject *
5043RangeNew(buf_T *buf, PyInt start, PyInt end)
5044{
5045 BufferObject *bufr;
5046 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005047 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005048 if (self == NULL)
5049 return NULL;
5050
5051 bufr = (BufferObject *)BufferNew(buf);
5052 if (bufr == NULL)
5053 {
5054 Py_DECREF(self);
5055 return NULL;
5056 }
5057 Py_INCREF(bufr);
5058
5059 self->buf = bufr;
5060 self->start = start;
5061 self->end = end;
5062
5063 return (PyObject *)(self);
5064}
5065
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005066 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005068{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005069 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005070 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005071 PyObject_GC_Del((void *)(self));
5072}
5073
5074 static int
5075RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5076{
5077 Py_VISIT(((PyObject *)(self->buf)));
5078 return 0;
5079}
5080
5081 static int
5082RangeClear(RangeObject *self)
5083{
5084 Py_CLEAR(self->buf);
5085 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005086}
5087
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005090{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005091 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005093 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094
Bram Moolenaard6e39182013-05-21 18:30:34 +02005095 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005096}
5097
5098 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005099RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005101 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005102}
5103
5104 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005105RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005107 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005108}
5109
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005110static char *RangeAttrs[] = {
5111 "start", "end",
5112 NULL
5113};
5114
5115 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005116RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005117{
5118 return ObjectDir(self, RangeAttrs);
5119}
5120
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005124 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005125}
5126
5127 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005130 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005131 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5132 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005133 else
5134 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005135 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136
5137 if (name == NULL)
5138 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005140 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005141 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005142 }
5143}
5144
5145static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005146 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005147 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005148 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5149 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005150};
5151
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005152static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005153static PySequenceMethods BufferAsSeq;
5154static PyMappingMethods BufferAsMapping;
5155
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005156 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005157BufferNew(buf_T *buf)
5158{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005159 /*
5160 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005161 * If we add a "b_python*_ref" field to the buf_T structure,
5162 * then we can get at it in buf_freeall() in vim. We then
5163 * need to create only ONE Python object per buffer - if
5164 * we try to create a second, just INCREF the existing one
5165 * and return it. The (single) Python object referring to
5166 * the buffer is stored in "b_python*_ref".
5167 * Question: what to do on a buf_freeall(). We'll probably
5168 * have to either delete the Python object (DECREF it to
5169 * zero - a bad idea, as it leaves dangling refs!) or
5170 * set the buf_T * value to an invalid value (-1?), which
5171 * means we need checks in all access functions... Bah.
5172 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005173 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005174 * b_python_ref and b_python3_ref fields respectively.
5175 */
5176
5177 BufferObject *self;
5178
5179 if (BUF_PYTHON_REF(buf) != NULL)
5180 {
5181 self = BUF_PYTHON_REF(buf);
5182 Py_INCREF(self);
5183 }
5184 else
5185 {
5186 self = PyObject_NEW(BufferObject, &BufferType);
5187 if (self == NULL)
5188 return NULL;
5189 self->buf = buf;
5190 BUF_PYTHON_REF(buf) = self;
5191 }
5192
5193 return (PyObject *)(self);
5194}
5195
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005196 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005197BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005198{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005199 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5200 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005201
5202 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005203}
5204
Bram Moolenaar971db462013-05-12 18:44:48 +02005205 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005206BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005207{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005208 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005209 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005210 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005211
Bram Moolenaard6e39182013-05-21 18:30:34 +02005212 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005213}
5214
5215 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005216BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005217{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005218 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005219}
5220
5221 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005222BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005223{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005224 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005225}
5226
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005227static char *BufferAttrs[] = {
5228 "name", "number", "vars", "options", "valid",
5229 NULL
5230};
5231
5232 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005233BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005234{
5235 return ObjectDir(self, BufferAttrs);
5236}
5237
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005238 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005239BufferAttrValid(BufferObject *self, char *name)
5240{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005241 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005242
5243 if (strcmp(name, "valid") != 0)
5244 return NULL;
5245
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005246 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5247 Py_INCREF(ret);
5248 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005249}
5250
5251 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005252BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005253{
5254 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005255 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005256 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005257 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005258 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005259 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005260 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005261 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005262 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5263 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005264 else if (strcmp(name, "__members__") == 0)
5265 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005266 else
5267 return NULL;
5268}
5269
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005270 static int
5271BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5272{
5273 if (CheckBuffer(self))
5274 return -1;
5275
5276 if (strcmp(name, "name") == 0)
5277 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005278 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005279 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005280 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005281 PyObject *todecref;
5282
5283 if (!(val = StringToChars(valObject, &todecref)))
5284 return -1;
5285
5286 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005287 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005288 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005289 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005290 aucmd_restbuf(&aco);
5291 Py_XDECREF(todecref);
5292 if (VimTryEnd())
5293 return -1;
5294
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005295 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005296 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005297 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005298 return -1;
5299 }
5300 return 0;
5301 }
5302 else
5303 {
5304 PyErr_SetString(PyExc_AttributeError, name);
5305 return -1;
5306 }
5307}
5308
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005309 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005312 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005313}
5314
5315 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005316BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005317{
5318 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005319 char_u *pmark;
5320 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005321 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005322 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005323
Bram Moolenaard6e39182013-05-21 18:30:34 +02005324 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005325 return NULL;
5326
Bram Moolenaar389a1792013-06-23 13:00:44 +02005327 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005328 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005329
Bram Moolenaar389a1792013-06-23 13:00:44 +02005330 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005331 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005332 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005333 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005334 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005335 return NULL;
5336 }
5337
5338 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005339
5340 Py_XDECREF(todecref);
5341
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005342 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005343 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005344 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005345 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005346 if (VimTryEnd())
5347 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005348
5349 if (posp == NULL)
5350 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005351 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005352 return NULL;
5353 }
5354
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005355 if (posp->lnum <= 0)
5356 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005357 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005358 Py_INCREF(Py_None);
5359 return Py_None;
5360 }
5361
5362 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5363}
5364
5365 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005366BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005367{
5368 PyInt start;
5369 PyInt end;
5370
Bram Moolenaard6e39182013-05-21 18:30:34 +02005371 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005372 return NULL;
5373
5374 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5375 return NULL;
5376
Bram Moolenaard6e39182013-05-21 18:30:34 +02005377 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005378}
5379
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005380 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005381BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005382{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005383 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005384 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005385 else
5386 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005387 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005388
5389 if (name == NULL)
5390 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005391
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005392 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005393 }
5394}
5395
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005396static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005397 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005398 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005399 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005400 {"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 +02005401 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5402 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005403};
5404
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005405/*
5406 * Buffer list object - Implementation
5407 */
5408
5409static PyTypeObject BufMapType;
5410
5411typedef struct
5412{
5413 PyObject_HEAD
5414} BufMapObject;
5415
5416 static PyInt
5417BufMapLength(PyObject *self UNUSED)
5418{
5419 buf_T *b = firstbuf;
5420 PyInt n = 0;
5421
5422 while (b)
5423 {
5424 ++n;
5425 b = b->b_next;
5426 }
5427
5428 return n;
5429}
5430
5431 static PyObject *
5432BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5433{
5434 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005435 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005436
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005437 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005438 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005439
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005440 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005441
5442 if (b)
5443 return BufferNew(b);
5444 else
5445 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005446 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005447 return NULL;
5448 }
5449}
5450
5451 static void
5452BufMapIterDestruct(PyObject *buffer)
5453{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005454 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005455 if (buffer)
5456 {
5457 Py_DECREF(buffer);
5458 }
5459}
5460
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005461 static int
5462BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5463{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005464 if (buffer)
5465 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005466 return 0;
5467}
5468
5469 static int
5470BufMapIterClear(PyObject **buffer)
5471{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005472 if (*buffer)
5473 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005474 return 0;
5475}
5476
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005477 static PyObject *
5478BufMapIterNext(PyObject **buffer)
5479{
5480 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005481 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005482
5483 if (!*buffer)
5484 return NULL;
5485
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005486 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005487
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005488 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005489 {
5490 *buffer = NULL;
5491 return NULL;
5492 }
5493
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005494 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005495 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005496 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005497 return NULL;
5498 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005499 // Do not increment reference: we no longer hold it (decref), but whoever
5500 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005501 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005502}
5503
5504 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005505BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005506{
5507 PyObject *buffer;
5508
5509 buffer = BufferNew(firstbuf);
5510 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005511 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005512 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5513 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005514}
5515
5516static PyMappingMethods BufMapAsMapping = {
5517 (lenfunc) BufMapLength,
5518 (binaryfunc) BufMapItem,
5519 (objobjargproc) 0,
5520};
5521
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005522// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005523
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005524static char *CurrentAttrs[] = {
5525 "buffer", "window", "line", "range", "tabpage",
5526 NULL
5527};
5528
5529 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005530CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005531{
5532 return ObjectDir(self, CurrentAttrs);
5533}
5534
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005535 static PyObject *
5536CurrentGetattr(PyObject *self UNUSED, char *name)
5537{
5538 if (strcmp(name, "buffer") == 0)
5539 return (PyObject *)BufferNew(curbuf);
5540 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005541 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005542 else if (strcmp(name, "tabpage") == 0)
5543 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005544 else if (strcmp(name, "line") == 0)
5545 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5546 else if (strcmp(name, "range") == 0)
5547 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005548 else if (strcmp(name, "__members__") == 0)
5549 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005550 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005551#if PY_MAJOR_VERSION < 3
5552 return Py_FindMethod(WindowMethods, self, name);
5553#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005554 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005555#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005556}
5557
5558 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005559CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005560{
5561 if (strcmp(name, "line") == 0)
5562 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5564 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005565 return -1;
5566
5567 return 0;
5568 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005569 else if (strcmp(name, "buffer") == 0)
5570 {
5571 int count;
5572
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005574 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005575 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005576 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005578 return -1;
5579 }
5580
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005583 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005584
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005585 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005586 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5587 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005588 if (VimTryEnd())
5589 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005590 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
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, "window") == 0)
5597 {
5598 int count;
5599
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005600 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005601 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005602 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005603 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005604 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005605 return -1;
5606 }
5607
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005608 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005609 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005610 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005611
5612 if (!count)
5613 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005614 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005615 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005616 return -1;
5617 }
5618
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005619 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005620 win_goto(((WindowObject *)(valObject))->win);
5621 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005622 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005623 if (VimTryEnd())
5624 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005625 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005626 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005627 return -1;
5628 }
5629
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005630 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005631 }
5632 else if (strcmp(name, "tabpage") == 0)
5633 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005634 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005635 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005636 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005637 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005638 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005639 return -1;
5640 }
5641
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005642 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005643 return -1;
5644
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005645 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005646 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5647 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005648 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005649 if (VimTryEnd())
5650 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005651 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005652 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005653 return -1;
5654 }
5655
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005656 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005657 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005658 else
5659 {
5660 PyErr_SetString(PyExc_AttributeError, name);
5661 return -1;
5662 }
5663}
5664
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005665static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005666 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005667 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5668 { NULL, NULL, 0, NULL}
5669};
5670
Bram Moolenaardb913952012-06-29 12:54:53 +02005671 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005672init_range_cmd(exarg_T *eap)
5673{
5674 RangeStart = eap->line1;
5675 RangeEnd = eap->line2;
5676}
5677
5678 static void
5679init_range_eval(typval_T *rettv UNUSED)
5680{
5681 RangeStart = (PyInt) curwin->w_cursor.lnum;
5682 RangeEnd = RangeStart;
5683}
5684
5685 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005686run_cmd(const char *cmd, void *arg UNUSED
5687#ifdef PY_CAN_RECURSE
5688 , PyGILState_STATE *pygilstate UNUSED
5689#endif
5690 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691{
Bram Moolenaar41009372013-07-01 22:03:04 +02005692 PyObject *run_ret;
5693 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5694 if (run_ret != NULL)
5695 {
5696 Py_DECREF(run_ret);
5697 }
5698 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005700 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005701 PyErr_Clear();
5702 }
5703 else
5704 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005705}
5706
5707static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5708static int code_hdr_len = 30;
5709
5710 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005711run_do(const char *cmd, void *arg UNUSED
5712#ifdef PY_CAN_RECURSE
5713 , PyGILState_STATE *pygilstate
5714#endif
5715 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005716{
5717 PyInt lnum;
5718 size_t len;
5719 char *code;
5720 int status;
5721 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005722 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005723 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005725 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005727 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005728 return;
5729 }
5730
5731 len = code_hdr_len + STRLEN(cmd);
5732 code = PyMem_New(char, len + 1);
5733 memcpy(code, code_hdr, code_hdr_len);
5734 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005735 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5736 status = -1;
5737 if (run_ret != NULL)
5738 {
5739 status = 0;
5740 Py_DECREF(run_ret);
5741 }
5742 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5743 {
5744 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005745 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005746 PyErr_Clear();
5747 return;
5748 }
5749 else
5750 PyErr_PrintEx(1);
5751
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005752 PyMem_Free(code);
5753
5754 if (status)
5755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005756 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005757 return;
5758 }
5759
5760 status = 0;
5761 pymain = PyImport_AddModule("__main__");
5762 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005763#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005765#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005766
5767 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5768 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005769 PyObject *line;
5770 PyObject *linenr;
5771 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005772
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005773#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005774 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005775#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005776 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005777 if (lnum > curbuf->b_ml.ml_line_count
5778 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005779 goto err;
5780 if (!(linenr = PyInt_FromLong((long) lnum)))
5781 {
5782 Py_DECREF(line);
5783 goto err;
5784 }
5785 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5786 Py_DECREF(line);
5787 Py_DECREF(linenr);
5788 if (!ret)
5789 goto err;
5790
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005791 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005792 if (curbuf != was_curbuf)
5793 {
5794 Py_XDECREF(ret);
5795 goto err;
5796 }
5797
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005798 if (ret != Py_None)
5799 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005800 {
5801 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005802 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005803 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005804
5805 Py_XDECREF(ret);
5806 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005807#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005808 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005809#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005810 }
5811 goto out;
5812err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005813#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005814 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005815#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005816 PyErr_PrintEx(0);
5817 PythonIO_Flush();
5818 status = 1;
5819out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005820#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005821 if (!status)
5822 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005823#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005824 Py_DECREF(pyfunc);
5825 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5826 if (status)
5827 return;
5828 check_cursor();
5829 update_curbuf(NOT_VALID);
5830}
5831
5832 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005833run_eval(const char *cmd, typval_T *rettv
5834#ifdef PY_CAN_RECURSE
5835 , PyGILState_STATE *pygilstate UNUSED
5836#endif
5837 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005838{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005839 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005840
Bram Moolenaar41009372013-07-01 22:03:04 +02005841 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005842 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005843 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005844 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005846 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005847 PyErr_Clear();
5848 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005849 else
5850 {
5851 if (PyErr_Occurred() && !msg_silent)
5852 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005853 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005854 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005855 }
5856 else
5857 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005858 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar86181df2020-05-11 23:14:04 +02005859 emsg(_("E859: Failed to convert returned python object to a Vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005860 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005861 }
5862 PyErr_Clear();
5863}
5864
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005865 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005866set_ref_in_py(const int copyID)
5867{
5868 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005869 list_T *ll;
5870 int i;
5871 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005872 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005873
5874 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005876 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005877 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5878 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005879 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005880
5881 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005882 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005883 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005884 {
5885 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005886 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005887 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005888 }
5889
Bram Moolenaar8110a092016-04-14 15:56:09 +02005890 if (lastfunc != NULL)
5891 {
5892 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5893 {
5894 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005895 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005896 if (func->argc)
5897 for (i = 0; !abort && i < func->argc; ++i)
5898 abort = abort
5899 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5900 }
5901 }
5902
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005903 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005904}
5905
5906 static int
5907set_string_copy(char_u *str, typval_T *tv)
5908{
5909 tv->vval.v_string = vim_strsave(str);
5910 if (tv->vval.v_string == NULL)
5911 {
5912 PyErr_NoMemory();
5913 return -1;
5914 }
5915 return 0;
5916}
5917
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005919pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005921 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922 char_u *key;
5923 dictitem_T *di;
5924 PyObject *keyObject;
5925 PyObject *valObject;
5926 Py_ssize_t iter = 0;
5927
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005928 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005929 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930
5931 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005932 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005933
5934 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5935 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005936 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937
Bram Moolenaara03e6312013-05-29 22:49:26 +02005938 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005939 {
5940 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005942 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005943
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005944 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005945 {
5946 dict_unref(dict);
5947 return -1;
5948 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005949
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005950 if (*key == NUL)
5951 {
5952 dict_unref(dict);
5953 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005954 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005955 return -1;
5956 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005957
5958 di = dictitem_alloc(key);
5959
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005960 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961
5962 if (di == NULL)
5963 {
5964 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005965 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return -1;
5967 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005969 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 {
5971 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005972 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 return -1;
5974 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005975
5976 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005978 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005979 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005980 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005982 return -1;
5983 }
5984 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985
5986 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005987 return 0;
5988}
5989
5990 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005991pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005992{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005993 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005994 char_u *key;
5995 dictitem_T *di;
5996 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005997 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998 PyObject *keyObject;
5999 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006000
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006001 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006002 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006003
6004 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006005 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006006
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006007 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006008 {
6009 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006010 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006011 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006012
6013 if (!(iterator = PyObject_GetIter(list)))
6014 {
6015 dict_unref(dict);
6016 Py_DECREF(list);
6017 return -1;
6018 }
6019 Py_DECREF(list);
6020
6021 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006022 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006024
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006025 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006026 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006027 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006028 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006029 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006030 return -1;
6031 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006032
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006033 if (*key == NUL)
6034 {
6035 Py_DECREF(keyObject);
6036 Py_DECREF(iterator);
6037 Py_XDECREF(todecref);
6038 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006039 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006040 return -1;
6041 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006043 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006044 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006045 Py_DECREF(keyObject);
6046 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006047 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006048 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006049 return -1;
6050 }
6051
6052 di = dictitem_alloc(key);
6053
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006054 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006055 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006056
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006057 if (di == NULL)
6058 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006059 Py_DECREF(iterator);
6060 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006061 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006062 PyErr_NoMemory();
6063 return -1;
6064 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006065
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006066 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006067 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006068 Py_DECREF(iterator);
6069 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006070 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006071 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006072 return -1;
6073 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006074
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006075 Py_DECREF(valObject);
6076
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006077 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006078 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006079 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006080 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006081 dictitem_free(di);
6082 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006083 return -1;
6084 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006085 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006086 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006087 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006088 return 0;
6089}
6090
6091 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006092pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006093{
6094 list_T *l;
6095
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006096 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006097 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006098
6099 tv->v_type = VAR_LIST;
6100 tv->vval.v_list = l;
6101
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006102 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006103 {
6104 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006105 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006106 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006107
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006108 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006109 return 0;
6110}
6111
Bram Moolenaardb913952012-06-29 12:54:53 +02006112typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6113
6114 static int
6115convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006116 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006117{
6118 PyObject *capsule;
6119 char hexBuf[sizeof(void *) * 2 + 3];
6120
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006121 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006122
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006123#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006124 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006125#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006126 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006127#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006128 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006129 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006130#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006131 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006132#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006133 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006134#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006135 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6136 {
6137 Py_DECREF(capsule);
6138 tv->v_type = VAR_UNKNOWN;
6139 return -1;
6140 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006141
6142 Py_DECREF(capsule);
6143
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006144 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006145 {
6146 tv->v_type = VAR_UNKNOWN;
6147 return -1;
6148 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006149 // As we are not using copy_tv which increments reference count we must
6150 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006151 if (tv->v_type == VAR_DICT)
6152 ++tv->vval.v_dict->dv_refcount;
6153 else if (tv->v_type == VAR_LIST)
6154 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006155 }
6156 else
6157 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006158 typval_T *v;
6159
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006160#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006161 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006162#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006163 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006164#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 copy_tv(v, tv);
6166 }
6167 return 0;
6168}
6169
6170 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006171ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6172{
6173 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006174 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006175
6176 if (!(lookup_dict = PyDict_New()))
6177 return -1;
6178
6179 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6180 {
6181 tv->v_type = VAR_DICT;
6182 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6183 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006184 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006185 }
6186 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006187 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006188 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006189 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006190 else
6191 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006192 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006193 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006194 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006195 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006196 }
6197 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006198 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006199}
6200
6201 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006202ConvertFromPySequence(PyObject *obj, typval_T *tv)
6203{
6204 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006205 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006206
6207 if (!(lookup_dict = PyDict_New()))
6208 return -1;
6209
6210 if (PyType_IsSubtype(obj->ob_type, &ListType))
6211 {
6212 tv->v_type = VAR_LIST;
6213 tv->vval.v_list = (((ListObject *)(obj))->list);
6214 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006215 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006216 }
6217 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006218 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006219 else
6220 {
6221 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006222 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006223 Py_TYPE_NAME(obj));
6224 ret = -1;
6225 }
6226 Py_DECREF(lookup_dict);
6227 return ret;
6228}
6229
6230 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006231ConvertFromPyObject(PyObject *obj, typval_T *tv)
6232{
6233 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006234 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006235
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006236 if (!(lookup_dict = PyDict_New()))
6237 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006238 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006239 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006240 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006241}
6242
6243 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006244_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006245{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006246 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006247 {
6248 tv->v_type = VAR_DICT;
6249 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6250 ++tv->vval.v_dict->dv_refcount;
6251 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006252 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006253 {
6254 tv->v_type = VAR_LIST;
6255 tv->vval.v_list = (((ListObject *)(obj))->list);
6256 ++tv->vval.v_list->lv_refcount;
6257 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006258 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006260 FunctionObject *func = (FunctionObject *) obj;
6261 if (func->self != NULL || func->argv != NULL)
6262 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006263 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6264
Bram Moolenaar8110a092016-04-14 15:56:09 +02006265 set_partial(func, pt, TRUE);
6266 tv->vval.v_partial = pt;
6267 tv->v_type = VAR_PARTIAL;
6268 }
6269 else
6270 {
6271 if (set_string_copy(func->name, tv) == -1)
6272 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006273
Bram Moolenaar8110a092016-04-14 15:56:09 +02006274 tv->v_type = VAR_FUNC;
6275 }
6276 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006277 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006278 else if (PyBytes_Check(obj))
6279 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006280 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006282 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006283 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006284 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 return -1;
6286
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006287 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006288 return -1;
6289
6290 tv->v_type = VAR_STRING;
6291 }
6292 else if (PyUnicode_Check(obj))
6293 {
6294 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006295 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006296
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006297 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006298 if (bytes == NULL)
6299 return -1;
6300
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006301 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006302 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006303 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 return -1;
6305
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006306 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006307 {
6308 Py_XDECREF(bytes);
6309 return -1;
6310 }
6311 Py_XDECREF(bytes);
6312
6313 tv->v_type = VAR_STRING;
6314 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006315#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006316 else if (PyInt_Check(obj))
6317 {
6318 tv->v_type = VAR_NUMBER;
6319 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006320 if (PyErr_Occurred())
6321 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006322 }
6323#endif
6324 else if (PyLong_Check(obj))
6325 {
6326 tv->v_type = VAR_NUMBER;
6327 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006328 if (PyErr_Occurred())
6329 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006330 }
6331 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006332 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006333#ifdef FEAT_FLOAT
6334 else if (PyFloat_Check(obj))
6335 {
6336 tv->v_type = VAR_FLOAT;
6337 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6338 }
6339#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006340 else if (PyObject_HasAttrString(obj, "keys"))
6341 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006342 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006343 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006344 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006345 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006346 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006347 else if (PyNumber_Check(obj))
6348 {
6349 PyObject *num;
6350
6351 if (!(num = PyNumber_Long(obj)))
6352 return -1;
6353
6354 tv->v_type = VAR_NUMBER;
6355 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6356
6357 Py_DECREF(num);
6358 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006359 else if (obj == Py_None)
6360 {
6361 tv->v_type = VAR_SPECIAL;
6362 tv->vval.v_number = VVAL_NONE;
6363 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006364 else
6365 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006366 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006367 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006368 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006369 return -1;
6370 }
6371 return 0;
6372}
6373
6374 static PyObject *
6375ConvertToPyObject(typval_T *tv)
6376{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006377 typval_T *argv;
6378 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006379 if (tv == NULL)
6380 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006381 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006382 return NULL;
6383 }
6384 switch (tv->v_type)
6385 {
6386 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006387 return PyBytes_FromString(tv->vval.v_string == NULL
6388 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006389 case VAR_NUMBER:
6390 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006391 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006392#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006393 return PyFloat_FromDouble((double) tv->vval.v_float);
6394#endif
6395 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006396 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006397 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006398 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006399 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006400 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006401 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006402 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006403 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006404 if (tv->vval.v_partial->pt_argc)
6405 {
6406 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6407 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6408 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6409 }
6410 else
6411 argv = NULL;
6412 if (tv->vval.v_partial->pt_dict != NULL)
6413 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006414 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006415 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006416 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006417 tv->vval.v_partial->pt_dict,
6418 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006419 case VAR_BLOB:
6420 return PyBytes_FromStringAndSize(
6421 (char*) tv->vval.v_blob->bv_ga.ga_data,
6422 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006423 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006424 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006426 case VAR_CHANNEL:
6427 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006428 case VAR_INSTR:
Bram Moolenaardb913952012-06-29 12:54:53 +02006429 Py_INCREF(Py_None);
6430 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006431 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006432 case VAR_SPECIAL:
6433 switch (tv->vval.v_number)
6434 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006435 case VVAL_FALSE: return ALWAYS_FALSE;
6436 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006437 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006438 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006439 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006440 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006441 return NULL;
6442 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006443 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006444}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006445
6446typedef struct
6447{
6448 PyObject_HEAD
6449} CurrentObject;
6450static PyTypeObject CurrentType;
6451
6452 static void
6453init_structs(void)
6454{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006455 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006456 OutputType.tp_name = "vim.message";
6457 OutputType.tp_basicsize = sizeof(OutputObject);
6458 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6459 OutputType.tp_doc = "vim message object";
6460 OutputType.tp_methods = OutputMethods;
6461#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006462 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6463 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006464 OutputType.tp_alloc = call_PyType_GenericAlloc;
6465 OutputType.tp_new = call_PyType_GenericNew;
6466 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006467 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006468#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006469 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6470 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006471 // Disabled, because this causes a crash in test86
6472 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006473#endif
6474
Bram Moolenaara80faa82020-04-12 19:37:17 +02006475 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006476 IterType.tp_name = "vim.iter";
6477 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006478 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006479 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006480 IterType.tp_iter = (getiterfunc)IterIter;
6481 IterType.tp_iternext = (iternextfunc)IterNext;
6482 IterType.tp_dealloc = (destructor)IterDestructor;
6483 IterType.tp_traverse = (traverseproc)IterTraverse;
6484 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006485
Bram Moolenaara80faa82020-04-12 19:37:17 +02006486 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006487 BufferType.tp_name = "vim.buffer";
6488 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 BufferType.tp_dealloc = (destructor)BufferDestructor;
6490 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491 BufferType.tp_as_sequence = &BufferAsSeq;
6492 BufferType.tp_as_mapping = &BufferAsMapping;
6493 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6494 BufferType.tp_doc = "vim buffer object";
6495 BufferType.tp_methods = BufferMethods;
6496#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006497 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006498 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006499 BufferType.tp_alloc = call_PyType_GenericAlloc;
6500 BufferType.tp_new = call_PyType_GenericNew;
6501 BufferType.tp_free = call_PyObject_Free;
6502#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006503 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006504 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006505#endif
6506
Bram Moolenaara80faa82020-04-12 19:37:17 +02006507 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006508 WindowType.tp_name = "vim.window";
6509 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006510 WindowType.tp_dealloc = (destructor)WindowDestructor;
6511 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006512 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006513 WindowType.tp_doc = "vim Window object";
6514 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6516 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006517#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006518 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6519 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006520 WindowType.tp_alloc = call_PyType_GenericAlloc;
6521 WindowType.tp_new = call_PyType_GenericNew;
6522 WindowType.tp_free = call_PyObject_Free;
6523#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006524 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6525 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006526#endif
6527
Bram Moolenaara80faa82020-04-12 19:37:17 +02006528 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006529 TabPageType.tp_name = "vim.tabpage";
6530 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006531 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6532 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006533 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6534 TabPageType.tp_doc = "vim tab page object";
6535 TabPageType.tp_methods = TabPageMethods;
6536#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006537 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006538 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6539 TabPageType.tp_new = call_PyType_GenericNew;
6540 TabPageType.tp_free = call_PyObject_Free;
6541#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006542 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006543#endif
6544
Bram Moolenaara80faa82020-04-12 19:37:17 +02006545 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006546 BufMapType.tp_name = "vim.bufferlist";
6547 BufMapType.tp_basicsize = sizeof(BufMapObject);
6548 BufMapType.tp_as_mapping = &BufMapAsMapping;
6549 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006550 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551 BufferType.tp_doc = "vim buffer list";
6552
Bram Moolenaara80faa82020-04-12 19:37:17 +02006553 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006554 WinListType.tp_name = "vim.windowlist";
6555 WinListType.tp_basicsize = sizeof(WinListType);
6556 WinListType.tp_as_sequence = &WinListAsSeq;
6557 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6558 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006559 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006560
Bram Moolenaara80faa82020-04-12 19:37:17 +02006561 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006562 TabListType.tp_name = "vim.tabpagelist";
6563 TabListType.tp_basicsize = sizeof(TabListType);
6564 TabListType.tp_as_sequence = &TabListAsSeq;
6565 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6566 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006567
Bram Moolenaara80faa82020-04-12 19:37:17 +02006568 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006569 RangeType.tp_name = "vim.range";
6570 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006571 RangeType.tp_dealloc = (destructor)RangeDestructor;
6572 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006573 RangeType.tp_as_sequence = &RangeAsSeq;
6574 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006575 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006576 RangeType.tp_doc = "vim Range object";
6577 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006578 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6579 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006580#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006581 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582 RangeType.tp_alloc = call_PyType_GenericAlloc;
6583 RangeType.tp_new = call_PyType_GenericNew;
6584 RangeType.tp_free = call_PyObject_Free;
6585#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006586 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006587#endif
6588
Bram Moolenaara80faa82020-04-12 19:37:17 +02006589 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006590 CurrentType.tp_name = "vim.currentdata";
6591 CurrentType.tp_basicsize = sizeof(CurrentObject);
6592 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6593 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006594 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006595#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006596 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6597 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006598#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006599 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6600 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006601#endif
6602
Bram Moolenaara80faa82020-04-12 19:37:17 +02006603 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006604 DictionaryType.tp_name = "vim.dictionary";
6605 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006606 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006607 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006609 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006610 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006611 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006612 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6613 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6614 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006615#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006616 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6617 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006618#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006619 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6620 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006621#endif
6622
Bram Moolenaara80faa82020-04-12 19:37:17 +02006623 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006624 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006625 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006626 ListType.tp_basicsize = sizeof(ListObject);
6627 ListType.tp_as_sequence = &ListAsSeq;
6628 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006629 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006630 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006631 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006632 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006633 ListType.tp_new = (newfunc)ListConstructor;
6634 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006635#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006636 ListType.tp_getattro = (getattrofunc)ListGetattro;
6637 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006638#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006639 ListType.tp_getattr = (getattrfunc)ListGetattr;
6640 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006641#endif
6642
Bram Moolenaara80faa82020-04-12 19:37:17 +02006643 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006644 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006645 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006646 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6647 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006648 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006649 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006650 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006651 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006652 FunctionType.tp_new = (newfunc)FunctionConstructor;
6653 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006654#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006655 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006656#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006657 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006658#endif
6659
Bram Moolenaara80faa82020-04-12 19:37:17 +02006660 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006661 OptionsType.tp_name = "vim.options";
6662 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006663 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006664 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006665 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006666 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006667 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006668 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6669 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6670 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006671
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006672#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006673 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006674 LoaderType.tp_name = "vim.Loader";
6675 LoaderType.tp_basicsize = sizeof(LoaderObject);
6676 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6677 LoaderType.tp_doc = "vim message object";
6678 LoaderType.tp_methods = LoaderMethods;
6679 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006680#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006681
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006682#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006683 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006684 vimmodule.m_name = "vim";
6685 vimmodule.m_doc = "Vim Python interface\n";
6686 vimmodule.m_size = -1;
6687 vimmodule.m_methods = VimMethods;
6688#endif
6689}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006690
6691#define PYTYPE_READY(type) \
6692 if (PyType_Ready(&type)) \
6693 return -1;
6694
6695 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006696init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006697{
6698 PYTYPE_READY(IterType);
6699 PYTYPE_READY(BufferType);
6700 PYTYPE_READY(RangeType);
6701 PYTYPE_READY(WindowType);
6702 PYTYPE_READY(TabPageType);
6703 PYTYPE_READY(BufMapType);
6704 PYTYPE_READY(WinListType);
6705 PYTYPE_READY(TabListType);
6706 PYTYPE_READY(CurrentType);
6707 PYTYPE_READY(DictionaryType);
6708 PYTYPE_READY(ListType);
6709 PYTYPE_READY(FunctionType);
6710 PYTYPE_READY(OptionsType);
6711 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006712#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006713 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006714#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006715 return 0;
6716}
6717
6718 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006719init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006720{
6721 PyObject *path;
6722 PyObject *path_hook;
6723 PyObject *path_hooks;
6724
6725 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6726 return -1;
6727
6728 if (!(path_hooks = PySys_GetObject("path_hooks")))
6729 {
6730 PyErr_Clear();
6731 path_hooks = PyList_New(1);
6732 PyList_SET_ITEM(path_hooks, 0, path_hook);
6733 if (PySys_SetObject("path_hooks", path_hooks))
6734 {
6735 Py_DECREF(path_hooks);
6736 return -1;
6737 }
6738 Py_DECREF(path_hooks);
6739 }
6740 else if (PyList_Check(path_hooks))
6741 {
6742 if (PyList_Append(path_hooks, path_hook))
6743 {
6744 Py_DECREF(path_hook);
6745 return -1;
6746 }
6747 Py_DECREF(path_hook);
6748 }
6749 else
6750 {
6751 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006752 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006753 "You should now do the following:\n"
6754 "- append vim.path_hook to sys.path_hooks\n"
6755 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006756 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006757 Py_DECREF(path_hook);
6758 return 0;
6759 }
6760
6761 if (!(path = PySys_GetObject("path")))
6762 {
6763 PyErr_Clear();
6764 path = PyList_New(1);
6765 Py_INCREF(vim_special_path_object);
6766 PyList_SET_ITEM(path, 0, vim_special_path_object);
6767 if (PySys_SetObject("path", path))
6768 {
6769 Py_DECREF(path);
6770 return -1;
6771 }
6772 Py_DECREF(path);
6773 }
6774 else if (PyList_Check(path))
6775 {
6776 if (PyList_Append(path, vim_special_path_object))
6777 return -1;
6778 }
6779 else
6780 {
6781 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006782 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006783 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006784 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006785 }
6786
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006787 return 0;
6788}
6789
6790static BufMapObject TheBufferMap =
6791{
6792 PyObject_HEAD_INIT(&BufMapType)
6793};
6794
6795static WinListObject TheWindowList =
6796{
6797 PyObject_HEAD_INIT(&WinListType)
6798 NULL
6799};
6800
6801static CurrentObject TheCurrent =
6802{
6803 PyObject_HEAD_INIT(&CurrentType)
6804};
6805
6806static TabListObject TheTabPageList =
6807{
6808 PyObject_HEAD_INIT(&TabListType)
6809};
6810
6811static struct numeric_constant {
6812 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006813 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006814} numeric_constants[] = {
6815 {"VAR_LOCKED", VAR_LOCKED},
6816 {"VAR_FIXED", VAR_FIXED},
6817 {"VAR_SCOPE", VAR_SCOPE},
6818 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6819};
6820
6821static struct object_constant {
6822 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006823 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006824} object_constants[] = {
6825 {"buffers", (PyObject *)(void *)&TheBufferMap},
6826 {"windows", (PyObject *)(void *)&TheWindowList},
6827 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6828 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006829
6830 {"Buffer", (PyObject *)&BufferType},
6831 {"Range", (PyObject *)&RangeType},
6832 {"Window", (PyObject *)&WindowType},
6833 {"TabPage", (PyObject *)&TabPageType},
6834 {"Dictionary", (PyObject *)&DictionaryType},
6835 {"List", (PyObject *)&ListType},
6836 {"Function", (PyObject *)&FunctionType},
6837 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006838#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006839 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006840#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006841};
6842
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006843#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006844 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006845 return -1;
6846
6847#define ADD_CHECKED_OBJECT(m, name, obj) \
6848 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006849 PyObject *valObject = obj; \
6850 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006851 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006852 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006853 }
6854
6855 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006856populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006857{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006858 int i;
6859 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006860 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006861 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006862#if PY_VERSION_HEX >= 0x030700f0
6863 PyObject *dict;
6864 PyObject *cls;
6865#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006866
6867 for (i = 0; i < (int)(sizeof(numeric_constants)
6868 / sizeof(struct numeric_constant));
6869 ++i)
6870 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006871 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006872
6873 for (i = 0; i < (int)(sizeof(object_constants)
6874 / sizeof(struct object_constant));
6875 ++i)
6876 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006877 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006878
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006879 valObject = object_constants[i].valObject;
6880 Py_INCREF(valObject);
6881 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006882 }
6883
6884 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6885 return -1;
6886 ADD_OBJECT(m, "error", VimError);
6887
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006888 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6889 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006890 ADD_CHECKED_OBJECT(m, "options",
6891 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006892
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006893 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006895 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896
Bram Moolenaar22081f42016-06-01 20:38:34 +02006897#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006898 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006899 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006900#else
6901 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6902 return -1;
6903#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006904 ADD_OBJECT(m, "_getcwd", py_getcwd)
6905
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006906 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006907 return -1;
6908 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006909 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006910 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006911 if (PyObject_SetAttrString(other_module, "chdir", attr))
6912 {
6913 Py_DECREF(attr);
6914 return -1;
6915 }
6916 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006917
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006918 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006919 {
6920 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006921 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006922 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006923 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6924 {
6925 Py_DECREF(attr);
6926 return -1;
6927 }
6928 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006929 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006930 else
6931 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006932
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006933 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6934 return -1;
6935
6936 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6937
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006938#if PY_VERSION_HEX >= 0x030700f0
6939 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6940 return -1;
6941
6942 dict = PyModule_GetDict(imp);
6943
6944 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6945 {
6946 Py_DECREF(imp);
6947 return -1;
6948 }
6949
6950 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6951 {
6952 Py_DECREF(imp);
6953 return -1;
6954 }
6955
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006956 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6957 {
6958 // find_module() is deprecated, this may stop working in some later
6959 // version.
6960 ADD_OBJECT(m, "_find_module", py_find_module);
6961 }
6962
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006963 Py_DECREF(imp);
6964
6965 ADD_OBJECT(m, "_find_spec", py_find_spec);
6966#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006967 if (!(imp = PyImport_ImportModule("imp")))
6968 return -1;
6969
6970 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6971 {
6972 Py_DECREF(imp);
6973 return -1;
6974 }
6975
6976 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6977 {
6978 Py_DECREF(py_find_module);
6979 Py_DECREF(imp);
6980 return -1;
6981 }
6982
6983 Py_DECREF(imp);
6984
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006985 ADD_OBJECT(m, "_find_module", py_find_module);
6986 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006987#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006988
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006989 return 0;
6990}