blob: 7b748b25e444d0070a528bfbe33869b901372499 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaar41009372013-07-01 22:03:04 +020016static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
17
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020018#if PY_VERSION_HEX < 0x02050000
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010019typedef int Py_ssize_t; // Python 2.4 and earlier don't have this type.
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020020#endif
21
Bram Moolenaard518f952020-01-01 15:04:17 +010022// Use values that are known to work, others may make Vim crash.
23#define ENC_OPT (enc_utf8 ? "utf-8" : enc_dbcs ? "euc-jp" : (char *)p_enc)
Bram Moolenaard620aa92013-05-17 16:40:06 +020024#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020025
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020026static const char *vim_special_path = "_vim_path_";
27
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020028#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020029#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020030#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010031#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
32#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
33#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020034
35#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020039#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020040 N_("empty keys are not allowed"))
41#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
42#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
43#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
44#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
45#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
46#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020047#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020049#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020050 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020051 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020053#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
54#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020055#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020056
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020058typedef void (*runner)(const char *, void *
59#ifdef PY_CAN_RECURSE
60 , PyGILState_STATE *
61#endif
62 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064static int ConvertFromPyObject(PyObject *, typval_T *);
65static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020066static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020067static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020068static PyObject *WindowNew(win_T *, tabpage_T *);
69static PyObject *BufferNew (buf_T *);
70static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020071
72static PyInt RangeStart;
73static PyInt RangeEnd;
74
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020075static PyObject *globals;
76
Bram Moolenaarf4258302013-06-02 18:20:17 +020077static PyObject *py_chdir;
78static PyObject *py_fchdir;
79static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020080static PyObject *vim_module;
81static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020082
Bram Moolenaar79a494d2018-07-22 04:30:21 +020083#if PY_VERSION_HEX >= 0x030700f0
84static PyObject *py_find_spec;
85#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020086static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020087#endif
Bram Moolenaarb999ba22019-02-14 13:28:45 +010088static PyObject *py_find_module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +020089
90static PyObject *VimError;
91
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020092/*
93 * obtain a lock on the Vim data structures
94 */
95 static void
96Python_Lock_Vim(void)
97{
98}
99
100/*
101 * release a lock on the Vim data structures
102 */
103 static void
104Python_Release_Vim(void)
105{
106}
107
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200108/*
109 * The "todecref" argument holds a pointer to PyObject * that must be
110 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
111 * was needed to generate returned value is object.
112 *
113 * Use Py_XDECREF to decrement reference count.
114 */
115 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200116StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200117{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200118 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200119
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121 {
122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
124 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 return NULL;
126
127 *todecref = NULL;
128 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200129 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200130 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200131 PyObject *bytes;
132
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 return NULL;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
137 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200138 {
139 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200140 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200142
143 *todecref = bytes;
144 }
145 else
146 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200147#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200148 PyErr_FORMAT(PyExc_TypeError,
149 N_("expected str() or unicode() instance, but got %s"),
150 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected bytes() or str() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200156 return NULL;
157 }
158
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200159 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160}
161
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200162#define NUMBER_LONG 1
163#define NUMBER_INT 2
164#define NUMBER_NATURAL 4
165#define NUMBER_UNSIGNED 8
166
167 static int
168NumberToLong(PyObject *obj, long *result, int flags)
169{
170#if PY_MAJOR_VERSION < 3
171 if (PyInt_Check(obj))
172 {
173 *result = PyInt_AsLong(obj);
174 if (PyErr_Occurred())
175 return -1;
176 }
177 else
178#endif
179 if (PyLong_Check(obj))
180 {
181 *result = PyLong_AsLong(obj);
182 if (PyErr_Occurred())
183 return -1;
184 }
185 else if (PyNumber_Check(obj))
186 {
187 PyObject *num;
188
189 if (!(num = PyNumber_Long(obj)))
190 return -1;
191
192 *result = PyLong_AsLong(num);
193
194 Py_DECREF(num);
195
196 if (PyErr_Occurred())
197 return -1;
198 }
199 else
200 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200201#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200202 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200203 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200204 "coercing to long(), but got %s"),
205 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200206#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200208 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200209 "but got %s"),
210 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200211#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200212 return -1;
213 }
214
215 if (flags & NUMBER_INT)
216 {
217 if (*result > INT_MAX)
218 {
219 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200220 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200221 return -1;
222 }
223 else if (*result < INT_MIN)
224 {
225 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200226 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200227 return -1;
228 }
229 }
230
231 if (flags & NUMBER_NATURAL)
232 {
233 if (*result <= 0)
234 {
235 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100236 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200237 return -1;
238 }
239 }
240 else if (flags & NUMBER_UNSIGNED)
241 {
242 if (*result < 0)
243 {
244 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200245 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200246 return -1;
247 }
248 }
249
250 return 0;
251}
252
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200253 static int
254add_string(PyObject *list, char *s)
255{
256 PyObject *string;
257
258 if (!(string = PyString_FromString(s)))
259 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200260
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200261 if (PyList_Append(list, string))
262 {
263 Py_DECREF(string);
264 return -1;
265 }
266
267 Py_DECREF(string);
268 return 0;
269}
270
271 static PyObject *
272ObjectDir(PyObject *self, char **attributes)
273{
274 PyMethodDef *method;
275 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200276 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200277
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200278 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200279 return NULL;
280
281 if (self)
282 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200283 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200284 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200285 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200286 return NULL;
287 }
288
289 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200290 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200291 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200292 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200293 return NULL;
294 }
295
296#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200297 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200298 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200299 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200300 return NULL;
301 }
302#endif
303
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200304 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200305}
306
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100307// Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100309// Function to write a line, points to either msg() or emsg().
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200310typedef int (*writefn)(char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311
312static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313
314typedef struct
315{
316 PyObject_HEAD
317 long softspace;
318 long error;
319} OutputObject;
320
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200321static char *OutputAttrs[] = {
322 "softspace",
323 NULL
324};
325
326 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200327OutputDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200328{
329 return ObjectDir(self, OutputAttrs);
330}
331
Bram Moolenaar77045652012-09-21 13:46:06 +0200332 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200333OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200334{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200335 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200337 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200338 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200339 return -1;
340 }
341
342 if (strcmp(name, "softspace") == 0)
343 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200344 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200345 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200346 return 0;
347 }
348
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200349 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
351}
352
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100353// Buffer IO, we write one whole line at a time.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200354static garray_T io_ga = {0, 0, 1, 80, NULL};
355static writefn old_fn = NULL;
356
357 static void
358PythonIO_Flush(void)
359{
360 if (old_fn != NULL && io_ga.ga_len > 0)
361 {
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200362 ((char *)io_ga.ga_data)[io_ga.ga_len] = NUL;
363 old_fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200364 }
365 io_ga.ga_len = 0;
366}
367
368 static void
369writer(writefn fn, char_u *str, PyInt n)
370{
371 char_u *ptr;
372
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100373 // Flush when switching output function.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200374 if (fn != old_fn)
375 PythonIO_Flush();
376 old_fn = fn;
377
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200378 // Write each NL separated line. Text after the last NL is kept for
379 // writing later.
380 // For normal messages: Do not output when "got_int" was set. This avoids
381 // a loop gone crazy flooding the terminal with messages. Also for when
382 // "q" is pressed at the more-prompt.
383 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
384 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200393 fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200399 // Put the remaining text into io_ga for later printing.
400 if (n > 0 && (fn == (writefn)emsg || !got_int)
401 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200420 if (error)
421 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100422 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423 Python_Release_Vim();
424 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200425 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200426
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200427 return 0;
428}
429
430 static PyObject *
431OutputWrite(OutputObject *self, PyObject *string)
432{
433 if (write_output(self, string))
434 return NULL;
435
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200436 Py_INCREF(Py_None);
437 return Py_None;
438}
439
440 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200441OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 PyObject *iterator;
444 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200449 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200451 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200452 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200453 Py_DECREF(iterator);
454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 return NULL;
456 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200458 }
459
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200460 Py_DECREF(iterator);
461
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100462 // Iterator may have finished due to an exception
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200463 if (PyErr_Occurred())
464 return NULL;
465
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200466 Py_INCREF(Py_None);
467 return Py_None;
468}
469
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200471AlwaysNone(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100472{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100473 // do nothing
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100474 Py_INCREF(Py_None);
475 return Py_None;
476}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200477#define ALWAYS_NONE AlwaysNone(NULL, NULL)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100478
Bram Moolenaard4247472015-11-02 13:28:59 +0100479 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200480AlwaysFalse(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100481{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100482 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100483 PyObject *ret = Py_False;
484 Py_INCREF(ret);
485 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100486}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200487#define ALWAYS_FALSE AlwaysFalse(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100488
489 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200490AlwaysTrue(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100491{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100492 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100493 PyObject *ret = Py_True;
494 Py_INCREF(ret);
495 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100496}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200497#define ALWAYS_TRUE AlwaysTrue(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100498
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200499/***************/
500
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200501static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100502 // name, function, calling, doc
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200503 {"write", (PyCFunction)OutputWrite, METH_O, ""},
504 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100505 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
506 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
507 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
508 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
509 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
510 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200511 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200512 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200513 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200514};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200515
516static OutputObject Output =
517{
518 PyObject_HEAD_INIT(&OutputType)
519 0,
520 0
521};
522
523static OutputObject Error =
524{
525 PyObject_HEAD_INIT(&OutputType)
526 0,
527 1
528};
529
530 static int
531PythonIO_Init_io(void)
532{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200533 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
534 return -1;
535 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
536 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200537
538 if (PyErr_Occurred())
539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100540 emsg(_("E264: Python: Error initialising I/O objects"));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200541 return -1;
542 }
543
544 return 0;
545}
546
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200547#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200548static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
549
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200550typedef struct
551{
552 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200553 char *fullname;
554 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200555} LoaderObject;
556static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200557
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200558 static void
559LoaderDestructor(LoaderObject *self)
560{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200561 vim_free(self->fullname);
562 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200563 DESTRUCTOR_FINISH(self);
564}
565
566 static PyObject *
567LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
568{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200569 char *fullname = self->fullname;
570 PyObject *result = self->result;
571 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200572
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200573 if (!fullname)
574 {
575 module = result ? result : Py_None;
576 Py_INCREF(module);
577 return module;
578 }
579
580 module = call_load_module(fullname, (int)STRLEN(fullname), result);
581
582 self->fullname = NULL;
583 self->result = module;
584
585 vim_free(fullname);
586 Py_DECREF(result);
587
588 if (!module)
589 {
590 if (PyErr_Occurred())
591 return NULL;
592
593 Py_INCREF(Py_None);
594 return Py_None;
595 }
596
597 Py_INCREF(module);
598 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200599}
600
601static struct PyMethodDef LoaderMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100602 // name, function, calling, doc
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200603 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
604 { NULL, NULL, 0, NULL}
605};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200606#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200607
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100608/*
609 * Check to see whether a Vim error has been reported, or a keyboard
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200610 * interrupt has been detected.
611 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200612 static void
613VimTryStart(void)
614{
615 ++trylevel;
616}
617
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200618 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200619VimTryEnd(void)
620{
621 --trylevel;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100622 // Without this it stops processing all subsequent Vim script commands and
623 // generates strange error messages if I e.g. try calling Test() in a cycle
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200624 did_emsg = FALSE;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100625 // Keyboard interrupt should be preferred over anything else
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200626 if (got_int)
627 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100628 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100629 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100630 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200631 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200632 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200633 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100634 else if (msg_list != NULL && *msg_list != NULL)
635 {
636 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100637 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100638
639 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
640
641 if (msg == NULL)
642 {
643 PyErr_NoMemory();
644 return -1;
645 }
646
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100647 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100648
649 free_global_msglist();
650
651 if (should_free)
652 vim_free(msg);
653
654 return -1;
655 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200656 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200657 return (PyErr_Occurred() ? -1 : 0);
Bram Moolenaar86181df2020-05-11 23:14:04 +0200658 // Python exception is preferred over Vim one; unlikely to occur though
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200659 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200660 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100661 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200662 return -1;
663 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100664 // Finally transform Vim script exception to python one
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 else
666 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200667 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200668 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200669 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200670 }
671}
672
673 static int
674VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200675{
676 if (got_int)
677 {
678 PyErr_SetNone(PyExc_KeyboardInterrupt);
679 return 1;
680 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200681 return 0;
682}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100684// Vim module - Implementation
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200685
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200686 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200687VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200689 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200690 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200691 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692
Bram Moolenaar389a1792013-06-23 13:00:44 +0200693 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200694 return NULL;
695
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200696 Py_BEGIN_ALLOW_THREADS
697 Python_Lock_Vim();
698
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200699 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200700 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701 update_screen(VALID);
702
703 Python_Release_Vim();
704 Py_END_ALLOW_THREADS
705
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200706 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200707 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200708 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200709 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200710
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200711 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200712 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200713 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200714}
715
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200716/*
717 * Function to translate a typval_T into a PyObject; this will recursively
718 * translate lists/dictionaries into their Python equivalents.
719 *
720 * The depth parameter is to avoid infinite recursion, set it to 1 when
721 * you call VimToPython.
722 */
723 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200724VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200725{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200726 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200727 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200728 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200729
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100730 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200731 if (depth > 100)
732 {
733 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200734 ret = Py_None;
735 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736 }
737
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100738 // Check if we run into a recursive loop. The item must be in lookup_dict
739 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200740 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
741 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
742 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200743 sprintf(ptrBuf, "%p",
744 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
745 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200746
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200747 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200748 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200749 Py_INCREF(ret);
750 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200751 }
752 }
753
754 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200755 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200756 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757 else if (our_tv->v_type == VAR_NUMBER)
758 {
759 char buf[NUMBUFLEN];
760
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100761 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200762 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200763 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200764 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100765#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200766 else if (our_tv->v_type == VAR_FLOAT)
767 {
768 char buf[NUMBUFLEN];
769
770 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200771 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200772 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100773#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200774 else if (our_tv->v_type == VAR_LIST)
775 {
776 list_T *list = our_tv->vval.v_list;
777 listitem_T *curr;
778
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 if (list == NULL)
780 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200783 return NULL;
784
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200785 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200786 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200787 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200788 return NULL;
789 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200790
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200791 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200792 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200793 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200794 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200796 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200797 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200799 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 {
801 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200802 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200803 return NULL;
804 }
805 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200806 }
807 }
808 else if (our_tv->v_type == VAR_DICT)
809 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100811 hashtab_T *ht;
812 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200813 hashitem_T *hi;
814 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100815
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200816 if (our_tv->vval.v_dict == NULL)
817 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100818 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200820 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200821 return NULL;
822
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200823 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200825 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200826 return NULL;
827 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200828
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100829 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200830 for (hi = ht->ht_array; todo > 0; ++hi)
831 {
832 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200833 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200834 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200835
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200836 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200837 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200838 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200840 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200842 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200843 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200844 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200845 Py_DECREF(newObj);
846 return NULL;
847 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848 }
849 }
850 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100851 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100852 {
853 if (our_tv->vval.v_number == VVAL_FALSE)
854 {
855 ret = Py_False;
856 Py_INCREF(ret);
857 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100858 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100859 {
860 ret = Py_True;
861 Py_INCREF(ret);
862 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100863 return ret;
864 }
865 else if (our_tv->v_type == VAR_SPECIAL)
866 {
867 Py_INCREF(Py_None);
868 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100869 return ret;
870 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100871 else if (our_tv->v_type == VAR_BLOB)
872 ret = PyBytes_FromStringAndSize(
873 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
874 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200875 else
876 {
877 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200878 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200879 }
880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200882}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200883
884 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200885VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200886{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200887 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200888 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200889 PyObject *string;
890 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200891 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200892 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200893
Bram Moolenaar389a1792013-06-23 13:00:44 +0200894 if (!PyArg_ParseTuple(args, "O", &string))
895 return NULL;
896
897 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200898 return NULL;
899
900 Py_BEGIN_ALLOW_THREADS
901 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200902 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200903 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200904 Python_Release_Vim();
905 Py_END_ALLOW_THREADS
906
Bram Moolenaar389a1792013-06-23 13:00:44 +0200907 Py_XDECREF(todecref);
908
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200909 if (VimTryEnd())
910 return NULL;
911
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200912 if (our_tv == NULL)
913 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200914 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200915 return NULL;
916 }
917
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100918 // Convert the Vim type into a Python type. Create a dictionary that's
919 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200920 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200921 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200922 else
923 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200924 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200925 Py_DECREF(lookup_dict);
926 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200927
928
929 Py_BEGIN_ALLOW_THREADS
930 Python_Lock_Vim();
931 free_tv(our_tv);
932 Python_Release_Vim();
933 Py_END_ALLOW_THREADS
934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200935 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200936}
937
Bram Moolenaardb913952012-06-29 12:54:53 +0200938static PyObject *ConvertToPyObject(typval_T *);
939
940 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200941VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200942{
Bram Moolenaardb913952012-06-29 12:54:53 +0200943 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200944 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200945 char_u *expr;
946 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200947
Bram Moolenaar389a1792013-06-23 13:00:44 +0200948 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200949 return NULL;
950
951 Py_BEGIN_ALLOW_THREADS
952 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200953 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200954 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200955 Python_Release_Vim();
956 Py_END_ALLOW_THREADS
957
Bram Moolenaar389a1792013-06-23 13:00:44 +0200958 Py_XDECREF(todecref);
959
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200960 if (VimTryEnd())
961 return NULL;
962
Bram Moolenaardb913952012-06-29 12:54:53 +0200963 if (our_tv == NULL)
964 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200965 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200966 return NULL;
967 }
968
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200969 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200970 Py_BEGIN_ALLOW_THREADS
971 Python_Lock_Vim();
972 free_tv(our_tv);
973 Python_Release_Vim();
974 Py_END_ALLOW_THREADS
975
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200976 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200977}
978
979 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200980VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200981{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200982 char_u *str;
983 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200984 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200985
Bram Moolenaar389a1792013-06-23 13:00:44 +0200986 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200987 return NULL;
988
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200989 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200990
991 Py_XDECREF(todecref);
992
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200993 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200994}
995
Bram Moolenaarf4258302013-06-02 18:20:17 +0200996 static PyObject *
997_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
998{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200999 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001000 PyObject *newwd;
1001 PyObject *todecref;
1002 char_u *new_dir;
1003
Bram Moolenaard4209d22013-06-05 20:34:15 +02001004 if (_chdir == NULL)
1005 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001006 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001007 return NULL;
1008
1009 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1010 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001011 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001012 return NULL;
1013 }
1014
1015 if (!(new_dir = StringToChars(newwd, &todecref)))
1016 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001017 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001018 Py_DECREF(newwd);
1019 return NULL;
1020 }
1021
1022 VimTryStart();
1023
1024 if (vim_chdir(new_dir))
1025 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001026 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001027 Py_DECREF(newwd);
1028 Py_XDECREF(todecref);
1029
1030 if (VimTryEnd())
1031 return NULL;
1032
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001033 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001034 return NULL;
1035 }
1036
1037 Py_DECREF(newwd);
1038 Py_XDECREF(todecref);
1039
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001040 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001041
1042 if (VimTryEnd())
1043 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001044 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001045 return NULL;
1046 }
1047
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001048 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001049}
1050
1051 static PyObject *
1052VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1053{
1054 return _VimChdir(py_chdir, args, kwargs);
1055}
1056
1057 static PyObject *
1058VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1059{
1060 return _VimChdir(py_fchdir, args, kwargs);
1061}
1062
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001063typedef struct {
1064 PyObject *callable;
1065 PyObject *result;
1066} map_rtp_data;
1067
1068 static void
1069map_rtp_callback(char_u *path, void *_data)
1070{
1071 void **data = (void **) _data;
1072 PyObject *pathObject;
1073 map_rtp_data *mr_data = *((map_rtp_data **) data);
1074
Bram Moolenaar41009372013-07-01 22:03:04 +02001075 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001076 {
1077 *data = NULL;
1078 return;
1079 }
1080
1081 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1082 pathObject, NULL);
1083
1084 Py_DECREF(pathObject);
1085
1086 if (!mr_data->result || mr_data->result != Py_None)
1087 *data = NULL;
1088 else
1089 {
1090 Py_DECREF(mr_data->result);
1091 mr_data->result = NULL;
1092 }
1093}
1094
1095 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001096VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001097{
1098 map_rtp_data data;
1099
Bram Moolenaar389a1792013-06-23 13:00:44 +02001100 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001101 data.result = NULL;
1102
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001103 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001104
1105 if (data.result == NULL)
1106 {
1107 if (PyErr_Occurred())
1108 return NULL;
1109 else
1110 {
1111 Py_INCREF(Py_None);
1112 return Py_None;
1113 }
1114 }
1115 return data.result;
1116}
1117
1118/*
1119 * _vim_runtimepath_ special path implementation.
1120 */
1121
1122 static void
1123map_finder_callback(char_u *path, void *_data)
1124{
1125 void **data = (void **) _data;
1126 PyObject *list = *((PyObject **) data);
1127 PyObject *pathObject1, *pathObject2;
1128 char *pathbuf;
1129 size_t pathlen;
1130
1131 pathlen = STRLEN(path);
1132
1133#if PY_MAJOR_VERSION < 3
1134# define PY_MAIN_DIR_STRING "python2"
1135#else
1136# define PY_MAIN_DIR_STRING "python3"
1137#endif
1138#define PY_ALTERNATE_DIR_STRING "pythonx"
1139
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001140#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001141 if (!(pathbuf = PyMem_New(char,
1142 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1143 {
1144 PyErr_NoMemory();
1145 *data = NULL;
1146 return;
1147 }
1148
1149 mch_memmove(pathbuf, path, pathlen + 1);
1150 add_pathsep((char_u *) pathbuf);
1151
1152 pathlen = STRLEN(pathbuf);
1153 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1154 PYTHONX_STRING_LENGTH + 1);
1155
1156 if (!(pathObject1 = PyString_FromString(pathbuf)))
1157 {
1158 *data = NULL;
1159 PyMem_Free(pathbuf);
1160 return;
1161 }
1162
1163 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1164 PYTHONX_STRING_LENGTH + 1);
1165
1166 if (!(pathObject2 = PyString_FromString(pathbuf)))
1167 {
1168 Py_DECREF(pathObject1);
1169 PyMem_Free(pathbuf);
1170 *data = NULL;
1171 return;
1172 }
1173
1174 PyMem_Free(pathbuf);
1175
1176 if (PyList_Append(list, pathObject1)
1177 || PyList_Append(list, pathObject2))
1178 *data = NULL;
1179
1180 Py_DECREF(pathObject1);
1181 Py_DECREF(pathObject2);
1182}
1183
1184 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001185Vim_GetPaths(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001186{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001187 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001188
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001189 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001190 return NULL;
1191
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001192 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001193
1194 if (PyErr_Occurred())
1195 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001196 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001197 return NULL;
1198 }
1199
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001200 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001201}
1202
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001203#if PY_VERSION_HEX >= 0x030700f0
1204 static PyObject *
1205FinderFindSpec(PyObject *self, PyObject *args)
1206{
1207 char *fullname;
1208 PyObject *paths;
1209 PyObject *target = Py_None;
1210 PyObject *spec;
1211
1212 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1213 return NULL;
1214
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001215 if (!(paths = Vim_GetPaths(self, NULL)))
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001216 return NULL;
1217
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001218 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001219
1220 Py_DECREF(paths);
1221
1222 if (!spec)
1223 {
1224 if (PyErr_Occurred())
1225 return NULL;
1226
1227 Py_INCREF(Py_None);
1228 return Py_None;
1229 }
1230
1231 return spec;
1232}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001233
1234 static PyObject *
1235FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1236{
1237 // Apparently returning None works.
1238 Py_INCREF(Py_None);
1239 return Py_None;
1240}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001241#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001242 static PyObject *
1243call_load_module(char *name, int len, PyObject *find_module_result)
1244{
1245 PyObject *fd, *pathname, *description;
1246
Bram Moolenaarc476e522013-06-23 13:46:40 +02001247 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001248 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001249 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001250 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001251 Py_TYPE_NAME(find_module_result));
1252 return NULL;
1253 }
1254 if (PyTuple_GET_SIZE(find_module_result) != 3)
1255 {
1256 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001257 N_("expected 3-tuple as imp.find_module() result, but got "
1258 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001259 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001260 return NULL;
1261 }
1262
1263 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1264 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1265 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1266 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001267 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001268 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001269 return NULL;
1270 }
1271
1272 return PyObject_CallFunction(py_load_module,
1273 "s#OOO", name, len, fd, pathname, description);
1274}
1275
1276 static PyObject *
1277find_module(char *fullname, char *tail, PyObject *new_path)
1278{
1279 PyObject *find_module_result;
1280 PyObject *module;
1281 char *dot;
1282
Bram Moolenaar41009372013-07-01 22:03:04 +02001283 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001284 {
1285 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001286 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001287 * first component
1288 */
1289 PyObject *newest_path;
1290 int partlen = (int) (dot - 1 - tail);
1291
1292 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1293 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001294 {
1295 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1296 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001297 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001298 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001299
1300 if (!(module = call_load_module(
1301 fullname,
1302 ((int) (tail - fullname)) + partlen,
1303 find_module_result)))
1304 {
1305 Py_DECREF(find_module_result);
1306 return NULL;
1307 }
1308
1309 Py_DECREF(find_module_result);
1310
1311 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1312 {
1313 Py_DECREF(module);
1314 return NULL;
1315 }
1316
1317 Py_DECREF(module);
1318
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001319 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001320
1321 Py_DECREF(newest_path);
1322
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001323 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001324 }
1325 else
1326 {
1327 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1328 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001329 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001330 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1331 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001332 return NULL;
1333 }
1334
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001335 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001336 }
1337}
1338
1339 static PyObject *
1340FinderFindModule(PyObject *self, PyObject *args)
1341{
1342 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001343 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001344 PyObject *new_path;
1345 LoaderObject *loader;
1346
1347 if (!PyArg_ParseTuple(args, "s", &fullname))
1348 return NULL;
1349
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001350 if (!(new_path = Vim_GetPaths(self, NULL)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001351 return NULL;
1352
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001353 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001354
1355 Py_DECREF(new_path);
1356
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001357 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001358 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001359 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001360 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001361
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001362 Py_INCREF(Py_None);
1363 return Py_None;
1364 }
1365
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001366 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001367 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001368 Py_DECREF(result);
1369 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001370 return NULL;
1371 }
1372
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001373 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1374 {
1375 vim_free(fullname);
1376 Py_DECREF(result);
1377 return NULL;
1378 }
1379
1380 loader->fullname = fullname;
1381 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001382
1383 return (PyObject *) loader;
1384}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001385#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001386
1387 static PyObject *
1388VimPathHook(PyObject *self UNUSED, PyObject *args)
1389{
1390 char *path;
1391
1392 if (PyArg_ParseTuple(args, "s", &path)
1393 && STRCMP(path, vim_special_path) == 0)
1394 {
1395 Py_INCREF(vim_module);
1396 return vim_module;
1397 }
1398
1399 PyErr_Clear();
1400 PyErr_SetNone(PyExc_ImportError);
1401 return NULL;
1402}
1403
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001404/*
1405 * Vim module - Definitions
1406 */
1407
1408static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001409 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001410 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001411 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001412 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001413 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001414 {"chdir", (PyCFunction)(void *)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1415 {"fchdir", (PyCFunction)(void *)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001416 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001417#if PY_VERSION_HEX >= 0x030700f0
1418 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001419#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001420 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001421 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1422 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1423 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001424};
1425
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001426/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001427 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001428 */
1429
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001430static PyTypeObject IterType;
1431
1432typedef PyObject *(*nextfun)(void **);
1433typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001434typedef int (*traversefun)(void *, visitproc, void *);
1435typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001436
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001437// Main purpose of this object is removing the need for do python
1438// initialization (i.e. PyType_Ready and setting type attributes) for a big
1439// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001440typedef struct
1441{
1442 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001443 void *cur;
1444 nextfun next;
1445 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001446 traversefun traverse;
1447 clearfun clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001448 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001449} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001450
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001451 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001452IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001453 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001454{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001455 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001456
Bram Moolenaar774267b2013-05-21 20:51:59 +02001457 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001458 self->cur = start;
1459 self->next = next;
1460 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001461 self->traverse = traverse;
1462 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001463 self->iter_object = iter_object;
1464
1465 if (iter_object)
1466 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001467
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001468 return (PyObject *)(self);
1469}
1470
1471 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001472IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001473{
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001474 if (self->iter_object)
1475 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001476 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001477 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001478 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001479}
1480
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001481 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001482IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001483{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001484 if (self->traverse != NULL)
1485 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001486 else
1487 return 0;
1488}
1489
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001490// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001491#ifdef clear
1492# undef clear
1493#endif
1494
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001495 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001496IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001497{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001498 if (self->clear != NULL)
1499 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001500 else
1501 return 0;
1502}
1503
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001504 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001505IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001506{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001507 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001508}
1509
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001510 static PyObject *
1511IterIter(PyObject *self)
1512{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001513 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001514 return self;
1515}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001516
Bram Moolenaardb913952012-06-29 12:54:53 +02001517typedef struct pylinkedlist_S {
1518 struct pylinkedlist_S *pll_next;
1519 struct pylinkedlist_S *pll_prev;
1520 PyObject *pll_obj;
1521} pylinkedlist_T;
1522
1523static pylinkedlist_T *lastdict = NULL;
1524static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001525static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001526
1527 static void
1528pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1529{
1530 if (ref->pll_prev == NULL)
1531 {
1532 if (ref->pll_next == NULL)
1533 {
1534 *last = NULL;
1535 return;
1536 }
1537 }
1538 else
1539 ref->pll_prev->pll_next = ref->pll_next;
1540
1541 if (ref->pll_next == NULL)
1542 *last = ref->pll_prev;
1543 else
1544 ref->pll_next->pll_prev = ref->pll_prev;
1545}
1546
1547 static void
1548pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1549{
1550 if (*last == NULL)
1551 ref->pll_prev = NULL;
1552 else
1553 {
1554 (*last)->pll_next = ref;
1555 ref->pll_prev = *last;
1556 }
1557 ref->pll_next = NULL;
1558 ref->pll_obj = self;
1559 *last = ref;
1560}
1561
1562static PyTypeObject DictionaryType;
1563
1564typedef struct
1565{
1566 PyObject_HEAD
1567 dict_T *dict;
1568 pylinkedlist_T ref;
1569} DictionaryObject;
1570
Bram Moolenaara9922d62013-05-30 13:01:18 +02001571static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1572
1573#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1574
Bram Moolenaardb913952012-06-29 12:54:53 +02001575 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001577{
1578 DictionaryObject *self;
1579
Bram Moolenaara9922d62013-05-30 13:01:18 +02001580 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001581 if (self == NULL)
1582 return NULL;
1583 self->dict = dict;
1584 ++dict->dv_refcount;
1585
1586 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1587
1588 return (PyObject *)(self);
1589}
1590
Bram Moolenaara9922d62013-05-30 13:01:18 +02001591 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001592py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001593{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001594 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001595
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001596 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001597 {
1598 PyErr_NoMemory();
1599 return NULL;
1600 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001601 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001602
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001603 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001604}
1605
1606 static PyObject *
1607DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1608{
1609 DictionaryObject *self;
1610 dict_T *dict;
1611
1612 if (!(dict = py_dict_alloc()))
1613 return NULL;
1614
1615 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1616
1617 --dict->dv_refcount;
1618
1619 if (kwargs || PyTuple_Size(args))
1620 {
1621 PyObject *tmp;
1622 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1623 {
1624 Py_DECREF(self);
1625 return NULL;
1626 }
1627
1628 Py_DECREF(tmp);
1629 }
1630
1631 return (PyObject *)(self);
1632}
1633
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001634 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001635DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001636{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637 pyll_remove(&self->ref, &lastdict);
1638 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001639
1640 DESTRUCTOR_FINISH(self);
1641}
1642
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001643static char *DictionaryAttrs[] = {
1644 "locked", "scope",
1645 NULL
1646};
1647
1648 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001649DictionaryDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001650{
1651 return ObjectDir(self, DictionaryAttrs);
1652}
1653
Bram Moolenaardb913952012-06-29 12:54:53 +02001654 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001655DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001656{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001657 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001658 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001659 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001660 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001661 return -1;
1662 }
1663
1664 if (strcmp(name, "locked") == 0)
1665 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001666 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001667 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001668 PyErr_SET_STRING(PyExc_TypeError,
1669 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001670 return -1;
1671 }
1672 else
1673 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001674 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001675 if (istrue == -1)
1676 return -1;
1677 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001678 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001679 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001680 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001681 }
1682 return 0;
1683 }
1684 else
1685 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001686 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001687 return -1;
1688 }
1689}
1690
1691 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001692DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001693{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001694 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001695}
1696
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697#define DICT_FLAG_HAS_DEFAULT 0x01
1698#define DICT_FLAG_POP 0x02
1699#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001700#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701#define DICT_FLAG_RETURN_PAIR 0x10
1702
Bram Moolenaardb913952012-06-29 12:54:53 +02001703 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001704_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001705{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001706 PyObject *keyObject;
1707 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001708 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001709 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001710 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001711 dict_T *dict = self->dict;
1712 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001713 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001714
Bram Moolenaara9922d62013-05-30 13:01:18 +02001715 if (flags & DICT_FLAG_HAS_DEFAULT)
1716 {
1717 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1718 return NULL;
1719 }
1720 else
1721 keyObject = args;
1722
1723 if (flags & DICT_FLAG_RETURN_BOOL)
1724 defObject = Py_False;
1725
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001726 if (!(key = StringToChars(keyObject, &todecref)))
1727 return NULL;
1728
1729 if (*key == NUL)
1730 {
1731 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001732 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001733 return NULL;
1734 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001735
Bram Moolenaara9922d62013-05-30 13:01:18 +02001736 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001737
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001738 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001739
Bram Moolenaara9922d62013-05-30 13:01:18 +02001740 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001741 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001742 if (defObject)
1743 {
1744 Py_INCREF(defObject);
1745 return defObject;
1746 }
1747 else
1748 {
1749 PyErr_SetObject(PyExc_KeyError, keyObject);
1750 return NULL;
1751 }
1752 }
1753 else if (flags & DICT_FLAG_RETURN_BOOL)
1754 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001755 ret = Py_True;
1756 Py_INCREF(ret);
1757 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001758 }
1759
1760 di = dict_lookup(hi);
1761
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001762 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001763 return NULL;
1764
1765 if (flags & DICT_FLAG_POP)
1766 {
1767 if (dict->dv_lock)
1768 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001769 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001770 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001771 return NULL;
1772 }
1773
1774 hash_remove(&dict->dv_hashtab, hi);
1775 dictitem_free(di);
1776 }
1777
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001778 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001779}
1780
1781 static PyObject *
1782DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1783{
1784 return _DictionaryItem(self, keyObject, 0);
1785}
1786
1787 static int
1788DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1789{
1790 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001791 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001793 if (rObj == NULL)
1794 return -1;
1795
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001796 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001797
Bram Moolenaardee2e312013-06-23 16:35:47 +02001798 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001799
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001800 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001801}
1802
1803typedef struct
1804{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001805 int dii_changed;
1806 hashtab_T *dii_ht;
1807 hashitem_T *dii_hi;
1808 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001809} dictiterinfo_T;
1810
1811 static PyObject *
1812DictionaryIterNext(dictiterinfo_T **dii)
1813{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001814 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001815
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001816 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001817 return NULL;
1818
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001819 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001820 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001821 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001822 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001823 return NULL;
1824 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001825
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001826 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
1827 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001828
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001829 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001830
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001831 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001832 return NULL;
1833
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001834 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001835}
1836
1837 static PyObject *
1838DictionaryIter(DictionaryObject *self)
1839{
1840 dictiterinfo_T *dii;
1841 hashtab_T *ht;
1842
1843 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1844 {
1845 PyErr_NoMemory();
1846 return NULL;
1847 }
1848
1849 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001850 dii->dii_changed = ht->ht_changed;
1851 dii->dii_ht = ht;
1852 dii->dii_hi = ht->ht_array;
1853 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001854
1855 return IterNew(dii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001856 (destructorfun)(void *) PyMem_Free, (nextfun) DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001857 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001858}
1859
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001860 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001861DictionaryAssItem(
1862 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001863{
1864 char_u *key;
1865 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001866 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001867 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001868 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001869
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001870 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001871 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001872 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001873 return -1;
1874 }
1875
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001876 if (!(key = StringToChars(keyObject, &todecref)))
1877 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001878
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001879 if (*key == NUL)
1880 {
1881 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001882 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001883 return -1;
1884 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001885
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001886 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001887
1888 if (valObject == NULL)
1889 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001890 hashitem_T *hi;
1891
Bram Moolenaardb913952012-06-29 12:54:53 +02001892 if (di == NULL)
1893 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001894 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001895 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001896 return -1;
1897 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001898 hi = hash_find(&dict->dv_hashtab, di->di_key);
1899 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001900 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001901 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001902 return 0;
1903 }
1904
1905 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001906 {
1907 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001908 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001909 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001910
1911 if (di == NULL)
1912 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001913 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001914 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001915 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001916 PyErr_NoMemory();
1917 return -1;
1918 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001919 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001920
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001921 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001922 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001923 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001924 RAISE_KEY_ADD_FAIL(key);
1925 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001926 return -1;
1927 }
1928 }
1929 else
1930 clear_tv(&di->di_tv);
1931
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001932 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001933
1934 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001935 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001936 return 0;
1937}
1938
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939typedef PyObject *(*hi_to_py)(hashitem_T *);
1940
Bram Moolenaardb913952012-06-29 12:54:53 +02001941 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001942DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001943{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001944 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001945 long_u todo = dict->dv_hashtab.ht_used;
1946 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001947 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001948 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001950
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001951 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001952 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1953 {
1954 if (!HASHITEM_EMPTY(hi))
1955 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001956 if (!(newObj = hiconvert(hi)))
1957 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001958 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001959 return NULL;
1960 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001961 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001962 --todo;
1963 ++i;
1964 }
1965 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001966 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001967}
1968
Bram Moolenaara9922d62013-05-30 13:01:18 +02001969 static PyObject *
1970dict_key(hashitem_T *hi)
1971{
1972 return PyBytes_FromString((char *)(hi->hi_key));
1973}
1974
1975 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001976DictionaryListKeys(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001977{
1978 return DictionaryListObjects(self, dict_key);
1979}
1980
1981 static PyObject *
1982dict_val(hashitem_T *hi)
1983{
1984 dictitem_T *di;
1985
1986 di = dict_lookup(hi);
1987 return ConvertToPyObject(&di->di_tv);
1988}
1989
1990 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001991DictionaryListValues(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001992{
1993 return DictionaryListObjects(self, dict_val);
1994}
1995
1996 static PyObject *
1997dict_item(hashitem_T *hi)
1998{
1999 PyObject *keyObject;
2000 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002001 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002002
2003 if (!(keyObject = dict_key(hi)))
2004 return NULL;
2005
2006 if (!(valObject = dict_val(hi)))
2007 {
2008 Py_DECREF(keyObject);
2009 return NULL;
2010 }
2011
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002012 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002013
2014 Py_DECREF(keyObject);
2015 Py_DECREF(valObject);
2016
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002017 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002018}
2019
2020 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002021DictionaryListItems(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002022{
2023 return DictionaryListObjects(self, dict_item);
2024}
2025
2026 static PyObject *
2027DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2028{
2029 dict_T *dict = self->dict;
2030
2031 if (dict->dv_lock)
2032 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002033 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002034 return NULL;
2035 }
2036
2037 if (kwargs)
2038 {
2039 typval_T tv;
2040
2041 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2042 return NULL;
2043
2044 VimTryStart();
2045 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2046 clear_tv(&tv);
2047 if (VimTryEnd())
2048 return NULL;
2049 }
2050 else
2051 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002052 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002053
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002054 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 return NULL;
2056
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002057 if (obj == NULL)
2058 {
2059 Py_INCREF(Py_None);
2060 return Py_None;
2061 }
2062
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002063 if (PyObject_HasAttrString(obj, "keys"))
2064 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002065 else
2066 {
2067 PyObject *iterator;
2068 PyObject *item;
2069
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002070 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002071 return NULL;
2072
2073 while ((item = PyIter_Next(iterator)))
2074 {
2075 PyObject *fast;
2076 PyObject *keyObject;
2077 PyObject *valObject;
2078 PyObject *todecref;
2079 char_u *key;
2080 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002081 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002082
2083 if (!(fast = PySequence_Fast(item, "")))
2084 {
2085 Py_DECREF(iterator);
2086 Py_DECREF(item);
2087 return NULL;
2088 }
2089
2090 Py_DECREF(item);
2091
2092 if (PySequence_Fast_GET_SIZE(fast) != 2)
2093 {
2094 Py_DECREF(iterator);
2095 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002096 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002097 N_("expected sequence element of size 2, "
2098 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002099 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002100 return NULL;
2101 }
2102
2103 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2104
2105 if (!(key = StringToChars(keyObject, &todecref)))
2106 {
2107 Py_DECREF(iterator);
2108 Py_DECREF(fast);
2109 return NULL;
2110 }
2111
2112 di = dictitem_alloc(key);
2113
2114 Py_XDECREF(todecref);
2115
2116 if (di == NULL)
2117 {
2118 Py_DECREF(fast);
2119 Py_DECREF(iterator);
2120 PyErr_NoMemory();
2121 return NULL;
2122 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002123 di->di_tv.v_type = VAR_UNKNOWN;
2124
2125 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2126
2127 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2128 {
2129 Py_DECREF(iterator);
2130 Py_DECREF(fast);
2131 dictitem_free(di);
2132 return NULL;
2133 }
2134
2135 Py_DECREF(fast);
2136
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002137 hi = hash_find(&dict->dv_hashtab, di->di_key);
2138 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002139 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002140 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002141 Py_DECREF(iterator);
2142 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002143 return NULL;
2144 }
2145 }
2146
2147 Py_DECREF(iterator);
2148
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002149 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002150 if (PyErr_Occurred())
2151 return NULL;
2152 }
2153 }
2154 Py_INCREF(Py_None);
2155 return Py_None;
2156}
2157
2158 static PyObject *
2159DictionaryGet(DictionaryObject *self, PyObject *args)
2160{
2161 return _DictionaryItem(self, args,
2162 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2163}
2164
2165 static PyObject *
2166DictionaryPop(DictionaryObject *self, PyObject *args)
2167{
2168 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2169}
2170
2171 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002172DictionaryPopItem(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002173{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002174 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002175 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002176 PyObject *valObject;
2177 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002178
Bram Moolenaarde71b562013-06-02 17:41:54 +02002179 if (self->dict->dv_hashtab.ht_used == 0)
2180 {
2181 PyErr_SetNone(PyExc_KeyError);
2182 return NULL;
2183 }
2184
2185 hi = self->dict->dv_hashtab.ht_array;
2186 while (HASHITEM_EMPTY(hi))
2187 ++hi;
2188
2189 di = dict_lookup(hi);
2190
2191 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002192 return NULL;
2193
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002194 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002195 {
2196 Py_DECREF(valObject);
2197 return NULL;
2198 }
2199
2200 hash_remove(&self->dict->dv_hashtab, hi);
2201 dictitem_free(di);
2202
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002203 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002204}
2205
2206 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002207DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002208{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002209 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2210}
2211
2212static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002213 0, // sq_length
2214 0, // sq_concat
2215 0, // sq_repeat
2216 0, // sq_item
2217 0, // sq_slice
2218 0, // sq_ass_item
2219 0, // sq_ass_slice
2220 (objobjproc) DictionaryContains, // sq_contains
2221 0, // sq_inplace_concat
2222 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002223};
2224
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002225static PyMappingMethods DictionaryAsMapping = {
2226 (lenfunc) DictionaryLength,
2227 (binaryfunc) DictionaryItem,
2228 (objobjargproc) DictionaryAssItem,
2229};
2230
Bram Moolenaardb913952012-06-29 12:54:53 +02002231static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002232 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002233 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2234 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002235 {"update", (PyCFunction)(void *)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002236 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2237 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002238 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002239 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002240 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2241 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002242};
2243
2244static PyTypeObject ListType;
2245
2246typedef struct
2247{
2248 PyObject_HEAD
2249 list_T *list;
2250 pylinkedlist_T ref;
2251} ListObject;
2252
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002253#define NEW_LIST(list) ListNew(&ListType, list)
2254
Bram Moolenaardb913952012-06-29 12:54:53 +02002255 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002256ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002257{
2258 ListObject *self;
2259
Bram Moolenaarab589462020-07-06 21:03:06 +02002260 if (list == NULL)
2261 return NULL;
2262
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002263 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002264 if (self == NULL)
2265 return NULL;
2266 self->list = list;
2267 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002268 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002269
2270 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2271
2272 return (PyObject *)(self);
2273}
2274
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002275 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002276py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002277{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002278 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002280 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002281 {
2282 PyErr_NoMemory();
2283 return NULL;
2284 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002285 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002286
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002287 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002288}
2289
2290 static int
2291list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2292{
2293 PyObject *iterator;
2294 PyObject *item;
2295 listitem_T *li;
2296
2297 if (!(iterator = PyObject_GetIter(obj)))
2298 return -1;
2299
2300 while ((item = PyIter_Next(iterator)))
2301 {
2302 if (!(li = listitem_alloc()))
2303 {
2304 PyErr_NoMemory();
2305 Py_DECREF(item);
2306 Py_DECREF(iterator);
2307 return -1;
2308 }
2309 li->li_tv.v_lock = 0;
2310 li->li_tv.v_type = VAR_UNKNOWN;
2311
2312 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2313 {
2314 Py_DECREF(item);
2315 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002317 return -1;
2318 }
2319
2320 Py_DECREF(item);
2321
2322 list_append(l, li);
2323 }
2324
2325 Py_DECREF(iterator);
2326
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002327 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002328 if (PyErr_Occurred())
2329 return -1;
2330
2331 return 0;
2332}
2333
2334 static PyObject *
2335ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2336{
2337 list_T *list;
2338 PyObject *obj = NULL;
2339
2340 if (kwargs)
2341 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002342 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002343 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002344 return NULL;
2345 }
2346
2347 if (!PyArg_ParseTuple(args, "|O", &obj))
2348 return NULL;
2349
2350 if (!(list = py_list_alloc()))
2351 return NULL;
2352
2353 if (obj)
2354 {
2355 PyObject *lookup_dict;
2356
2357 if (!(lookup_dict = PyDict_New()))
2358 {
2359 list_unref(list);
2360 return NULL;
2361 }
2362
2363 if (list_py_concat(list, obj, lookup_dict) == -1)
2364 {
2365 Py_DECREF(lookup_dict);
2366 list_unref(list);
2367 return NULL;
2368 }
2369
2370 Py_DECREF(lookup_dict);
2371 }
2372
2373 return ListNew(subtype, list);
2374}
2375
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002376 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002377ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002378{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002379 pyll_remove(&self->ref, &lastlist);
2380 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002381
2382 DESTRUCTOR_FINISH(self);
2383}
2384
Bram Moolenaardb913952012-06-29 12:54:53 +02002385 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002386ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002387{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002388 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002389}
2390
2391 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002392ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002393{
2394 listitem_T *li;
2395
Bram Moolenaard6e39182013-05-21 18:30:34 +02002396 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002397 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002398 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002399 return NULL;
2400 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002401 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002402 if (li == NULL)
2403 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002404 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002405 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002406 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002407 return NULL;
2408 }
2409 return ConvertToPyObject(&li->li_tv);
2410}
2411
Bram Moolenaardb913952012-06-29 12:54:53 +02002412 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002413ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2414 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002415{
2416 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002417 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002418
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002419 if (step == 0)
2420 {
2421 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2422 return NULL;
2423 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002424
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002425 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002426 if (list == NULL)
2427 return NULL;
2428
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002429 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002430 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002431 PyObject *item;
2432
2433 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002434 if (item == NULL)
2435 {
2436 Py_DECREF(list);
2437 return NULL;
2438 }
2439
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002440 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002441 }
2442
2443 return list;
2444}
2445
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002446 static PyObject *
2447ListItem(ListObject *self, PyObject* idx)
2448{
2449#if PY_MAJOR_VERSION < 3
2450 if (PyInt_Check(idx))
2451 {
2452 long _idx = PyInt_AsLong(idx);
2453 return ListIndex(self, _idx);
2454 }
2455 else
2456#endif
2457 if (PyLong_Check(idx))
2458 {
2459 long _idx = PyLong_AsLong(idx);
2460 return ListIndex(self, _idx);
2461 }
2462 else if (PySlice_Check(idx))
2463 {
2464 Py_ssize_t start, stop, step, slicelen;
2465
Bram Moolenaar922a4662014-03-30 16:11:43 +02002466 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002467 &start, &stop, &step, &slicelen) < 0)
2468 return NULL;
2469 return ListSlice(self, start, step, slicelen);
2470 }
2471 else
2472 {
2473 RAISE_INVALID_INDEX_TYPE(idx);
2474 return NULL;
2475 }
2476}
2477
2478 static void
2479list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2480 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2481{
2482 while (numreplaced--)
2483 {
2484 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2485 listitem_remove(l, lis[slicelen + numreplaced]);
2486 }
2487 while (numadded--)
2488 {
2489 listitem_T *next;
2490
2491 next = lastaddedli->li_prev;
2492 listitem_remove(l, lastaddedli);
2493 lastaddedli = next;
2494 }
2495}
2496
2497 static int
2498ListAssSlice(ListObject *self, Py_ssize_t first,
2499 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2500{
2501 PyObject *iterator;
2502 PyObject *item;
2503 listitem_T *li;
2504 listitem_T *lastaddedli = NULL;
2505 listitem_T *next;
2506 typval_T v;
2507 list_T *l = self->list;
2508 PyInt i;
2509 PyInt j;
2510 PyInt numreplaced = 0;
2511 PyInt numadded = 0;
2512 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002513 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002514
2515 size = ListLength(self);
2516
2517 if (l->lv_lock)
2518 {
2519 RAISE_LOCKED_LIST;
2520 return -1;
2521 }
2522
2523 if (step == 0)
2524 {
2525 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2526 return -1;
2527 }
2528
2529 if (step != 1 && slicelen == 0)
2530 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002531 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002532 int ret = 0;
2533
2534 if (obj == NULL)
2535 return 0;
2536
2537 if (!(iterator = PyObject_GetIter(obj)))
2538 return -1;
2539
2540 if ((item = PyIter_Next(iterator)))
2541 {
2542 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002543 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002544 "to extended slice"), 0);
2545 Py_DECREF(item);
2546 ret = -1;
2547 }
2548 Py_DECREF(iterator);
2549 return ret;
2550 }
2551
2552 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002553 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002554 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2555 {
2556 PyErr_NoMemory();
2557 return -1;
2558 }
2559
2560 if (first == size)
2561 li = NULL;
2562 else
2563 {
2564 li = list_find(l, (long) first);
2565 if (li == NULL)
2566 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002567 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002568 (int)first);
2569 if (obj != NULL)
2570 PyMem_Free(lis);
2571 return -1;
2572 }
2573 i = slicelen;
2574 while (i-- && li != NULL)
2575 {
2576 j = step;
2577 next = li;
2578 if (step > 0)
2579 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2580 else
2581 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2582
2583 if (obj == NULL)
2584 listitem_remove(l, li);
2585 else
2586 lis[slicelen - i - 1] = li;
2587
2588 li = next;
2589 }
2590 if (li == NULL && i != -1)
2591 {
2592 PyErr_SET_VIM(N_("internal error: not enough list items"));
2593 if (obj != NULL)
2594 PyMem_Free(lis);
2595 return -1;
2596 }
2597 }
2598
2599 if (obj == NULL)
2600 return 0;
2601
2602 if (!(iterator = PyObject_GetIter(obj)))
2603 {
2604 PyMem_Free(lis);
2605 return -1;
2606 }
2607
2608 i = 0;
2609 while ((item = PyIter_Next(iterator)))
2610 {
2611 if (ConvertFromPyObject(item, &v) == -1)
2612 {
2613 Py_DECREF(iterator);
2614 Py_DECREF(item);
2615 PyMem_Free(lis);
2616 return -1;
2617 }
2618 Py_DECREF(item);
2619 if (list_insert_tv(l, &v, numreplaced < slicelen
2620 ? lis[numreplaced]
2621 : li) == FAIL)
2622 {
2623 clear_tv(&v);
2624 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2625 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2626 PyMem_Free(lis);
2627 return -1;
2628 }
2629 if (numreplaced < slicelen)
2630 {
2631 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002632 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002633 numreplaced++;
2634 }
2635 else
2636 {
2637 if (li)
2638 lastaddedli = li->li_prev;
2639 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002640 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002641 numadded++;
2642 }
2643 clear_tv(&v);
2644 if (step != 1 && i >= slicelen)
2645 {
2646 Py_DECREF(iterator);
2647 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002648 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002649 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002650 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2651 PyMem_Free(lis);
2652 return -1;
2653 }
2654 ++i;
2655 }
2656 Py_DECREF(iterator);
2657
2658 if (step != 1 && i != slicelen)
2659 {
2660 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002661 N_("attempt to assign sequence of size %d to extended slice "
2662 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002663 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2664 PyMem_Free(lis);
2665 return -1;
2666 }
2667
2668 if (PyErr_Occurred())
2669 {
2670 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2671 PyMem_Free(lis);
2672 return -1;
2673 }
2674
2675 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002677 if (step == 1)
2678 for (i = numreplaced; i < slicelen; i++)
2679 listitem_remove(l, lis[i]);
2680
2681 PyMem_Free(lis);
2682
2683 return 0;
2684}
2685
2686 static int
2687ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2688{
2689 typval_T tv;
2690 list_T *l = self->list;
2691 listitem_T *li;
2692 Py_ssize_t length = ListLength(self);
2693
2694 if (l->lv_lock)
2695 {
2696 RAISE_LOCKED_LIST;
2697 return -1;
2698 }
2699 if (index > length || (index == length && obj == NULL))
2700 {
2701 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2702 return -1;
2703 }
2704
2705 if (obj == NULL)
2706 {
2707 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002708 if (li == NULL)
2709 {
2710 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2711 "list item %d"), (int) index);
2712 return -1;
2713 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002714 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002715 clear_tv(&li->li_tv);
2716 vim_free(li);
2717 return 0;
2718 }
2719
2720 if (ConvertFromPyObject(obj, &tv) == -1)
2721 return -1;
2722
2723 if (index == length)
2724 {
2725 if (list_append_tv(l, &tv) == FAIL)
2726 {
2727 clear_tv(&tv);
2728 PyErr_SET_VIM(N_("failed to add item to list"));
2729 return -1;
2730 }
2731 }
2732 else
2733 {
2734 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002735 if (li == NULL)
2736 {
2737 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2738 "list item %d"), (int) index);
2739 return -1;
2740 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002741 clear_tv(&li->li_tv);
2742 copy_tv(&tv, &li->li_tv);
2743 clear_tv(&tv);
2744 }
2745 return 0;
2746}
2747
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002748 static int
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002749ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2750{
2751#if PY_MAJOR_VERSION < 3
2752 if (PyInt_Check(idx))
2753 {
2754 long _idx = PyInt_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002755 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002756 }
2757 else
2758#endif
2759 if (PyLong_Check(idx))
2760 {
2761 long _idx = PyLong_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002762 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002763 }
2764 else if (PySlice_Check(idx))
2765 {
2766 Py_ssize_t start, stop, step, slicelen;
2767
Bram Moolenaar922a4662014-03-30 16:11:43 +02002768 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002769 &start, &stop, &step, &slicelen) < 0)
2770 return -1;
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002771 return (int)ListAssSlice(self, start, step, slicelen,
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002772 obj);
2773 }
2774 else
2775 {
2776 RAISE_INVALID_INDEX_TYPE(idx);
2777 return -1;
2778 }
2779}
2780
2781 static PyObject *
2782ListConcatInPlace(ListObject *self, PyObject *obj)
2783{
2784 list_T *l = self->list;
2785 PyObject *lookup_dict;
2786
2787 if (l->lv_lock)
2788 {
2789 RAISE_LOCKED_LIST;
2790 return NULL;
2791 }
2792
2793 if (!(lookup_dict = PyDict_New()))
2794 return NULL;
2795
2796 if (list_py_concat(l, obj, lookup_dict) == -1)
2797 {
2798 Py_DECREF(lookup_dict);
2799 return NULL;
2800 }
2801 Py_DECREF(lookup_dict);
2802
2803 Py_INCREF(self);
2804 return (PyObject *)(self);
2805}
2806
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002807typedef struct
2808{
2809 listwatch_T lw;
2810 list_T *list;
2811} listiterinfo_T;
2812
2813 static void
2814ListIterDestruct(listiterinfo_T *lii)
2815{
2816 list_rem_watch(lii->list, &lii->lw);
2817 PyMem_Free(lii);
2818}
2819
2820 static PyObject *
2821ListIterNext(listiterinfo_T **lii)
2822{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002823 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002824
2825 if (!((*lii)->lw.lw_item))
2826 return NULL;
2827
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002828 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002829 return NULL;
2830
2831 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2832
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002833 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002834}
2835
2836 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002837ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002838{
2839 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002840 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002841
2842 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2843 {
2844 PyErr_NoMemory();
2845 return NULL;
2846 }
2847
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002848 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002849 list_add_watch(l, &lii->lw);
2850 lii->lw.lw_item = l->lv_first;
2851 lii->list = l;
2852
2853 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002854 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002855 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002856}
2857
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002858static char *ListAttrs[] = {
2859 "locked",
2860 NULL
2861};
2862
2863 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002864ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002865{
2866 return ObjectDir(self, ListAttrs);
2867}
2868
Bram Moolenaar66b79852012-09-21 14:00:35 +02002869 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002870ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002871{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002872 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002873 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002874 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002875 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002876 return -1;
2877 }
2878
2879 if (strcmp(name, "locked") == 0)
2880 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002881 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002882 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002883 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002884 return -1;
2885 }
2886 else
2887 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002888 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002889 if (istrue == -1)
2890 return -1;
2891 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002892 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002893 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002894 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002895 }
2896 return 0;
2897 }
2898 else
2899 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002900 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002901 return -1;
2902 }
2903}
2904
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002905static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002906 (lenfunc) ListLength, // sq_length, len(x)
2907 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2908 0, // RangeRepeat, sq_repeat, x*n
2909 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2910 0, // was_sq_slice, x[i:j]
2911 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2912 0, // was_sq_ass_slice, x[i:j]=v
2913 0, // sq_contains
2914 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2915 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002916};
2917
2918static PyMappingMethods ListAsMapping = {
2919 /* mp_length */ (lenfunc) ListLength,
2920 /* mp_subscript */ (binaryfunc) ListItem,
2921 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2922};
2923
Bram Moolenaardb913952012-06-29 12:54:53 +02002924static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002925 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2926 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2927 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002928};
2929
2930typedef struct
2931{
2932 PyObject_HEAD
2933 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002934 int argc;
2935 typval_T *argv;
2936 dict_T *self;
2937 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002938 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002939} FunctionObject;
2940
2941static PyTypeObject FunctionType;
2942
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002943#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2944 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002945
Bram Moolenaardb913952012-06-29 12:54:53 +02002946 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002947FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002948 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002949{
2950 FunctionObject *self;
2951
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002952 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002953 if (self == NULL)
2954 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002955
2956 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002957 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002958 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002959 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002960 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002961 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002962 return NULL;
2963 }
2964 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002965 }
2966 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002967 {
2968 char_u *p;
2969
2970 if ((p = get_expanded_name(name,
2971 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002972 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002973 PyErr_FORMAT(PyExc_ValueError,
2974 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002975 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002976 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002977
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002978 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2979 {
2980 char_u *np;
2981 size_t len = STRLEN(p) + 1;
2982
Bram Moolenaar51e14382019-05-25 20:21:28 +02002983 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002984 {
2985 vim_free(p);
2986 return NULL;
2987 }
2988 mch_memmove(np, "<SNR>", 5);
2989 mch_memmove(np + 5, p + 3, len - 3);
2990 vim_free(p);
2991 self->name = np;
2992 }
2993 else
2994 self->name = p;
2995 }
2996
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002997 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002998 self->argc = argc;
2999 self->argv = argv;
3000 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003001 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003002
3003 if (self->argv || self->self)
3004 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3005
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003006 return (PyObject *)(self);
3007}
3008
3009 static PyObject *
3010FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3011{
3012 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003013 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003014 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003015 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003016 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003017 typval_T selfdicttv;
3018 typval_T argstv;
3019 list_T *argslist = NULL;
3020 dict_T *selfdict = NULL;
3021 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003022 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003023 typval_T *argv = NULL;
3024 typval_T *curtv;
3025 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003026
Bram Moolenaar8110a092016-04-14 15:56:09 +02003027 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003028 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003029 selfdictObject = PyDict_GetItemString(kwargs, "self");
3030 if (selfdictObject != NULL)
3031 {
3032 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3033 return NULL;
3034 selfdict = selfdicttv.vval.v_dict;
3035 }
3036 argsObject = PyDict_GetItemString(kwargs, "args");
3037 if (argsObject != NULL)
3038 {
3039 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3040 {
3041 dict_unref(selfdict);
3042 return NULL;
3043 }
3044 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003045 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003046
3047 argc = argslist->lv_len;
3048 if (argc != 0)
3049 {
3050 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003051 if (argv == NULL)
3052 {
3053 PyErr_NoMemory();
3054 dict_unref(selfdict);
3055 list_unref(argslist);
3056 return NULL;
3057 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003058 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003059 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003060 copy_tv(&li->li_tv, curtv++);
3061 }
3062 list_unref(argslist);
3063 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003064 if (selfdict != NULL)
3065 {
3066 auto_rebind = FALSE;
3067 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3068 if (autoRebindObject != NULL)
3069 {
3070 auto_rebind = PyObject_IsTrue(autoRebindObject);
3071 if (auto_rebind == -1)
3072 {
3073 dict_unref(selfdict);
3074 list_unref(argslist);
3075 return NULL;
3076 }
3077 }
3078 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003079 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003080
Bram Moolenaar389a1792013-06-23 13:00:44 +02003081 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003082 {
3083 dict_unref(selfdict);
3084 while (argc--)
3085 clear_tv(&argv[argc]);
3086 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003087 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003088 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003089
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003090 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003091
Bram Moolenaar389a1792013-06-23 13:00:44 +02003092 PyMem_Free(name);
3093
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003094 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003095}
3096
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003097 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003098FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003099{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003100 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003101 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003102 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003103 for (i = 0; i < self->argc; ++i)
3104 clear_tv(&self->argv[i]);
3105 PyMem_Free(self->argv);
3106 dict_unref(self->self);
3107 if (self->argv || self->self)
3108 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003109
3110 DESTRUCTOR_FINISH(self);
3111}
3112
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003113static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003114 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003115 NULL
3116};
3117
3118 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003119FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003120{
3121 return ObjectDir(self, FunctionAttrs);
3122}
3123
Bram Moolenaardb913952012-06-29 12:54:53 +02003124 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003125FunctionAttr(FunctionObject *self, char *name)
3126{
3127 list_T *list;
3128 int i;
3129 if (strcmp(name, "name") == 0)
3130 return PyString_FromString((char *)(self->name));
3131 else if (strcmp(name, "args") == 0)
3132 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003133 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003134 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003135
Bram Moolenaar8110a092016-04-14 15:56:09 +02003136 for (i = 0; i < self->argc; ++i)
3137 list_append_tv(list, &self->argv[i]);
3138 return NEW_LIST(list);
3139 }
3140 else if (strcmp(name, "self") == 0)
3141 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003142 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003143 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003144 else if (strcmp(name, "auto_rebind") == 0)
3145 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003146 ? ALWAYS_TRUE
3147 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003148 else if (strcmp(name, "__members__") == 0)
3149 return ObjectDir(NULL, FunctionAttrs);
3150 return NULL;
3151}
3152
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003153/*
3154 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003155 *
3156 * "exported" should be set to true when it is needed to construct a partial
3157 * that may be stored in a variable (i.e. may be freed by Vim).
3158 */
3159 static void
3160set_partial(FunctionObject *self, partial_T *pt, int exported)
3161{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003162 int i;
3163
3164 pt->pt_name = self->name;
3165 if (self->argv)
3166 {
3167 pt->pt_argc = self->argc;
3168 if (exported)
3169 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003170 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003171 for (i = 0; i < pt->pt_argc; ++i)
3172 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3173 }
3174 else
3175 pt->pt_argv = self->argv;
3176 }
3177 else
3178 {
3179 pt->pt_argc = 0;
3180 pt->pt_argv = NULL;
3181 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003182 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003183 pt->pt_dict = self->self;
3184 if (exported && self->self)
3185 ++pt->pt_dict->dv_refcount;
3186 if (exported)
3187 pt->pt_name = vim_strsave(pt->pt_name);
3188 pt->pt_refcount = 1;
3189}
3190
3191 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003192FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003193{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003194 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003195 typval_T args;
3196 typval_T selfdicttv;
3197 typval_T rettv;
3198 dict_T *selfdict = NULL;
3199 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003200 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003201 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003202 partial_T pt;
3203 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003204
Bram Moolenaar8110a092016-04-14 15:56:09 +02003205 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003206 return NULL;
3207
3208 if (kwargs != NULL)
3209 {
3210 selfdictObject = PyDict_GetItemString(kwargs, "self");
3211 if (selfdictObject != NULL)
3212 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003213 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003214 {
3215 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003216 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003217 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003218 selfdict = selfdicttv.vval.v_dict;
3219 }
3220 }
3221
Bram Moolenaar8110a092016-04-14 15:56:09 +02003222 if (self->argv || self->self)
3223 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003224 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003225 set_partial(self, &pt, FALSE);
3226 pt_ptr = &pt;
3227 }
3228
Bram Moolenaar71700b82013-05-15 17:49:05 +02003229 Py_BEGIN_ALLOW_THREADS
3230 Python_Lock_Vim();
3231
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003232 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003233 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003234
3235 Python_Release_Vim();
3236 Py_END_ALLOW_THREADS
3237
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003238 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003239 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003240 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003241 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003242 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003243 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003244 }
3245 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003246 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003247
Bram Moolenaardb913952012-06-29 12:54:53 +02003248 clear_tv(&args);
3249 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003250 if (selfdict != NULL)
3251 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003252
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003253 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003254}
3255
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003256 static PyObject *
3257FunctionRepr(FunctionObject *self)
3258{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003259 PyObject *ret;
3260 garray_T repr_ga;
3261 int i;
3262 char_u *tofree = NULL;
3263 typval_T tv;
3264 char_u numbuf[NUMBUFLEN];
3265
3266 ga_init2(&repr_ga, (int)sizeof(char), 70);
3267 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3268 if (self->name)
3269 ga_concat(&repr_ga, self->name);
3270 else
3271 ga_concat(&repr_ga, (char_u *)"<NULL>");
3272 ga_append(&repr_ga, '\'');
3273 if (self->argv)
3274 {
3275 ga_concat(&repr_ga, (char_u *)", args=[");
3276 ++emsg_silent;
3277 for (i = 0; i < self->argc; i++)
3278 {
3279 if (i != 0)
3280 ga_concat(&repr_ga, (char_u *)", ");
3281 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3282 get_copyID()));
3283 vim_free(tofree);
3284 }
3285 --emsg_silent;
3286 ga_append(&repr_ga, ']');
3287 }
3288 if (self->self)
3289 {
3290 ga_concat(&repr_ga, (char_u *)", self=");
3291 tv.v_type = VAR_DICT;
3292 tv.vval.v_dict = self->self;
3293 ++emsg_silent;
3294 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3295 --emsg_silent;
3296 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003297 if (self->auto_rebind)
3298 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003299 }
3300 ga_append(&repr_ga, '>');
3301 ret = PyString_FromString((char *)repr_ga.ga_data);
3302 ga_clear(&repr_ga);
3303 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003304}
3305
Bram Moolenaardb913952012-06-29 12:54:53 +02003306static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003307 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3308 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003309};
3310
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003311/*
3312 * Options object
3313 */
3314
3315static PyTypeObject OptionsType;
3316
3317typedef int (*checkfun)(void *);
3318
3319typedef struct
3320{
3321 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003322 int opt_type;
3323 void *from;
3324 checkfun Check;
3325 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003326} OptionsObject;
3327
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003328 static int
3329dummy_check(void *arg UNUSED)
3330{
3331 return 0;
3332}
3333
3334 static PyObject *
3335OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3336{
3337 OptionsObject *self;
3338
Bram Moolenaar774267b2013-05-21 20:51:59 +02003339 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003340 if (self == NULL)
3341 return NULL;
3342
3343 self->opt_type = opt_type;
3344 self->from = from;
3345 self->Check = Check;
3346 self->fromObj = fromObj;
3347 if (fromObj)
3348 Py_INCREF(fromObj);
3349
3350 return (PyObject *)(self);
3351}
3352
3353 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003354OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003355{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003356 PyObject_GC_UnTrack((void *)(self));
3357 Py_XDECREF(self->fromObj);
3358 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003359}
3360
3361 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003362OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003363{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003364 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003365 return 0;
3366}
3367
3368 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003369OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003370{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003371 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003372 return 0;
3373}
3374
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003375 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003376OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003377{
3378 char_u *key;
3379 int flags;
3380 long numval;
3381 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003382 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003383
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003384 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003385 return NULL;
3386
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003387 if (!(key = StringToChars(keyObject, &todecref)))
3388 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003389
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003390 if (*key == NUL)
3391 {
3392 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003393 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003394 return NULL;
3395 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003396
3397 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003398 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003399
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003400 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003401
3402 if (flags == 0)
3403 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003404 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003405 return NULL;
3406 }
3407
3408 if (flags & SOPT_UNSET)
3409 {
3410 Py_INCREF(Py_None);
3411 return Py_None;
3412 }
3413 else if (flags & SOPT_BOOL)
3414 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003415 PyObject *ret;
3416 ret = numval ? Py_True : Py_False;
3417 Py_INCREF(ret);
3418 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003419 }
3420 else if (flags & SOPT_NUM)
3421 return PyInt_FromLong(numval);
3422 else if (flags & SOPT_STRING)
3423 {
3424 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003425 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003426 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003427 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003428 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003429 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003430 else
3431 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003432 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003433 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003434 return NULL;
3435 }
3436 }
3437 else
3438 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003439 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003440 return NULL;
3441 }
3442}
3443
3444 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003445OptionsContains(OptionsObject *self, PyObject *keyObject)
3446{
3447 char_u *key;
3448 PyObject *todecref;
3449
3450 if (!(key = StringToChars(keyObject, &todecref)))
3451 return -1;
3452
3453 if (*key == NUL)
3454 {
3455 Py_XDECREF(todecref);
3456 return 0;
3457 }
3458
3459 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3460 {
3461 Py_XDECREF(todecref);
3462 return 1;
3463 }
3464 else
3465 {
3466 Py_XDECREF(todecref);
3467 return 0;
3468 }
3469}
3470
3471typedef struct
3472{
3473 void *lastoption;
3474 int opt_type;
3475} optiterinfo_T;
3476
3477 static PyObject *
3478OptionsIterNext(optiterinfo_T **oii)
3479{
3480 char_u *name;
3481
3482 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3483 return PyString_FromString((char *)name);
3484
3485 return NULL;
3486}
3487
3488 static PyObject *
3489OptionsIter(OptionsObject *self)
3490{
3491 optiterinfo_T *oii;
3492
3493 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3494 {
3495 PyErr_NoMemory();
3496 return NULL;
3497 }
3498
3499 oii->opt_type = self->opt_type;
3500 oii->lastoption = NULL;
3501
3502 return IterNew(oii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003503 (destructorfun)(void *) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003504 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003505}
3506
3507 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003508set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003509{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003510 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003511
3512 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3513 {
3514 if (VimTryEnd())
3515 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003516 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003517 return FAIL;
3518 }
3519 return OK;
3520}
3521
3522 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003523set_option_value_for(
3524 char_u *key,
3525 int numval,
3526 char_u *stringval,
3527 int opt_flags,
3528 int opt_type,
3529 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003530{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003531 win_T *save_curwin = NULL;
3532 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003533 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003534 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003535
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003536 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003537 switch (opt_type)
3538 {
3539 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003540 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003541 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003542 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003543 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003544 if (VimTryEnd())
3545 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003546 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003547 return -1;
3548 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003549 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3550 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003551 break;
3552 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003553 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003554 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003555 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003556 break;
3557 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003558 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003559 break;
3560 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003561 if (set_ret == FAIL)
3562 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003563 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003564}
3565
3566 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003567OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003568{
3569 char_u *key;
3570 int flags;
3571 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003572 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003573 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003574
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003575 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003576 return -1;
3577
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003578 if (!(key = StringToChars(keyObject, &todecref)))
3579 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003580
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003581 if (*key == NUL)
3582 {
3583 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003584 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003585 return -1;
3586 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003587
3588 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003589 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003590
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003591 if (flags == 0)
3592 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003593 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003594 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003595 return -1;
3596 }
3597
3598 if (valObject == NULL)
3599 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003600 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003601 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003602 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003603 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003604 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003605 return -1;
3606 }
3607 else if (!(flags & SOPT_GLOBAL))
3608 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003609 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003610 N_("unable to unset option %s "
3611 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003612 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003613 return -1;
3614 }
3615 else
3616 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003617 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003618 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003619 return 0;
3620 }
3621 }
3622
Bram Moolenaard6e39182013-05-21 18:30:34 +02003623 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003624
3625 if (flags & SOPT_BOOL)
3626 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003627 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003628
Bram Moolenaarb983f752013-05-15 16:11:50 +02003629 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003630 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003631 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003632 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003633 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003634 }
3635 else if (flags & SOPT_NUM)
3636 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003637 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003638
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003639 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003640 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003641 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003642 return -1;
3643 }
3644
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003645 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003646 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003647 }
3648 else
3649 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003650 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003651 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003652
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003653 if ((val = StringToChars(valObject, &todecref2)))
3654 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003655 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003656 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003657 Py_XDECREF(todecref2);
3658 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003659 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003660 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003661 }
3662
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003663 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003664
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003665 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003666}
3667
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003668static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003669 0, // sq_length
3670 0, // sq_concat
3671 0, // sq_repeat
3672 0, // sq_item
3673 0, // sq_slice
3674 0, // sq_ass_item
3675 0, // sq_ass_slice
3676 (objobjproc) OptionsContains, // sq_contains
3677 0, // sq_inplace_concat
3678 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003679};
3680
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003681static PyMappingMethods OptionsAsMapping = {
3682 (lenfunc) NULL,
3683 (binaryfunc) OptionsItem,
3684 (objobjargproc) OptionsAssItem,
3685};
3686
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003687// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003688
3689typedef struct
3690{
3691 PyObject_HEAD
3692 tabpage_T *tab;
3693} TabPageObject;
3694
3695static PyObject *WinListNew(TabPageObject *tabObject);
3696
3697static PyTypeObject TabPageType;
3698
3699 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003700CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003701{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003702 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003703 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003704 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003705 return -1;
3706 }
3707
3708 return 0;
3709}
3710
3711 static PyObject *
3712TabPageNew(tabpage_T *tab)
3713{
3714 TabPageObject *self;
3715
3716 if (TAB_PYTHON_REF(tab))
3717 {
3718 self = TAB_PYTHON_REF(tab);
3719 Py_INCREF(self);
3720 }
3721 else
3722 {
3723 self = PyObject_NEW(TabPageObject, &TabPageType);
3724 if (self == NULL)
3725 return NULL;
3726 self->tab = tab;
3727 TAB_PYTHON_REF(tab) = self;
3728 }
3729
3730 return (PyObject *)(self);
3731}
3732
3733 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003734TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003735{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003736 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3737 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003738
3739 DESTRUCTOR_FINISH(self);
3740}
3741
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003742static char *TabPageAttrs[] = {
3743 "windows", "number", "vars", "window", "valid",
3744 NULL
3745};
3746
3747 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003748TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003749{
3750 return ObjectDir(self, TabPageAttrs);
3751}
3752
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003753 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003754TabPageAttrValid(TabPageObject *self, char *name)
3755{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003756 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003757
3758 if (strcmp(name, "valid") != 0)
3759 return NULL;
3760
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003761 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3762 Py_INCREF(ret);
3763 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003764}
3765
3766 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003767TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003768{
3769 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003770 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003772 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003773 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003774 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003775 else if (strcmp(name, "window") == 0)
3776 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003777 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003778 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003779 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003780 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003781 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003782 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003783 else if (strcmp(name, "__members__") == 0)
3784 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003785 return NULL;
3786}
3787
3788 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003789TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003790{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003791 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003792 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003793 else
3794 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003795 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003796
3797 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003798 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3799 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003800 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003801 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003802 }
3803}
3804
3805static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003806 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003807 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3808 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003809};
3810
3811/*
3812 * Window list object
3813 */
3814
3815static PyTypeObject TabListType;
3816static PySequenceMethods TabListAsSeq;
3817
3818typedef struct
3819{
3820 PyObject_HEAD
3821} TabListObject;
3822
3823 static PyInt
3824TabListLength(PyObject *self UNUSED)
3825{
3826 tabpage_T *tp = first_tabpage;
3827 PyInt n = 0;
3828
3829 while (tp != NULL)
3830 {
3831 ++n;
3832 tp = tp->tp_next;
3833 }
3834
3835 return n;
3836}
3837
3838 static PyObject *
3839TabListItem(PyObject *self UNUSED, PyInt n)
3840{
3841 tabpage_T *tp;
3842
3843 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3844 if (n == 0)
3845 return TabPageNew(tp);
3846
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003847 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003848 return NULL;
3849}
3850
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003851/*
3852 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003853 */
3854
3855typedef struct
3856{
3857 PyObject_HEAD
3858 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003859 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003860} WindowObject;
3861
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003862static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003863
3864 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003865CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003866{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003867 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003868 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003869 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003870 return -1;
3871 }
3872
3873 return 0;
3874}
3875
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003876 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003877WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003878{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003879 /*
3880 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003881 * If we add a "w_python*_ref" field to the win_T structure,
3882 * then we can get at it in win_free() in vim. We then
3883 * need to create only ONE Python object per window - if
3884 * we try to create a second, just INCREF the existing one
3885 * and return it. The (single) Python object referring to
3886 * the window is stored in "w_python*_ref".
3887 * On a win_free() we set the Python object's win_T* field
3888 * to an invalid value. We trap all uses of a window
3889 * object, and reject them if the win_T* field is invalid.
3890 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003891 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003892 * w_python_ref and w_python3_ref fields respectively.
3893 */
3894
3895 WindowObject *self;
3896
3897 if (WIN_PYTHON_REF(win))
3898 {
3899 self = WIN_PYTHON_REF(win);
3900 Py_INCREF(self);
3901 }
3902 else
3903 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003904 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003905 if (self == NULL)
3906 return NULL;
3907 self->win = win;
3908 WIN_PYTHON_REF(win) = self;
3909 }
3910
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003911 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3912
Bram Moolenaar971db462013-05-12 18:44:48 +02003913 return (PyObject *)(self);
3914}
3915
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003916 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003917WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003918{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003919 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003920 if (self->win && self->win != INVALID_WINDOW_VALUE)
3921 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003922 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003923 PyObject_GC_Del((void *)(self));
3924}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003925
Bram Moolenaar774267b2013-05-21 20:51:59 +02003926 static int
3927WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3928{
3929 Py_VISIT(((PyObject *)(self->tabObject)));
3930 return 0;
3931}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003932
Bram Moolenaar774267b2013-05-21 20:51:59 +02003933 static int
3934WindowClear(WindowObject *self)
3935{
3936 Py_CLEAR(self->tabObject);
3937 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003938}
3939
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003940 static win_T *
3941get_firstwin(TabPageObject *tabObject)
3942{
3943 if (tabObject)
3944 {
3945 if (CheckTabPage(tabObject))
3946 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003947 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003948 else if (tabObject->tab == curtab)
3949 return firstwin;
3950 else
3951 return tabObject->tab->tp_firstwin;
3952 }
3953 else
3954 return firstwin;
3955}
Bram Moolenaare950f992018-06-10 13:55:55 +02003956
3957// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003958static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003959 "buffer",
3960 "cursor",
3961 "height",
3962 "row",
3963 "width",
3964 "col",
3965 "vars",
3966 "options",
3967 "number",
3968 "tabpage",
3969 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003970 NULL
3971};
3972
3973 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003974WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003975{
3976 return ObjectDir(self, WindowAttrs);
3977}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003978
Bram Moolenaar971db462013-05-12 18:44:48 +02003979 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003980WindowAttrValid(WindowObject *self, char *name)
3981{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003982 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003983
3984 if (strcmp(name, "valid") != 0)
3985 return NULL;
3986
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003987 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3988 Py_INCREF(ret);
3989 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003990}
3991
3992 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003993WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003994{
3995 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003997 else if (strcmp(name, "cursor") == 0)
3998 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004000
4001 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4002 }
4003 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004004 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004005 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004006 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004007 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004008 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004009 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004010 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004011 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004012 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004013 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004014 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4015 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004016 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004017 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004018 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004019 return NULL;
4020 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004021 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004022 }
4023 else if (strcmp(name, "tabpage") == 0)
4024 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004025 Py_INCREF(self->tabObject);
4026 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004027 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004028 else if (strcmp(name, "__members__") == 0)
4029 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004030 else
4031 return NULL;
4032}
4033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004035WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004037 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 return -1;
4039
4040 if (strcmp(name, "buffer") == 0)
4041 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004042 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043 return -1;
4044 }
4045 else if (strcmp(name, "cursor") == 0)
4046 {
4047 long lnum;
4048 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004050 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 return -1;
4052
Bram Moolenaard6e39182013-05-21 18:30:34 +02004053 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004055 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056 return -1;
4057 }
4058
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004059 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004060 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061 return -1;
4062
Bram Moolenaard6e39182013-05-21 18:30:34 +02004063 self->win->w_cursor.lnum = lnum;
4064 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004065 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004066 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004067 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004068 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004070 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071 return 0;
4072 }
4073 else if (strcmp(name, "height") == 0)
4074 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004075 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 win_T *savewin;
4077
Bram Moolenaardee2e312013-06-23 16:35:47 +02004078 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 return -1;
4080
4081#ifdef FEAT_GUI
4082 need_mouse_correct = TRUE;
4083#endif
4084 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004085 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004086
4087 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004088 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004090 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 return -1;
4092
4093 return 0;
4094 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 else if (strcmp(name, "width") == 0)
4096 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004097 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 win_T *savewin;
4099
Bram Moolenaardee2e312013-06-23 16:35:47 +02004100 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 return -1;
4102
4103#ifdef FEAT_GUI
4104 need_mouse_correct = TRUE;
4105#endif
4106 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004107 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004108
4109 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004110 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004112 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 return -1;
4114
4115 return 0;
4116 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004117 else
4118 {
4119 PyErr_SetString(PyExc_AttributeError, name);
4120 return -1;
4121 }
4122}
4123
4124 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004125WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004127 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004128 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129 else
4130 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004131 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004132
Bram Moolenaar6d216452013-05-12 19:00:41 +02004133 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004134 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 (self));
4136 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004137 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138 }
4139}
4140
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004141static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004142 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004143 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4144 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004145};
4146
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004148 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004149 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004150
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004151static PyTypeObject WinListType;
4152static PySequenceMethods WinListAsSeq;
4153
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004154typedef struct
4155{
4156 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004157 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004158} WinListObject;
4159
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004160 static PyObject *
4161WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004162{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004163 WinListObject *self;
4164
4165 self = PyObject_NEW(WinListObject, &WinListType);
4166 self->tabObject = tabObject;
4167 Py_INCREF(tabObject);
4168
4169 return (PyObject *)(self);
4170}
4171
4172 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004173WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004174{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004175 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004176
4177 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004178 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004179 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004180 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004181
4182 DESTRUCTOR_FINISH(self);
4183}
4184
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004185 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004186WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004187{
4188 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004189 PyInt n = 0;
4190
Bram Moolenaard6e39182013-05-21 18:30:34 +02004191 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004192 return -1;
4193
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004194 while (w != NULL)
4195 {
4196 ++n;
4197 w = W_NEXT(w);
4198 }
4199
4200 return n;
4201}
4202
4203 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004204WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004205{
4206 win_T *w;
4207
Bram Moolenaard6e39182013-05-21 18:30:34 +02004208 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004209 return NULL;
4210
4211 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004213 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004215 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 return NULL;
4217}
4218
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004219/*
4220 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004221 *
4222 * The result is in allocated memory. All internal nulls are replaced by
4223 * newline characters. It is an error for the string to contain newline
4224 * characters.
4225 *
4226 * On errors, the Python exception data is set, and NULL is returned.
4227 */
4228 static char *
4229StringToLine(PyObject *obj)
4230{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004231 char *str;
4232 char *save;
4233 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004234 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004235 PyInt i;
4236 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004237
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004238 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004239 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004240 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4241 || str == NULL)
4242 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004243 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004244 else if (PyUnicode_Check(obj))
4245 {
4246 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4247 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004248
Bram Moolenaardaa27022013-06-24 22:33:30 +02004249 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004250 || str == NULL)
4251 {
4252 Py_DECREF(bytes);
4253 return NULL;
4254 }
4255 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004256 else
4257 {
4258#if PY_MAJOR_VERSION < 3
4259 PyErr_FORMAT(PyExc_TypeError,
4260 N_("expected str() or unicode() instance, but got %s"),
4261 Py_TYPE_NAME(obj));
4262#else
4263 PyErr_FORMAT(PyExc_TypeError,
4264 N_("expected bytes() or str() instance, but got %s"),
4265 Py_TYPE_NAME(obj));
4266#endif
4267 return NULL;
4268 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269
4270 /*
4271 * Error checking: String must not contain newlines, as we
4272 * are replacing a single line, and we must replace it with
4273 * a single line.
4274 * A trailing newline is removed, so that append(f.readlines()) works.
4275 */
4276 p = memchr(str, '\n', len);
4277 if (p != NULL)
4278 {
4279 if (p == str + len - 1)
4280 --len;
4281 else
4282 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004283 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004284 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004285 return NULL;
4286 }
4287 }
4288
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004289 /*
4290 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004291 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004293 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004294 if (save == NULL)
4295 {
4296 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004297 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004298 return NULL;
4299 }
4300
4301 for (i = 0; i < len; ++i)
4302 {
4303 if (str[i] == '\0')
4304 save[i] = '\n';
4305 else
4306 save[i] = str[i];
4307 }
4308
4309 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004310 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004311
4312 return save;
4313}
4314
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004315/*
4316 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004317 * in Vim format (1-based). The line is returned as a Python
4318 * string object.
4319 */
4320 static PyObject *
4321GetBufferLine(buf_T *buf, PyInt n)
4322{
4323 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4324}
4325
4326
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004327/*
4328 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004329 * are in Vim format (1-based). The range is from lo up to, but not
4330 * including, hi. The list is returned as a Python list of string objects.
4331 */
4332 static PyObject *
4333GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4334{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004335 PyInt i;
4336 PyInt n = hi - lo;
4337 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004338
4339 if (list == NULL)
4340 return NULL;
4341
4342 for (i = 0; i < n; ++i)
4343 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004344 linenr_T lnum = (linenr_T)(lo + i);
4345 char *text;
4346 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004347
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004348 if (lnum > buf->b_ml.ml_line_count)
4349 text = "";
4350 else
4351 text = (char *)ml_get_buf(buf, lnum, FALSE);
4352 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004353 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004354 {
4355 Py_DECREF(list);
4356 return NULL;
4357 }
4358
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004359 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004360 }
4361
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004362 // The ownership of the Python list is passed to the caller (ie,
4363 // the caller should Py_DECREF() the object when it is finished
4364 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004365
4366 return list;
4367}
4368
4369/*
4370 * Check if deleting lines made the cursor position invalid.
4371 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4372 * deleted).
4373 */
4374 static void
4375py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4376{
4377 if (curwin->w_cursor.lnum >= lo)
4378 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004379 // Adjust the cursor position if it's in/after the changed
4380 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004381 if (curwin->w_cursor.lnum >= hi)
4382 {
4383 curwin->w_cursor.lnum += extra;
4384 check_cursor_col();
4385 }
4386 else if (extra < 0)
4387 {
4388 curwin->w_cursor.lnum = lo;
4389 check_cursor();
4390 }
4391 else
4392 check_cursor_col();
4393 changed_cline_bef_curs();
4394 }
4395 invalidate_botline();
4396}
4397
Bram Moolenaar19e60942011-06-19 00:27:51 +02004398/*
4399 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004400 * in Vim format (1-based). The replacement line is given as
4401 * a Python string object. The object is checked for validity
4402 * and correct format. Errors are returned as a value of FAIL.
4403 * The return value is OK on success.
4404 * If OK is returned and len_change is not NULL, *len_change
4405 * is set to the change in the buffer length.
4406 */
4407 static int
4408SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4409{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004410 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004411 win_T *save_curwin = NULL;
4412 tabpage_T *save_curtab = NULL;
4413
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004414 // First of all, we check the type of the supplied Python object.
4415 // There are three cases:
4416 // 1. NULL, or None - this is a deletion.
4417 // 2. A string - this is a replacement.
4418 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004419 if (line == Py_None || line == NULL)
4420 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004422 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004424 VimTryStart();
4425
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004427 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004428 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004429 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004430 else
4431 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004432 if (buf == curbuf && (save_curwin != NULL
4433 || save_curbuf.br_buf == NULL))
4434 // Using an existing window for the buffer, adjust the cursor
4435 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004436 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004437 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004438 // Only adjust marks if we managed to switch to a window that
4439 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004440 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441 }
4442
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004443 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004444
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004445 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446 return FAIL;
4447
4448 if (len_change)
4449 *len_change = -1;
4450
4451 return OK;
4452 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004453 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004454 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004455 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456
4457 if (save == NULL)
4458 return FAIL;
4459
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004460 VimTryStart();
4461
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004462 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004463 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004464 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465
4466 if (u_savesub((linenr_T)n) == FAIL)
4467 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004468 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004469 vim_free(save);
4470 }
4471 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4472 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004473 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004474 vim_free(save);
4475 }
4476 else
4477 changed_bytes((linenr_T)n, 0);
4478
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004479 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004480
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004481 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004482 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004483 check_cursor_col();
4484
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004485 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004486 return FAIL;
4487
4488 if (len_change)
4489 *len_change = 0;
4490
4491 return OK;
4492 }
4493 else
4494 {
4495 PyErr_BadArgument();
4496 return FAIL;
4497 }
4498}
4499
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004500/*
4501 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004502 * Vim format (1-based). The range is from lo up to, but not including, hi.
4503 * The replacement lines are given as a Python list of string objects. The
4504 * list is checked for validity and correct format. Errors are returned as a
4505 * value of FAIL. The return value is OK on success.
4506 * If OK is returned and len_change is not NULL, *len_change
4507 * is set to the change in the buffer length.
4508 */
4509 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004510SetBufferLineList(
4511 buf_T *buf,
4512 PyInt lo,
4513 PyInt hi,
4514 PyObject *list,
4515 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004516{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004517 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004518 win_T *save_curwin = NULL;
4519 tabpage_T *save_curtab = NULL;
4520
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004521 // First of all, we check the type of the supplied Python object.
4522 // There are three cases:
4523 // 1. NULL, or None - this is a deletion.
4524 // 2. A list - this is a replacement.
4525 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004526 if (list == Py_None || list == NULL)
4527 {
4528 PyInt i;
4529 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004530
4531 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004532 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004533 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004534
4535 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004536 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004537 else
4538 {
4539 for (i = 0; i < n; ++i)
4540 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004541 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004542 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004543 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004544 break;
4545 }
4546 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004547 if (buf == curbuf && (save_curwin != NULL
4548 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004549 // Using an existing window for the buffer, adjust the cursor
4550 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004552 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004553 // Only adjust marks if we managed to switch to a window that
4554 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004555 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004556 }
4557
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004558 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004559
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004560 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004561 return FAIL;
4562
4563 if (len_change)
4564 *len_change = -n;
4565
4566 return OK;
4567 }
4568 else if (PyList_Check(list))
4569 {
4570 PyInt i;
4571 PyInt new_len = PyList_Size(list);
4572 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004573 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004574 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004575
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004576 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004577 array = NULL;
4578 else
4579 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004580 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004581 if (array == NULL)
4582 {
4583 PyErr_NoMemory();
4584 return FAIL;
4585 }
4586 }
4587
4588 for (i = 0; i < new_len; ++i)
4589 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004590 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004591
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004592 if (!(line = PyList_GetItem(list, i)) ||
4593 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004594 {
4595 while (i)
4596 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004597 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004598 return FAIL;
4599 }
4600 }
4601
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004602 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004603 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004604
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004605 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004606 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004607
4608 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004609 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004610
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004611 // If the size of the range is reducing (ie, new_len < old_len) we
4612 // need to delete some old_len. We do this at the start, by
4613 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004614 if (!PyErr_Occurred())
4615 {
4616 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004617 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004618 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004619 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004620 break;
4621 }
4622 extra -= i;
4623 }
4624
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004625 // For as long as possible, replace the existing old_len with the
4626 // new old_len. This is a more efficient operation, as it requires
4627 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004628 if (!PyErr_Occurred())
4629 {
4630 for (i = 0; i < old_len && i < new_len; ++i)
4631 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4632 == FAIL)
4633 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004634 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004635 break;
4636 }
4637 }
4638 else
4639 i = 0;
4640
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004641 // Now we may need to insert the remaining new old_len. If we do, we
4642 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004643 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004644 if (!PyErr_Occurred())
4645 {
4646 while (i < new_len)
4647 {
4648 if (ml_append((linenr_T)(lo + i - 1),
4649 (char_u *)array[i], 0, FALSE) == FAIL)
4650 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004651 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004652 break;
4653 }
4654 vim_free(array[i]);
4655 ++i;
4656 ++extra;
4657 }
4658 }
4659
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004660 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004661 while (i < new_len)
4662 {
4663 vim_free(array[i]);
4664 ++i;
4665 }
4666
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004667 // Free the array of old_len. All of its contents have now
4668 // been dealt with (either freed, or the responsibility passed
4669 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004670 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004671
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004672 // Adjust marks. Invalidate any which lie in the
4673 // changed range, and move any in the remainder of the buffer.
4674 // Only adjust marks if we managed to switch to a window that holds
4675 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004676 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004677 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004678 (long)MAXLNUM, (long)extra);
4679 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4680
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004681 if (buf == curbuf && (save_curwin != NULL
4682 || save_curbuf.br_buf == NULL))
4683 // Using an existing window for the buffer, adjust the cursor
4684 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004685 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4686
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004687 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004688 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004689
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004690 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004691 return FAIL;
4692
4693 if (len_change)
4694 *len_change = new_len - old_len;
4695
4696 return OK;
4697 }
4698 else
4699 {
4700 PyErr_BadArgument();
4701 return FAIL;
4702 }
4703}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004704
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004705/*
4706 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004707 * The line number is in Vim format (1-based). The lines to be inserted are
4708 * given as a Python list of string objects or as a single string. The lines
4709 * to be added are checked for validity and correct format. Errors are
4710 * returned as a value of FAIL. The return value is OK on success.
4711 * If OK is returned and len_change is not NULL, *len_change
4712 * is set to the change in the buffer length.
4713 */
4714 static int
4715InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4716{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004717 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004718 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004719 tabpage_T *save_curtab = NULL;
4720
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004721 // First of all, we check the type of the supplied Python object.
4722 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004723 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004724 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004725 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004726
4727 if (str == NULL)
4728 return FAIL;
4729
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004731 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004732 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004734 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004735 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004736 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004737 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004738 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004739 // Only adjust marks if we managed to switch to a window that
4740 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741 appended_lines_mark((linenr_T)n, 1L);
4742
4743 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004744 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 update_screen(VALID);
4746
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004747 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004748 return FAIL;
4749
4750 if (len_change)
4751 *len_change = 1;
4752
4753 return OK;
4754 }
4755 else if (PyList_Check(lines))
4756 {
4757 PyInt i;
4758 PyInt size = PyList_Size(lines);
4759 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004760
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004761 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004762 if (array == NULL)
4763 {
4764 PyErr_NoMemory();
4765 return FAIL;
4766 }
4767
4768 for (i = 0; i < size; ++i)
4769 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004770 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004771
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004772 if (!(line = PyList_GetItem(lines, i)) ||
4773 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004774 {
4775 while (i)
4776 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004777 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004778 return FAIL;
4779 }
4780 }
4781
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004782 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004783 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004784 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004785
4786 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004787 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004788 else
4789 {
4790 for (i = 0; i < size; ++i)
4791 {
4792 if (ml_append((linenr_T)(n + i),
4793 (char_u *)array[i], 0, FALSE) == FAIL)
4794 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004795 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004796
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004797 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004798 while (i < size)
4799 vim_free(array[i++]);
4800
4801 break;
4802 }
4803 vim_free(array[i]);
4804 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004805 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004806 // Only adjust marks if we managed to switch to a window that
4807 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004808 appended_lines_mark((linenr_T)n, (long)i);
4809 }
4810
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004811 // Free the array of lines. All of its contents have now
4812 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004813 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004814 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004815
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004816 update_screen(VALID);
4817
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004818 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004819 return FAIL;
4820
4821 if (len_change)
4822 *len_change = size;
4823
4824 return OK;
4825 }
4826 else
4827 {
4828 PyErr_BadArgument();
4829 return FAIL;
4830 }
4831}
4832
4833/*
4834 * Common routines for buffers and line ranges
4835 * -------------------------------------------
4836 */
4837
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004838typedef struct
4839{
4840 PyObject_HEAD
4841 buf_T *buf;
4842} BufferObject;
4843
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004844 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004845CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004846{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004847 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004848 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004849 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004850 return -1;
4851 }
4852
4853 return 0;
4854}
4855
4856 static PyObject *
4857RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4858{
4859 if (CheckBuffer(self))
4860 return NULL;
4861
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004862 if (end == -1)
4863 end = self->buf->b_ml.ml_line_count;
4864
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004865 if (n < 0)
4866 n += end - start + 1;
4867
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004868 if (n < 0 || n > end - start)
4869 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004870 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004871 return NULL;
4872 }
4873
4874 return GetBufferLine(self->buf, n+start);
4875}
4876
4877 static PyObject *
4878RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4879{
4880 PyInt size;
4881
4882 if (CheckBuffer(self))
4883 return NULL;
4884
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004885 if (end == -1)
4886 end = self->buf->b_ml.ml_line_count;
4887
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004888 size = end - start + 1;
4889
4890 if (lo < 0)
4891 lo = 0;
4892 else if (lo > size)
4893 lo = size;
4894 if (hi < 0)
4895 hi = 0;
4896 if (hi < lo)
4897 hi = lo;
4898 else if (hi > size)
4899 hi = size;
4900
4901 return GetBufferLineList(self->buf, lo+start, hi+start);
4902}
4903
4904 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004905RBAsItem(
4906 BufferObject *self,
4907 PyInt n,
4908 PyObject *valObject,
4909 PyInt start,
4910 PyInt end,
4911 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004912{
4913 PyInt len_change;
4914
4915 if (CheckBuffer(self))
4916 return -1;
4917
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004918 if (end == -1)
4919 end = self->buf->b_ml.ml_line_count;
4920
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004921 if (n < 0)
4922 n += end - start + 1;
4923
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004924 if (n < 0 || n > end - start)
4925 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004926 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004927 return -1;
4928 }
4929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004930 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004931 return -1;
4932
4933 if (new_end)
4934 *new_end = end + len_change;
4935
4936 return 0;
4937}
4938
Bram Moolenaar19e60942011-06-19 00:27:51 +02004939 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004940RBAsSlice(
4941 BufferObject *self,
4942 PyInt lo,
4943 PyInt hi,
4944 PyObject *valObject,
4945 PyInt start,
4946 PyInt end,
4947 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004948{
4949 PyInt size;
4950 PyInt len_change;
4951
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004952 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004953 if (CheckBuffer(self))
4954 return -1;
4955
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004956 if (end == -1)
4957 end = self->buf->b_ml.ml_line_count;
4958
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004959 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004960 size = end - start + 1;
4961
4962 if (lo < 0)
4963 lo = 0;
4964 else if (lo > size)
4965 lo = size;
4966 if (hi < 0)
4967 hi = 0;
4968 if (hi < lo)
4969 hi = lo;
4970 else if (hi > size)
4971 hi = size;
4972
4973 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004974 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004975 return -1;
4976
4977 if (new_end)
4978 *new_end = end + len_change;
4979
4980 return 0;
4981}
4982
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004983
4984 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004985RBAppend(
4986 BufferObject *self,
4987 PyObject *args,
4988 PyInt start,
4989 PyInt end,
4990 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004991{
4992 PyObject *lines;
4993 PyInt len_change;
4994 PyInt max;
4995 PyInt n;
4996
4997 if (CheckBuffer(self))
4998 return NULL;
4999
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005000 if (end == -1)
5001 end = self->buf->b_ml.ml_line_count;
5002
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005003 max = n = end - start + 1;
5004
5005 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5006 return NULL;
5007
5008 if (n < 0 || n > max)
5009 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005010 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005011 return NULL;
5012 }
5013
5014 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5015 return NULL;
5016
5017 if (new_end)
5018 *new_end = end + len_change;
5019
5020 Py_INCREF(Py_None);
5021 return Py_None;
5022}
5023
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005024// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005025
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005026static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005027static PySequenceMethods RangeAsSeq;
5028static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005029
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005030typedef struct
5031{
5032 PyObject_HEAD
5033 BufferObject *buf;
5034 PyInt start;
5035 PyInt end;
5036} RangeObject;
5037
5038 static PyObject *
5039RangeNew(buf_T *buf, PyInt start, PyInt end)
5040{
5041 BufferObject *bufr;
5042 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005043 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005044 if (self == NULL)
5045 return NULL;
5046
5047 bufr = (BufferObject *)BufferNew(buf);
5048 if (bufr == NULL)
5049 {
5050 Py_DECREF(self);
5051 return NULL;
5052 }
5053 Py_INCREF(bufr);
5054
5055 self->buf = bufr;
5056 self->start = start;
5057 self->end = end;
5058
5059 return (PyObject *)(self);
5060}
5061
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005062 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005064{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005065 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005066 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005067 PyObject_GC_Del((void *)(self));
5068}
5069
5070 static int
5071RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5072{
5073 Py_VISIT(((PyObject *)(self->buf)));
5074 return 0;
5075}
5076
5077 static int
5078RangeClear(RangeObject *self)
5079{
5080 Py_CLEAR(self->buf);
5081 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005082}
5083
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005084 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005085RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005087 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005089 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005090
Bram Moolenaard6e39182013-05-21 18:30:34 +02005091 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005092}
5093
5094 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005095RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005096{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005097 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005098}
5099
5100 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005101RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005102{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005103 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104}
5105
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005106static char *RangeAttrs[] = {
5107 "start", "end",
5108 NULL
5109};
5110
5111 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005112RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005113{
5114 return ObjectDir(self, RangeAttrs);
5115}
5116
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005117 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005118RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005120 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121}
5122
5123 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005124RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005125{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005126 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005127 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5128 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129 else
5130 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005131 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005132
5133 if (name == NULL)
5134 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005135
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005136 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005137 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005138 }
5139}
5140
5141static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005142 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005143 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005144 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5145 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005146};
5147
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005148static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005149static PySequenceMethods BufferAsSeq;
5150static PyMappingMethods BufferAsMapping;
5151
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005152 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005153BufferNew(buf_T *buf)
5154{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005155 /*
5156 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005157 * If we add a "b_python*_ref" field to the buf_T structure,
5158 * then we can get at it in buf_freeall() in vim. We then
5159 * need to create only ONE Python object per buffer - if
5160 * we try to create a second, just INCREF the existing one
5161 * and return it. The (single) Python object referring to
5162 * the buffer is stored in "b_python*_ref".
5163 * Question: what to do on a buf_freeall(). We'll probably
5164 * have to either delete the Python object (DECREF it to
5165 * zero - a bad idea, as it leaves dangling refs!) or
5166 * set the buf_T * value to an invalid value (-1?), which
5167 * means we need checks in all access functions... Bah.
5168 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005170 * b_python_ref and b_python3_ref fields respectively.
5171 */
5172
5173 BufferObject *self;
5174
5175 if (BUF_PYTHON_REF(buf) != NULL)
5176 {
5177 self = BUF_PYTHON_REF(buf);
5178 Py_INCREF(self);
5179 }
5180 else
5181 {
5182 self = PyObject_NEW(BufferObject, &BufferType);
5183 if (self == NULL)
5184 return NULL;
5185 self->buf = buf;
5186 BUF_PYTHON_REF(buf) = self;
5187 }
5188
5189 return (PyObject *)(self);
5190}
5191
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005192 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005193BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005194{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5196 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005197
5198 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005199}
5200
Bram Moolenaar971db462013-05-12 18:44:48 +02005201 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005202BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005203{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005204 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005205 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005206 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005207
Bram Moolenaard6e39182013-05-21 18:30:34 +02005208 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005209}
5210
5211 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005212BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005213{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005214 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005215}
5216
5217 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005218BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005219{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005220 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005221}
5222
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005223static char *BufferAttrs[] = {
5224 "name", "number", "vars", "options", "valid",
5225 NULL
5226};
5227
5228 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005229BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005230{
5231 return ObjectDir(self, BufferAttrs);
5232}
5233
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005234 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005235BufferAttrValid(BufferObject *self, char *name)
5236{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005237 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005238
5239 if (strcmp(name, "valid") != 0)
5240 return NULL;
5241
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005242 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5243 Py_INCREF(ret);
5244 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005245}
5246
5247 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005248BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005249{
5250 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005251 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005252 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005253 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005254 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005255 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005256 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005257 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005258 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5259 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005260 else if (strcmp(name, "__members__") == 0)
5261 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005262 else
5263 return NULL;
5264}
5265
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005266 static int
5267BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5268{
5269 if (CheckBuffer(self))
5270 return -1;
5271
5272 if (strcmp(name, "name") == 0)
5273 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005274 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005275 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005276 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005277 PyObject *todecref;
5278
5279 if (!(val = StringToChars(valObject, &todecref)))
5280 return -1;
5281
5282 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005283 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005284 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005285 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005286 aucmd_restbuf(&aco);
5287 Py_XDECREF(todecref);
5288 if (VimTryEnd())
5289 return -1;
5290
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005291 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005292 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005293 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005294 return -1;
5295 }
5296 return 0;
5297 }
5298 else
5299 {
5300 PyErr_SetString(PyExc_AttributeError, name);
5301 return -1;
5302 }
5303}
5304
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005305 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005306BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005307{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005308 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005309}
5310
5311 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005312BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005313{
5314 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005315 char_u *pmark;
5316 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005317 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005318 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005319
Bram Moolenaard6e39182013-05-21 18:30:34 +02005320 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321 return NULL;
5322
Bram Moolenaar389a1792013-06-23 13:00:44 +02005323 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005324 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005325
Bram Moolenaar389a1792013-06-23 13:00:44 +02005326 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005327 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005328 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005329 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005330 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005331 return NULL;
5332 }
5333
5334 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005335
5336 Py_XDECREF(todecref);
5337
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005338 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005339 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005340 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005341 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005342 if (VimTryEnd())
5343 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005344
5345 if (posp == NULL)
5346 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005347 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005348 return NULL;
5349 }
5350
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005351 if (posp->lnum <= 0)
5352 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005353 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005354 Py_INCREF(Py_None);
5355 return Py_None;
5356 }
5357
5358 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5359}
5360
5361 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005362BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005363{
5364 PyInt start;
5365 PyInt end;
5366
Bram Moolenaard6e39182013-05-21 18:30:34 +02005367 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005368 return NULL;
5369
5370 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5371 return NULL;
5372
Bram Moolenaard6e39182013-05-21 18:30:34 +02005373 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005374}
5375
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005376 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005377BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005378{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005379 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005380 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005381 else
5382 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005383 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005384
5385 if (name == NULL)
5386 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005387
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005388 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005389 }
5390}
5391
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005392static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005393 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005394 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005395 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005396 {"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 +02005397 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5398 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005399};
5400
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005401/*
5402 * Buffer list object - Implementation
5403 */
5404
5405static PyTypeObject BufMapType;
5406
5407typedef struct
5408{
5409 PyObject_HEAD
5410} BufMapObject;
5411
5412 static PyInt
5413BufMapLength(PyObject *self UNUSED)
5414{
5415 buf_T *b = firstbuf;
5416 PyInt n = 0;
5417
5418 while (b)
5419 {
5420 ++n;
5421 b = b->b_next;
5422 }
5423
5424 return n;
5425}
5426
5427 static PyObject *
5428BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5429{
5430 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005431 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005432
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005433 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005434 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005435
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005436 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005437
5438 if (b)
5439 return BufferNew(b);
5440 else
5441 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005442 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005443 return NULL;
5444 }
5445}
5446
5447 static void
5448BufMapIterDestruct(PyObject *buffer)
5449{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005450 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005451 if (buffer)
5452 {
5453 Py_DECREF(buffer);
5454 }
5455}
5456
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005457 static int
5458BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5459{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005460 if (buffer)
5461 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005462 return 0;
5463}
5464
5465 static int
5466BufMapIterClear(PyObject **buffer)
5467{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005468 if (*buffer)
5469 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005470 return 0;
5471}
5472
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005473 static PyObject *
5474BufMapIterNext(PyObject **buffer)
5475{
5476 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005477 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005478
5479 if (!*buffer)
5480 return NULL;
5481
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005482 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005483
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005484 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005485 {
5486 *buffer = NULL;
5487 return NULL;
5488 }
5489
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005490 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005491 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005493 return NULL;
5494 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005495 // Do not increment reference: we no longer hold it (decref), but whoever
5496 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005497 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005498}
5499
5500 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005501BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005502{
5503 PyObject *buffer;
5504
5505 buffer = BufferNew(firstbuf);
5506 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005507 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005508 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5509 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005510}
5511
5512static PyMappingMethods BufMapAsMapping = {
5513 (lenfunc) BufMapLength,
5514 (binaryfunc) BufMapItem,
5515 (objobjargproc) 0,
5516};
5517
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005518// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005519
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005520static char *CurrentAttrs[] = {
5521 "buffer", "window", "line", "range", "tabpage",
5522 NULL
5523};
5524
5525 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005526CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005527{
5528 return ObjectDir(self, CurrentAttrs);
5529}
5530
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005531 static PyObject *
5532CurrentGetattr(PyObject *self UNUSED, char *name)
5533{
5534 if (strcmp(name, "buffer") == 0)
5535 return (PyObject *)BufferNew(curbuf);
5536 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005537 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005538 else if (strcmp(name, "tabpage") == 0)
5539 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005540 else if (strcmp(name, "line") == 0)
5541 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5542 else if (strcmp(name, "range") == 0)
5543 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005544 else if (strcmp(name, "__members__") == 0)
5545 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005546 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005547#if PY_MAJOR_VERSION < 3
5548 return Py_FindMethod(WindowMethods, self, name);
5549#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005550 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005551#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005552}
5553
5554 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005555CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005556{
5557 if (strcmp(name, "line") == 0)
5558 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005559 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5560 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005561 return -1;
5562
5563 return 0;
5564 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005565 else if (strcmp(name, "buffer") == 0)
5566 {
5567 int count;
5568
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005569 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005570 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005571 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005572 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005574 return -1;
5575 }
5576
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005578 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005579 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005580
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005581 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5583 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005584 if (VimTryEnd())
5585 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005586 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005587 return -1;
5588 }
5589
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005590 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005591 }
5592 else if (strcmp(name, "window") == 0)
5593 {
5594 int count;
5595
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005596 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005597 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005598 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005599 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005600 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005601 return -1;
5602 }
5603
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005604 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005605 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005606 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005607
5608 if (!count)
5609 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005610 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005611 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005612 return -1;
5613 }
5614
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005615 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005616 win_goto(((WindowObject *)(valObject))->win);
5617 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005618 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005619 if (VimTryEnd())
5620 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005621 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005622 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005623 return -1;
5624 }
5625
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005626 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005627 }
5628 else if (strcmp(name, "tabpage") == 0)
5629 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005630 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005631 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005632 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005633 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005634 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005635 return -1;
5636 }
5637
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005638 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005639 return -1;
5640
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005641 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005642 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5643 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005644 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005645 if (VimTryEnd())
5646 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005647 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005648 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005649 return -1;
5650 }
5651
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005652 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005653 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005654 else
5655 {
5656 PyErr_SetString(PyExc_AttributeError, name);
5657 return -1;
5658 }
5659}
5660
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005661static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005662 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005663 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5664 { NULL, NULL, 0, NULL}
5665};
5666
Bram Moolenaardb913952012-06-29 12:54:53 +02005667 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005668init_range_cmd(exarg_T *eap)
5669{
5670 RangeStart = eap->line1;
5671 RangeEnd = eap->line2;
5672}
5673
5674 static void
5675init_range_eval(typval_T *rettv UNUSED)
5676{
5677 RangeStart = (PyInt) curwin->w_cursor.lnum;
5678 RangeEnd = RangeStart;
5679}
5680
5681 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005682run_cmd(const char *cmd, void *arg UNUSED
5683#ifdef PY_CAN_RECURSE
5684 , PyGILState_STATE *pygilstate UNUSED
5685#endif
5686 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005687{
Bram Moolenaar41009372013-07-01 22:03:04 +02005688 PyObject *run_ret;
5689 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5690 if (run_ret != NULL)
5691 {
5692 Py_DECREF(run_ret);
5693 }
5694 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005696 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005697 PyErr_Clear();
5698 }
5699 else
5700 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005701}
5702
5703static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5704static int code_hdr_len = 30;
5705
5706 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005707run_do(const char *cmd, void *arg UNUSED
5708#ifdef PY_CAN_RECURSE
5709 , PyGILState_STATE *pygilstate
5710#endif
5711 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005712{
5713 PyInt lnum;
5714 size_t len;
5715 char *code;
5716 int status;
5717 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005718 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005719 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005720
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005721 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005723 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724 return;
5725 }
5726
5727 len = code_hdr_len + STRLEN(cmd);
5728 code = PyMem_New(char, len + 1);
5729 memcpy(code, code_hdr, code_hdr_len);
5730 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005731 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5732 status = -1;
5733 if (run_ret != NULL)
5734 {
5735 status = 0;
5736 Py_DECREF(run_ret);
5737 }
5738 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5739 {
5740 PyMem_Free(code);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005741 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005742 PyErr_Clear();
5743 return;
5744 }
5745 else
5746 PyErr_PrintEx(1);
5747
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005748 PyMem_Free(code);
5749
5750 if (status)
5751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005752 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005753 return;
5754 }
5755
5756 status = 0;
5757 pymain = PyImport_AddModule("__main__");
5758 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005759#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005760 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005761#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005762
5763 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5764 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005765 PyObject *line;
5766 PyObject *linenr;
5767 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005768
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005769#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005770 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005771#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005772 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005773 if (lnum > curbuf->b_ml.ml_line_count
5774 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005775 goto err;
5776 if (!(linenr = PyInt_FromLong((long) lnum)))
5777 {
5778 Py_DECREF(line);
5779 goto err;
5780 }
5781 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5782 Py_DECREF(line);
5783 Py_DECREF(linenr);
5784 if (!ret)
5785 goto err;
5786
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005787 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005788 if (curbuf != was_curbuf)
5789 {
5790 Py_XDECREF(ret);
5791 goto err;
5792 }
5793
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005794 if (ret != Py_None)
5795 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005796 {
5797 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005798 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005799 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005800
5801 Py_XDECREF(ret);
5802 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005803#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005804 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005805#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806 }
5807 goto out;
5808err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005809#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005810 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005811#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005812 PyErr_PrintEx(0);
5813 PythonIO_Flush();
5814 status = 1;
5815out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005816#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005817 if (!status)
5818 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005819#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005820 Py_DECREF(pyfunc);
5821 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5822 if (status)
5823 return;
5824 check_cursor();
5825 update_curbuf(NOT_VALID);
5826}
5827
5828 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005829run_eval(const char *cmd, typval_T *rettv
5830#ifdef PY_CAN_RECURSE
5831 , PyGILState_STATE *pygilstate UNUSED
5832#endif
5833 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005834{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005835 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005836
Bram Moolenaar41009372013-07-01 22:03:04 +02005837 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005838 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005839 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005840 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005842 semsg(_(e_py_systemexit), "python");
Bram Moolenaar41009372013-07-01 22:03:04 +02005843 PyErr_Clear();
5844 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005845 else
5846 {
5847 if (PyErr_Occurred() && !msg_silent)
5848 PyErr_PrintEx(0);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005849 emsg(_("E858: Eval did not return a valid python object"));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005850 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005851 }
5852 else
5853 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005854 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar86181df2020-05-11 23:14:04 +02005855 emsg(_("E859: Failed to convert returned python object to a Vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005856 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005857 }
5858 PyErr_Clear();
5859}
5860
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005861 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005862set_ref_in_py(const int copyID)
5863{
5864 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005865 list_T *ll;
5866 int i;
5867 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005868 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005869
5870 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005871 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005872 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005873 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5874 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005876
5877 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005878 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005879 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005880 {
5881 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005882 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005883 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005884 }
5885
Bram Moolenaar8110a092016-04-14 15:56:09 +02005886 if (lastfunc != NULL)
5887 {
5888 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5889 {
5890 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005891 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005892 if (func->argc)
5893 for (i = 0; !abort && i < func->argc; ++i)
5894 abort = abort
5895 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5896 }
5897 }
5898
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005899 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005900}
5901
5902 static int
5903set_string_copy(char_u *str, typval_T *tv)
5904{
5905 tv->vval.v_string = vim_strsave(str);
5906 if (tv->vval.v_string == NULL)
5907 {
5908 PyErr_NoMemory();
5909 return -1;
5910 }
5911 return 0;
5912}
5913
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005914 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005915pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005917 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918 char_u *key;
5919 dictitem_T *di;
5920 PyObject *keyObject;
5921 PyObject *valObject;
5922 Py_ssize_t iter = 0;
5923
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005924 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005925 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926
5927 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005928 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005929
5930 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5931 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005932 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005933
Bram Moolenaara03e6312013-05-29 22:49:26 +02005934 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005935 {
5936 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005938 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005940 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005941 {
5942 dict_unref(dict);
5943 return -1;
5944 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005945
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005946 if (*key == NUL)
5947 {
5948 dict_unref(dict);
5949 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005950 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005951 return -1;
5952 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953
5954 di = dictitem_alloc(key);
5955
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005956 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005957
5958 if (di == NULL)
5959 {
5960 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005961 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005962 return -1;
5963 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005964
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005965 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 {
5967 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005968 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969 return -1;
5970 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005971
5972 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005974 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005975 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005977 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005978 return -1;
5979 }
5980 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981
5982 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983 return 0;
5984}
5985
5986 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005987pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005988{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005989 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005990 char_u *key;
5991 dictitem_T *di;
5992 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005993 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005994 PyObject *keyObject;
5995 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005996
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005997 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005999
6000 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006001 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006002
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006003 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006004 {
6005 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006006 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006007 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006008
6009 if (!(iterator = PyObject_GetIter(list)))
6010 {
6011 dict_unref(dict);
6012 Py_DECREF(list);
6013 return -1;
6014 }
6015 Py_DECREF(list);
6016
6017 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006018 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006019 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006020
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006021 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006022 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006024 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006025 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006026 return -1;
6027 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006028
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006029 if (*key == NUL)
6030 {
6031 Py_DECREF(keyObject);
6032 Py_DECREF(iterator);
6033 Py_XDECREF(todecref);
6034 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006035 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006036 return -1;
6037 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006038
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006039 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006040 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006041 Py_DECREF(keyObject);
6042 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006043 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006044 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006045 return -1;
6046 }
6047
6048 di = dictitem_alloc(key);
6049
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006050 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006051 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006053 if (di == NULL)
6054 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006055 Py_DECREF(iterator);
6056 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006057 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006058 PyErr_NoMemory();
6059 return -1;
6060 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006062 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006063 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006064 Py_DECREF(iterator);
6065 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006066 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006067 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 return -1;
6069 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006070
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006071 Py_DECREF(valObject);
6072
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006073 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006074 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006075 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006076 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006077 dictitem_free(di);
6078 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006079 return -1;
6080 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006081 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006082 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006083 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006084 return 0;
6085}
6086
6087 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006088pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006089{
6090 list_T *l;
6091
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006092 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006093 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006094
6095 tv->v_type = VAR_LIST;
6096 tv->vval.v_list = l;
6097
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006098 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006099 {
6100 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006101 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006102 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006103
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006104 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006105 return 0;
6106}
6107
Bram Moolenaardb913952012-06-29 12:54:53 +02006108typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6109
6110 static int
6111convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006112 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006113{
6114 PyObject *capsule;
6115 char hexBuf[sizeof(void *) * 2 + 3];
6116
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006117 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006118
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006119#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006120 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006121#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006122 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006123#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006124 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006125 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006126#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006127 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006128#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006129 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006130#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006131 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6132 {
6133 Py_DECREF(capsule);
6134 tv->v_type = VAR_UNKNOWN;
6135 return -1;
6136 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006137
6138 Py_DECREF(capsule);
6139
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006140 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006141 {
6142 tv->v_type = VAR_UNKNOWN;
6143 return -1;
6144 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006145 // As we are not using copy_tv which increments reference count we must
6146 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006147 if (tv->v_type == VAR_DICT)
6148 ++tv->vval.v_dict->dv_refcount;
6149 else if (tv->v_type == VAR_LIST)
6150 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006151 }
6152 else
6153 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006154 typval_T *v;
6155
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006156#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006157 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006158#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006159 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006160#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006161 copy_tv(v, tv);
6162 }
6163 return 0;
6164}
6165
6166 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006167ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6168{
6169 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006170 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006171
6172 if (!(lookup_dict = PyDict_New()))
6173 return -1;
6174
6175 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6176 {
6177 tv->v_type = VAR_DICT;
6178 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6179 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006180 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006181 }
6182 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006183 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006184 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006185 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006186 else
6187 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006188 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006189 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006190 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006191 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006192 }
6193 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006194 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006195}
6196
6197 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006198ConvertFromPySequence(PyObject *obj, typval_T *tv)
6199{
6200 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006201 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006202
6203 if (!(lookup_dict = PyDict_New()))
6204 return -1;
6205
6206 if (PyType_IsSubtype(obj->ob_type, &ListType))
6207 {
6208 tv->v_type = VAR_LIST;
6209 tv->vval.v_list = (((ListObject *)(obj))->list);
6210 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006211 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006212 }
6213 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006214 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006215 else
6216 {
6217 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006218 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006219 Py_TYPE_NAME(obj));
6220 ret = -1;
6221 }
6222 Py_DECREF(lookup_dict);
6223 return ret;
6224}
6225
6226 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006227ConvertFromPyObject(PyObject *obj, typval_T *tv)
6228{
6229 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006230 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006231
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006232 if (!(lookup_dict = PyDict_New()))
6233 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006234 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006235 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006236 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006237}
6238
6239 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006240_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006241{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006242 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006243 {
6244 tv->v_type = VAR_DICT;
6245 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6246 ++tv->vval.v_dict->dv_refcount;
6247 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006248 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006249 {
6250 tv->v_type = VAR_LIST;
6251 tv->vval.v_list = (((ListObject *)(obj))->list);
6252 ++tv->vval.v_list->lv_refcount;
6253 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006254 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006255 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006256 FunctionObject *func = (FunctionObject *) obj;
6257 if (func->self != NULL || func->argv != NULL)
6258 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006259 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6260
Bram Moolenaar8110a092016-04-14 15:56:09 +02006261 set_partial(func, pt, TRUE);
6262 tv->vval.v_partial = pt;
6263 tv->v_type = VAR_PARTIAL;
6264 }
6265 else
6266 {
6267 if (set_string_copy(func->name, tv) == -1)
6268 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006269
Bram Moolenaar8110a092016-04-14 15:56:09 +02006270 tv->v_type = VAR_FUNC;
6271 }
6272 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006273 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006274 else if (PyBytes_Check(obj))
6275 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006276 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006277
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006278 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006279 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006280 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006281 return -1;
6282
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006283 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006284 return -1;
6285
6286 tv->v_type = VAR_STRING;
6287 }
6288 else if (PyUnicode_Check(obj))
6289 {
6290 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006291 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006292
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006293 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006294 if (bytes == NULL)
6295 return -1;
6296
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006297 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006298 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006299 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006300 return -1;
6301
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006302 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006303 {
6304 Py_XDECREF(bytes);
6305 return -1;
6306 }
6307 Py_XDECREF(bytes);
6308
6309 tv->v_type = VAR_STRING;
6310 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006311#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006312 else if (PyInt_Check(obj))
6313 {
6314 tv->v_type = VAR_NUMBER;
6315 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006316 if (PyErr_Occurred())
6317 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006318 }
6319#endif
6320 else if (PyLong_Check(obj))
6321 {
6322 tv->v_type = VAR_NUMBER;
6323 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006324 if (PyErr_Occurred())
6325 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006326 }
6327 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006328 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006329#ifdef FEAT_FLOAT
6330 else if (PyFloat_Check(obj))
6331 {
6332 tv->v_type = VAR_FLOAT;
6333 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6334 }
6335#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006336 else if (PyObject_HasAttrString(obj, "keys"))
6337 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006338 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006339 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006340 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006341 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006342 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006343 else if (PyNumber_Check(obj))
6344 {
6345 PyObject *num;
6346
6347 if (!(num = PyNumber_Long(obj)))
6348 return -1;
6349
6350 tv->v_type = VAR_NUMBER;
6351 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6352
6353 Py_DECREF(num);
6354 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006355 else if (obj == Py_None)
6356 {
6357 tv->v_type = VAR_SPECIAL;
6358 tv->vval.v_number = VVAL_NONE;
6359 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006360 else
6361 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006362 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006363 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006364 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006365 return -1;
6366 }
6367 return 0;
6368}
6369
6370 static PyObject *
6371ConvertToPyObject(typval_T *tv)
6372{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006373 typval_T *argv;
6374 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006375 if (tv == NULL)
6376 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006377 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006378 return NULL;
6379 }
6380 switch (tv->v_type)
6381 {
6382 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006383 return PyBytes_FromString(tv->vval.v_string == NULL
6384 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006385 case VAR_NUMBER:
6386 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006387 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006388#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006389 return PyFloat_FromDouble((double) tv->vval.v_float);
6390#endif
6391 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006392 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006393 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006394 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006396 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006397 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006398 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006399 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006400 if (tv->vval.v_partial->pt_argc)
6401 {
6402 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6403 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6404 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6405 }
6406 else
6407 argv = NULL;
6408 if (tv->vval.v_partial->pt_dict != NULL)
6409 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006410 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006411 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006412 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006413 tv->vval.v_partial->pt_dict,
6414 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006415 case VAR_BLOB:
6416 return PyBytes_FromStringAndSize(
6417 (char*) tv->vval.v_blob->bv_ga.ga_data,
6418 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006419 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006420 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006422 case VAR_CHANNEL:
6423 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006424 Py_INCREF(Py_None);
6425 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006426 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006427 case VAR_SPECIAL:
6428 switch (tv->vval.v_number)
6429 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006430 case VVAL_FALSE: return ALWAYS_FALSE;
6431 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006432 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006433 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006434 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006435 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006436 return NULL;
6437 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006438 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006439}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006440
6441typedef struct
6442{
6443 PyObject_HEAD
6444} CurrentObject;
6445static PyTypeObject CurrentType;
6446
6447 static void
6448init_structs(void)
6449{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006450 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006451 OutputType.tp_name = "vim.message";
6452 OutputType.tp_basicsize = sizeof(OutputObject);
6453 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6454 OutputType.tp_doc = "vim message object";
6455 OutputType.tp_methods = OutputMethods;
6456#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006457 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6458 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006459 OutputType.tp_alloc = call_PyType_GenericAlloc;
6460 OutputType.tp_new = call_PyType_GenericNew;
6461 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006462 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006463#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006464 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6465 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006466 // Disabled, because this causes a crash in test86
6467 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006468#endif
6469
Bram Moolenaara80faa82020-04-12 19:37:17 +02006470 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006471 IterType.tp_name = "vim.iter";
6472 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006473 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006474 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006475 IterType.tp_iter = (getiterfunc)IterIter;
6476 IterType.tp_iternext = (iternextfunc)IterNext;
6477 IterType.tp_dealloc = (destructor)IterDestructor;
6478 IterType.tp_traverse = (traverseproc)IterTraverse;
6479 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006480
Bram Moolenaara80faa82020-04-12 19:37:17 +02006481 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006482 BufferType.tp_name = "vim.buffer";
6483 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006484 BufferType.tp_dealloc = (destructor)BufferDestructor;
6485 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006486 BufferType.tp_as_sequence = &BufferAsSeq;
6487 BufferType.tp_as_mapping = &BufferAsMapping;
6488 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6489 BufferType.tp_doc = "vim buffer object";
6490 BufferType.tp_methods = BufferMethods;
6491#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006492 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006493 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006494 BufferType.tp_alloc = call_PyType_GenericAlloc;
6495 BufferType.tp_new = call_PyType_GenericNew;
6496 BufferType.tp_free = call_PyObject_Free;
6497#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006498 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006499 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006500#endif
6501
Bram Moolenaara80faa82020-04-12 19:37:17 +02006502 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006503 WindowType.tp_name = "vim.window";
6504 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 WindowType.tp_dealloc = (destructor)WindowDestructor;
6506 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006507 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006508 WindowType.tp_doc = "vim Window object";
6509 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006510 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6511 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006512#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006513 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6514 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006515 WindowType.tp_alloc = call_PyType_GenericAlloc;
6516 WindowType.tp_new = call_PyType_GenericNew;
6517 WindowType.tp_free = call_PyObject_Free;
6518#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006519 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6520 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006521#endif
6522
Bram Moolenaara80faa82020-04-12 19:37:17 +02006523 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006524 TabPageType.tp_name = "vim.tabpage";
6525 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006526 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6527 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006528 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6529 TabPageType.tp_doc = "vim tab page object";
6530 TabPageType.tp_methods = TabPageMethods;
6531#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006532 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006533 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6534 TabPageType.tp_new = call_PyType_GenericNew;
6535 TabPageType.tp_free = call_PyObject_Free;
6536#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006537 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006538#endif
6539
Bram Moolenaara80faa82020-04-12 19:37:17 +02006540 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006541 BufMapType.tp_name = "vim.bufferlist";
6542 BufMapType.tp_basicsize = sizeof(BufMapObject);
6543 BufMapType.tp_as_mapping = &BufMapAsMapping;
6544 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006545 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006546 BufferType.tp_doc = "vim buffer list";
6547
Bram Moolenaara80faa82020-04-12 19:37:17 +02006548 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006549 WinListType.tp_name = "vim.windowlist";
6550 WinListType.tp_basicsize = sizeof(WinListType);
6551 WinListType.tp_as_sequence = &WinListAsSeq;
6552 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6553 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006554 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006555
Bram Moolenaara80faa82020-04-12 19:37:17 +02006556 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006557 TabListType.tp_name = "vim.tabpagelist";
6558 TabListType.tp_basicsize = sizeof(TabListType);
6559 TabListType.tp_as_sequence = &TabListAsSeq;
6560 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6561 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006562
Bram Moolenaara80faa82020-04-12 19:37:17 +02006563 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006564 RangeType.tp_name = "vim.range";
6565 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006566 RangeType.tp_dealloc = (destructor)RangeDestructor;
6567 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568 RangeType.tp_as_sequence = &RangeAsSeq;
6569 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006570 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006571 RangeType.tp_doc = "vim Range object";
6572 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006573 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6574 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006575#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006576 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006577 RangeType.tp_alloc = call_PyType_GenericAlloc;
6578 RangeType.tp_new = call_PyType_GenericNew;
6579 RangeType.tp_free = call_PyObject_Free;
6580#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006581 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582#endif
6583
Bram Moolenaara80faa82020-04-12 19:37:17 +02006584 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006585 CurrentType.tp_name = "vim.currentdata";
6586 CurrentType.tp_basicsize = sizeof(CurrentObject);
6587 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6588 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006589 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006590#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006591 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6592 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006593#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006594 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6595 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006596#endif
6597
Bram Moolenaara80faa82020-04-12 19:37:17 +02006598 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006599 DictionaryType.tp_name = "vim.dictionary";
6600 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006601 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006602 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006603 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006604 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006605 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006606 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006607 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6608 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6609 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006610#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006611 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6612 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006613#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006614 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6615 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616#endif
6617
Bram Moolenaara80faa82020-04-12 19:37:17 +02006618 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006620 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006621 ListType.tp_basicsize = sizeof(ListObject);
6622 ListType.tp_as_sequence = &ListAsSeq;
6623 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006624 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006625 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006626 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006627 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006628 ListType.tp_new = (newfunc)ListConstructor;
6629 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006630#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006631 ListType.tp_getattro = (getattrofunc)ListGetattro;
6632 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006633#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006634 ListType.tp_getattr = (getattrfunc)ListGetattr;
6635 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006636#endif
6637
Bram Moolenaara80faa82020-04-12 19:37:17 +02006638 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006639 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006640 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006641 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6642 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006643 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006644 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006645 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006646 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006647 FunctionType.tp_new = (newfunc)FunctionConstructor;
6648 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006649#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006650 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006651#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006652 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006653#endif
6654
Bram Moolenaara80faa82020-04-12 19:37:17 +02006655 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006656 OptionsType.tp_name = "vim.options";
6657 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006658 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006659 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006660 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006661 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006662 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006663 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6664 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6665 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006666
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006667#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006668 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006669 LoaderType.tp_name = "vim.Loader";
6670 LoaderType.tp_basicsize = sizeof(LoaderObject);
6671 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6672 LoaderType.tp_doc = "vim message object";
6673 LoaderType.tp_methods = LoaderMethods;
6674 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006675#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006676
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006677#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006678 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006679 vimmodule.m_name = "vim";
6680 vimmodule.m_doc = "Vim Python interface\n";
6681 vimmodule.m_size = -1;
6682 vimmodule.m_methods = VimMethods;
6683#endif
6684}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006685
6686#define PYTYPE_READY(type) \
6687 if (PyType_Ready(&type)) \
6688 return -1;
6689
6690 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006691init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006692{
6693 PYTYPE_READY(IterType);
6694 PYTYPE_READY(BufferType);
6695 PYTYPE_READY(RangeType);
6696 PYTYPE_READY(WindowType);
6697 PYTYPE_READY(TabPageType);
6698 PYTYPE_READY(BufMapType);
6699 PYTYPE_READY(WinListType);
6700 PYTYPE_READY(TabListType);
6701 PYTYPE_READY(CurrentType);
6702 PYTYPE_READY(DictionaryType);
6703 PYTYPE_READY(ListType);
6704 PYTYPE_READY(FunctionType);
6705 PYTYPE_READY(OptionsType);
6706 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006707#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006708 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006709#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006710 return 0;
6711}
6712
6713 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006714init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006715{
6716 PyObject *path;
6717 PyObject *path_hook;
6718 PyObject *path_hooks;
6719
6720 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6721 return -1;
6722
6723 if (!(path_hooks = PySys_GetObject("path_hooks")))
6724 {
6725 PyErr_Clear();
6726 path_hooks = PyList_New(1);
6727 PyList_SET_ITEM(path_hooks, 0, path_hook);
6728 if (PySys_SetObject("path_hooks", path_hooks))
6729 {
6730 Py_DECREF(path_hooks);
6731 return -1;
6732 }
6733 Py_DECREF(path_hooks);
6734 }
6735 else if (PyList_Check(path_hooks))
6736 {
6737 if (PyList_Append(path_hooks, path_hook))
6738 {
6739 Py_DECREF(path_hook);
6740 return -1;
6741 }
6742 Py_DECREF(path_hook);
6743 }
6744 else
6745 {
6746 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006747 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006748 "You should now do the following:\n"
6749 "- append vim.path_hook to sys.path_hooks\n"
6750 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006751 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006752 Py_DECREF(path_hook);
6753 return 0;
6754 }
6755
6756 if (!(path = PySys_GetObject("path")))
6757 {
6758 PyErr_Clear();
6759 path = PyList_New(1);
6760 Py_INCREF(vim_special_path_object);
6761 PyList_SET_ITEM(path, 0, vim_special_path_object);
6762 if (PySys_SetObject("path", path))
6763 {
6764 Py_DECREF(path);
6765 return -1;
6766 }
6767 Py_DECREF(path);
6768 }
6769 else if (PyList_Check(path))
6770 {
6771 if (PyList_Append(path, vim_special_path_object))
6772 return -1;
6773 }
6774 else
6775 {
6776 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006777 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006778 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006779 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006780 }
6781
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006782 return 0;
6783}
6784
6785static BufMapObject TheBufferMap =
6786{
6787 PyObject_HEAD_INIT(&BufMapType)
6788};
6789
6790static WinListObject TheWindowList =
6791{
6792 PyObject_HEAD_INIT(&WinListType)
6793 NULL
6794};
6795
6796static CurrentObject TheCurrent =
6797{
6798 PyObject_HEAD_INIT(&CurrentType)
6799};
6800
6801static TabListObject TheTabPageList =
6802{
6803 PyObject_HEAD_INIT(&TabListType)
6804};
6805
6806static struct numeric_constant {
6807 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006808 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006809} numeric_constants[] = {
6810 {"VAR_LOCKED", VAR_LOCKED},
6811 {"VAR_FIXED", VAR_FIXED},
6812 {"VAR_SCOPE", VAR_SCOPE},
6813 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6814};
6815
6816static struct object_constant {
6817 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006818 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006819} object_constants[] = {
6820 {"buffers", (PyObject *)(void *)&TheBufferMap},
6821 {"windows", (PyObject *)(void *)&TheWindowList},
6822 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6823 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006824
6825 {"Buffer", (PyObject *)&BufferType},
6826 {"Range", (PyObject *)&RangeType},
6827 {"Window", (PyObject *)&WindowType},
6828 {"TabPage", (PyObject *)&TabPageType},
6829 {"Dictionary", (PyObject *)&DictionaryType},
6830 {"List", (PyObject *)&ListType},
6831 {"Function", (PyObject *)&FunctionType},
6832 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006833#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006834 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006835#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006836};
6837
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006838#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006839 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840 return -1;
6841
6842#define ADD_CHECKED_OBJECT(m, name, obj) \
6843 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006844 PyObject *valObject = obj; \
6845 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006846 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006847 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006848 }
6849
6850 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006851populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006852{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006853 int i;
6854 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006855 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006856 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006857#if PY_VERSION_HEX >= 0x030700f0
6858 PyObject *dict;
6859 PyObject *cls;
6860#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006861
6862 for (i = 0; i < (int)(sizeof(numeric_constants)
6863 / sizeof(struct numeric_constant));
6864 ++i)
6865 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006866 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006867
6868 for (i = 0; i < (int)(sizeof(object_constants)
6869 / sizeof(struct object_constant));
6870 ++i)
6871 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006872 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006873
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006874 valObject = object_constants[i].valObject;
6875 Py_INCREF(valObject);
6876 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006877 }
6878
6879 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6880 return -1;
6881 ADD_OBJECT(m, "error", VimError);
6882
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006883 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6884 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006885 ADD_CHECKED_OBJECT(m, "options",
6886 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006887
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006888 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006890 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891
Bram Moolenaar22081f42016-06-01 20:38:34 +02006892#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006893 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006895#else
6896 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6897 return -1;
6898#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006899 ADD_OBJECT(m, "_getcwd", py_getcwd)
6900
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006901 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006902 return -1;
6903 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006904 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006905 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006906 if (PyObject_SetAttrString(other_module, "chdir", attr))
6907 {
6908 Py_DECREF(attr);
6909 return -1;
6910 }
6911 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006912
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006913 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006914 {
6915 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006916 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006917 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006918 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6919 {
6920 Py_DECREF(attr);
6921 return -1;
6922 }
6923 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006924 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006925 else
6926 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006927
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006928 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6929 return -1;
6930
6931 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6932
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006933#if PY_VERSION_HEX >= 0x030700f0
6934 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6935 return -1;
6936
6937 dict = PyModule_GetDict(imp);
6938
6939 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6940 {
6941 Py_DECREF(imp);
6942 return -1;
6943 }
6944
6945 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6946 {
6947 Py_DECREF(imp);
6948 return -1;
6949 }
6950
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006951 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6952 {
6953 // find_module() is deprecated, this may stop working in some later
6954 // version.
6955 ADD_OBJECT(m, "_find_module", py_find_module);
6956 }
6957
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006958 Py_DECREF(imp);
6959
6960 ADD_OBJECT(m, "_find_spec", py_find_spec);
6961#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006962 if (!(imp = PyImport_ImportModule("imp")))
6963 return -1;
6964
6965 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6966 {
6967 Py_DECREF(imp);
6968 return -1;
6969 }
6970
6971 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6972 {
6973 Py_DECREF(py_find_module);
6974 Py_DECREF(imp);
6975 return -1;
6976 }
6977
6978 Py_DECREF(imp);
6979
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006980 ADD_OBJECT(m, "_find_module", py_find_module);
6981 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006982#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006983
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006984 return 0;
6985}