blob: 2903b0ba9a1f42c3e8a5572d66acf0b9f8bb297b [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();
2046 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2047 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);
2818 PyMem_Free(lii);
2819}
2820
2821 static PyObject *
2822ListIterNext(listiterinfo_T **lii)
2823{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002824 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002825
2826 if (!((*lii)->lw.lw_item))
2827 return NULL;
2828
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002829 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002830 return NULL;
2831
2832 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2833
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002834 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002835}
2836
2837 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002838ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002839{
2840 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002841 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002842
2843 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2844 {
2845 PyErr_NoMemory();
2846 return NULL;
2847 }
2848
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002849 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002850 list_add_watch(l, &lii->lw);
2851 lii->lw.lw_item = l->lv_first;
2852 lii->list = l;
2853
2854 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002855 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002856 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002857}
2858
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002859static char *ListAttrs[] = {
2860 "locked",
2861 NULL
2862};
2863
2864 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002865ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002866{
2867 return ObjectDir(self, ListAttrs);
2868}
2869
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002872{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002873 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002874 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002875 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002876 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002877 return -1;
2878 }
2879
2880 if (strcmp(name, "locked") == 0)
2881 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002882 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002883 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002884 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002885 return -1;
2886 }
2887 else
2888 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002889 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002890 if (istrue == -1)
2891 return -1;
2892 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002893 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002894 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002896 }
2897 return 0;
2898 }
2899 else
2900 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002901 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002902 return -1;
2903 }
2904}
2905
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002906static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002907 (lenfunc) ListLength, // sq_length, len(x)
2908 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2909 0, // RangeRepeat, sq_repeat, x*n
2910 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2911 0, // was_sq_slice, x[i:j]
2912 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2913 0, // was_sq_ass_slice, x[i:j]=v
2914 0, // sq_contains
2915 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2916 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002917};
2918
2919static PyMappingMethods ListAsMapping = {
2920 /* mp_length */ (lenfunc) ListLength,
2921 /* mp_subscript */ (binaryfunc) ListItem,
2922 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2923};
2924
Bram Moolenaardb913952012-06-29 12:54:53 +02002925static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002926 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2927 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2928 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002929};
2930
2931typedef struct
2932{
2933 PyObject_HEAD
2934 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002935 int argc;
2936 typval_T *argv;
2937 dict_T *self;
2938 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002939 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002940} FunctionObject;
2941
2942static PyTypeObject FunctionType;
2943
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002944#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2945 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002946
Bram Moolenaardb913952012-06-29 12:54:53 +02002947 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002948FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002949 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002950{
2951 FunctionObject *self;
2952
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002953 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002954 if (self == NULL)
2955 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002956
2957 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002958 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002959 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002960 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002961 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002962 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002963 return NULL;
2964 }
2965 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002966 }
2967 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002968 {
2969 char_u *p;
2970
2971 if ((p = get_expanded_name(name,
2972 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002973 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002974 PyErr_FORMAT(PyExc_ValueError,
2975 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002976 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002977 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002978
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002979 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2980 {
2981 char_u *np;
2982 size_t len = STRLEN(p) + 1;
2983
Bram Moolenaar51e14382019-05-25 20:21:28 +02002984 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002985 {
2986 vim_free(p);
2987 return NULL;
2988 }
2989 mch_memmove(np, "<SNR>", 5);
2990 mch_memmove(np + 5, p + 3, len - 3);
2991 vim_free(p);
2992 self->name = np;
2993 }
2994 else
2995 self->name = p;
2996 }
2997
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002998 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002999 self->argc = argc;
3000 self->argv = argv;
3001 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003002 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003003
3004 if (self->argv || self->self)
3005 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3006
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003007 return (PyObject *)(self);
3008}
3009
3010 static PyObject *
3011FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3012{
3013 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003014 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003015 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003016 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003017 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003018 typval_T selfdicttv;
3019 typval_T argstv;
3020 list_T *argslist = NULL;
3021 dict_T *selfdict = NULL;
3022 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003023 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003024 typval_T *argv = NULL;
3025 typval_T *curtv;
3026 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003027
Bram Moolenaar8110a092016-04-14 15:56:09 +02003028 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003029 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003030 selfdictObject = PyDict_GetItemString(kwargs, "self");
3031 if (selfdictObject != NULL)
3032 {
3033 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3034 return NULL;
3035 selfdict = selfdicttv.vval.v_dict;
3036 }
3037 argsObject = PyDict_GetItemString(kwargs, "args");
3038 if (argsObject != NULL)
3039 {
3040 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3041 {
3042 dict_unref(selfdict);
3043 return NULL;
3044 }
3045 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003046 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003047
3048 argc = argslist->lv_len;
3049 if (argc != 0)
3050 {
3051 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003052 if (argv == NULL)
3053 {
3054 PyErr_NoMemory();
3055 dict_unref(selfdict);
3056 list_unref(argslist);
3057 return NULL;
3058 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003059 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003060 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003061 copy_tv(&li->li_tv, curtv++);
3062 }
3063 list_unref(argslist);
3064 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003065 if (selfdict != NULL)
3066 {
3067 auto_rebind = FALSE;
3068 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3069 if (autoRebindObject != NULL)
3070 {
3071 auto_rebind = PyObject_IsTrue(autoRebindObject);
3072 if (auto_rebind == -1)
3073 {
3074 dict_unref(selfdict);
3075 list_unref(argslist);
3076 return NULL;
3077 }
3078 }
3079 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003080 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003081
Bram Moolenaar389a1792013-06-23 13:00:44 +02003082 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003083 {
3084 dict_unref(selfdict);
3085 while (argc--)
3086 clear_tv(&argv[argc]);
3087 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003088 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003089 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003090
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003091 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003092
Bram Moolenaar389a1792013-06-23 13:00:44 +02003093 PyMem_Free(name);
3094
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003095 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003096}
3097
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003098 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003099FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003100{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003101 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003102 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003103 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003104 for (i = 0; i < self->argc; ++i)
3105 clear_tv(&self->argv[i]);
3106 PyMem_Free(self->argv);
3107 dict_unref(self->self);
3108 if (self->argv || self->self)
3109 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003110
3111 DESTRUCTOR_FINISH(self);
3112}
3113
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003114static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003115 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003116 NULL
3117};
3118
3119 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003120FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003121{
3122 return ObjectDir(self, FunctionAttrs);
3123}
3124
Bram Moolenaardb913952012-06-29 12:54:53 +02003125 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003126FunctionAttr(FunctionObject *self, char *name)
3127{
3128 list_T *list;
3129 int i;
3130 if (strcmp(name, "name") == 0)
3131 return PyString_FromString((char *)(self->name));
3132 else if (strcmp(name, "args") == 0)
3133 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003134 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003135 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003136
Bram Moolenaar8110a092016-04-14 15:56:09 +02003137 for (i = 0; i < self->argc; ++i)
3138 list_append_tv(list, &self->argv[i]);
3139 return NEW_LIST(list);
3140 }
3141 else if (strcmp(name, "self") == 0)
3142 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003143 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003144 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003145 else if (strcmp(name, "auto_rebind") == 0)
3146 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003147 ? ALWAYS_TRUE
3148 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003149 else if (strcmp(name, "__members__") == 0)
3150 return ObjectDir(NULL, FunctionAttrs);
3151 return NULL;
3152}
3153
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003154/*
3155 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003156 *
3157 * "exported" should be set to true when it is needed to construct a partial
3158 * that may be stored in a variable (i.e. may be freed by Vim).
3159 */
3160 static void
3161set_partial(FunctionObject *self, partial_T *pt, int exported)
3162{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003163 int i;
3164
3165 pt->pt_name = self->name;
3166 if (self->argv)
3167 {
3168 pt->pt_argc = self->argc;
3169 if (exported)
3170 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003171 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003172 for (i = 0; i < pt->pt_argc; ++i)
3173 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3174 }
3175 else
3176 pt->pt_argv = self->argv;
3177 }
3178 else
3179 {
3180 pt->pt_argc = 0;
3181 pt->pt_argv = NULL;
3182 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003183 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003184 pt->pt_dict = self->self;
3185 if (exported && self->self)
3186 ++pt->pt_dict->dv_refcount;
3187 if (exported)
3188 pt->pt_name = vim_strsave(pt->pt_name);
3189 pt->pt_refcount = 1;
3190}
3191
3192 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003193FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003194{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003195 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003196 typval_T args;
3197 typval_T selfdicttv;
3198 typval_T rettv;
3199 dict_T *selfdict = NULL;
3200 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003201 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003202 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003203 partial_T pt;
3204 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003205
Bram Moolenaar8110a092016-04-14 15:56:09 +02003206 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003207 return NULL;
3208
3209 if (kwargs != NULL)
3210 {
3211 selfdictObject = PyDict_GetItemString(kwargs, "self");
3212 if (selfdictObject != NULL)
3213 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003214 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003215 {
3216 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003217 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003218 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003219 selfdict = selfdicttv.vval.v_dict;
3220 }
3221 }
3222
Bram Moolenaar8110a092016-04-14 15:56:09 +02003223 if (self->argv || self->self)
3224 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003225 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003226 set_partial(self, &pt, FALSE);
3227 pt_ptr = &pt;
3228 }
3229
Bram Moolenaar71700b82013-05-15 17:49:05 +02003230 Py_BEGIN_ALLOW_THREADS
3231 Python_Lock_Vim();
3232
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003233 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003234 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003235
3236 Python_Release_Vim();
3237 Py_END_ALLOW_THREADS
3238
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003239 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003240 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003241 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003242 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003244 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003245 }
3246 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003247 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003248
Bram Moolenaardb913952012-06-29 12:54:53 +02003249 clear_tv(&args);
3250 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003251 if (selfdict != NULL)
3252 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003253
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003254 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003255}
3256
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003257 static PyObject *
3258FunctionRepr(FunctionObject *self)
3259{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003260 PyObject *ret;
3261 garray_T repr_ga;
3262 int i;
3263 char_u *tofree = NULL;
3264 typval_T tv;
3265 char_u numbuf[NUMBUFLEN];
3266
3267 ga_init2(&repr_ga, (int)sizeof(char), 70);
3268 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3269 if (self->name)
3270 ga_concat(&repr_ga, self->name);
3271 else
3272 ga_concat(&repr_ga, (char_u *)"<NULL>");
3273 ga_append(&repr_ga, '\'');
3274 if (self->argv)
3275 {
3276 ga_concat(&repr_ga, (char_u *)", args=[");
3277 ++emsg_silent;
3278 for (i = 0; i < self->argc; i++)
3279 {
3280 if (i != 0)
3281 ga_concat(&repr_ga, (char_u *)", ");
3282 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3283 get_copyID()));
3284 vim_free(tofree);
3285 }
3286 --emsg_silent;
3287 ga_append(&repr_ga, ']');
3288 }
3289 if (self->self)
3290 {
3291 ga_concat(&repr_ga, (char_u *)", self=");
3292 tv.v_type = VAR_DICT;
3293 tv.vval.v_dict = self->self;
3294 ++emsg_silent;
3295 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3296 --emsg_silent;
3297 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003298 if (self->auto_rebind)
3299 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003300 }
3301 ga_append(&repr_ga, '>');
3302 ret = PyString_FromString((char *)repr_ga.ga_data);
3303 ga_clear(&repr_ga);
3304 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003305}
3306
Bram Moolenaardb913952012-06-29 12:54:53 +02003307static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003308 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3309 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003310};
3311
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003312/*
3313 * Options object
3314 */
3315
3316static PyTypeObject OptionsType;
3317
3318typedef int (*checkfun)(void *);
3319
3320typedef struct
3321{
3322 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003323 int opt_type;
3324 void *from;
3325 checkfun Check;
3326 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003327} OptionsObject;
3328
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003329 static int
3330dummy_check(void *arg UNUSED)
3331{
3332 return 0;
3333}
3334
3335 static PyObject *
3336OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3337{
3338 OptionsObject *self;
3339
Bram Moolenaar774267b2013-05-21 20:51:59 +02003340 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341 if (self == NULL)
3342 return NULL;
3343
3344 self->opt_type = opt_type;
3345 self->from = from;
3346 self->Check = Check;
3347 self->fromObj = fromObj;
3348 if (fromObj)
3349 Py_INCREF(fromObj);
3350
3351 return (PyObject *)(self);
3352}
3353
3354 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003355OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003356{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003357 PyObject_GC_UnTrack((void *)(self));
3358 Py_XDECREF(self->fromObj);
3359 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003360}
3361
3362 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003364{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003365 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003366 return 0;
3367}
3368
3369 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003371{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003373 return 0;
3374}
3375
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003376 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003377OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003378{
3379 char_u *key;
3380 int flags;
3381 long numval;
3382 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003383 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003384
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003385 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386 return NULL;
3387
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003388 if (!(key = StringToChars(keyObject, &todecref)))
3389 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003390
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003391 if (*key == NUL)
3392 {
3393 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003394 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003395 return NULL;
3396 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003397
3398 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003399 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003400
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003401 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402
3403 if (flags == 0)
3404 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003405 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003406 return NULL;
3407 }
3408
3409 if (flags & SOPT_UNSET)
3410 {
3411 Py_INCREF(Py_None);
3412 return Py_None;
3413 }
3414 else if (flags & SOPT_BOOL)
3415 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003416 PyObject *ret;
3417 ret = numval ? Py_True : Py_False;
3418 Py_INCREF(ret);
3419 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003420 }
3421 else if (flags & SOPT_NUM)
3422 return PyInt_FromLong(numval);
3423 else if (flags & SOPT_STRING)
3424 {
3425 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003426 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003427 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003428 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003429 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003430 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003431 else
3432 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003433 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003434 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003435 return NULL;
3436 }
3437 }
3438 else
3439 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003440 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003441 return NULL;
3442 }
3443}
3444
3445 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003446OptionsContains(OptionsObject *self, PyObject *keyObject)
3447{
3448 char_u *key;
3449 PyObject *todecref;
3450
3451 if (!(key = StringToChars(keyObject, &todecref)))
3452 return -1;
3453
3454 if (*key == NUL)
3455 {
3456 Py_XDECREF(todecref);
3457 return 0;
3458 }
3459
3460 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3461 {
3462 Py_XDECREF(todecref);
3463 return 1;
3464 }
3465 else
3466 {
3467 Py_XDECREF(todecref);
3468 return 0;
3469 }
3470}
3471
3472typedef struct
3473{
3474 void *lastoption;
3475 int opt_type;
3476} optiterinfo_T;
3477
3478 static PyObject *
3479OptionsIterNext(optiterinfo_T **oii)
3480{
3481 char_u *name;
3482
3483 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3484 return PyString_FromString((char *)name);
3485
3486 return NULL;
3487}
3488
3489 static PyObject *
3490OptionsIter(OptionsObject *self)
3491{
3492 optiterinfo_T *oii;
3493
3494 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3495 {
3496 PyErr_NoMemory();
3497 return NULL;
3498 }
3499
3500 oii->opt_type = self->opt_type;
3501 oii->lastoption = NULL;
3502
3503 return IterNew(oii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003504 (destructorfun)(void *) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003505 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003506}
3507
3508 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003509set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003510{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003511 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003512
3513 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3514 {
3515 if (VimTryEnd())
3516 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003517 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003518 return FAIL;
3519 }
3520 return OK;
3521}
3522
3523 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003524set_option_value_for(
3525 char_u *key,
3526 int numval,
3527 char_u *stringval,
3528 int opt_flags,
3529 int opt_type,
3530 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003531{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003532 win_T *save_curwin = NULL;
3533 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003534 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003535 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003536
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003537 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003538 switch (opt_type)
3539 {
3540 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003541 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003542 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003543 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003544 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003545 if (VimTryEnd())
3546 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003547 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003548 return -1;
3549 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003550 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3551 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003552 break;
3553 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003554 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003555 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003556 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003557 break;
3558 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003559 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003560 break;
3561 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003562 if (set_ret == FAIL)
3563 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003564 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003565}
3566
3567 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003568OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003569{
3570 char_u *key;
3571 int flags;
3572 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003573 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003574 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003575
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003576 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577 return -1;
3578
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003579 if (!(key = StringToChars(keyObject, &todecref)))
3580 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003581
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003582 if (*key == NUL)
3583 {
3584 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003585 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003586 return -1;
3587 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003588
3589 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003590 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003591
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003592 if (flags == 0)
3593 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003594 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003595 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003596 return -1;
3597 }
3598
3599 if (valObject == NULL)
3600 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003601 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003602 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003603 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003604 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003605 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003606 return -1;
3607 }
3608 else if (!(flags & SOPT_GLOBAL))
3609 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003610 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003611 N_("unable to unset option %s "
3612 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003613 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614 return -1;
3615 }
3616 else
3617 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003619 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003620 return 0;
3621 }
3622 }
3623
Bram Moolenaard6e39182013-05-21 18:30:34 +02003624 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003625
3626 if (flags & SOPT_BOOL)
3627 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003628 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003629
Bram Moolenaarb983f752013-05-15 16:11:50 +02003630 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003632 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003633 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003634 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003635 }
3636 else if (flags & SOPT_NUM)
3637 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003638 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003639
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003640 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003641 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003642 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003643 return -1;
3644 }
3645
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003646 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003647 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003648 }
3649 else
3650 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003651 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003652 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003653
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003654 if ((val = StringToChars(valObject, &todecref2)))
3655 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003656 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003657 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003658 Py_XDECREF(todecref2);
3659 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003660 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003661 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003662 }
3663
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003664 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003665
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003666 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003667}
3668
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003669static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003670 0, // sq_length
3671 0, // sq_concat
3672 0, // sq_repeat
3673 0, // sq_item
3674 0, // sq_slice
3675 0, // sq_ass_item
3676 0, // sq_ass_slice
3677 (objobjproc) OptionsContains, // sq_contains
3678 0, // sq_inplace_concat
3679 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003680};
3681
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003682static PyMappingMethods OptionsAsMapping = {
3683 (lenfunc) NULL,
3684 (binaryfunc) OptionsItem,
3685 (objobjargproc) OptionsAssItem,
3686};
3687
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003688// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003689
3690typedef struct
3691{
3692 PyObject_HEAD
3693 tabpage_T *tab;
3694} TabPageObject;
3695
3696static PyObject *WinListNew(TabPageObject *tabObject);
3697
3698static PyTypeObject TabPageType;
3699
3700 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003701CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003702{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003703 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003704 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003705 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003706 return -1;
3707 }
3708
3709 return 0;
3710}
3711
3712 static PyObject *
3713TabPageNew(tabpage_T *tab)
3714{
3715 TabPageObject *self;
3716
3717 if (TAB_PYTHON_REF(tab))
3718 {
3719 self = TAB_PYTHON_REF(tab);
3720 Py_INCREF(self);
3721 }
3722 else
3723 {
3724 self = PyObject_NEW(TabPageObject, &TabPageType);
3725 if (self == NULL)
3726 return NULL;
3727 self->tab = tab;
3728 TAB_PYTHON_REF(tab) = self;
3729 }
3730
3731 return (PyObject *)(self);
3732}
3733
3734 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003735TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003736{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003737 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3738 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003739
3740 DESTRUCTOR_FINISH(self);
3741}
3742
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003743static char *TabPageAttrs[] = {
3744 "windows", "number", "vars", "window", "valid",
3745 NULL
3746};
3747
3748 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003749TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003750{
3751 return ObjectDir(self, TabPageAttrs);
3752}
3753
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003754 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003755TabPageAttrValid(TabPageObject *self, char *name)
3756{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003757 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003758
3759 if (strcmp(name, "valid") != 0)
3760 return NULL;
3761
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003762 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3763 Py_INCREF(ret);
3764 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003765}
3766
3767 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003768TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003769{
3770 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003771 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003772 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003773 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003775 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003776 else if (strcmp(name, "window") == 0)
3777 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003778 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003779 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003780 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003781 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003782 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003784 else if (strcmp(name, "__members__") == 0)
3785 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003786 return NULL;
3787}
3788
3789 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003790TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003791{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003792 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003793 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003794 else
3795 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003796 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003797
3798 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003799 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3800 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003801 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003802 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003803 }
3804}
3805
3806static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003807 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003808 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3809 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003810};
3811
3812/*
3813 * Window list object
3814 */
3815
3816static PyTypeObject TabListType;
3817static PySequenceMethods TabListAsSeq;
3818
3819typedef struct
3820{
3821 PyObject_HEAD
3822} TabListObject;
3823
3824 static PyInt
3825TabListLength(PyObject *self UNUSED)
3826{
3827 tabpage_T *tp = first_tabpage;
3828 PyInt n = 0;
3829
3830 while (tp != NULL)
3831 {
3832 ++n;
3833 tp = tp->tp_next;
3834 }
3835
3836 return n;
3837}
3838
3839 static PyObject *
3840TabListItem(PyObject *self UNUSED, PyInt n)
3841{
3842 tabpage_T *tp;
3843
3844 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3845 if (n == 0)
3846 return TabPageNew(tp);
3847
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003848 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003849 return NULL;
3850}
3851
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003852/*
3853 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003854 */
3855
3856typedef struct
3857{
3858 PyObject_HEAD
3859 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003860 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003861} WindowObject;
3862
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003863static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003864
3865 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003866CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003867{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003868 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003869 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003870 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003871 return -1;
3872 }
3873
3874 return 0;
3875}
3876
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003877 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003878WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003879{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003880 /*
3881 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003882 * If we add a "w_python*_ref" field to the win_T structure,
3883 * then we can get at it in win_free() in vim. We then
3884 * need to create only ONE Python object per window - if
3885 * we try to create a second, just INCREF the existing one
3886 * and return it. The (single) Python object referring to
3887 * the window is stored in "w_python*_ref".
3888 * On a win_free() we set the Python object's win_T* field
3889 * to an invalid value. We trap all uses of a window
3890 * object, and reject them if the win_T* field is invalid.
3891 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003892 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003893 * w_python_ref and w_python3_ref fields respectively.
3894 */
3895
3896 WindowObject *self;
3897
3898 if (WIN_PYTHON_REF(win))
3899 {
3900 self = WIN_PYTHON_REF(win);
3901 Py_INCREF(self);
3902 }
3903 else
3904 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003905 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003906 if (self == NULL)
3907 return NULL;
3908 self->win = win;
3909 WIN_PYTHON_REF(win) = self;
3910 }
3911
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003912 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3913
Bram Moolenaar971db462013-05-12 18:44:48 +02003914 return (PyObject *)(self);
3915}
3916
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003917 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003918WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003919{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003920 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003921 if (self->win && self->win != INVALID_WINDOW_VALUE)
3922 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003923 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003924 PyObject_GC_Del((void *)(self));
3925}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003926
Bram Moolenaar774267b2013-05-21 20:51:59 +02003927 static int
3928WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3929{
3930 Py_VISIT(((PyObject *)(self->tabObject)));
3931 return 0;
3932}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003933
Bram Moolenaar774267b2013-05-21 20:51:59 +02003934 static int
3935WindowClear(WindowObject *self)
3936{
3937 Py_CLEAR(self->tabObject);
3938 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003939}
3940
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003941 static win_T *
3942get_firstwin(TabPageObject *tabObject)
3943{
3944 if (tabObject)
3945 {
3946 if (CheckTabPage(tabObject))
3947 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003948 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003949 else if (tabObject->tab == curtab)
3950 return firstwin;
3951 else
3952 return tabObject->tab->tp_firstwin;
3953 }
3954 else
3955 return firstwin;
3956}
Bram Moolenaare950f992018-06-10 13:55:55 +02003957
3958// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003959static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003960 "buffer",
3961 "cursor",
3962 "height",
3963 "row",
3964 "width",
3965 "col",
3966 "vars",
3967 "options",
3968 "number",
3969 "tabpage",
3970 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003971 NULL
3972};
3973
3974 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003975WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003976{
3977 return ObjectDir(self, WindowAttrs);
3978}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003979
Bram Moolenaar971db462013-05-12 18:44:48 +02003980 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003981WindowAttrValid(WindowObject *self, char *name)
3982{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003983 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003984
3985 if (strcmp(name, "valid") != 0)
3986 return NULL;
3987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003988 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3989 Py_INCREF(ret);
3990 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003991}
3992
3993 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003994WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003995{
3996 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003997 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003998 else if (strcmp(name, "cursor") == 0)
3999 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004000 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004001
4002 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4003 }
4004 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004005 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004006 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004007 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004008 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004009 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004010 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004011 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004012 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004013 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004014 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4016 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004017 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004018 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004019 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004020 return NULL;
4021 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004022 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004023 }
4024 else if (strcmp(name, "tabpage") == 0)
4025 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004026 Py_INCREF(self->tabObject);
4027 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004028 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004029 else if (strcmp(name, "__members__") == 0)
4030 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004031 else
4032 return NULL;
4033}
4034
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004036WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004038 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039 return -1;
4040
4041 if (strcmp(name, "buffer") == 0)
4042 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004043 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044 return -1;
4045 }
4046 else if (strcmp(name, "cursor") == 0)
4047 {
4048 long lnum;
4049 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004050
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004051 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 return -1;
4053
Bram Moolenaard6e39182013-05-21 18:30:34 +02004054 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004056 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 return -1;
4058 }
4059
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004060 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004061 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 return -1;
4063
Bram Moolenaard6e39182013-05-21 18:30:34 +02004064 self->win->w_cursor.lnum = lnum;
4065 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004066 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004067 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004068 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004069 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004071 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072 return 0;
4073 }
4074 else if (strcmp(name, "height") == 0)
4075 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004076 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077 win_T *savewin;
4078
Bram Moolenaardee2e312013-06-23 16:35:47 +02004079 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004080 return -1;
4081
4082#ifdef FEAT_GUI
4083 need_mouse_correct = TRUE;
4084#endif
4085 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004086 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004087
4088 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004089 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004091 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 return -1;
4093
4094 return 0;
4095 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096 else if (strcmp(name, "width") == 0)
4097 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004098 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099 win_T *savewin;
4100
Bram Moolenaardee2e312013-06-23 16:35:47 +02004101 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102 return -1;
4103
4104#ifdef FEAT_GUI
4105 need_mouse_correct = TRUE;
4106#endif
4107 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004108 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004109
4110 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004111 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004113 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 return -1;
4115
4116 return 0;
4117 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118 else
4119 {
4120 PyErr_SetString(PyExc_AttributeError, name);
4121 return -1;
4122 }
4123}
4124
4125 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004126WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004128 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004129 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130 else
4131 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004132 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004133
Bram Moolenaar6d216452013-05-12 19:00:41 +02004134 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004135 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 (self));
4137 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004138 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139 }
4140}
4141
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004142static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004143 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004144 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4145 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004146};
4147
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004148/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004149 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004151
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004152static PyTypeObject WinListType;
4153static PySequenceMethods WinListAsSeq;
4154
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004155typedef struct
4156{
4157 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004158 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004159} WinListObject;
4160
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004161 static PyObject *
4162WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004163{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004164 WinListObject *self;
4165
4166 self = PyObject_NEW(WinListObject, &WinListType);
4167 self->tabObject = tabObject;
4168 Py_INCREF(tabObject);
4169
4170 return (PyObject *)(self);
4171}
4172
4173 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004174WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004175{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004176 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004177
4178 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004179 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004180 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004181 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004182
4183 DESTRUCTOR_FINISH(self);
4184}
4185
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004186 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004187WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004188{
4189 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190 PyInt n = 0;
4191
Bram Moolenaard6e39182013-05-21 18:30:34 +02004192 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004193 return -1;
4194
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004195 while (w != NULL)
4196 {
4197 ++n;
4198 w = W_NEXT(w);
4199 }
4200
4201 return n;
4202}
4203
4204 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004205WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004206{
4207 win_T *w;
4208
Bram Moolenaard6e39182013-05-21 18:30:34 +02004209 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004210 return NULL;
4211
4212 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004213 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004214 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004215
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004216 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004217 return NULL;
4218}
4219
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004220/*
4221 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004222 *
4223 * The result is in allocated memory. All internal nulls are replaced by
4224 * newline characters. It is an error for the string to contain newline
4225 * characters.
4226 *
4227 * On errors, the Python exception data is set, and NULL is returned.
4228 */
4229 static char *
4230StringToLine(PyObject *obj)
4231{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004232 char *str;
4233 char *save;
4234 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004235 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004236 PyInt i;
4237 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004238
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004239 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004240 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004241 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4242 || str == NULL)
4243 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004244 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004245 else if (PyUnicode_Check(obj))
4246 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004247 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4248 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004249 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004250
Bram Moolenaardaa27022013-06-24 22:33:30 +02004251 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004252 || str == NULL)
4253 {
4254 Py_DECREF(bytes);
4255 return NULL;
4256 }
4257 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004258 else
4259 {
4260#if PY_MAJOR_VERSION < 3
4261 PyErr_FORMAT(PyExc_TypeError,
4262 N_("expected str() or unicode() instance, but got %s"),
4263 Py_TYPE_NAME(obj));
4264#else
4265 PyErr_FORMAT(PyExc_TypeError,
4266 N_("expected bytes() or str() instance, but got %s"),
4267 Py_TYPE_NAME(obj));
4268#endif
4269 return NULL;
4270 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004271
4272 /*
4273 * Error checking: String must not contain newlines, as we
4274 * are replacing a single line, and we must replace it with
4275 * a single line.
4276 * A trailing newline is removed, so that append(f.readlines()) works.
4277 */
4278 p = memchr(str, '\n', len);
4279 if (p != NULL)
4280 {
4281 if (p == str + len - 1)
4282 --len;
4283 else
4284 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004285 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004286 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004287 return NULL;
4288 }
4289 }
4290
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004291 /*
4292 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004293 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004294 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004295 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004296 if (save == NULL)
4297 {
4298 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004299 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004300 return NULL;
4301 }
4302
4303 for (i = 0; i < len; ++i)
4304 {
4305 if (str[i] == '\0')
4306 save[i] = '\n';
4307 else
4308 save[i] = str[i];
4309 }
4310
4311 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004312 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004313
4314 return save;
4315}
4316
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004317/*
4318 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004319 * in Vim format (1-based). The line is returned as a Python
4320 * string object.
4321 */
4322 static PyObject *
4323GetBufferLine(buf_T *buf, PyInt n)
4324{
4325 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4326}
4327
4328
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004329/*
4330 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004331 * are in Vim format (1-based). The range is from lo up to, but not
4332 * including, hi. The list is returned as a Python list of string objects.
4333 */
4334 static PyObject *
4335GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4336{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004337 PyInt i;
4338 PyInt n = hi - lo;
4339 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004340
4341 if (list == NULL)
4342 return NULL;
4343
4344 for (i = 0; i < n; ++i)
4345 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004346 linenr_T lnum = (linenr_T)(lo + i);
4347 char *text;
4348 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004349
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004350 if (lnum > buf->b_ml.ml_line_count)
4351 text = "";
4352 else
4353 text = (char *)ml_get_buf(buf, lnum, FALSE);
4354 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004355 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004356 {
4357 Py_DECREF(list);
4358 return NULL;
4359 }
4360
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004361 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004362 }
4363
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004364 // The ownership of the Python list is passed to the caller (ie,
4365 // the caller should Py_DECREF() the object when it is finished
4366 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004367
4368 return list;
4369}
4370
4371/*
4372 * Check if deleting lines made the cursor position invalid.
4373 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4374 * deleted).
4375 */
4376 static void
4377py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4378{
4379 if (curwin->w_cursor.lnum >= lo)
4380 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004381 // Adjust the cursor position if it's in/after the changed
4382 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383 if (curwin->w_cursor.lnum >= hi)
4384 {
4385 curwin->w_cursor.lnum += extra;
4386 check_cursor_col();
4387 }
4388 else if (extra < 0)
4389 {
4390 curwin->w_cursor.lnum = lo;
4391 check_cursor();
4392 }
4393 else
4394 check_cursor_col();
4395 changed_cline_bef_curs();
4396 }
4397 invalidate_botline();
4398}
4399
Bram Moolenaar19e60942011-06-19 00:27:51 +02004400/*
4401 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004402 * in Vim format (1-based). The replacement line is given as
4403 * a Python string object. The object is checked for validity
4404 * and correct format. Errors are returned as a value of FAIL.
4405 * The return value is OK on success.
4406 * If OK is returned and len_change is not NULL, *len_change
4407 * is set to the change in the buffer length.
4408 */
4409 static int
4410SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4411{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004412 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004413 win_T *save_curwin = NULL;
4414 tabpage_T *save_curtab = NULL;
4415
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004416 // First of all, we check the type of the supplied Python object.
4417 // There are three cases:
4418 // 1. NULL, or None - this is a deletion.
4419 // 2. A string - this is a replacement.
4420 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 if (line == Py_None || line == NULL)
4422 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004424 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004426 VimTryStart();
4427
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004429 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004430 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004431 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 else
4433 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004434 if (buf == curbuf && (save_curwin != NULL
4435 || save_curbuf.br_buf == NULL))
4436 // Using an existing window for the buffer, adjust the cursor
4437 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004439 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004440 // Only adjust marks if we managed to switch to a window that
4441 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004442 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 }
4444
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004445 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004447 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448 return FAIL;
4449
4450 if (len_change)
4451 *len_change = -1;
4452
4453 return OK;
4454 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004455 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004457 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004458
4459 if (save == NULL)
4460 return FAIL;
4461
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004462 VimTryStart();
4463
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004464 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004466 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004467
4468 if (u_savesub((linenr_T)n) == FAIL)
4469 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004470 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004471 vim_free(save);
4472 }
4473 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4474 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004475 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004476 vim_free(save);
4477 }
4478 else
4479 changed_bytes((linenr_T)n, 0);
4480
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004481 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004482
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004483 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004484 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004485 check_cursor_col();
4486
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004487 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004488 return FAIL;
4489
4490 if (len_change)
4491 *len_change = 0;
4492
4493 return OK;
4494 }
4495 else
4496 {
4497 PyErr_BadArgument();
4498 return FAIL;
4499 }
4500}
4501
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004502/*
4503 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504 * Vim format (1-based). The range is from lo up to, but not including, hi.
4505 * The replacement lines are given as a Python list of string objects. The
4506 * list is checked for validity and correct format. Errors are returned as a
4507 * value of FAIL. The return value is OK on success.
4508 * If OK is returned and len_change is not NULL, *len_change
4509 * is set to the change in the buffer length.
4510 */
4511 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004512SetBufferLineList(
4513 buf_T *buf,
4514 PyInt lo,
4515 PyInt hi,
4516 PyObject *list,
4517 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004518{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004519 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004520 win_T *save_curwin = NULL;
4521 tabpage_T *save_curtab = NULL;
4522
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004523 // First of all, we check the type of the supplied Python object.
4524 // There are three cases:
4525 // 1. NULL, or None - this is a deletion.
4526 // 2. A list - this is a replacement.
4527 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004528 if (list == Py_None || list == NULL)
4529 {
4530 PyInt i;
4531 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004532
4533 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004534 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004535 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004536
4537 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004538 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004539 else
4540 {
4541 for (i = 0; i < n; ++i)
4542 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004543 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004544 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004545 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004546 break;
4547 }
4548 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004549 if (buf == curbuf && (save_curwin != NULL
4550 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004551 // Using an existing window for the buffer, adjust the cursor
4552 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004553 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004554 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004555 // Only adjust marks if we managed to switch to a window that
4556 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004557 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558 }
4559
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004560 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004561
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004562 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004563 return FAIL;
4564
4565 if (len_change)
4566 *len_change = -n;
4567
4568 return OK;
4569 }
4570 else if (PyList_Check(list))
4571 {
4572 PyInt i;
4573 PyInt new_len = PyList_Size(list);
4574 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004575 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004576 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004577
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004578 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004579 array = NULL;
4580 else
4581 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004582 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004583 if (array == NULL)
4584 {
4585 PyErr_NoMemory();
4586 return FAIL;
4587 }
4588 }
4589
4590 for (i = 0; i < new_len; ++i)
4591 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004592 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004593
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004594 if (!(line = PyList_GetItem(list, i)) ||
4595 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004596 {
4597 while (i)
4598 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004599 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004600 return FAIL;
4601 }
4602 }
4603
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004604 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004605 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004606
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004607 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004608 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004609
4610 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004611 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004612
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004613 // If the size of the range is reducing (ie, new_len < old_len) we
4614 // need to delete some old_len. We do this at the start, by
4615 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004616 if (!PyErr_Occurred())
4617 {
4618 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004619 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004620 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004621 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 break;
4623 }
4624 extra -= i;
4625 }
4626
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004627 // For as long as possible, replace the existing old_len with the
4628 // new old_len. This is a more efficient operation, as it requires
4629 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004630 if (!PyErr_Occurred())
4631 {
4632 for (i = 0; i < old_len && i < new_len; ++i)
4633 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4634 == FAIL)
4635 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004636 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004637 break;
4638 }
4639 }
4640 else
4641 i = 0;
4642
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004643 // Now we may need to insert the remaining new old_len. If we do, we
4644 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004645 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004646 if (!PyErr_Occurred())
4647 {
4648 while (i < new_len)
4649 {
4650 if (ml_append((linenr_T)(lo + i - 1),
4651 (char_u *)array[i], 0, FALSE) == FAIL)
4652 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004653 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004654 break;
4655 }
4656 vim_free(array[i]);
4657 ++i;
4658 ++extra;
4659 }
4660 }
4661
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004662 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004663 while (i < new_len)
4664 {
4665 vim_free(array[i]);
4666 ++i;
4667 }
4668
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004669 // Free the array of old_len. All of its contents have now
4670 // been dealt with (either freed, or the responsibility passed
4671 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004672 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004673
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004674 // Adjust marks. Invalidate any which lie in the
4675 // changed range, and move any in the remainder of the buffer.
4676 // Only adjust marks if we managed to switch to a window that holds
4677 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004678 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004679 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004680 (long)MAXLNUM, (long)extra);
4681 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4682
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004683 if (buf == curbuf && (save_curwin != NULL
4684 || save_curbuf.br_buf == NULL))
4685 // Using an existing window for the buffer, adjust the cursor
4686 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004687 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4688
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004689 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004690 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004691
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004692 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004693 return FAIL;
4694
4695 if (len_change)
4696 *len_change = new_len - old_len;
4697
4698 return OK;
4699 }
4700 else
4701 {
4702 PyErr_BadArgument();
4703 return FAIL;
4704 }
4705}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004707/*
4708 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004709 * The line number is in Vim format (1-based). The lines to be inserted are
4710 * given as a Python list of string objects or as a single string. The lines
4711 * to be added are checked for validity and correct format. Errors are
4712 * returned as a value of FAIL. The return value is OK on success.
4713 * If OK is returned and len_change is not NULL, *len_change
4714 * is set to the change in the buffer length.
4715 */
4716 static int
4717InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4718{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004719 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004720 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004721 tabpage_T *save_curtab = NULL;
4722
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004723 // First of all, we check the type of the supplied Python object.
4724 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004725 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004726 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004727 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004728
4729 if (str == NULL)
4730 return FAIL;
4731
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004733 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004734 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004736 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004737 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004739 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004740 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004741 // Only adjust marks if we managed to switch to a window that
4742 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004743 appended_lines_mark((linenr_T)n, 1L);
4744
4745 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004746 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004747 update_screen(VALID);
4748
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004749 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004750 return FAIL;
4751
4752 if (len_change)
4753 *len_change = 1;
4754
4755 return OK;
4756 }
4757 else if (PyList_Check(lines))
4758 {
4759 PyInt i;
4760 PyInt size = PyList_Size(lines);
4761 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004762
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004763 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004764 if (array == NULL)
4765 {
4766 PyErr_NoMemory();
4767 return FAIL;
4768 }
4769
4770 for (i = 0; i < size; ++i)
4771 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004772 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004773
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004774 if (!(line = PyList_GetItem(lines, i)) ||
4775 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004776 {
4777 while (i)
4778 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004779 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004780 return FAIL;
4781 }
4782 }
4783
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004784 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004785 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004786 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004787
4788 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004789 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004790 else
4791 {
4792 for (i = 0; i < size; ++i)
4793 {
4794 if (ml_append((linenr_T)(n + i),
4795 (char_u *)array[i], 0, FALSE) == FAIL)
4796 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004797 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004798
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004799 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004800 while (i < size)
4801 vim_free(array[i++]);
4802
4803 break;
4804 }
4805 vim_free(array[i]);
4806 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004807 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004808 // Only adjust marks if we managed to switch to a window that
4809 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004810 appended_lines_mark((linenr_T)n, (long)i);
4811 }
4812
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004813 // Free the array of lines. All of its contents have now
4814 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004815 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004816 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004818 update_screen(VALID);
4819
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004820 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004821 return FAIL;
4822
4823 if (len_change)
4824 *len_change = size;
4825
4826 return OK;
4827 }
4828 else
4829 {
4830 PyErr_BadArgument();
4831 return FAIL;
4832 }
4833}
4834
4835/*
4836 * Common routines for buffers and line ranges
4837 * -------------------------------------------
4838 */
4839
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004840typedef struct
4841{
4842 PyObject_HEAD
4843 buf_T *buf;
4844} BufferObject;
4845
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004846 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004847CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004848{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004849 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004850 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004851 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004852 return -1;
4853 }
4854
4855 return 0;
4856}
4857
4858 static PyObject *
4859RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4860{
4861 if (CheckBuffer(self))
4862 return NULL;
4863
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004864 if (end == -1)
4865 end = self->buf->b_ml.ml_line_count;
4866
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004867 if (n < 0)
4868 n += end - start + 1;
4869
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004870 if (n < 0 || n > end - start)
4871 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004872 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004873 return NULL;
4874 }
4875
4876 return GetBufferLine(self->buf, n+start);
4877}
4878
4879 static PyObject *
4880RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4881{
4882 PyInt size;
4883
4884 if (CheckBuffer(self))
4885 return NULL;
4886
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004887 if (end == -1)
4888 end = self->buf->b_ml.ml_line_count;
4889
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004890 size = end - start + 1;
4891
4892 if (lo < 0)
4893 lo = 0;
4894 else if (lo > size)
4895 lo = size;
4896 if (hi < 0)
4897 hi = 0;
4898 if (hi < lo)
4899 hi = lo;
4900 else if (hi > size)
4901 hi = size;
4902
4903 return GetBufferLineList(self->buf, lo+start, hi+start);
4904}
4905
4906 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004907RBAsItem(
4908 BufferObject *self,
4909 PyInt n,
4910 PyObject *valObject,
4911 PyInt start,
4912 PyInt end,
4913 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004914{
4915 PyInt len_change;
4916
4917 if (CheckBuffer(self))
4918 return -1;
4919
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004920 if (end == -1)
4921 end = self->buf->b_ml.ml_line_count;
4922
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004923 if (n < 0)
4924 n += end - start + 1;
4925
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004926 if (n < 0 || n > end - start)
4927 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004928 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004929 return -1;
4930 }
4931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004932 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004933 return -1;
4934
4935 if (new_end)
4936 *new_end = end + len_change;
4937
4938 return 0;
4939}
4940
Bram Moolenaar19e60942011-06-19 00:27:51 +02004941 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004942RBAsSlice(
4943 BufferObject *self,
4944 PyInt lo,
4945 PyInt hi,
4946 PyObject *valObject,
4947 PyInt start,
4948 PyInt end,
4949 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004950{
4951 PyInt size;
4952 PyInt len_change;
4953
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004954 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004955 if (CheckBuffer(self))
4956 return -1;
4957
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004958 if (end == -1)
4959 end = self->buf->b_ml.ml_line_count;
4960
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004961 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004962 size = end - start + 1;
4963
4964 if (lo < 0)
4965 lo = 0;
4966 else if (lo > size)
4967 lo = size;
4968 if (hi < 0)
4969 hi = 0;
4970 if (hi < lo)
4971 hi = lo;
4972 else if (hi > size)
4973 hi = size;
4974
4975 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004976 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004977 return -1;
4978
4979 if (new_end)
4980 *new_end = end + len_change;
4981
4982 return 0;
4983}
4984
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004985
4986 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004987RBAppend(
4988 BufferObject *self,
4989 PyObject *args,
4990 PyInt start,
4991 PyInt end,
4992 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004993{
4994 PyObject *lines;
4995 PyInt len_change;
4996 PyInt max;
4997 PyInt n;
4998
4999 if (CheckBuffer(self))
5000 return NULL;
5001
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005002 if (end == -1)
5003 end = self->buf->b_ml.ml_line_count;
5004
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005005 max = n = end - start + 1;
5006
5007 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5008 return NULL;
5009
5010 if (n < 0 || n > max)
5011 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005012 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005013 return NULL;
5014 }
5015
5016 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5017 return NULL;
5018
5019 if (new_end)
5020 *new_end = end + len_change;
5021
5022 Py_INCREF(Py_None);
5023 return Py_None;
5024}
5025
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005026// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005027
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005028static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005029static PySequenceMethods RangeAsSeq;
5030static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005031
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005032typedef struct
5033{
5034 PyObject_HEAD
5035 BufferObject *buf;
5036 PyInt start;
5037 PyInt end;
5038} RangeObject;
5039
5040 static PyObject *
5041RangeNew(buf_T *buf, PyInt start, PyInt end)
5042{
5043 BufferObject *bufr;
5044 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005045 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005046 if (self == NULL)
5047 return NULL;
5048
5049 bufr = (BufferObject *)BufferNew(buf);
5050 if (bufr == NULL)
5051 {
5052 Py_DECREF(self);
5053 return NULL;
5054 }
5055 Py_INCREF(bufr);
5056
5057 self->buf = bufr;
5058 self->start = start;
5059 self->end = end;
5060
5061 return (PyObject *)(self);
5062}
5063
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005064 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005066{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005067 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005068 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005069 PyObject_GC_Del((void *)(self));
5070}
5071
5072 static int
5073RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5074{
5075 Py_VISIT(((PyObject *)(self->buf)));
5076 return 0;
5077}
5078
5079 static int
5080RangeClear(RangeObject *self)
5081{
5082 Py_CLEAR(self->buf);
5083 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005084}
5085
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005087RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005089 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005091 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005092
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094}
5095
5096 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005097RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005098{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005099 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100}
5101
5102 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005103RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005105 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106}
5107
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005108static char *RangeAttrs[] = {
5109 "start", "end",
5110 NULL
5111};
5112
5113 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005114RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005115{
5116 return ObjectDir(self, RangeAttrs);
5117}
5118
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005120RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123}
5124
5125 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005126RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005127{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005129 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5130 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005131 else
5132 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005133 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005134
5135 if (name == NULL)
5136 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005137
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005138 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005139 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005140 }
5141}
5142
5143static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005144 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005145 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005146 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5147 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005148};
5149
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005150static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005151static PySequenceMethods BufferAsSeq;
5152static PyMappingMethods BufferAsMapping;
5153
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005154 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005155BufferNew(buf_T *buf)
5156{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005157 /*
5158 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005159 * If we add a "b_python*_ref" field to the buf_T structure,
5160 * then we can get at it in buf_freeall() in vim. We then
5161 * need to create only ONE Python object per buffer - if
5162 * we try to create a second, just INCREF the existing one
5163 * and return it. The (single) Python object referring to
5164 * the buffer is stored in "b_python*_ref".
5165 * Question: what to do on a buf_freeall(). We'll probably
5166 * have to either delete the Python object (DECREF it to
5167 * zero - a bad idea, as it leaves dangling refs!) or
5168 * set the buf_T * value to an invalid value (-1?), which
5169 * means we need checks in all access functions... Bah.
5170 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005171 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005172 * b_python_ref and b_python3_ref fields respectively.
5173 */
5174
5175 BufferObject *self;
5176
5177 if (BUF_PYTHON_REF(buf) != NULL)
5178 {
5179 self = BUF_PYTHON_REF(buf);
5180 Py_INCREF(self);
5181 }
5182 else
5183 {
5184 self = PyObject_NEW(BufferObject, &BufferType);
5185 if (self == NULL)
5186 return NULL;
5187 self->buf = buf;
5188 BUF_PYTHON_REF(buf) = self;
5189 }
5190
5191 return (PyObject *)(self);
5192}
5193
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005194 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005196{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005197 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5198 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005199
5200 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005201}
5202
Bram Moolenaar971db462013-05-12 18:44:48 +02005203 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005204BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005205{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005206 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005207 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005208 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005209
Bram Moolenaard6e39182013-05-21 18:30:34 +02005210 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005211}
5212
5213 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005214BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005215{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005216 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005217}
5218
5219 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005220BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005221{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005222 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005223}
5224
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005225static char *BufferAttrs[] = {
5226 "name", "number", "vars", "options", "valid",
5227 NULL
5228};
5229
5230 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005231BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005232{
5233 return ObjectDir(self, BufferAttrs);
5234}
5235
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005236 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005237BufferAttrValid(BufferObject *self, char *name)
5238{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005239 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005240
5241 if (strcmp(name, "valid") != 0)
5242 return NULL;
5243
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005244 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5245 Py_INCREF(ret);
5246 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005247}
5248
5249 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005250BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005251{
5252 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005253 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005254 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005255 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005256 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005257 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005258 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005259 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005260 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5261 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005262 else if (strcmp(name, "__members__") == 0)
5263 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005264 else
5265 return NULL;
5266}
5267
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005268 static int
5269BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5270{
5271 if (CheckBuffer(self))
5272 return -1;
5273
5274 if (strcmp(name, "name") == 0)
5275 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005276 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005277 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005278 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005279 PyObject *todecref;
5280
5281 if (!(val = StringToChars(valObject, &todecref)))
5282 return -1;
5283
5284 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005285 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005286 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005287 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005288 aucmd_restbuf(&aco);
5289 Py_XDECREF(todecref);
5290 if (VimTryEnd())
5291 return -1;
5292
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005293 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005294 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005295 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005296 return -1;
5297 }
5298 return 0;
5299 }
5300 else
5301 {
5302 PyErr_SetString(PyExc_AttributeError, name);
5303 return -1;
5304 }
5305}
5306
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005307 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005308BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005309{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311}
5312
5313 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005314BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315{
5316 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005317 char_u *pmark;
5318 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005319 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005320 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321
Bram Moolenaard6e39182013-05-21 18:30:34 +02005322 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005323 return NULL;
5324
Bram Moolenaar389a1792013-06-23 13:00:44 +02005325 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005326 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005327
Bram Moolenaar389a1792013-06-23 13:00:44 +02005328 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005329 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005330 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005331 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005332 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005333 return NULL;
5334 }
5335
5336 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005337
5338 Py_XDECREF(todecref);
5339
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005340 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005341 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005342 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005343 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005344 if (VimTryEnd())
5345 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005346
5347 if (posp == NULL)
5348 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005349 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005350 return NULL;
5351 }
5352
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005353 if (posp->lnum <= 0)
5354 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005355 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005356 Py_INCREF(Py_None);
5357 return Py_None;
5358 }
5359
5360 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5361}
5362
5363 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005364BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005365{
5366 PyInt start;
5367 PyInt end;
5368
Bram Moolenaard6e39182013-05-21 18:30:34 +02005369 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005370 return NULL;
5371
5372 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5373 return NULL;
5374
Bram Moolenaard6e39182013-05-21 18:30:34 +02005375 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005376}
5377
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005378 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005379BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005380{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005381 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005382 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005383 else
5384 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005385 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005386
5387 if (name == NULL)
5388 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005389
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005390 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005391 }
5392}
5393
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005394static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005395 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005396 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005397 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005398 {"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 +02005399 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5400 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005401};
5402
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005403/*
5404 * Buffer list object - Implementation
5405 */
5406
5407static PyTypeObject BufMapType;
5408
5409typedef struct
5410{
5411 PyObject_HEAD
5412} BufMapObject;
5413
5414 static PyInt
5415BufMapLength(PyObject *self UNUSED)
5416{
5417 buf_T *b = firstbuf;
5418 PyInt n = 0;
5419
5420 while (b)
5421 {
5422 ++n;
5423 b = b->b_next;
5424 }
5425
5426 return n;
5427}
5428
5429 static PyObject *
5430BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5431{
5432 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005433 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005434
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005435 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005436 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005437
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005438 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005439
5440 if (b)
5441 return BufferNew(b);
5442 else
5443 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005444 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005445 return NULL;
5446 }
5447}
5448
5449 static void
5450BufMapIterDestruct(PyObject *buffer)
5451{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005452 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005453 if (buffer)
5454 {
5455 Py_DECREF(buffer);
5456 }
5457}
5458
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005459 static int
5460BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5461{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005462 if (buffer)
5463 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005464 return 0;
5465}
5466
5467 static int
5468BufMapIterClear(PyObject **buffer)
5469{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005470 if (*buffer)
5471 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005472 return 0;
5473}
5474
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005475 static PyObject *
5476BufMapIterNext(PyObject **buffer)
5477{
5478 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005479 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005480
5481 if (!*buffer)
5482 return NULL;
5483
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005484 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005485
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005486 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005487 {
5488 *buffer = NULL;
5489 return NULL;
5490 }
5491
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005493 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005494 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005495 return NULL;
5496 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005497 // Do not increment reference: we no longer hold it (decref), but whoever
5498 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005499 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005500}
5501
5502 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005503BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005504{
5505 PyObject *buffer;
5506
5507 buffer = BufferNew(firstbuf);
5508 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005509 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005510 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5511 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005512}
5513
5514static PyMappingMethods BufMapAsMapping = {
5515 (lenfunc) BufMapLength,
5516 (binaryfunc) BufMapItem,
5517 (objobjargproc) 0,
5518};
5519
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005520// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005521
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005522static char *CurrentAttrs[] = {
5523 "buffer", "window", "line", "range", "tabpage",
5524 NULL
5525};
5526
5527 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005528CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005529{
5530 return ObjectDir(self, CurrentAttrs);
5531}
5532
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005533 static PyObject *
5534CurrentGetattr(PyObject *self UNUSED, char *name)
5535{
5536 if (strcmp(name, "buffer") == 0)
5537 return (PyObject *)BufferNew(curbuf);
5538 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005539 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005540 else if (strcmp(name, "tabpage") == 0)
5541 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005542 else if (strcmp(name, "line") == 0)
5543 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5544 else if (strcmp(name, "range") == 0)
5545 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005546 else if (strcmp(name, "__members__") == 0)
5547 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005548 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005549#if PY_MAJOR_VERSION < 3
5550 return Py_FindMethod(WindowMethods, self, name);
5551#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005552 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005553#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005554}
5555
5556 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005557CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005558{
5559 if (strcmp(name, "line") == 0)
5560 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005561 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5562 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005563 return -1;
5564
5565 return 0;
5566 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005567 else if (strcmp(name, "buffer") == 0)
5568 {
5569 int count;
5570
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005572 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005573 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005574 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005575 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005576 return -1;
5577 }
5578
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005579 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005580 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005582
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005583 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005584 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5585 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005586 if (VimTryEnd())
5587 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005588 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005589 return -1;
5590 }
5591
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005592 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005593 }
5594 else if (strcmp(name, "window") == 0)
5595 {
5596 int count;
5597
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005598 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005599 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005600 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005601 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005602 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005603 return -1;
5604 }
5605
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005606 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005607 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005608 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005609
5610 if (!count)
5611 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005612 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005613 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005614 return -1;
5615 }
5616
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005617 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005618 win_goto(((WindowObject *)(valObject))->win);
5619 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005621 if (VimTryEnd())
5622 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005623 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005624 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005625 return -1;
5626 }
5627
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005628 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005629 }
5630 else if (strcmp(name, "tabpage") == 0)
5631 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005632 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005633 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005634 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005635 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005636 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005637 return -1;
5638 }
5639
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005640 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005641 return -1;
5642
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005643 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005644 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5645 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005646 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005647 if (VimTryEnd())
5648 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005649 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005650 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005651 return -1;
5652 }
5653
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005654 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005655 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005656 else
5657 {
5658 PyErr_SetString(PyExc_AttributeError, name);
5659 return -1;
5660 }
5661}
5662
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005663static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005664 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005665 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5666 { NULL, NULL, 0, NULL}
5667};
5668
Bram Moolenaardb913952012-06-29 12:54:53 +02005669 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005670init_range_cmd(exarg_T *eap)
5671{
5672 RangeStart = eap->line1;
5673 RangeEnd = eap->line2;
5674}
5675
5676 static void
5677init_range_eval(typval_T *rettv UNUSED)
5678{
5679 RangeStart = (PyInt) curwin->w_cursor.lnum;
5680 RangeEnd = RangeStart;
5681}
5682
5683 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005684run_cmd(const char *cmd, void *arg UNUSED
5685#ifdef PY_CAN_RECURSE
5686 , PyGILState_STATE *pygilstate UNUSED
5687#endif
5688 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005689{
Bram Moolenaar41009372013-07-01 22:03:04 +02005690 PyObject *run_ret;
5691 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5692 if (run_ret != NULL)
5693 {
5694 Py_DECREF(run_ret);
5695 }
5696 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005698 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005699 PyErr_Clear();
5700 }
5701 else
5702 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005703}
5704
5705static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5706static int code_hdr_len = 30;
5707
5708 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005709run_do(const char *cmd, void *arg UNUSED
5710#ifdef PY_CAN_RECURSE
5711 , PyGILState_STATE *pygilstate
5712#endif
5713 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005714{
5715 PyInt lnum;
5716 size_t len;
5717 char *code;
5718 int status;
5719 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005720 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005721 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005722
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005723 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005725 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005726 return;
5727 }
5728
5729 len = code_hdr_len + STRLEN(cmd);
5730 code = PyMem_New(char, len + 1);
5731 memcpy(code, code_hdr, code_hdr_len);
5732 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005733 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5734 status = -1;
5735 if (run_ret != NULL)
5736 {
5737 status = 0;
5738 Py_DECREF(run_ret);
5739 }
5740 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5741 {
5742 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005743 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005744 PyErr_Clear();
5745 return;
5746 }
5747 else
5748 PyErr_PrintEx(1);
5749
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005750 PyMem_Free(code);
5751
5752 if (status)
5753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005754 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005755 return;
5756 }
5757
5758 status = 0;
5759 pymain = PyImport_AddModule("__main__");
5760 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005761#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005762 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005763#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764
5765 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5766 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005767 PyObject *line;
5768 PyObject *linenr;
5769 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005770
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005771#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005772 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005773#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005774 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005775 if (lnum > curbuf->b_ml.ml_line_count
5776 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005777 goto err;
5778 if (!(linenr = PyInt_FromLong((long) lnum)))
5779 {
5780 Py_DECREF(line);
5781 goto err;
5782 }
5783 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5784 Py_DECREF(line);
5785 Py_DECREF(linenr);
5786 if (!ret)
5787 goto err;
5788
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005789 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005790 if (curbuf != was_curbuf)
5791 {
5792 Py_XDECREF(ret);
5793 goto err;
5794 }
5795
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005796 if (ret != Py_None)
5797 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005798 {
5799 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005800 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005801 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005802
5803 Py_XDECREF(ret);
5804 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005805#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005807#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005808 }
5809 goto out;
5810err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005811#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005812 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005813#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005814 PyErr_PrintEx(0);
5815 PythonIO_Flush();
5816 status = 1;
5817out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005818#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005819 if (!status)
5820 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005821#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005822 Py_DECREF(pyfunc);
5823 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5824 if (status)
5825 return;
5826 check_cursor();
5827 update_curbuf(NOT_VALID);
5828}
5829
5830 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005831run_eval(const char *cmd, typval_T *rettv
5832#ifdef PY_CAN_RECURSE
5833 , PyGILState_STATE *pygilstate UNUSED
5834#endif
5835 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005836{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005837 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005838
Bram Moolenaar41009372013-07-01 22:03:04 +02005839 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005840 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005841 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005842 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005844 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005845 PyErr_Clear();
5846 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005847 else
5848 {
5849 if (PyErr_Occurred() && !msg_silent)
5850 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005851 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005852 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005853 }
5854 else
5855 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005856 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar86181df2020-05-11 23:14:04 +02005857 emsg(_("E859: Failed to convert returned python object to a Vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005858 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005859 }
5860 PyErr_Clear();
5861}
5862
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005864set_ref_in_py(const int copyID)
5865{
5866 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005867 list_T *ll;
5868 int i;
5869 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005870 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005871
5872 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005873 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005874 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005875 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5876 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005877 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005878
5879 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005880 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005881 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005882 {
5883 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005884 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005885 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005886 }
5887
Bram Moolenaar8110a092016-04-14 15:56:09 +02005888 if (lastfunc != NULL)
5889 {
5890 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5891 {
5892 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005893 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005894 if (func->argc)
5895 for (i = 0; !abort && i < func->argc; ++i)
5896 abort = abort
5897 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5898 }
5899 }
5900
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005901 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005902}
5903
5904 static int
5905set_string_copy(char_u *str, typval_T *tv)
5906{
5907 tv->vval.v_string = vim_strsave(str);
5908 if (tv->vval.v_string == NULL)
5909 {
5910 PyErr_NoMemory();
5911 return -1;
5912 }
5913 return 0;
5914}
5915
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005917pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005919 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920 char_u *key;
5921 dictitem_T *di;
5922 PyObject *keyObject;
5923 PyObject *valObject;
5924 Py_ssize_t iter = 0;
5925
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005926 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005927 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005928
5929 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005930 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005931
5932 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5933 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005934 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005935
Bram Moolenaara03e6312013-05-29 22:49:26 +02005936 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005937 {
5938 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005940 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005942 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005943 {
5944 dict_unref(dict);
5945 return -1;
5946 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005947
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005948 if (*key == NUL)
5949 {
5950 dict_unref(dict);
5951 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005952 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005953 return -1;
5954 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005955
5956 di = dictitem_alloc(key);
5957
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005958 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959
5960 if (di == NULL)
5961 {
5962 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005963 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005964 return -1;
5965 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005967 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968 {
5969 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005970 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971 return -1;
5972 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005973
5974 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005976 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005977 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005978 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005979 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005980 return -1;
5981 }
5982 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005983
5984 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985 return 0;
5986}
5987
5988 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005989pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005990{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005991 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005992 char_u *key;
5993 dictitem_T *di;
5994 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005995 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005996 PyObject *keyObject;
5997 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005999 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006000 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006001
6002 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006003 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006004
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006005 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006006 {
6007 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006008 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006009 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006010
6011 if (!(iterator = PyObject_GetIter(list)))
6012 {
6013 dict_unref(dict);
6014 Py_DECREF(list);
6015 return -1;
6016 }
6017 Py_DECREF(list);
6018
6019 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006020 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006021 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006022
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006024 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006025 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006026 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006027 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006028 return -1;
6029 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006030
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006031 if (*key == NUL)
6032 {
6033 Py_DECREF(keyObject);
6034 Py_DECREF(iterator);
6035 Py_XDECREF(todecref);
6036 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006037 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006038 return -1;
6039 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006040
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006041 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006043 Py_DECREF(keyObject);
6044 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006045 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006046 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006047 return -1;
6048 }
6049
6050 di = dictitem_alloc(key);
6051
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006052 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006053 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006054
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006055 if (di == NULL)
6056 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006057 Py_DECREF(iterator);
6058 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006059 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006060 PyErr_NoMemory();
6061 return -1;
6062 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006063
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006064 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006065 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006066 Py_DECREF(iterator);
6067 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006069 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006070 return -1;
6071 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006072
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006073 Py_DECREF(valObject);
6074
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006075 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006077 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006078 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 dictitem_free(di);
6080 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006081 return -1;
6082 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006083 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006084 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006085 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006086 return 0;
6087}
6088
6089 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006090pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006091{
6092 list_T *l;
6093
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006094 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006095 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006096
6097 tv->v_type = VAR_LIST;
6098 tv->vval.v_list = l;
6099
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006100 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006101 {
6102 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006103 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006104 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006105
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006106 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006107 return 0;
6108}
6109
Bram Moolenaardb913952012-06-29 12:54:53 +02006110typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6111
6112 static int
6113convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006114 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006115{
6116 PyObject *capsule;
6117 char hexBuf[sizeof(void *) * 2 + 3];
6118
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006119 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006120
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006121#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006122 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006123#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006124 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006125#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006126 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006127 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006128#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006129 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006130#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006131 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006132#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006133 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6134 {
6135 Py_DECREF(capsule);
6136 tv->v_type = VAR_UNKNOWN;
6137 return -1;
6138 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006139
6140 Py_DECREF(capsule);
6141
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006142 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006143 {
6144 tv->v_type = VAR_UNKNOWN;
6145 return -1;
6146 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006147 // As we are not using copy_tv which increments reference count we must
6148 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006149 if (tv->v_type == VAR_DICT)
6150 ++tv->vval.v_dict->dv_refcount;
6151 else if (tv->v_type == VAR_LIST)
6152 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006153 }
6154 else
6155 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006156 typval_T *v;
6157
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006158#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006159 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006160#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006161 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006162#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006163 copy_tv(v, tv);
6164 }
6165 return 0;
6166}
6167
6168 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006169ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6170{
6171 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006172 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006173
6174 if (!(lookup_dict = PyDict_New()))
6175 return -1;
6176
6177 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6178 {
6179 tv->v_type = VAR_DICT;
6180 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6181 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006182 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006183 }
6184 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006185 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006186 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006187 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006188 else
6189 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006190 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006191 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006192 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006193 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006194 }
6195 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006196 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006197}
6198
6199 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006200ConvertFromPySequence(PyObject *obj, typval_T *tv)
6201{
6202 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006203 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006204
6205 if (!(lookup_dict = PyDict_New()))
6206 return -1;
6207
6208 if (PyType_IsSubtype(obj->ob_type, &ListType))
6209 {
6210 tv->v_type = VAR_LIST;
6211 tv->vval.v_list = (((ListObject *)(obj))->list);
6212 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006213 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006214 }
6215 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006216 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006217 else
6218 {
6219 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006220 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006221 Py_TYPE_NAME(obj));
6222 ret = -1;
6223 }
6224 Py_DECREF(lookup_dict);
6225 return ret;
6226}
6227
6228 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006229ConvertFromPyObject(PyObject *obj, typval_T *tv)
6230{
6231 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006232 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006233
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006234 if (!(lookup_dict = PyDict_New()))
6235 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006236 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006237 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006238 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006239}
6240
6241 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006242_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006243{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006244 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006245 {
6246 tv->v_type = VAR_DICT;
6247 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6248 ++tv->vval.v_dict->dv_refcount;
6249 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006250 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 {
6252 tv->v_type = VAR_LIST;
6253 tv->vval.v_list = (((ListObject *)(obj))->list);
6254 ++tv->vval.v_list->lv_refcount;
6255 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006256 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006258 FunctionObject *func = (FunctionObject *) obj;
6259 if (func->self != NULL || func->argv != NULL)
6260 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006261 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6262
Bram Moolenaar8110a092016-04-14 15:56:09 +02006263 set_partial(func, pt, TRUE);
6264 tv->vval.v_partial = pt;
6265 tv->v_type = VAR_PARTIAL;
6266 }
6267 else
6268 {
6269 if (set_string_copy(func->name, tv) == -1)
6270 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006271
Bram Moolenaar8110a092016-04-14 15:56:09 +02006272 tv->v_type = VAR_FUNC;
6273 }
6274 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006275 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 else if (PyBytes_Check(obj))
6277 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006278 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006280 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006281 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006282 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 return -1;
6284
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006285 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006286 return -1;
6287
6288 tv->v_type = VAR_STRING;
6289 }
6290 else if (PyUnicode_Check(obj))
6291 {
6292 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006293 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006294
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006295 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006296 if (bytes == NULL)
6297 return -1;
6298
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006299 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006300 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006301 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006302 return -1;
6303
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006304 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006305 {
6306 Py_XDECREF(bytes);
6307 return -1;
6308 }
6309 Py_XDECREF(bytes);
6310
6311 tv->v_type = VAR_STRING;
6312 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006313#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006314 else if (PyInt_Check(obj))
6315 {
6316 tv->v_type = VAR_NUMBER;
6317 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006318 if (PyErr_Occurred())
6319 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006320 }
6321#endif
6322 else if (PyLong_Check(obj))
6323 {
6324 tv->v_type = VAR_NUMBER;
6325 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006326 if (PyErr_Occurred())
6327 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006328 }
6329 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006330 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006331#ifdef FEAT_FLOAT
6332 else if (PyFloat_Check(obj))
6333 {
6334 tv->v_type = VAR_FLOAT;
6335 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6336 }
6337#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006338 else if (PyObject_HasAttrString(obj, "keys"))
6339 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006340 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006341 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006342 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006343 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006344 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006345 else if (PyNumber_Check(obj))
6346 {
6347 PyObject *num;
6348
6349 if (!(num = PyNumber_Long(obj)))
6350 return -1;
6351
6352 tv->v_type = VAR_NUMBER;
6353 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6354
6355 Py_DECREF(num);
6356 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006357 else if (obj == Py_None)
6358 {
6359 tv->v_type = VAR_SPECIAL;
6360 tv->vval.v_number = VVAL_NONE;
6361 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006362 else
6363 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006364 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006365 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006366 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006367 return -1;
6368 }
6369 return 0;
6370}
6371
6372 static PyObject *
6373ConvertToPyObject(typval_T *tv)
6374{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006375 typval_T *argv;
6376 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006377 if (tv == NULL)
6378 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006379 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006380 return NULL;
6381 }
6382 switch (tv->v_type)
6383 {
6384 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006385 return PyBytes_FromString(tv->vval.v_string == NULL
6386 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006387 case VAR_NUMBER:
6388 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006389 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006390#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006391 return PyFloat_FromDouble((double) tv->vval.v_float);
6392#endif
6393 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006394 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006396 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006397 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006398 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006399 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006400 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006401 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006402 if (tv->vval.v_partial->pt_argc)
6403 {
6404 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6405 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6406 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6407 }
6408 else
6409 argv = NULL;
6410 if (tv->vval.v_partial->pt_dict != NULL)
6411 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006412 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006413 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006414 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006415 tv->vval.v_partial->pt_dict,
6416 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006417 case VAR_BLOB:
6418 return PyBytes_FromStringAndSize(
6419 (char*) tv->vval.v_blob->bv_ga.ga_data,
6420 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006421 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006422 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006424 case VAR_CHANNEL:
6425 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006426 Py_INCREF(Py_None);
6427 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006428 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006429 case VAR_SPECIAL:
6430 switch (tv->vval.v_number)
6431 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006432 case VVAL_FALSE: return ALWAYS_FALSE;
6433 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006434 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006435 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006436 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006437 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006438 return NULL;
6439 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006440 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006441}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006442
6443typedef struct
6444{
6445 PyObject_HEAD
6446} CurrentObject;
6447static PyTypeObject CurrentType;
6448
6449 static void
6450init_structs(void)
6451{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006452 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006453 OutputType.tp_name = "vim.message";
6454 OutputType.tp_basicsize = sizeof(OutputObject);
6455 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6456 OutputType.tp_doc = "vim message object";
6457 OutputType.tp_methods = OutputMethods;
6458#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006459 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6460 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006461 OutputType.tp_alloc = call_PyType_GenericAlloc;
6462 OutputType.tp_new = call_PyType_GenericNew;
6463 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006464 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006466 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6467 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006468 // Disabled, because this causes a crash in test86
6469 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006470#endif
6471
Bram Moolenaara80faa82020-04-12 19:37:17 +02006472 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006473 IterType.tp_name = "vim.iter";
6474 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006475 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006476 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006477 IterType.tp_iter = (getiterfunc)IterIter;
6478 IterType.tp_iternext = (iternextfunc)IterNext;
6479 IterType.tp_dealloc = (destructor)IterDestructor;
6480 IterType.tp_traverse = (traverseproc)IterTraverse;
6481 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006482
Bram Moolenaara80faa82020-04-12 19:37:17 +02006483 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006484 BufferType.tp_name = "vim.buffer";
6485 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 BufferType.tp_dealloc = (destructor)BufferDestructor;
6487 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488 BufferType.tp_as_sequence = &BufferAsSeq;
6489 BufferType.tp_as_mapping = &BufferAsMapping;
6490 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6491 BufferType.tp_doc = "vim buffer object";
6492 BufferType.tp_methods = BufferMethods;
6493#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006494 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006495 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006496 BufferType.tp_alloc = call_PyType_GenericAlloc;
6497 BufferType.tp_new = call_PyType_GenericNew;
6498 BufferType.tp_free = call_PyObject_Free;
6499#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006500 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006501 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006502#endif
6503
Bram Moolenaara80faa82020-04-12 19:37:17 +02006504 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006505 WindowType.tp_name = "vim.window";
6506 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006507 WindowType.tp_dealloc = (destructor)WindowDestructor;
6508 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006509 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006510 WindowType.tp_doc = "vim Window object";
6511 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006512 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6513 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006514#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6516 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006517 WindowType.tp_alloc = call_PyType_GenericAlloc;
6518 WindowType.tp_new = call_PyType_GenericNew;
6519 WindowType.tp_free = call_PyObject_Free;
6520#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6522 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006523#endif
6524
Bram Moolenaara80faa82020-04-12 19:37:17 +02006525 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006526 TabPageType.tp_name = "vim.tabpage";
6527 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006528 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6529 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006530 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6531 TabPageType.tp_doc = "vim tab page object";
6532 TabPageType.tp_methods = TabPageMethods;
6533#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006534 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006535 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6536 TabPageType.tp_new = call_PyType_GenericNew;
6537 TabPageType.tp_free = call_PyObject_Free;
6538#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006539 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006540#endif
6541
Bram Moolenaara80faa82020-04-12 19:37:17 +02006542 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006543 BufMapType.tp_name = "vim.bufferlist";
6544 BufMapType.tp_basicsize = sizeof(BufMapObject);
6545 BufMapType.tp_as_mapping = &BufMapAsMapping;
6546 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006547 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006548 BufferType.tp_doc = "vim buffer list";
6549
Bram Moolenaara80faa82020-04-12 19:37:17 +02006550 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551 WinListType.tp_name = "vim.windowlist";
6552 WinListType.tp_basicsize = sizeof(WinListType);
6553 WinListType.tp_as_sequence = &WinListAsSeq;
6554 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6555 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006556 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006557
Bram Moolenaara80faa82020-04-12 19:37:17 +02006558 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006559 TabListType.tp_name = "vim.tabpagelist";
6560 TabListType.tp_basicsize = sizeof(TabListType);
6561 TabListType.tp_as_sequence = &TabListAsSeq;
6562 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6563 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006564
Bram Moolenaara80faa82020-04-12 19:37:17 +02006565 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006566 RangeType.tp_name = "vim.range";
6567 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006568 RangeType.tp_dealloc = (destructor)RangeDestructor;
6569 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006570 RangeType.tp_as_sequence = &RangeAsSeq;
6571 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006572 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006573 RangeType.tp_doc = "vim Range object";
6574 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006575 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6576 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006577#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006578 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579 RangeType.tp_alloc = call_PyType_GenericAlloc;
6580 RangeType.tp_new = call_PyType_GenericNew;
6581 RangeType.tp_free = call_PyObject_Free;
6582#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006583 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006584#endif
6585
Bram Moolenaara80faa82020-04-12 19:37:17 +02006586 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006587 CurrentType.tp_name = "vim.currentdata";
6588 CurrentType.tp_basicsize = sizeof(CurrentObject);
6589 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6590 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006591 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006593 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6594 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006595#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006596 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6597 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006598#endif
6599
Bram Moolenaara80faa82020-04-12 19:37:17 +02006600 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006601 DictionaryType.tp_name = "vim.dictionary";
6602 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006604 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006605 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006606 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006607 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006609 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6610 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6611 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006612#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006613 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6614 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006615#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006616 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6617 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006618#endif
6619
Bram Moolenaara80faa82020-04-12 19:37:17 +02006620 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006621 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006622 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623 ListType.tp_basicsize = sizeof(ListObject);
6624 ListType.tp_as_sequence = &ListAsSeq;
6625 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006626 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006627 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006628 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006629 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006630 ListType.tp_new = (newfunc)ListConstructor;
6631 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006632#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006633 ListType.tp_getattro = (getattrofunc)ListGetattro;
6634 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006635#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006636 ListType.tp_getattr = (getattrfunc)ListGetattr;
6637 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006638#endif
6639
Bram Moolenaara80faa82020-04-12 19:37:17 +02006640 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006641 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006642 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006643 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6644 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006645 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006646 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006647 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006648 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006649 FunctionType.tp_new = (newfunc)FunctionConstructor;
6650 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006651#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006652 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006653#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006654 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006655#endif
6656
Bram Moolenaara80faa82020-04-12 19:37:17 +02006657 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006658 OptionsType.tp_name = "vim.options";
6659 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006660 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006661 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006662 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006663 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006664 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006665 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6666 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6667 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006668
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006669#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006670 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006671 LoaderType.tp_name = "vim.Loader";
6672 LoaderType.tp_basicsize = sizeof(LoaderObject);
6673 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6674 LoaderType.tp_doc = "vim message object";
6675 LoaderType.tp_methods = LoaderMethods;
6676 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006677#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006678
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006679#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006680 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006681 vimmodule.m_name = "vim";
6682 vimmodule.m_doc = "Vim Python interface\n";
6683 vimmodule.m_size = -1;
6684 vimmodule.m_methods = VimMethods;
6685#endif
6686}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006687
6688#define PYTYPE_READY(type) \
6689 if (PyType_Ready(&type)) \
6690 return -1;
6691
6692 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006693init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006694{
6695 PYTYPE_READY(IterType);
6696 PYTYPE_READY(BufferType);
6697 PYTYPE_READY(RangeType);
6698 PYTYPE_READY(WindowType);
6699 PYTYPE_READY(TabPageType);
6700 PYTYPE_READY(BufMapType);
6701 PYTYPE_READY(WinListType);
6702 PYTYPE_READY(TabListType);
6703 PYTYPE_READY(CurrentType);
6704 PYTYPE_READY(DictionaryType);
6705 PYTYPE_READY(ListType);
6706 PYTYPE_READY(FunctionType);
6707 PYTYPE_READY(OptionsType);
6708 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006709#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006710 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006711#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006712 return 0;
6713}
6714
6715 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006716init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006717{
6718 PyObject *path;
6719 PyObject *path_hook;
6720 PyObject *path_hooks;
6721
6722 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6723 return -1;
6724
6725 if (!(path_hooks = PySys_GetObject("path_hooks")))
6726 {
6727 PyErr_Clear();
6728 path_hooks = PyList_New(1);
6729 PyList_SET_ITEM(path_hooks, 0, path_hook);
6730 if (PySys_SetObject("path_hooks", path_hooks))
6731 {
6732 Py_DECREF(path_hooks);
6733 return -1;
6734 }
6735 Py_DECREF(path_hooks);
6736 }
6737 else if (PyList_Check(path_hooks))
6738 {
6739 if (PyList_Append(path_hooks, path_hook))
6740 {
6741 Py_DECREF(path_hook);
6742 return -1;
6743 }
6744 Py_DECREF(path_hook);
6745 }
6746 else
6747 {
6748 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006749 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006750 "You should now do the following:\n"
6751 "- append vim.path_hook to sys.path_hooks\n"
6752 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006753 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006754 Py_DECREF(path_hook);
6755 return 0;
6756 }
6757
6758 if (!(path = PySys_GetObject("path")))
6759 {
6760 PyErr_Clear();
6761 path = PyList_New(1);
6762 Py_INCREF(vim_special_path_object);
6763 PyList_SET_ITEM(path, 0, vim_special_path_object);
6764 if (PySys_SetObject("path", path))
6765 {
6766 Py_DECREF(path);
6767 return -1;
6768 }
6769 Py_DECREF(path);
6770 }
6771 else if (PyList_Check(path))
6772 {
6773 if (PyList_Append(path, vim_special_path_object))
6774 return -1;
6775 }
6776 else
6777 {
6778 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006779 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006780 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006781 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006782 }
6783
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006784 return 0;
6785}
6786
6787static BufMapObject TheBufferMap =
6788{
6789 PyObject_HEAD_INIT(&BufMapType)
6790};
6791
6792static WinListObject TheWindowList =
6793{
6794 PyObject_HEAD_INIT(&WinListType)
6795 NULL
6796};
6797
6798static CurrentObject TheCurrent =
6799{
6800 PyObject_HEAD_INIT(&CurrentType)
6801};
6802
6803static TabListObject TheTabPageList =
6804{
6805 PyObject_HEAD_INIT(&TabListType)
6806};
6807
6808static struct numeric_constant {
6809 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006810 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006811} numeric_constants[] = {
6812 {"VAR_LOCKED", VAR_LOCKED},
6813 {"VAR_FIXED", VAR_FIXED},
6814 {"VAR_SCOPE", VAR_SCOPE},
6815 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6816};
6817
6818static struct object_constant {
6819 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006820 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006821} object_constants[] = {
6822 {"buffers", (PyObject *)(void *)&TheBufferMap},
6823 {"windows", (PyObject *)(void *)&TheWindowList},
6824 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6825 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006826
6827 {"Buffer", (PyObject *)&BufferType},
6828 {"Range", (PyObject *)&RangeType},
6829 {"Window", (PyObject *)&WindowType},
6830 {"TabPage", (PyObject *)&TabPageType},
6831 {"Dictionary", (PyObject *)&DictionaryType},
6832 {"List", (PyObject *)&ListType},
6833 {"Function", (PyObject *)&FunctionType},
6834 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006835#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006836 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006837#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006838};
6839
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006841 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006842 return -1;
6843
6844#define ADD_CHECKED_OBJECT(m, name, obj) \
6845 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006846 PyObject *valObject = obj; \
6847 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006848 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006849 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850 }
6851
6852 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006853populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006854{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006855 int i;
6856 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006857 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006858 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006859#if PY_VERSION_HEX >= 0x030700f0
6860 PyObject *dict;
6861 PyObject *cls;
6862#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006863
6864 for (i = 0; i < (int)(sizeof(numeric_constants)
6865 / sizeof(struct numeric_constant));
6866 ++i)
6867 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006868 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006869
6870 for (i = 0; i < (int)(sizeof(object_constants)
6871 / sizeof(struct object_constant));
6872 ++i)
6873 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006874 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006875
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006876 valObject = object_constants[i].valObject;
6877 Py_INCREF(valObject);
6878 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006879 }
6880
6881 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6882 return -1;
6883 ADD_OBJECT(m, "error", VimError);
6884
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006885 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6886 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006887 ADD_CHECKED_OBJECT(m, "options",
6888 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006890 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006892 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006893
Bram Moolenaar22081f42016-06-01 20:38:34 +02006894#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006895 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006897#else
6898 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6899 return -1;
6900#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006901 ADD_OBJECT(m, "_getcwd", py_getcwd)
6902
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006903 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006904 return -1;
6905 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006906 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006907 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006908 if (PyObject_SetAttrString(other_module, "chdir", attr))
6909 {
6910 Py_DECREF(attr);
6911 return -1;
6912 }
6913 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006914
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006915 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006916 {
6917 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006918 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006919 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006920 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6921 {
6922 Py_DECREF(attr);
6923 return -1;
6924 }
6925 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006926 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006927 else
6928 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006929
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006930 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6931 return -1;
6932
6933 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6934
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006935#if PY_VERSION_HEX >= 0x030700f0
6936 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6937 return -1;
6938
6939 dict = PyModule_GetDict(imp);
6940
6941 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6942 {
6943 Py_DECREF(imp);
6944 return -1;
6945 }
6946
6947 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6948 {
6949 Py_DECREF(imp);
6950 return -1;
6951 }
6952
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006953 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6954 {
6955 // find_module() is deprecated, this may stop working in some later
6956 // version.
6957 ADD_OBJECT(m, "_find_module", py_find_module);
6958 }
6959
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006960 Py_DECREF(imp);
6961
6962 ADD_OBJECT(m, "_find_spec", py_find_spec);
6963#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006964 if (!(imp = PyImport_ImportModule("imp")))
6965 return -1;
6966
6967 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6968 {
6969 Py_DECREF(imp);
6970 return -1;
6971 }
6972
6973 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6974 {
6975 Py_DECREF(py_find_module);
6976 Py_DECREF(imp);
6977 return -1;
6978 }
6979
6980 Py_DECREF(imp);
6981
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006982 ADD_OBJECT(m, "_find_module", py_find_module);
6983 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006984#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006985
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006986 return 0;
6987}