blob: 128d12e87cfbbf4e209c1bd75c4fdeea7e0b49c4 [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 Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010017typedef int Py_ssize_t; // Python 2.4 and earlier don't have this type.
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020018#endif
19
Bram Moolenaard518f952020-01-01 15:04:17 +010020// Use values that are known to work, others may make Vim crash.
21#define ENC_OPT (enc_utf8 ? "utf-8" : enc_dbcs ? "euc-jp" : (char *)p_enc)
Bram Moolenaard620aa92013-05-17 16:40:06 +020022#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020023
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020024static const char *vim_special_path = "_vim_path_";
25
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020026#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020027#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020028#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010029#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
30#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
31#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020032
33#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
34 ? "(NULL)" \
35 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020036
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020037#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020038 N_("empty keys are not allowed"))
39#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
40#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
41#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
42#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
43#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
44#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020045#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020046 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020047#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020049 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020050
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020051#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
52#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020053#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020054
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020055typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020056typedef void (*runner)(const char *, void *
57#ifdef PY_CAN_RECURSE
58 , PyGILState_STATE *
59#endif
60 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020061
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020062static int ConvertFromPyObject(PyObject *, typval_T *);
63static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020064static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020065static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020066static PyObject *WindowNew(win_T *, tabpage_T *);
67static PyObject *BufferNew (buf_T *);
68static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020069
70static PyInt RangeStart;
71static PyInt RangeEnd;
72
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020073static PyObject *globals;
74
Bram Moolenaarf4258302013-06-02 18:20:17 +020075static PyObject *py_chdir;
76static PyObject *py_fchdir;
77static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020078static PyObject *vim_module;
79static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020080
Bram Moolenaar79a494d2018-07-22 04:30:21 +020081#if PY_VERSION_HEX >= 0x030700f0
82static PyObject *py_find_spec;
83#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020084static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020085#endif
Bram Moolenaarb999ba22019-02-14 13:28:45 +010086static PyObject *py_find_module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +020087
88static PyObject *VimError;
89
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020090/*
91 * obtain a lock on the Vim data structures
92 */
93 static void
94Python_Lock_Vim(void)
95{
96}
97
98/*
99 * release a lock on the Vim data structures
100 */
101 static void
102Python_Release_Vim(void)
103{
104}
105
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200106/*
107 * The "todecref" argument holds a pointer to PyObject * that must be
108 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
109 * was needed to generate returned value is object.
110 *
111 * Use Py_XDECREF to decrement reference count.
112 */
113 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200114StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200115{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200116 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200117
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200118 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200119 {
120
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200121 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
122 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200123 return NULL;
124
125 *todecref = NULL;
126 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200127 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200129 PyObject *bytes;
130
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100131 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
132 ERRORS_ENCODE_ARG)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 return NULL;
134
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100135 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200137 {
138 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200139 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200140 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200141
142 *todecref = bytes;
143 }
144 else
145 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200146#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200147 PyErr_FORMAT(PyExc_TypeError,
148 N_("expected str() or unicode() instance, but got %s"),
149 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200151 PyErr_FORMAT(PyExc_TypeError,
152 N_("expected bytes() or str() instance, but got %s"),
153 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200154#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200155 return NULL;
156 }
157
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200158 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200159}
160
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200161#define NUMBER_LONG 1
162#define NUMBER_INT 2
163#define NUMBER_NATURAL 4
164#define NUMBER_UNSIGNED 8
165
166 static int
167NumberToLong(PyObject *obj, long *result, int flags)
168{
169#if PY_MAJOR_VERSION < 3
170 if (PyInt_Check(obj))
171 {
172 *result = PyInt_AsLong(obj);
173 if (PyErr_Occurred())
174 return -1;
175 }
176 else
177#endif
178 if (PyLong_Check(obj))
179 {
180 *result = PyLong_AsLong(obj);
181 if (PyErr_Occurred())
182 return -1;
183 }
184 else if (PyNumber_Check(obj))
185 {
186 PyObject *num;
187
188 if (!(num = PyNumber_Long(obj)))
189 return -1;
190
191 *result = PyLong_AsLong(num);
192
193 Py_DECREF(num);
194
195 if (PyErr_Occurred())
196 return -1;
197 }
198 else
199 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200200#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200201 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200202 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200203 "coercing to long(), but got %s"),
204 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200205#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200207 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 "but got %s"),
209 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200210#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200211 return -1;
212 }
213
214 if (flags & NUMBER_INT)
215 {
216 if (*result > INT_MAX)
217 {
218 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200219 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200220 return -1;
221 }
222 else if (*result < INT_MIN)
223 {
224 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200225 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200226 return -1;
227 }
228 }
229
230 if (flags & NUMBER_NATURAL)
231 {
232 if (*result <= 0)
233 {
234 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100235 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200236 return -1;
237 }
238 }
239 else if (flags & NUMBER_UNSIGNED)
240 {
241 if (*result < 0)
242 {
243 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200244 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200245 return -1;
246 }
247 }
248
249 return 0;
250}
251
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200252 static int
253add_string(PyObject *list, char *s)
254{
255 PyObject *string;
256
257 if (!(string = PyString_FromString(s)))
258 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200259
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200260 if (PyList_Append(list, string))
261 {
262 Py_DECREF(string);
263 return -1;
264 }
265
266 Py_DECREF(string);
267 return 0;
268}
269
270 static PyObject *
271ObjectDir(PyObject *self, char **attributes)
272{
273 PyMethodDef *method;
274 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200275 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200277 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200278 return NULL;
279
280 if (self)
281 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200282 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200284 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200285 return NULL;
286 }
287
288 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200291 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200292 return NULL;
293 }
294
295#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200298 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200299 return NULL;
300 }
301#endif
302
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200303 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200304}
305
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100306// Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200307
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100308// Function to write a line, points to either msg() or emsg().
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200309typedef int (*writefn)(char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200310
311static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312
313typedef struct
314{
315 PyObject_HEAD
316 long softspace;
317 long error;
318} OutputObject;
319
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200320static char *OutputAttrs[] = {
321 "softspace",
322 NULL
323};
324
325 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200326OutputDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200327{
328 return ObjectDir(self, OutputAttrs);
329}
330
Bram Moolenaar77045652012-09-21 13:46:06 +0200331 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200332OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200333{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200334 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200335 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200336 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200337 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200338 return -1;
339 }
340
341 if (strcmp(name, "softspace") == 0)
342 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200343 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200344 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200345 return 0;
346 }
347
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200348 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return -1;
350}
351
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100352// Buffer IO, we write one whole line at a time.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200353static garray_T io_ga = {0, 0, 1, 80, NULL};
354static writefn old_fn = NULL;
355
356 static void
357PythonIO_Flush(void)
358{
359 if (old_fn != NULL && io_ga.ga_len > 0)
360 {
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200361 ((char *)io_ga.ga_data)[io_ga.ga_len] = NUL;
362 old_fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200363 }
364 io_ga.ga_len = 0;
365}
366
367 static void
368writer(writefn fn, char_u *str, PyInt n)
369{
370 char_u *ptr;
371
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100372 // Flush when switching output function.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200373 if (fn != old_fn)
374 PythonIO_Flush();
375 old_fn = fn;
376
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200377 // Write each NL separated line. Text after the last NL is kept for
378 // writing later.
379 // For normal messages: Do not output when "got_int" was set. This avoids
380 // a loop gone crazy flooding the terminal with messages. Also for when
381 // "q" is pressed at the more-prompt.
382 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
383 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200384 {
385 PyInt len = ptr - str;
386
387 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
388 break;
389
390 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
391 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200392 fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200393 str = ptr + 1;
394 n -= len + 1;
395 io_ga.ga_len = 0;
396 }
397
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200398 // Put the remaining text into io_ga for later printing.
399 if (n > 0 && (fn == (writefn)emsg || !got_int)
400 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200401 {
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 io_ga.ga_len += (int)n;
404 }
405}
406
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200407 static int
408write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 Py_ssize_t len = 0;
411 char *str = NULL;
412 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200413
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200416
417 Py_BEGIN_ALLOW_THREADS
418 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200419 if (error)
420 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100421 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200422 Python_Release_Vim();
423 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200424 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200425
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200426 return 0;
427}
428
429 static PyObject *
430OutputWrite(OutputObject *self, PyObject *string)
431{
432 if (write_output(self, string))
433 return NULL;
434
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200435 Py_INCREF(Py_None);
436 return Py_None;
437}
438
439 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200440OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200441{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200442 PyObject *iterator;
443 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200445 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200448 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200450 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200451 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200452 Py_DECREF(iterator);
453 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200454 return NULL;
455 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200456 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200457 }
458
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200459 Py_DECREF(iterator);
460
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100461 // Iterator may have finished due to an exception
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200462 if (PyErr_Occurred())
463 return NULL;
464
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200465 Py_INCREF(Py_None);
466 return Py_None;
467}
468
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100469 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200470AlwaysNone(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100471{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100472 // do nothing
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100473 Py_INCREF(Py_None);
474 return Py_None;
475}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200476#define ALWAYS_NONE AlwaysNone(NULL, NULL)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100477
Bram Moolenaard4247472015-11-02 13:28:59 +0100478 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200479AlwaysFalse(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100480{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100481 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100482 PyObject *ret = Py_False;
483 Py_INCREF(ret);
484 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100485}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200486#define ALWAYS_FALSE AlwaysFalse(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100487
488 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200489AlwaysTrue(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100490{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100491 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100492 PyObject *ret = Py_True;
493 Py_INCREF(ret);
494 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100495}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200496#define ALWAYS_TRUE AlwaysTrue(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100497
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200498/***************/
499
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200500static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100501 // name, function, calling, doc
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200502 {"write", (PyCFunction)OutputWrite, METH_O, ""},
503 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100504 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
505 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
506 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
507 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
508 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
509 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200510 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200511 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200512 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200513};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200514
515static OutputObject Output =
516{
517 PyObject_HEAD_INIT(&OutputType)
518 0,
519 0
520};
521
522static OutputObject Error =
523{
524 PyObject_HEAD_INIT(&OutputType)
525 0,
526 1
527};
528
529 static int
530PythonIO_Init_io(void)
531{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200532 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
533 return -1;
534 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
535 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200536
537 if (PyErr_Occurred())
538 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000539 emsg(_(e_python_error_initialising_io_object));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200540 return -1;
541 }
542
543 return 0;
544}
545
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200546#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200547static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
548
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200549typedef struct
550{
551 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200552 char *fullname;
553 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200554} LoaderObject;
555static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200557 static void
558LoaderDestructor(LoaderObject *self)
559{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200560 vim_free(self->fullname);
561 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200562 DESTRUCTOR_FINISH(self);
563}
564
565 static PyObject *
566LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
567{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200568 char *fullname = self->fullname;
569 PyObject *result = self->result;
570 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200571
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200572 if (!fullname)
573 {
574 module = result ? result : Py_None;
575 Py_INCREF(module);
576 return module;
577 }
578
579 module = call_load_module(fullname, (int)STRLEN(fullname), result);
580
581 self->fullname = NULL;
582 self->result = module;
583
584 vim_free(fullname);
585 Py_DECREF(result);
586
587 if (!module)
588 {
589 if (PyErr_Occurred())
590 return NULL;
591
592 Py_INCREF(Py_None);
593 return Py_None;
594 }
595
596 Py_INCREF(module);
597 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200598}
599
600static struct PyMethodDef LoaderMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100601 // name, function, calling, doc
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200602 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
603 { NULL, NULL, 0, NULL}
604};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200605#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200606
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100607/*
608 * Check to see whether a Vim error has been reported, or a keyboard
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200609 * interrupt has been detected.
610 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200611 static void
612VimTryStart(void)
613{
614 ++trylevel;
615}
616
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200617 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200618VimTryEnd(void)
619{
620 --trylevel;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100621 // Without this it stops processing all subsequent Vim script commands and
622 // generates strange error messages if I e.g. try calling Test() in a cycle
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200623 did_emsg = FALSE;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100624 // Keyboard interrupt should be preferred over anything else
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200625 if (got_int)
626 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100627 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100628 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100629 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200630 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200631 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200632 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100633 else if (msg_list != NULL && *msg_list != NULL)
634 {
635 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100636 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100637
638 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
639
640 if (msg == NULL)
641 {
642 PyErr_NoMemory();
643 return -1;
644 }
645
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100646 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100647
648 free_global_msglist();
649
650 if (should_free)
651 vim_free(msg);
652
653 return -1;
654 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200655 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200656 return (PyErr_Occurred() ? -1 : 0);
Bram Moolenaar86181df2020-05-11 23:14:04 +0200657 // Python exception is preferred over Vim one; unlikely to occur though
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200658 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200659 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100660 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200661 return -1;
662 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100663 // Finally transform Vim script exception to python one
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200664 else
665 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200666 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200667 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200668 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200669 }
670}
671
672 static int
673VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200674{
675 if (got_int)
676 {
677 PyErr_SetNone(PyExc_KeyboardInterrupt);
678 return 1;
679 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200680 return 0;
681}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200682
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100683// Vim module - Implementation
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200684
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200685 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200686VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200688 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200690 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691
Bram Moolenaar389a1792013-06-23 13:00:44 +0200692 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 return NULL;
694
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695 Py_BEGIN_ALLOW_THREADS
696 Python_Lock_Vim();
697
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200698 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200699 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700 update_screen(VALID);
701
702 Python_Release_Vim();
703 Py_END_ALLOW_THREADS
704
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200705 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200706 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200711 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713}
714
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715/*
716 * Function to translate a typval_T into a PyObject; this will recursively
717 * translate lists/dictionaries into their Python equivalents.
718 *
719 * The depth parameter is to avoid infinite recursion, set it to 1 when
720 * you call VimToPython.
721 */
722 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200723VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200724{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200727 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100729 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730 if (depth > 100)
731 {
732 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200733 ret = Py_None;
734 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735 }
736
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100737 // Check if we run into a recursive loop. The item must be in lookup_dict
738 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200739 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
740 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
741 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200742 sprintf(ptrBuf, "%p",
743 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
744 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200745
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200747 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 Py_INCREF(ret);
749 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750 }
751 }
752
753 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200754 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200755 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200756 else if (our_tv->v_type == VAR_NUMBER)
757 {
758 char buf[NUMBUFLEN];
759
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100760 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200762 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100764#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 else if (our_tv->v_type == VAR_FLOAT)
766 {
767 char buf[NUMBUFLEN];
768
769 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200770 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100772#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 else if (our_tv->v_type == VAR_LIST)
774 {
775 list_T *list = our_tv->vval.v_list;
776 listitem_T *curr;
777
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200778 if (list == NULL)
779 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200781 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200782 return NULL;
783
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200785 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200786 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200787 return NULL;
788 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200789
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200790 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200791 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200792 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200793 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200798 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200799 {
800 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200801 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200802 return NULL;
803 }
804 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805 }
806 }
807 else if (our_tv->v_type == VAR_DICT)
808 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200809
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100810 hashtab_T *ht;
811 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200812 hashitem_T *hi;
813 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100814
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200815 if (our_tv->vval.v_dict == NULL)
816 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100817 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200818
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200819 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200820 return NULL;
821
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200822 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200823 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200824 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200825 return NULL;
826 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100828 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200829 for (hi = ht->ht_array; todo > 0; ++hi)
830 {
831 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200833 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200834
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200835 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200836 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200837 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200839 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200841 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200842 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200843 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200844 Py_DECREF(newObj);
845 return NULL;
846 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200847 }
848 }
849 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100850 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100851 {
852 if (our_tv->vval.v_number == VVAL_FALSE)
853 {
854 ret = Py_False;
855 Py_INCREF(ret);
856 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100857 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100858 {
859 ret = Py_True;
860 Py_INCREF(ret);
861 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100862 return ret;
863 }
864 else if (our_tv->v_type == VAR_SPECIAL)
865 {
866 Py_INCREF(Py_None);
867 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100868 return ret;
869 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100870 else if (our_tv->v_type == VAR_BLOB)
871 ret = PyBytes_FromStringAndSize(
872 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
873 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 else
875 {
876 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200877 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878 }
879
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200880 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200881}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200882
883 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200884VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200886 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200887 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200888 PyObject *string;
889 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200890 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200891 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200892
Bram Moolenaar389a1792013-06-23 13:00:44 +0200893 if (!PyArg_ParseTuple(args, "O", &string))
894 return NULL;
895
896 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200897 return NULL;
898
899 Py_BEGIN_ALLOW_THREADS
900 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200901 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200903 Python_Release_Vim();
904 Py_END_ALLOW_THREADS
905
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906 Py_XDECREF(todecref);
907
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200908 if (VimTryEnd())
909 return NULL;
910
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200911 if (our_tv == NULL)
912 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200913 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200914 return NULL;
915 }
916
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100917 // Convert the Vim type into a Python type. Create a dictionary that's
918 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200919 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200920 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200921 else
922 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200923 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200924 Py_DECREF(lookup_dict);
925 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200926
927
928 Py_BEGIN_ALLOW_THREADS
929 Python_Lock_Vim();
930 free_tv(our_tv);
931 Python_Release_Vim();
932 Py_END_ALLOW_THREADS
933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200934 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200935}
936
Bram Moolenaardb913952012-06-29 12:54:53 +0200937static PyObject *ConvertToPyObject(typval_T *);
938
939 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200941{
Bram Moolenaardb913952012-06-29 12:54:53 +0200942 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200943 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200944 char_u *expr;
945 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200946
Bram Moolenaar389a1792013-06-23 13:00:44 +0200947 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200948 return NULL;
949
950 Py_BEGIN_ALLOW_THREADS
951 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200952 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200954 Python_Release_Vim();
955 Py_END_ALLOW_THREADS
956
Bram Moolenaar389a1792013-06-23 13:00:44 +0200957 Py_XDECREF(todecref);
958
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200959 if (VimTryEnd())
960 return NULL;
961
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 if (our_tv == NULL)
963 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200964 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200965 return NULL;
966 }
967
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200969 Py_BEGIN_ALLOW_THREADS
970 Python_Lock_Vim();
971 free_tv(our_tv);
972 Python_Release_Vim();
973 Py_END_ALLOW_THREADS
974
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200975 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200976}
977
978 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200979VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200980{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200981 char_u *str;
982 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200983 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200984
Bram Moolenaar389a1792013-06-23 13:00:44 +0200985 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200986 return NULL;
987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200988 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200989
990 Py_XDECREF(todecref);
991
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200992 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200993}
994
Bram Moolenaarf4258302013-06-02 18:20:17 +0200995 static PyObject *
996_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
997{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200998 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200999 PyObject *newwd;
1000 PyObject *todecref;
1001 char_u *new_dir;
1002
Bram Moolenaard4209d22013-06-05 20:34:15 +02001003 if (_chdir == NULL)
1004 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001005 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001006 return NULL;
1007
1008 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1009 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001010 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001011 return NULL;
1012 }
1013
1014 if (!(new_dir = StringToChars(newwd, &todecref)))
1015 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001016 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001017 Py_DECREF(newwd);
1018 return NULL;
1019 }
1020
1021 VimTryStart();
1022
1023 if (vim_chdir(new_dir))
1024 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001025 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001026 Py_DECREF(newwd);
1027 Py_XDECREF(todecref);
1028
1029 if (VimTryEnd())
1030 return NULL;
1031
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001032 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001033 return NULL;
1034 }
1035
1036 Py_DECREF(newwd);
1037 Py_XDECREF(todecref);
1038
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001039 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001040
1041 if (VimTryEnd())
1042 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001043 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001044 return NULL;
1045 }
1046
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001047 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001048}
1049
1050 static PyObject *
1051VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1052{
1053 return _VimChdir(py_chdir, args, kwargs);
1054}
1055
1056 static PyObject *
1057VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1058{
1059 return _VimChdir(py_fchdir, args, kwargs);
1060}
1061
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001062typedef struct {
1063 PyObject *callable;
1064 PyObject *result;
1065} map_rtp_data;
1066
1067 static void
1068map_rtp_callback(char_u *path, void *_data)
1069{
1070 void **data = (void **) _data;
1071 PyObject *pathObject;
1072 map_rtp_data *mr_data = *((map_rtp_data **) data);
1073
Bram Moolenaar41009372013-07-01 22:03:04 +02001074 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001075 {
1076 *data = NULL;
1077 return;
1078 }
1079
1080 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1081 pathObject, NULL);
1082
1083 Py_DECREF(pathObject);
1084
1085 if (!mr_data->result || mr_data->result != Py_None)
1086 *data = NULL;
1087 else
1088 {
1089 Py_DECREF(mr_data->result);
1090 mr_data->result = NULL;
1091 }
1092}
1093
1094 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001095VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001096{
1097 map_rtp_data data;
1098
Bram Moolenaar389a1792013-06-23 13:00:44 +02001099 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001100 data.result = NULL;
1101
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001102 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001103
1104 if (data.result == NULL)
1105 {
1106 if (PyErr_Occurred())
1107 return NULL;
1108 else
1109 {
1110 Py_INCREF(Py_None);
1111 return Py_None;
1112 }
1113 }
1114 return data.result;
1115}
1116
1117/*
1118 * _vim_runtimepath_ special path implementation.
1119 */
1120
1121 static void
1122map_finder_callback(char_u *path, void *_data)
1123{
1124 void **data = (void **) _data;
1125 PyObject *list = *((PyObject **) data);
1126 PyObject *pathObject1, *pathObject2;
1127 char *pathbuf;
1128 size_t pathlen;
1129
1130 pathlen = STRLEN(path);
1131
1132#if PY_MAJOR_VERSION < 3
1133# define PY_MAIN_DIR_STRING "python2"
1134#else
1135# define PY_MAIN_DIR_STRING "python3"
1136#endif
1137#define PY_ALTERNATE_DIR_STRING "pythonx"
1138
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001139#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001140 if (!(pathbuf = PyMem_New(char,
1141 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1142 {
1143 PyErr_NoMemory();
1144 *data = NULL;
1145 return;
1146 }
1147
1148 mch_memmove(pathbuf, path, pathlen + 1);
1149 add_pathsep((char_u *) pathbuf);
1150
1151 pathlen = STRLEN(pathbuf);
1152 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1153 PYTHONX_STRING_LENGTH + 1);
1154
1155 if (!(pathObject1 = PyString_FromString(pathbuf)))
1156 {
1157 *data = NULL;
1158 PyMem_Free(pathbuf);
1159 return;
1160 }
1161
1162 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1163 PYTHONX_STRING_LENGTH + 1);
1164
1165 if (!(pathObject2 = PyString_FromString(pathbuf)))
1166 {
1167 Py_DECREF(pathObject1);
1168 PyMem_Free(pathbuf);
1169 *data = NULL;
1170 return;
1171 }
1172
1173 PyMem_Free(pathbuf);
1174
1175 if (PyList_Append(list, pathObject1)
1176 || PyList_Append(list, pathObject2))
1177 *data = NULL;
1178
1179 Py_DECREF(pathObject1);
1180 Py_DECREF(pathObject2);
1181}
1182
1183 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001184Vim_GetPaths(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001185{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001186 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001187
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001188 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001189 return NULL;
1190
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001191 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001192
1193 if (PyErr_Occurred())
1194 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001195 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001196 return NULL;
1197 }
1198
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001199 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001200}
1201
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001202#if PY_VERSION_HEX >= 0x030700f0
1203 static PyObject *
1204FinderFindSpec(PyObject *self, PyObject *args)
1205{
1206 char *fullname;
1207 PyObject *paths;
1208 PyObject *target = Py_None;
1209 PyObject *spec;
1210
1211 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1212 return NULL;
1213
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001214 if (!(paths = Vim_GetPaths(self, NULL)))
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001215 return NULL;
1216
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001217 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001218
1219 Py_DECREF(paths);
1220
1221 if (!spec)
1222 {
1223 if (PyErr_Occurred())
1224 return NULL;
1225
1226 Py_INCREF(Py_None);
1227 return Py_None;
1228 }
1229
1230 return spec;
1231}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001232
1233 static PyObject *
1234FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1235{
1236 // Apparently returning None works.
1237 Py_INCREF(Py_None);
1238 return Py_None;
1239}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001240#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001241 static PyObject *
1242call_load_module(char *name, int len, PyObject *find_module_result)
1243{
1244 PyObject *fd, *pathname, *description;
1245
Bram Moolenaarc476e522013-06-23 13:46:40 +02001246 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001247 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001248 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001249 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001250 Py_TYPE_NAME(find_module_result));
1251 return NULL;
1252 }
1253 if (PyTuple_GET_SIZE(find_module_result) != 3)
1254 {
1255 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001256 N_("expected 3-tuple as imp.find_module() result, but got "
1257 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001258 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001259 return NULL;
1260 }
1261
1262 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1263 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1264 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1265 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001266 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001267 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001268 return NULL;
1269 }
1270
1271 return PyObject_CallFunction(py_load_module,
1272 "s#OOO", name, len, fd, pathname, description);
1273}
1274
1275 static PyObject *
1276find_module(char *fullname, char *tail, PyObject *new_path)
1277{
1278 PyObject *find_module_result;
1279 PyObject *module;
1280 char *dot;
1281
Bram Moolenaar41009372013-07-01 22:03:04 +02001282 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001283 {
1284 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001285 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001286 * first component
1287 */
1288 PyObject *newest_path;
1289 int partlen = (int) (dot - 1 - tail);
1290
1291 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1292 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001293 {
1294 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1295 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001296 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001297 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001298
1299 if (!(module = call_load_module(
1300 fullname,
1301 ((int) (tail - fullname)) + partlen,
1302 find_module_result)))
1303 {
1304 Py_DECREF(find_module_result);
1305 return NULL;
1306 }
1307
1308 Py_DECREF(find_module_result);
1309
1310 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1311 {
1312 Py_DECREF(module);
1313 return NULL;
1314 }
1315
1316 Py_DECREF(module);
1317
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001318 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001319
1320 Py_DECREF(newest_path);
1321
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001322 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001323 }
1324 else
1325 {
1326 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1327 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001328 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001329 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1330 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001331 return NULL;
1332 }
1333
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001334 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001335 }
1336}
1337
1338 static PyObject *
1339FinderFindModule(PyObject *self, PyObject *args)
1340{
1341 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001342 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001343 PyObject *new_path;
1344 LoaderObject *loader;
1345
1346 if (!PyArg_ParseTuple(args, "s", &fullname))
1347 return NULL;
1348
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001349 if (!(new_path = Vim_GetPaths(self, NULL)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001350 return NULL;
1351
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001352 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001353
1354 Py_DECREF(new_path);
1355
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001356 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001357 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001358 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001359 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001360
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001361 Py_INCREF(Py_None);
1362 return Py_None;
1363 }
1364
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001365 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001366 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001367 Py_DECREF(result);
1368 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001369 return NULL;
1370 }
1371
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001372 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1373 {
1374 vim_free(fullname);
1375 Py_DECREF(result);
1376 return NULL;
1377 }
1378
1379 loader->fullname = fullname;
1380 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001381
1382 return (PyObject *) loader;
1383}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001384#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001385
1386 static PyObject *
1387VimPathHook(PyObject *self UNUSED, PyObject *args)
1388{
1389 char *path;
1390
1391 if (PyArg_ParseTuple(args, "s", &path)
1392 && STRCMP(path, vim_special_path) == 0)
1393 {
1394 Py_INCREF(vim_module);
1395 return vim_module;
1396 }
1397
1398 PyErr_Clear();
1399 PyErr_SetNone(PyExc_ImportError);
1400 return NULL;
1401}
1402
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001403/*
1404 * Vim module - Definitions
1405 */
1406
1407static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001408 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001409 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001410 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001411 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001412 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001413 {"chdir", (PyCFunction)(void *)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1414 {"fchdir", (PyCFunction)(void *)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001415 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001416#if PY_VERSION_HEX >= 0x030700f0
1417 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001418#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001419 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001420 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1421 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1422 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001423};
1424
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001425/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001426 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001427 */
1428
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001429static PyTypeObject IterType;
1430
1431typedef PyObject *(*nextfun)(void **);
1432typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001433typedef int (*traversefun)(void *, visitproc, void *);
1434typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001435
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001436// Main purpose of this object is removing the need for do python
1437// initialization (i.e. PyType_Ready and setting type attributes) for a big
1438// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001439typedef struct
1440{
1441 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001442 void *cur;
1443 nextfun next;
1444 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001445 traversefun traverse;
1446 clearfun clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001447 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001448} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001449
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001450 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001451IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001452 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001453{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001454 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001455
Bram Moolenaar774267b2013-05-21 20:51:59 +02001456 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001457 self->cur = start;
1458 self->next = next;
1459 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001460 self->traverse = traverse;
1461 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001462 self->iter_object = iter_object;
1463
1464 if (iter_object)
1465 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001466
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001467 return (PyObject *)(self);
1468}
1469
1470 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001471IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001472{
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001473 if (self->iter_object)
1474 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001475 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001476 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001477 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001478}
1479
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001480 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001481IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001482{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001483 if (self->traverse != NULL)
1484 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001485 else
1486 return 0;
1487}
1488
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001489// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001490#ifdef clear
1491# undef clear
1492#endif
1493
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001494 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001495IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001496{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001497 if (self->clear != NULL)
1498 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001499 else
1500 return 0;
1501}
1502
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001503 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001504IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001505{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001506 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001507}
1508
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001509 static PyObject *
1510IterIter(PyObject *self)
1511{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001512 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001513 return self;
1514}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001515
Bram Moolenaardb913952012-06-29 12:54:53 +02001516typedef struct pylinkedlist_S {
1517 struct pylinkedlist_S *pll_next;
1518 struct pylinkedlist_S *pll_prev;
1519 PyObject *pll_obj;
1520} pylinkedlist_T;
1521
1522static pylinkedlist_T *lastdict = NULL;
1523static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001524static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001525
1526 static void
1527pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1528{
1529 if (ref->pll_prev == NULL)
1530 {
1531 if (ref->pll_next == NULL)
1532 {
1533 *last = NULL;
1534 return;
1535 }
1536 }
1537 else
1538 ref->pll_prev->pll_next = ref->pll_next;
1539
1540 if (ref->pll_next == NULL)
1541 *last = ref->pll_prev;
1542 else
1543 ref->pll_next->pll_prev = ref->pll_prev;
1544}
1545
1546 static void
1547pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1548{
1549 if (*last == NULL)
1550 ref->pll_prev = NULL;
1551 else
1552 {
1553 (*last)->pll_next = ref;
1554 ref->pll_prev = *last;
1555 }
1556 ref->pll_next = NULL;
1557 ref->pll_obj = self;
1558 *last = ref;
1559}
1560
1561static PyTypeObject DictionaryType;
1562
1563typedef struct
1564{
1565 PyObject_HEAD
1566 dict_T *dict;
1567 pylinkedlist_T ref;
1568} DictionaryObject;
1569
Bram Moolenaara9922d62013-05-30 13:01:18 +02001570static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1571
1572#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1573
Bram Moolenaardb913952012-06-29 12:54:53 +02001574 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001575DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001576{
1577 DictionaryObject *self;
1578
Bram Moolenaara9922d62013-05-30 13:01:18 +02001579 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001580 if (self == NULL)
1581 return NULL;
1582 self->dict = dict;
1583 ++dict->dv_refcount;
1584
1585 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1586
1587 return (PyObject *)(self);
1588}
1589
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001591py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001595 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001596 {
1597 PyErr_NoMemory();
1598 return NULL;
1599 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001600 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001602 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001603}
1604
1605 static PyObject *
1606DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1607{
1608 DictionaryObject *self;
1609 dict_T *dict;
1610
1611 if (!(dict = py_dict_alloc()))
1612 return NULL;
1613
1614 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1615
1616 --dict->dv_refcount;
1617
1618 if (kwargs || PyTuple_Size(args))
1619 {
1620 PyObject *tmp;
1621 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1622 {
1623 Py_DECREF(self);
1624 return NULL;
1625 }
1626
1627 Py_DECREF(tmp);
1628 }
1629
1630 return (PyObject *)(self);
1631}
1632
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001633 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001634DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001635{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001636 pyll_remove(&self->ref, &lastdict);
1637 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001638
1639 DESTRUCTOR_FINISH(self);
1640}
1641
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001642static char *DictionaryAttrs[] = {
1643 "locked", "scope",
1644 NULL
1645};
1646
1647 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001648DictionaryDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001649{
1650 return ObjectDir(self, DictionaryAttrs);
1651}
1652
Bram Moolenaardb913952012-06-29 12:54:53 +02001653 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001654DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001655{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001656 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001657 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001658 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001659 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001660 return -1;
1661 }
1662
1663 if (strcmp(name, "locked") == 0)
1664 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001665 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001666 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001667 PyErr_SET_STRING(PyExc_TypeError,
1668 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001669 return -1;
1670 }
1671 else
1672 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001673 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001674 if (istrue == -1)
1675 return -1;
1676 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001677 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001678 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001679 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001680 }
1681 return 0;
1682 }
1683 else
1684 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001685 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001686 return -1;
1687 }
1688}
1689
1690 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001691DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001692{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001693 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001694}
1695
Bram Moolenaara9922d62013-05-30 13:01:18 +02001696#define DICT_FLAG_HAS_DEFAULT 0x01
1697#define DICT_FLAG_POP 0x02
1698#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001699#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001700#define DICT_FLAG_RETURN_PAIR 0x10
1701
Bram Moolenaardb913952012-06-29 12:54:53 +02001702 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001703_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001704{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705 PyObject *keyObject;
1706 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001707 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001708 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001709 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001710 dict_T *dict = self->dict;
1711 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001712 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001713
Bram Moolenaara9922d62013-05-30 13:01:18 +02001714 if (flags & DICT_FLAG_HAS_DEFAULT)
1715 {
1716 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1717 return NULL;
1718 }
1719 else
1720 keyObject = args;
1721
1722 if (flags & DICT_FLAG_RETURN_BOOL)
1723 defObject = Py_False;
1724
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001725 if (!(key = StringToChars(keyObject, &todecref)))
1726 return NULL;
1727
1728 if (*key == NUL)
1729 {
1730 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001731 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001732 return NULL;
1733 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001734
Bram Moolenaara9922d62013-05-30 13:01:18 +02001735 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001736
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001737 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001738
Bram Moolenaara9922d62013-05-30 13:01:18 +02001739 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001740 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001741 if (defObject)
1742 {
1743 Py_INCREF(defObject);
1744 return defObject;
1745 }
1746 else
1747 {
1748 PyErr_SetObject(PyExc_KeyError, keyObject);
1749 return NULL;
1750 }
1751 }
1752 else if (flags & DICT_FLAG_RETURN_BOOL)
1753 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001754 ret = Py_True;
1755 Py_INCREF(ret);
1756 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001757 }
1758
1759 di = dict_lookup(hi);
1760
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001761 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001762 return NULL;
1763
1764 if (flags & DICT_FLAG_POP)
1765 {
1766 if (dict->dv_lock)
1767 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001768 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001769 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001770 return NULL;
1771 }
1772
1773 hash_remove(&dict->dv_hashtab, hi);
1774 dictitem_free(di);
1775 }
1776
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001777 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778}
1779
1780 static PyObject *
1781DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1782{
1783 return _DictionaryItem(self, keyObject, 0);
1784}
1785
1786 static int
1787DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1788{
1789 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001790 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001791
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001792 if (rObj == NULL)
1793 return -1;
1794
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001795 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796
Bram Moolenaardee2e312013-06-23 16:35:47 +02001797 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001799 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001800}
1801
1802typedef struct
1803{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001804 int dii_changed;
1805 hashtab_T *dii_ht;
1806 hashitem_T *dii_hi;
1807 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001808} dictiterinfo_T;
1809
1810 static PyObject *
1811DictionaryIterNext(dictiterinfo_T **dii)
1812{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001813 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001814
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001815 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816 return NULL;
1817
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001818 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001819 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001820 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001821 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001822 return NULL;
1823 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001824
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001825 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
1826 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001827
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001828 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001830 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001831 return NULL;
1832
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001833 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001834}
1835
1836 static PyObject *
1837DictionaryIter(DictionaryObject *self)
1838{
1839 dictiterinfo_T *dii;
1840 hashtab_T *ht;
1841
1842 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1843 {
1844 PyErr_NoMemory();
1845 return NULL;
1846 }
1847
1848 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001849 dii->dii_changed = ht->ht_changed;
1850 dii->dii_ht = ht;
1851 dii->dii_hi = ht->ht_array;
1852 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001853
1854 return IterNew(dii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001855 (destructorfun)(void *) PyMem_Free, (nextfun) DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001856 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001857}
1858
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001859 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001860DictionaryAssItem(
1861 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001862{
1863 char_u *key;
1864 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001865 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001867 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001868
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001869 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001871 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001872 return -1;
1873 }
1874
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001875 if (!(key = StringToChars(keyObject, &todecref)))
1876 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001877
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001878 if (*key == NUL)
1879 {
1880 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001881 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001882 return -1;
1883 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001884
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001885 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001886
1887 if (valObject == NULL)
1888 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001889 hashitem_T *hi;
1890
Bram Moolenaardb913952012-06-29 12:54:53 +02001891 if (di == NULL)
1892 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001893 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001894 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 return -1;
1896 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001897 hi = hash_find(&dict->dv_hashtab, di->di_key);
1898 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001900 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 return 0;
1902 }
1903
1904 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001905 {
1906 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001908 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001909
1910 if (di == NULL)
1911 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001912 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001913 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001914 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001915 PyErr_NoMemory();
1916 return -1;
1917 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001918 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001919
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001920 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001921 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001922 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001923 RAISE_KEY_ADD_FAIL(key);
1924 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001925 return -1;
1926 }
1927 }
1928 else
1929 clear_tv(&di->di_tv);
1930
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001931 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001932
1933 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001934 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001935 return 0;
1936}
1937
Bram Moolenaara9922d62013-05-30 13:01:18 +02001938typedef PyObject *(*hi_to_py)(hashitem_T *);
1939
Bram Moolenaardb913952012-06-29 12:54:53 +02001940 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001941DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001942{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001943 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001944 long_u todo = dict->dv_hashtab.ht_used;
1945 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001946 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001947 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001948 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001949
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001950 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001951 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1952 {
1953 if (!HASHITEM_EMPTY(hi))
1954 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001955 if (!(newObj = hiconvert(hi)))
1956 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001957 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001958 return NULL;
1959 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001960 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001961 --todo;
1962 ++i;
1963 }
1964 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001965 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001966}
1967
Bram Moolenaara9922d62013-05-30 13:01:18 +02001968 static PyObject *
1969dict_key(hashitem_T *hi)
1970{
1971 return PyBytes_FromString((char *)(hi->hi_key));
1972}
1973
1974 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001975DictionaryListKeys(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001976{
1977 return DictionaryListObjects(self, dict_key);
1978}
1979
1980 static PyObject *
1981dict_val(hashitem_T *hi)
1982{
1983 dictitem_T *di;
1984
1985 di = dict_lookup(hi);
1986 return ConvertToPyObject(&di->di_tv);
1987}
1988
1989 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001990DictionaryListValues(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001991{
1992 return DictionaryListObjects(self, dict_val);
1993}
1994
1995 static PyObject *
1996dict_item(hashitem_T *hi)
1997{
1998 PyObject *keyObject;
1999 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002000 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002001
2002 if (!(keyObject = dict_key(hi)))
2003 return NULL;
2004
2005 if (!(valObject = dict_val(hi)))
2006 {
2007 Py_DECREF(keyObject);
2008 return NULL;
2009 }
2010
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002011 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012
2013 Py_DECREF(keyObject);
2014 Py_DECREF(valObject);
2015
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002016 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002017}
2018
2019 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002020DictionaryListItems(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002021{
2022 return DictionaryListObjects(self, dict_item);
2023}
2024
2025 static PyObject *
2026DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2027{
2028 dict_T *dict = self->dict;
2029
2030 if (dict->dv_lock)
2031 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002032 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002033 return NULL;
2034 }
2035
2036 if (kwargs)
2037 {
2038 typval_T tv;
2039
2040 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2041 return NULL;
2042
2043 VimTryStart();
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002044 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force", NULL);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045 clear_tv(&tv);
2046 if (VimTryEnd())
2047 return NULL;
2048 }
2049 else
2050 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002051 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002052
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002053 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002054 return NULL;
2055
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002056 if (obj == NULL)
2057 {
2058 Py_INCREF(Py_None);
2059 return Py_None;
2060 }
2061
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002062 if (PyObject_HasAttrString(obj, "keys"))
2063 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002064 else
2065 {
2066 PyObject *iterator;
2067 PyObject *item;
2068
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002069 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002070 return NULL;
2071
2072 while ((item = PyIter_Next(iterator)))
2073 {
2074 PyObject *fast;
2075 PyObject *keyObject;
2076 PyObject *valObject;
2077 PyObject *todecref;
2078 char_u *key;
2079 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002080 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002081
2082 if (!(fast = PySequence_Fast(item, "")))
2083 {
2084 Py_DECREF(iterator);
2085 Py_DECREF(item);
2086 return NULL;
2087 }
2088
2089 Py_DECREF(item);
2090
2091 if (PySequence_Fast_GET_SIZE(fast) != 2)
2092 {
2093 Py_DECREF(iterator);
2094 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002095 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002096 N_("expected sequence element of size 2, "
2097 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002098 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002099 return NULL;
2100 }
2101
2102 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2103
2104 if (!(key = StringToChars(keyObject, &todecref)))
2105 {
2106 Py_DECREF(iterator);
2107 Py_DECREF(fast);
2108 return NULL;
2109 }
2110
2111 di = dictitem_alloc(key);
2112
2113 Py_XDECREF(todecref);
2114
2115 if (di == NULL)
2116 {
2117 Py_DECREF(fast);
2118 Py_DECREF(iterator);
2119 PyErr_NoMemory();
2120 return NULL;
2121 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002122 di->di_tv.v_type = VAR_UNKNOWN;
2123
2124 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2125
2126 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2127 {
2128 Py_DECREF(iterator);
2129 Py_DECREF(fast);
2130 dictitem_free(di);
2131 return NULL;
2132 }
2133
2134 Py_DECREF(fast);
2135
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002136 hi = hash_find(&dict->dv_hashtab, di->di_key);
2137 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002138 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002139 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002140 Py_DECREF(iterator);
2141 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002142 return NULL;
2143 }
2144 }
2145
2146 Py_DECREF(iterator);
2147
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002148 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002149 if (PyErr_Occurred())
2150 return NULL;
2151 }
2152 }
2153 Py_INCREF(Py_None);
2154 return Py_None;
2155}
2156
2157 static PyObject *
2158DictionaryGet(DictionaryObject *self, PyObject *args)
2159{
2160 return _DictionaryItem(self, args,
2161 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2162}
2163
2164 static PyObject *
2165DictionaryPop(DictionaryObject *self, PyObject *args)
2166{
2167 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2168}
2169
2170 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002171DictionaryPopItem(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002172{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002173 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002174 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002175 PyObject *valObject;
2176 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002177
Bram Moolenaarde71b562013-06-02 17:41:54 +02002178 if (self->dict->dv_hashtab.ht_used == 0)
2179 {
2180 PyErr_SetNone(PyExc_KeyError);
2181 return NULL;
2182 }
2183
2184 hi = self->dict->dv_hashtab.ht_array;
2185 while (HASHITEM_EMPTY(hi))
2186 ++hi;
2187
2188 di = dict_lookup(hi);
2189
2190 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002191 return NULL;
2192
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002193 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002194 {
2195 Py_DECREF(valObject);
2196 return NULL;
2197 }
2198
2199 hash_remove(&self->dict->dv_hashtab, hi);
2200 dictitem_free(di);
2201
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002202 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002203}
2204
2205 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002206DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002207{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002208 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2209}
2210
2211static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002212 0, // sq_length
2213 0, // sq_concat
2214 0, // sq_repeat
2215 0, // sq_item
2216 0, // sq_slice
2217 0, // sq_ass_item
2218 0, // sq_ass_slice
2219 (objobjproc) DictionaryContains, // sq_contains
2220 0, // sq_inplace_concat
2221 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002222};
2223
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002224static PyMappingMethods DictionaryAsMapping = {
2225 (lenfunc) DictionaryLength,
2226 (binaryfunc) DictionaryItem,
2227 (objobjargproc) DictionaryAssItem,
2228};
2229
Bram Moolenaardb913952012-06-29 12:54:53 +02002230static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002231 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002232 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2233 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002234 {"update", (PyCFunction)(void *)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002235 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2236 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002237 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002238 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002239 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2240 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002241};
2242
2243static PyTypeObject ListType;
2244
2245typedef struct
2246{
2247 PyObject_HEAD
2248 list_T *list;
2249 pylinkedlist_T ref;
2250} ListObject;
2251
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002252#define NEW_LIST(list) ListNew(&ListType, list)
2253
Bram Moolenaardb913952012-06-29 12:54:53 +02002254 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002255ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002256{
2257 ListObject *self;
2258
Bram Moolenaarab589462020-07-06 21:03:06 +02002259 if (list == NULL)
2260 return NULL;
2261
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002262 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002263 if (self == NULL)
2264 return NULL;
2265 self->list = list;
2266 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002267 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002268
2269 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2270
2271 return (PyObject *)(self);
2272}
2273
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002274 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002275py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002277 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002279 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002280 {
2281 PyErr_NoMemory();
2282 return NULL;
2283 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002284 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002286 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002287}
2288
2289 static int
2290list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2291{
2292 PyObject *iterator;
2293 PyObject *item;
2294 listitem_T *li;
2295
2296 if (!(iterator = PyObject_GetIter(obj)))
2297 return -1;
2298
2299 while ((item = PyIter_Next(iterator)))
2300 {
2301 if (!(li = listitem_alloc()))
2302 {
2303 PyErr_NoMemory();
2304 Py_DECREF(item);
2305 Py_DECREF(iterator);
2306 return -1;
2307 }
2308 li->li_tv.v_lock = 0;
2309 li->li_tv.v_type = VAR_UNKNOWN;
2310
2311 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2312 {
2313 Py_DECREF(item);
2314 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002316 return -1;
2317 }
2318
2319 Py_DECREF(item);
2320
2321 list_append(l, li);
2322 }
2323
2324 Py_DECREF(iterator);
2325
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002326 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002327 if (PyErr_Occurred())
2328 return -1;
2329
2330 return 0;
2331}
2332
2333 static PyObject *
2334ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2335{
2336 list_T *list;
2337 PyObject *obj = NULL;
2338
2339 if (kwargs)
2340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002341 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002342 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002343 return NULL;
2344 }
2345
2346 if (!PyArg_ParseTuple(args, "|O", &obj))
2347 return NULL;
2348
2349 if (!(list = py_list_alloc()))
2350 return NULL;
2351
2352 if (obj)
2353 {
2354 PyObject *lookup_dict;
2355
2356 if (!(lookup_dict = PyDict_New()))
2357 {
2358 list_unref(list);
2359 return NULL;
2360 }
2361
2362 if (list_py_concat(list, obj, lookup_dict) == -1)
2363 {
2364 Py_DECREF(lookup_dict);
2365 list_unref(list);
2366 return NULL;
2367 }
2368
2369 Py_DECREF(lookup_dict);
2370 }
2371
2372 return ListNew(subtype, list);
2373}
2374
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002375 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002377{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002378 pyll_remove(&self->ref, &lastlist);
2379 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002380
2381 DESTRUCTOR_FINISH(self);
2382}
2383
Bram Moolenaardb913952012-06-29 12:54:53 +02002384 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002385ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002386{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002387 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002388}
2389
2390 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002391ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002392{
2393 listitem_T *li;
2394
Bram Moolenaard6e39182013-05-21 18:30:34 +02002395 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002396 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002397 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 return NULL;
2399 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002400 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002401 if (li == NULL)
2402 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002403 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002404 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002405 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002406 return NULL;
2407 }
2408 return ConvertToPyObject(&li->li_tv);
2409}
2410
Bram Moolenaardb913952012-06-29 12:54:53 +02002411 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002412ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2413 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002414{
2415 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002417
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002418 if (step == 0)
2419 {
2420 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2421 return NULL;
2422 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002423
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002424 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002425 if (list == NULL)
2426 return NULL;
2427
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002428 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002429 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002430 PyObject *item;
2431
2432 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002433 if (item == NULL)
2434 {
2435 Py_DECREF(list);
2436 return NULL;
2437 }
2438
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002439 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002440 }
2441
2442 return list;
2443}
2444
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002445 static PyObject *
2446ListItem(ListObject *self, PyObject* idx)
2447{
2448#if PY_MAJOR_VERSION < 3
2449 if (PyInt_Check(idx))
2450 {
2451 long _idx = PyInt_AsLong(idx);
2452 return ListIndex(self, _idx);
2453 }
2454 else
2455#endif
2456 if (PyLong_Check(idx))
2457 {
2458 long _idx = PyLong_AsLong(idx);
2459 return ListIndex(self, _idx);
2460 }
2461 else if (PySlice_Check(idx))
2462 {
2463 Py_ssize_t start, stop, step, slicelen;
2464
Bram Moolenaar922a4662014-03-30 16:11:43 +02002465 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002466 &start, &stop, &step, &slicelen) < 0)
2467 return NULL;
2468 return ListSlice(self, start, step, slicelen);
2469 }
2470 else
2471 {
2472 RAISE_INVALID_INDEX_TYPE(idx);
2473 return NULL;
2474 }
2475}
2476
2477 static void
2478list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2479 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2480{
2481 while (numreplaced--)
2482 {
2483 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2484 listitem_remove(l, lis[slicelen + numreplaced]);
2485 }
2486 while (numadded--)
2487 {
2488 listitem_T *next;
2489
2490 next = lastaddedli->li_prev;
2491 listitem_remove(l, lastaddedli);
2492 lastaddedli = next;
2493 }
2494}
2495
2496 static int
2497ListAssSlice(ListObject *self, Py_ssize_t first,
2498 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2499{
2500 PyObject *iterator;
2501 PyObject *item;
2502 listitem_T *li;
2503 listitem_T *lastaddedli = NULL;
2504 listitem_T *next;
2505 typval_T v;
2506 list_T *l = self->list;
2507 PyInt i;
2508 PyInt j;
2509 PyInt numreplaced = 0;
2510 PyInt numadded = 0;
2511 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002512 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002513
2514 size = ListLength(self);
2515
2516 if (l->lv_lock)
2517 {
2518 RAISE_LOCKED_LIST;
2519 return -1;
2520 }
2521
2522 if (step == 0)
2523 {
2524 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2525 return -1;
2526 }
2527
2528 if (step != 1 && slicelen == 0)
2529 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002530 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002531 int ret = 0;
2532
2533 if (obj == NULL)
2534 return 0;
2535
2536 if (!(iterator = PyObject_GetIter(obj)))
2537 return -1;
2538
2539 if ((item = PyIter_Next(iterator)))
2540 {
2541 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002542 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002543 "to extended slice"), 0);
2544 Py_DECREF(item);
2545 ret = -1;
2546 }
2547 Py_DECREF(iterator);
2548 return ret;
2549 }
2550
2551 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002552 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002553 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2554 {
2555 PyErr_NoMemory();
2556 return -1;
2557 }
2558
2559 if (first == size)
2560 li = NULL;
2561 else
2562 {
2563 li = list_find(l, (long) first);
2564 if (li == NULL)
2565 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002566 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002567 (int)first);
2568 if (obj != NULL)
2569 PyMem_Free(lis);
2570 return -1;
2571 }
2572 i = slicelen;
2573 while (i-- && li != NULL)
2574 {
2575 j = step;
2576 next = li;
2577 if (step > 0)
2578 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2579 else
2580 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2581
2582 if (obj == NULL)
2583 listitem_remove(l, li);
2584 else
2585 lis[slicelen - i - 1] = li;
2586
2587 li = next;
2588 }
2589 if (li == NULL && i != -1)
2590 {
2591 PyErr_SET_VIM(N_("internal error: not enough list items"));
2592 if (obj != NULL)
2593 PyMem_Free(lis);
2594 return -1;
2595 }
2596 }
2597
2598 if (obj == NULL)
2599 return 0;
2600
2601 if (!(iterator = PyObject_GetIter(obj)))
2602 {
2603 PyMem_Free(lis);
2604 return -1;
2605 }
2606
2607 i = 0;
2608 while ((item = PyIter_Next(iterator)))
2609 {
2610 if (ConvertFromPyObject(item, &v) == -1)
2611 {
2612 Py_DECREF(iterator);
2613 Py_DECREF(item);
2614 PyMem_Free(lis);
2615 return -1;
2616 }
2617 Py_DECREF(item);
2618 if (list_insert_tv(l, &v, numreplaced < slicelen
2619 ? lis[numreplaced]
2620 : li) == FAIL)
2621 {
2622 clear_tv(&v);
2623 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2624 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2625 PyMem_Free(lis);
2626 return -1;
2627 }
2628 if (numreplaced < slicelen)
2629 {
2630 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002631 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002632 numreplaced++;
2633 }
2634 else
2635 {
2636 if (li)
2637 lastaddedli = li->li_prev;
2638 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002639 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002640 numadded++;
2641 }
2642 clear_tv(&v);
2643 if (step != 1 && i >= slicelen)
2644 {
2645 Py_DECREF(iterator);
2646 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002647 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002648 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002649 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2650 PyMem_Free(lis);
2651 return -1;
2652 }
2653 ++i;
2654 }
2655 Py_DECREF(iterator);
2656
2657 if (step != 1 && i != slicelen)
2658 {
2659 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002660 N_("attempt to assign sequence of size %d to extended slice "
2661 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002662 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2663 PyMem_Free(lis);
2664 return -1;
2665 }
2666
2667 if (PyErr_Occurred())
2668 {
2669 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2670 PyMem_Free(lis);
2671 return -1;
2672 }
2673
2674 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002676 if (step == 1)
2677 for (i = numreplaced; i < slicelen; i++)
2678 listitem_remove(l, lis[i]);
2679
2680 PyMem_Free(lis);
2681
2682 return 0;
2683}
2684
2685 static int
2686ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2687{
2688 typval_T tv;
2689 list_T *l = self->list;
2690 listitem_T *li;
2691 Py_ssize_t length = ListLength(self);
2692
2693 if (l->lv_lock)
2694 {
2695 RAISE_LOCKED_LIST;
2696 return -1;
2697 }
2698 if (index > length || (index == length && obj == NULL))
2699 {
2700 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2701 return -1;
2702 }
2703
2704 if (obj == NULL)
2705 {
2706 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002707 if (li == NULL)
2708 {
2709 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2710 "list item %d"), (int) index);
2711 return -1;
2712 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002713 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002714 clear_tv(&li->li_tv);
2715 vim_free(li);
2716 return 0;
2717 }
2718
2719 if (ConvertFromPyObject(obj, &tv) == -1)
2720 return -1;
2721
2722 if (index == length)
2723 {
2724 if (list_append_tv(l, &tv) == FAIL)
2725 {
2726 clear_tv(&tv);
2727 PyErr_SET_VIM(N_("failed to add item to list"));
2728 return -1;
2729 }
2730 }
2731 else
2732 {
2733 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002734 if (li == NULL)
2735 {
2736 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2737 "list item %d"), (int) index);
2738 return -1;
2739 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002740 clear_tv(&li->li_tv);
2741 copy_tv(&tv, &li->li_tv);
2742 clear_tv(&tv);
2743 }
2744 return 0;
2745}
2746
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002747 static int
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002748ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2749{
2750#if PY_MAJOR_VERSION < 3
2751 if (PyInt_Check(idx))
2752 {
2753 long _idx = PyInt_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002754 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002755 }
2756 else
2757#endif
2758 if (PyLong_Check(idx))
2759 {
2760 long _idx = PyLong_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002761 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002762 }
2763 else if (PySlice_Check(idx))
2764 {
2765 Py_ssize_t start, stop, step, slicelen;
2766
Bram Moolenaar922a4662014-03-30 16:11:43 +02002767 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002768 &start, &stop, &step, &slicelen) < 0)
2769 return -1;
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002770 return (int)ListAssSlice(self, start, step, slicelen,
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002771 obj);
2772 }
2773 else
2774 {
2775 RAISE_INVALID_INDEX_TYPE(idx);
2776 return -1;
2777 }
2778}
2779
2780 static PyObject *
2781ListConcatInPlace(ListObject *self, PyObject *obj)
2782{
2783 list_T *l = self->list;
2784 PyObject *lookup_dict;
2785
2786 if (l->lv_lock)
2787 {
2788 RAISE_LOCKED_LIST;
2789 return NULL;
2790 }
2791
2792 if (!(lookup_dict = PyDict_New()))
2793 return NULL;
2794
2795 if (list_py_concat(l, obj, lookup_dict) == -1)
2796 {
2797 Py_DECREF(lookup_dict);
2798 return NULL;
2799 }
2800 Py_DECREF(lookup_dict);
2801
2802 Py_INCREF(self);
2803 return (PyObject *)(self);
2804}
2805
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002806typedef struct
2807{
2808 listwatch_T lw;
2809 list_T *list;
2810} listiterinfo_T;
2811
2812 static void
2813ListIterDestruct(listiterinfo_T *lii)
2814{
2815 list_rem_watch(lii->list, &lii->lw);
Bram Moolenaar21578272021-02-21 19:12:47 +01002816 list_unref(lii->list);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002817 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;
Bram Moolenaar21578272021-02-21 19:12:47 +01002852 ++l->lv_refcount;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002853
2854 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002855 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002856 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002857}
2858
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002859static char *ListAttrs[] = {
2860 "locked",
2861 NULL
2862};
2863
2864 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002865ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002866{
2867 return ObjectDir(self, ListAttrs);
2868}
2869
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002872{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002873 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002874 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002875 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002876 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002877 return -1;
2878 }
2879
2880 if (strcmp(name, "locked") == 0)
2881 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002882 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002883 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002884 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002885 return -1;
2886 }
2887 else
2888 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002889 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002890 if (istrue == -1)
2891 return -1;
2892 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002893 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002894 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002896 }
2897 return 0;
2898 }
2899 else
2900 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002901 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002902 return -1;
2903 }
2904}
2905
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002906static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002907 (lenfunc) ListLength, // sq_length, len(x)
2908 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2909 0, // RangeRepeat, sq_repeat, x*n
2910 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2911 0, // was_sq_slice, x[i:j]
2912 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2913 0, // was_sq_ass_slice, x[i:j]=v
2914 0, // sq_contains
2915 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2916 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002917};
2918
2919static PyMappingMethods ListAsMapping = {
2920 /* mp_length */ (lenfunc) ListLength,
2921 /* mp_subscript */ (binaryfunc) ListItem,
2922 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2923};
2924
Bram Moolenaardb913952012-06-29 12:54:53 +02002925static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002926 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2927 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2928 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002929};
2930
2931typedef struct
2932{
2933 PyObject_HEAD
2934 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002935 int argc;
2936 typval_T *argv;
2937 dict_T *self;
2938 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002939 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002940} FunctionObject;
2941
2942static PyTypeObject FunctionType;
2943
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002944#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2945 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002946
Bram Moolenaardb913952012-06-29 12:54:53 +02002947 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002948FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002949 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002950{
2951 FunctionObject *self;
2952
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002953 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002954 if (self == NULL)
2955 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002956
2957 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002958 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002959 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002960 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002961 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002962 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002963 return NULL;
2964 }
2965 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002966 }
2967 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002968 {
2969 char_u *p;
2970
2971 if ((p = get_expanded_name(name,
2972 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002973 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002974 PyErr_FORMAT(PyExc_ValueError,
2975 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002976 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002977 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002978
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002979 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2980 {
2981 char_u *np;
2982 size_t len = STRLEN(p) + 1;
2983
Bram Moolenaar51e14382019-05-25 20:21:28 +02002984 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002985 {
2986 vim_free(p);
2987 return NULL;
2988 }
2989 mch_memmove(np, "<SNR>", 5);
2990 mch_memmove(np + 5, p + 3, len - 3);
2991 vim_free(p);
2992 self->name = np;
2993 }
2994 else
2995 self->name = p;
2996 }
2997
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002998 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002999 self->argc = argc;
3000 self->argv = argv;
3001 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003002 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003003
3004 if (self->argv || self->self)
3005 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3006
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003007 return (PyObject *)(self);
3008}
3009
3010 static PyObject *
3011FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3012{
3013 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003014 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003015 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003016 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003017 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003018 typval_T selfdicttv;
3019 typval_T argstv;
3020 list_T *argslist = NULL;
3021 dict_T *selfdict = NULL;
3022 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003023 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003024 typval_T *argv = NULL;
3025 typval_T *curtv;
3026 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003027
Bram Moolenaar8110a092016-04-14 15:56:09 +02003028 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003029 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003030 selfdictObject = PyDict_GetItemString(kwargs, "self");
3031 if (selfdictObject != NULL)
3032 {
3033 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3034 return NULL;
3035 selfdict = selfdicttv.vval.v_dict;
3036 }
3037 argsObject = PyDict_GetItemString(kwargs, "args");
3038 if (argsObject != NULL)
3039 {
3040 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3041 {
3042 dict_unref(selfdict);
3043 return NULL;
3044 }
3045 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003046 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003047
3048 argc = argslist->lv_len;
3049 if (argc != 0)
3050 {
3051 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003052 if (argv == NULL)
3053 {
3054 PyErr_NoMemory();
3055 dict_unref(selfdict);
3056 list_unref(argslist);
3057 return NULL;
3058 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003059 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003060 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003061 copy_tv(&li->li_tv, curtv++);
3062 }
3063 list_unref(argslist);
3064 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003065 if (selfdict != NULL)
3066 {
3067 auto_rebind = FALSE;
3068 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3069 if (autoRebindObject != NULL)
3070 {
3071 auto_rebind = PyObject_IsTrue(autoRebindObject);
3072 if (auto_rebind == -1)
3073 {
3074 dict_unref(selfdict);
3075 list_unref(argslist);
3076 return NULL;
3077 }
3078 }
3079 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003080 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003081
Bram Moolenaar389a1792013-06-23 13:00:44 +02003082 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003083 {
3084 dict_unref(selfdict);
3085 while (argc--)
3086 clear_tv(&argv[argc]);
3087 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003088 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003089 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003090
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003091 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003092
Bram Moolenaar389a1792013-06-23 13:00:44 +02003093 PyMem_Free(name);
3094
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003095 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003096}
3097
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003098 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003099FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003100{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003101 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003102 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003103 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003104 for (i = 0; i < self->argc; ++i)
3105 clear_tv(&self->argv[i]);
3106 PyMem_Free(self->argv);
3107 dict_unref(self->self);
3108 if (self->argv || self->self)
3109 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003110
3111 DESTRUCTOR_FINISH(self);
3112}
3113
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003114static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003115 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003116 NULL
3117};
3118
3119 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003120FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003121{
3122 return ObjectDir(self, FunctionAttrs);
3123}
3124
Bram Moolenaardb913952012-06-29 12:54:53 +02003125 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003126FunctionAttr(FunctionObject *self, char *name)
3127{
3128 list_T *list;
3129 int i;
3130 if (strcmp(name, "name") == 0)
3131 return PyString_FromString((char *)(self->name));
3132 else if (strcmp(name, "args") == 0)
3133 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003134 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003135 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003136
Bram Moolenaar8110a092016-04-14 15:56:09 +02003137 for (i = 0; i < self->argc; ++i)
3138 list_append_tv(list, &self->argv[i]);
3139 return NEW_LIST(list);
3140 }
3141 else if (strcmp(name, "self") == 0)
3142 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003143 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003144 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003145 else if (strcmp(name, "auto_rebind") == 0)
3146 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003147 ? ALWAYS_TRUE
3148 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003149 else if (strcmp(name, "__members__") == 0)
3150 return ObjectDir(NULL, FunctionAttrs);
3151 return NULL;
3152}
3153
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003154/*
3155 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003156 *
3157 * "exported" should be set to true when it is needed to construct a partial
3158 * that may be stored in a variable (i.e. may be freed by Vim).
3159 */
3160 static void
3161set_partial(FunctionObject *self, partial_T *pt, int exported)
3162{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003163 int i;
3164
3165 pt->pt_name = self->name;
3166 if (self->argv)
3167 {
3168 pt->pt_argc = self->argc;
3169 if (exported)
3170 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003171 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003172 for (i = 0; i < pt->pt_argc; ++i)
3173 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3174 }
3175 else
3176 pt->pt_argv = self->argv;
3177 }
3178 else
3179 {
3180 pt->pt_argc = 0;
3181 pt->pt_argv = NULL;
3182 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003183 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003184 pt->pt_dict = self->self;
3185 if (exported && self->self)
3186 ++pt->pt_dict->dv_refcount;
3187 if (exported)
3188 pt->pt_name = vim_strsave(pt->pt_name);
3189 pt->pt_refcount = 1;
3190}
3191
3192 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003193FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003194{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003195 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003196 typval_T args;
3197 typval_T selfdicttv;
3198 typval_T rettv;
3199 dict_T *selfdict = NULL;
3200 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003201 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003202 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003203 partial_T pt;
3204 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003205
Bram Moolenaar8110a092016-04-14 15:56:09 +02003206 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003207 return NULL;
3208
3209 if (kwargs != NULL)
3210 {
3211 selfdictObject = PyDict_GetItemString(kwargs, "self");
3212 if (selfdictObject != NULL)
3213 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003214 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003215 {
3216 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003217 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003218 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003219 selfdict = selfdicttv.vval.v_dict;
3220 }
3221 }
3222
Bram Moolenaar8110a092016-04-14 15:56:09 +02003223 if (self->argv || self->self)
3224 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003225 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003226 set_partial(self, &pt, FALSE);
3227 pt_ptr = &pt;
3228 }
3229
Bram Moolenaar71700b82013-05-15 17:49:05 +02003230 Py_BEGIN_ALLOW_THREADS
3231 Python_Lock_Vim();
3232
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003233 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003234 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003235
3236 Python_Release_Vim();
3237 Py_END_ALLOW_THREADS
3238
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003239 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003240 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003241 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003242 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003244 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003245 }
3246 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003247 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003248
Bram Moolenaardb913952012-06-29 12:54:53 +02003249 clear_tv(&args);
3250 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003251 if (selfdict != NULL)
3252 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003253
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003254 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003255}
3256
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003257 static PyObject *
3258FunctionRepr(FunctionObject *self)
3259{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003260 PyObject *ret;
3261 garray_T repr_ga;
3262 int i;
3263 char_u *tofree = NULL;
3264 typval_T tv;
3265 char_u numbuf[NUMBUFLEN];
3266
3267 ga_init2(&repr_ga, (int)sizeof(char), 70);
3268 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3269 if (self->name)
3270 ga_concat(&repr_ga, self->name);
3271 else
3272 ga_concat(&repr_ga, (char_u *)"<NULL>");
3273 ga_append(&repr_ga, '\'');
3274 if (self->argv)
3275 {
3276 ga_concat(&repr_ga, (char_u *)", args=[");
3277 ++emsg_silent;
3278 for (i = 0; i < self->argc; i++)
3279 {
3280 if (i != 0)
3281 ga_concat(&repr_ga, (char_u *)", ");
3282 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3283 get_copyID()));
3284 vim_free(tofree);
3285 }
3286 --emsg_silent;
3287 ga_append(&repr_ga, ']');
3288 }
3289 if (self->self)
3290 {
3291 ga_concat(&repr_ga, (char_u *)", self=");
3292 tv.v_type = VAR_DICT;
3293 tv.vval.v_dict = self->self;
3294 ++emsg_silent;
3295 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3296 --emsg_silent;
3297 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003298 if (self->auto_rebind)
3299 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003300 }
3301 ga_append(&repr_ga, '>');
3302 ret = PyString_FromString((char *)repr_ga.ga_data);
3303 ga_clear(&repr_ga);
3304 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003305}
3306
Bram Moolenaardb913952012-06-29 12:54:53 +02003307static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003308 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3309 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003310};
3311
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003312/*
3313 * Options object
3314 */
3315
3316static PyTypeObject OptionsType;
3317
3318typedef int (*checkfun)(void *);
3319
3320typedef struct
3321{
3322 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003323 int opt_type;
3324 void *from;
3325 checkfun Check;
3326 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003327} OptionsObject;
3328
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003329 static int
3330dummy_check(void *arg UNUSED)
3331{
3332 return 0;
3333}
3334
3335 static PyObject *
3336OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3337{
3338 OptionsObject *self;
3339
Bram Moolenaar774267b2013-05-21 20:51:59 +02003340 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341 if (self == NULL)
3342 return NULL;
3343
3344 self->opt_type = opt_type;
3345 self->from = from;
3346 self->Check = Check;
3347 self->fromObj = fromObj;
3348 if (fromObj)
3349 Py_INCREF(fromObj);
3350
3351 return (PyObject *)(self);
3352}
3353
3354 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003355OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003356{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003357 PyObject_GC_UnTrack((void *)(self));
3358 Py_XDECREF(self->fromObj);
3359 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003360}
3361
3362 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003364{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003365 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003366 return 0;
3367}
3368
3369 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003371{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003373 return 0;
3374}
3375
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003376 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003377OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003378{
3379 char_u *key;
3380 int flags;
3381 long numval;
3382 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003383 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003384
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003385 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386 return NULL;
3387
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003388 if (!(key = StringToChars(keyObject, &todecref)))
3389 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003390
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003391 if (*key == NUL)
3392 {
3393 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003394 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003395 return NULL;
3396 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003397
3398 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003399 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003400
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003401 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402
3403 if (flags == 0)
3404 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003405 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003406 return NULL;
3407 }
3408
3409 if (flags & SOPT_UNSET)
3410 {
3411 Py_INCREF(Py_None);
3412 return Py_None;
3413 }
3414 else if (flags & SOPT_BOOL)
3415 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003416 PyObject *ret;
3417 ret = numval ? Py_True : Py_False;
3418 Py_INCREF(ret);
3419 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003420 }
3421 else if (flags & SOPT_NUM)
3422 return PyInt_FromLong(numval);
3423 else if (flags & SOPT_STRING)
3424 {
3425 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003426 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003427 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003428 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003429 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003430 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003431 else
3432 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003433 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003434 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003435 return NULL;
3436 }
3437 }
3438 else
3439 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003440 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003441 return NULL;
3442 }
3443}
3444
3445 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003446OptionsContains(OptionsObject *self, PyObject *keyObject)
3447{
3448 char_u *key;
3449 PyObject *todecref;
3450
3451 if (!(key = StringToChars(keyObject, &todecref)))
3452 return -1;
3453
3454 if (*key == NUL)
3455 {
3456 Py_XDECREF(todecref);
3457 return 0;
3458 }
3459
3460 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3461 {
3462 Py_XDECREF(todecref);
3463 return 1;
3464 }
3465 else
3466 {
3467 Py_XDECREF(todecref);
3468 return 0;
3469 }
3470}
3471
3472typedef struct
3473{
3474 void *lastoption;
3475 int opt_type;
3476} optiterinfo_T;
3477
3478 static PyObject *
3479OptionsIterNext(optiterinfo_T **oii)
3480{
3481 char_u *name;
3482
3483 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3484 return PyString_FromString((char *)name);
3485
3486 return NULL;
3487}
3488
3489 static PyObject *
3490OptionsIter(OptionsObject *self)
3491{
3492 optiterinfo_T *oii;
3493
3494 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3495 {
3496 PyErr_NoMemory();
3497 return NULL;
3498 }
3499
3500 oii->opt_type = self->opt_type;
3501 oii->lastoption = NULL;
3502
3503 return IterNew(oii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003504 (destructorfun)(void *) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003505 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003506}
3507
3508 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003509set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003510{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003511 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003512
3513 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3514 {
3515 if (VimTryEnd())
3516 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003517 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003518 return FAIL;
3519 }
3520 return OK;
3521}
3522
3523 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003524set_option_value_for(
3525 char_u *key,
3526 int numval,
3527 char_u *stringval,
3528 int opt_flags,
3529 int opt_type,
3530 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003531{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003532 win_T *save_curwin = NULL;
3533 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003534 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003535 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003536
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003537 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003538 switch (opt_type)
3539 {
3540 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003541 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003542 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003543 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003544 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003545 if (VimTryEnd())
3546 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003547 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003548 return -1;
3549 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003550 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3551 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003552 break;
3553 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003554 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003555 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003556 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003557 break;
3558 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003559 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003560 break;
3561 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003562 if (set_ret == FAIL)
3563 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003564 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003565}
3566
3567 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003568OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003569{
3570 char_u *key;
3571 int flags;
3572 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003573 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003574 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003575
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003576 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577 return -1;
3578
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003579 if (!(key = StringToChars(keyObject, &todecref)))
3580 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003581
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003582 if (*key == NUL)
3583 {
3584 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003585 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003586 return -1;
3587 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003588
3589 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003590 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003591
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003592 if (flags == 0)
3593 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003594 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003595 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003596 return -1;
3597 }
3598
3599 if (valObject == NULL)
3600 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003601 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003602 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003603 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003604 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003605 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003606 return -1;
3607 }
3608 else if (!(flags & SOPT_GLOBAL))
3609 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003610 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003611 N_("unable to unset option %s "
3612 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003613 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614 return -1;
3615 }
3616 else
3617 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003619 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003620 return 0;
3621 }
3622 }
3623
Bram Moolenaard6e39182013-05-21 18:30:34 +02003624 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003625
3626 if (flags & SOPT_BOOL)
3627 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003628 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003629
Bram Moolenaarb983f752013-05-15 16:11:50 +02003630 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003632 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003633 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003634 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003635 }
3636 else if (flags & SOPT_NUM)
3637 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003638 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003639
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003640 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003641 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003642 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003643 return -1;
3644 }
3645
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003646 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003647 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003648 }
3649 else
3650 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003651 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003652 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003653
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003654 if ((val = StringToChars(valObject, &todecref2)))
3655 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003656 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003657 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003658 Py_XDECREF(todecref2);
3659 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003660 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003661 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003662 }
3663
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003664 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003665
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003666 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003667}
3668
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003669static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003670 0, // sq_length
3671 0, // sq_concat
3672 0, // sq_repeat
3673 0, // sq_item
3674 0, // sq_slice
3675 0, // sq_ass_item
3676 0, // sq_ass_slice
3677 (objobjproc) OptionsContains, // sq_contains
3678 0, // sq_inplace_concat
3679 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003680};
3681
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003682static PyMappingMethods OptionsAsMapping = {
3683 (lenfunc) NULL,
3684 (binaryfunc) OptionsItem,
3685 (objobjargproc) OptionsAssItem,
3686};
3687
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003688// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003689
3690typedef struct
3691{
3692 PyObject_HEAD
3693 tabpage_T *tab;
3694} TabPageObject;
3695
3696static PyObject *WinListNew(TabPageObject *tabObject);
3697
3698static PyTypeObject TabPageType;
3699
3700 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003701CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003702{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003703 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003704 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003705 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003706 return -1;
3707 }
3708
3709 return 0;
3710}
3711
3712 static PyObject *
3713TabPageNew(tabpage_T *tab)
3714{
3715 TabPageObject *self;
3716
3717 if (TAB_PYTHON_REF(tab))
3718 {
3719 self = TAB_PYTHON_REF(tab);
3720 Py_INCREF(self);
3721 }
3722 else
3723 {
3724 self = PyObject_NEW(TabPageObject, &TabPageType);
3725 if (self == NULL)
3726 return NULL;
3727 self->tab = tab;
3728 TAB_PYTHON_REF(tab) = self;
3729 }
3730
3731 return (PyObject *)(self);
3732}
3733
3734 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003735TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003736{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003737 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3738 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003739
3740 DESTRUCTOR_FINISH(self);
3741}
3742
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003743static char *TabPageAttrs[] = {
3744 "windows", "number", "vars", "window", "valid",
3745 NULL
3746};
3747
3748 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003749TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003750{
3751 return ObjectDir(self, TabPageAttrs);
3752}
3753
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003754 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003755TabPageAttrValid(TabPageObject *self, char *name)
3756{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003757 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003758
3759 if (strcmp(name, "valid") != 0)
3760 return NULL;
3761
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003762 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3763 Py_INCREF(ret);
3764 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003765}
3766
3767 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003768TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003769{
3770 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003771 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003772 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003773 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003775 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003776 else if (strcmp(name, "window") == 0)
3777 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003778 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003779 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003780 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003781 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003782 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003784 else if (strcmp(name, "__members__") == 0)
3785 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003786 return NULL;
3787}
3788
3789 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003790TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003791{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003792 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003793 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003794 else
3795 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003796 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003797
3798 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003799 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3800 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003801 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003802 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003803 }
3804}
3805
3806static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003807 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003808 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3809 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003810};
3811
3812/*
3813 * Window list object
3814 */
3815
3816static PyTypeObject TabListType;
3817static PySequenceMethods TabListAsSeq;
3818
3819typedef struct
3820{
3821 PyObject_HEAD
3822} TabListObject;
3823
3824 static PyInt
3825TabListLength(PyObject *self UNUSED)
3826{
3827 tabpage_T *tp = first_tabpage;
3828 PyInt n = 0;
3829
3830 while (tp != NULL)
3831 {
3832 ++n;
3833 tp = tp->tp_next;
3834 }
3835
3836 return n;
3837}
3838
3839 static PyObject *
3840TabListItem(PyObject *self UNUSED, PyInt n)
3841{
3842 tabpage_T *tp;
3843
3844 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3845 if (n == 0)
3846 return TabPageNew(tp);
3847
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003848 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003849 return NULL;
3850}
3851
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003852/*
3853 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003854 */
3855
3856typedef struct
3857{
3858 PyObject_HEAD
3859 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003860 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003861} WindowObject;
3862
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003863static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003864
3865 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003866CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003867{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003868 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003869 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003870 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003871 return -1;
3872 }
3873
3874 return 0;
3875}
3876
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003877 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003878WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003879{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003880 /*
3881 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003882 * If we add a "w_python*_ref" field to the win_T structure,
3883 * then we can get at it in win_free() in vim. We then
3884 * need to create only ONE Python object per window - if
3885 * we try to create a second, just INCREF the existing one
3886 * and return it. The (single) Python object referring to
3887 * the window is stored in "w_python*_ref".
3888 * On a win_free() we set the Python object's win_T* field
3889 * to an invalid value. We trap all uses of a window
3890 * object, and reject them if the win_T* field is invalid.
3891 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003892 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003893 * w_python_ref and w_python3_ref fields respectively.
3894 */
3895
3896 WindowObject *self;
3897
3898 if (WIN_PYTHON_REF(win))
3899 {
3900 self = WIN_PYTHON_REF(win);
3901 Py_INCREF(self);
3902 }
3903 else
3904 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003905 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003906 if (self == NULL)
3907 return NULL;
3908 self->win = win;
3909 WIN_PYTHON_REF(win) = self;
3910 }
3911
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003912 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3913
Bram Moolenaar971db462013-05-12 18:44:48 +02003914 return (PyObject *)(self);
3915}
3916
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003917 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003918WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003919{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003920 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003921 if (self->win && self->win != INVALID_WINDOW_VALUE)
3922 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003923 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003924 PyObject_GC_Del((void *)(self));
3925}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003926
Bram Moolenaar774267b2013-05-21 20:51:59 +02003927 static int
3928WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3929{
3930 Py_VISIT(((PyObject *)(self->tabObject)));
3931 return 0;
3932}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003933
Bram Moolenaar774267b2013-05-21 20:51:59 +02003934 static int
3935WindowClear(WindowObject *self)
3936{
3937 Py_CLEAR(self->tabObject);
3938 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003939}
3940
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003941 static win_T *
3942get_firstwin(TabPageObject *tabObject)
3943{
3944 if (tabObject)
3945 {
3946 if (CheckTabPage(tabObject))
3947 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003948 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003949 else if (tabObject->tab == curtab)
3950 return firstwin;
3951 else
3952 return tabObject->tab->tp_firstwin;
3953 }
3954 else
3955 return firstwin;
3956}
Bram Moolenaare950f992018-06-10 13:55:55 +02003957
3958// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003959static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003960 "buffer",
3961 "cursor",
3962 "height",
3963 "row",
3964 "width",
3965 "col",
3966 "vars",
3967 "options",
3968 "number",
3969 "tabpage",
3970 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003971 NULL
3972};
3973
3974 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003975WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003976{
3977 return ObjectDir(self, WindowAttrs);
3978}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003979
Bram Moolenaar971db462013-05-12 18:44:48 +02003980 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003981WindowAttrValid(WindowObject *self, char *name)
3982{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003983 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003984
3985 if (strcmp(name, "valid") != 0)
3986 return NULL;
3987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003988 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3989 Py_INCREF(ret);
3990 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003991}
3992
3993 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003994WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003995{
3996 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003997 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003998 else if (strcmp(name, "cursor") == 0)
3999 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004000 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004001
4002 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4003 }
4004 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004005 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004006 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004007 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004008 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004009 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004010 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004011 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004012 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004013 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004014 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4016 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004017 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004018 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004019 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004020 return NULL;
4021 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004022 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004023 }
4024 else if (strcmp(name, "tabpage") == 0)
4025 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004026 Py_INCREF(self->tabObject);
4027 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004028 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004029 else if (strcmp(name, "__members__") == 0)
4030 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004031 else
4032 return NULL;
4033}
4034
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004036WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004038 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039 return -1;
4040
4041 if (strcmp(name, "buffer") == 0)
4042 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004043 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044 return -1;
4045 }
4046 else if (strcmp(name, "cursor") == 0)
4047 {
4048 long lnum;
4049 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004050
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004051 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 return -1;
4053
Bram Moolenaard6e39182013-05-21 18:30:34 +02004054 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004056 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 return -1;
4058 }
4059
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004060 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004061 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 return -1;
4063
Bram Moolenaard6e39182013-05-21 18:30:34 +02004064 self->win->w_cursor.lnum = lnum;
4065 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004066 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004067 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004068 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004069 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004071 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072 return 0;
4073 }
4074 else if (strcmp(name, "height") == 0)
4075 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004076 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077 win_T *savewin;
4078
Bram Moolenaardee2e312013-06-23 16:35:47 +02004079 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004080 return -1;
4081
4082#ifdef FEAT_GUI
4083 need_mouse_correct = TRUE;
4084#endif
4085 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004086 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004087
4088 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004089 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004091 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 return -1;
4093
4094 return 0;
4095 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096 else if (strcmp(name, "width") == 0)
4097 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004098 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099 win_T *savewin;
4100
Bram Moolenaardee2e312013-06-23 16:35:47 +02004101 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102 return -1;
4103
4104#ifdef FEAT_GUI
4105 need_mouse_correct = TRUE;
4106#endif
4107 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004108 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004109
4110 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004111 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004113 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 return -1;
4115
4116 return 0;
4117 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118 else
4119 {
4120 PyErr_SetString(PyExc_AttributeError, name);
4121 return -1;
4122 }
4123}
4124
4125 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004126WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004128 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004129 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130 else
4131 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004132 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004133
Bram Moolenaar6d216452013-05-12 19:00:41 +02004134 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004135 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 (self));
4137 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004138 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139 }
4140}
4141
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004142static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004143 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004144 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4145 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004146};
4147
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004148/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004149 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004151
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004152static PyTypeObject WinListType;
4153static PySequenceMethods WinListAsSeq;
4154
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004155typedef struct
4156{
4157 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004158 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004159} WinListObject;
4160
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004161 static PyObject *
4162WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004163{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004164 WinListObject *self;
4165
4166 self = PyObject_NEW(WinListObject, &WinListType);
4167 self->tabObject = tabObject;
4168 Py_INCREF(tabObject);
4169
4170 return (PyObject *)(self);
4171}
4172
4173 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004174WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004175{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004176 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004177
4178 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004179 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004180 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004181 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004182
4183 DESTRUCTOR_FINISH(self);
4184}
4185
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004186 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004187WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004188{
4189 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190 PyInt n = 0;
4191
Bram Moolenaard6e39182013-05-21 18:30:34 +02004192 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004193 return -1;
4194
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004195 while (w != NULL)
4196 {
4197 ++n;
4198 w = W_NEXT(w);
4199 }
4200
4201 return n;
4202}
4203
4204 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004205WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004206{
4207 win_T *w;
4208
Bram Moolenaard6e39182013-05-21 18:30:34 +02004209 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004210 return NULL;
4211
4212 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004213 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004214 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004215
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004216 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004217 return NULL;
4218}
4219
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004220/*
4221 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004222 *
4223 * The result is in allocated memory. All internal nulls are replaced by
4224 * newline characters. It is an error for the string to contain newline
4225 * characters.
4226 *
4227 * On errors, the Python exception data is set, and NULL is returned.
4228 */
4229 static char *
4230StringToLine(PyObject *obj)
4231{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004232 char *str;
4233 char *save;
4234 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004235 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004236 PyInt i;
4237 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004238
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004239 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004240 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004241 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4242 || str == NULL)
4243 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004244 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004245 else if (PyUnicode_Check(obj))
4246 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004247 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4248 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004249 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004250
Bram Moolenaardaa27022013-06-24 22:33:30 +02004251 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004252 || str == NULL)
4253 {
4254 Py_DECREF(bytes);
4255 return NULL;
4256 }
4257 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004258 else
4259 {
4260#if PY_MAJOR_VERSION < 3
4261 PyErr_FORMAT(PyExc_TypeError,
4262 N_("expected str() or unicode() instance, but got %s"),
4263 Py_TYPE_NAME(obj));
4264#else
4265 PyErr_FORMAT(PyExc_TypeError,
4266 N_("expected bytes() or str() instance, but got %s"),
4267 Py_TYPE_NAME(obj));
4268#endif
4269 return NULL;
4270 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004271
4272 /*
4273 * Error checking: String must not contain newlines, as we
4274 * are replacing a single line, and we must replace it with
4275 * a single line.
4276 * A trailing newline is removed, so that append(f.readlines()) works.
4277 */
4278 p = memchr(str, '\n', len);
4279 if (p != NULL)
4280 {
4281 if (p == str + len - 1)
4282 --len;
4283 else
4284 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004285 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004286 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004287 return NULL;
4288 }
4289 }
4290
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004291 /*
4292 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004293 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004294 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004295 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004296 if (save == NULL)
4297 {
4298 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004299 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004300 return NULL;
4301 }
4302
4303 for (i = 0; i < len; ++i)
4304 {
4305 if (str[i] == '\0')
4306 save[i] = '\n';
4307 else
4308 save[i] = str[i];
4309 }
4310
4311 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004312 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004313
4314 return save;
4315}
4316
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004317/*
4318 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004319 * in Vim format (1-based). The line is returned as a Python
4320 * string object.
4321 */
4322 static PyObject *
4323GetBufferLine(buf_T *buf, PyInt n)
4324{
4325 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4326}
4327
4328
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004329/*
4330 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004331 * are in Vim format (1-based). The range is from lo up to, but not
4332 * including, hi. The list is returned as a Python list of string objects.
4333 */
4334 static PyObject *
4335GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4336{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004337 PyInt i;
4338 PyInt n = hi - lo;
4339 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004340
4341 if (list == NULL)
4342 return NULL;
4343
4344 for (i = 0; i < n; ++i)
4345 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004346 linenr_T lnum = (linenr_T)(lo + i);
4347 char *text;
4348 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004349
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004350 if (lnum > buf->b_ml.ml_line_count)
4351 text = "";
4352 else
4353 text = (char *)ml_get_buf(buf, lnum, FALSE);
4354 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004355 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004356 {
4357 Py_DECREF(list);
4358 return NULL;
4359 }
4360
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004361 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004362 }
4363
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004364 // The ownership of the Python list is passed to the caller (ie,
4365 // the caller should Py_DECREF() the object when it is finished
4366 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004367
4368 return list;
4369}
4370
4371/*
4372 * Check if deleting lines made the cursor position invalid.
4373 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4374 * deleted).
4375 */
4376 static void
4377py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4378{
4379 if (curwin->w_cursor.lnum >= lo)
4380 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004381 // Adjust the cursor position if it's in/after the changed
4382 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383 if (curwin->w_cursor.lnum >= hi)
4384 {
4385 curwin->w_cursor.lnum += extra;
4386 check_cursor_col();
4387 }
4388 else if (extra < 0)
4389 {
4390 curwin->w_cursor.lnum = lo;
4391 check_cursor();
4392 }
4393 else
4394 check_cursor_col();
4395 changed_cline_bef_curs();
4396 }
4397 invalidate_botline();
4398}
4399
Bram Moolenaar19e60942011-06-19 00:27:51 +02004400/*
4401 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004402 * in Vim format (1-based). The replacement line is given as
4403 * a Python string object. The object is checked for validity
4404 * and correct format. Errors are returned as a value of FAIL.
4405 * The return value is OK on success.
4406 * If OK is returned and len_change is not NULL, *len_change
4407 * is set to the change in the buffer length.
4408 */
4409 static int
4410SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4411{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004412 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004413 win_T *save_curwin = NULL;
4414 tabpage_T *save_curtab = NULL;
4415
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004416 // First of all, we check the type of the supplied Python object.
4417 // There are three cases:
4418 // 1. NULL, or None - this is a deletion.
4419 // 2. A string - this is a replacement.
4420 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 if (line == Py_None || line == NULL)
4422 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004424 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004426 VimTryStart();
4427
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004429 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004430 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004431 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 else
4433 {
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004434 if (buf == curbuf && (save_curwin != NULL
4435 || save_curbuf.br_buf == NULL))
4436 // Using an existing window for the buffer, adjust the cursor
4437 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004439 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004440 // Only adjust marks if we managed to switch to a window that
4441 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004442 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 }
4444
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004445 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004447 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448 return FAIL;
4449
4450 if (len_change)
4451 *len_change = -1;
4452
4453 return OK;
4454 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004455 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004457 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004458
4459 if (save == NULL)
4460 return FAIL;
4461
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004462 VimTryStart();
4463
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004464 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004466 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004467
4468 if (u_savesub((linenr_T)n) == FAIL)
4469 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004470 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004471 vim_free(save);
4472 }
4473 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4474 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004475 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004476 vim_free(save);
4477 }
4478 else
4479 changed_bytes((linenr_T)n, 0);
4480
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004481 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004482
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004483 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004484 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004485 check_cursor_col();
4486
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004487 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004488 return FAIL;
4489
4490 if (len_change)
4491 *len_change = 0;
4492
4493 return OK;
4494 }
4495 else
4496 {
4497 PyErr_BadArgument();
4498 return FAIL;
4499 }
4500}
4501
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004502/*
4503 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504 * Vim format (1-based). The range is from lo up to, but not including, hi.
4505 * The replacement lines are given as a Python list of string objects. The
4506 * list is checked for validity and correct format. Errors are returned as a
4507 * value of FAIL. The return value is OK on success.
4508 * If OK is returned and len_change is not NULL, *len_change
4509 * is set to the change in the buffer length.
4510 */
4511 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004512SetBufferLineList(
4513 buf_T *buf,
4514 PyInt lo,
4515 PyInt hi,
4516 PyObject *list,
4517 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004518{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004519 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004520 win_T *save_curwin = NULL;
4521 tabpage_T *save_curtab = NULL;
4522
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004523 // First of all, we check the type of the supplied Python object.
4524 // There are three cases:
4525 // 1. NULL, or None - this is a deletion.
4526 // 2. A list - this is a replacement.
4527 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004528 if (list == Py_None || list == NULL)
4529 {
4530 PyInt i;
4531 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004532
4533 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004534 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004535 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004536
4537 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004538 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004539 else
4540 {
4541 for (i = 0; i < n; ++i)
4542 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004543 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004544 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004545 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004546 break;
4547 }
4548 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004549 if (buf == curbuf && (save_curwin != NULL
4550 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004551 // Using an existing window for the buffer, adjust the cursor
4552 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004553 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004554 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004555 // Only adjust marks if we managed to switch to a window that
4556 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004557 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558 }
4559
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004560 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004561
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004562 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004563 return FAIL;
4564
4565 if (len_change)
4566 *len_change = -n;
4567
4568 return OK;
4569 }
4570 else if (PyList_Check(list))
4571 {
4572 PyInt i;
4573 PyInt new_len = PyList_Size(list);
4574 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004575 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004576 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004577
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004578 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004579 array = NULL;
4580 else
4581 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004582 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004583 if (array == NULL)
4584 {
4585 PyErr_NoMemory();
4586 return FAIL;
4587 }
4588 }
4589
4590 for (i = 0; i < new_len; ++i)
4591 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004592 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004593
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004594 if (!(line = PyList_GetItem(list, i)) ||
4595 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004596 {
4597 while (i)
4598 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004599 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004600 return FAIL;
4601 }
4602 }
4603
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004604 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004605 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004606
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004607 // START of region without "return". Must call restore_buffer()!
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004608 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004609
4610 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004611 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004612
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004613 // If the size of the range is reducing (ie, new_len < old_len) we
4614 // need to delete some old_len. We do this at the start, by
4615 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004616 if (!PyErr_Occurred())
4617 {
4618 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004619 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004620 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004621 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 break;
4623 }
4624 extra -= i;
4625 }
4626
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004627 // For as long as possible, replace the existing old_len with the
4628 // new old_len. This is a more efficient operation, as it requires
4629 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004630 if (!PyErr_Occurred())
4631 {
4632 for (i = 0; i < old_len && i < new_len; ++i)
4633 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4634 == FAIL)
4635 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004636 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004637 break;
4638 }
4639 }
4640 else
4641 i = 0;
4642
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004643 // Now we may need to insert the remaining new old_len. If we do, we
4644 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004645 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004646 if (!PyErr_Occurred())
4647 {
4648 while (i < new_len)
4649 {
4650 if (ml_append((linenr_T)(lo + i - 1),
4651 (char_u *)array[i], 0, FALSE) == FAIL)
4652 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004653 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004654 break;
4655 }
4656 vim_free(array[i]);
4657 ++i;
4658 ++extra;
4659 }
4660 }
4661
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004662 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004663 while (i < new_len)
4664 {
4665 vim_free(array[i]);
4666 ++i;
4667 }
4668
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004669 // Free the array of old_len. All of its contents have now
4670 // been dealt with (either freed, or the responsibility passed
4671 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004672 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004673
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004674 // Adjust marks. Invalidate any which lie in the
4675 // changed range, and move any in the remainder of the buffer.
4676 // Only adjust marks if we managed to switch to a window that holds
4677 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004678 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004679 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004680 (long)MAXLNUM, (long)extra);
4681 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4682
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004683 if (buf == curbuf && (save_curwin != NULL
4684 || save_curbuf.br_buf == NULL))
4685 // Using an existing window for the buffer, adjust the cursor
4686 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004687 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4688
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004689 // END of region without "return".
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004690 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004691
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004692 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004693 return FAIL;
4694
4695 if (len_change)
4696 *len_change = new_len - old_len;
4697
4698 return OK;
4699 }
4700 else
4701 {
4702 PyErr_BadArgument();
4703 return FAIL;
4704 }
4705}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004707/*
4708 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004709 * The line number is in Vim format (1-based). The lines to be inserted are
4710 * given as a Python list of string objects or as a single string. The lines
4711 * to be added are checked for validity and correct format. Errors are
4712 * returned as a value of FAIL. The return value is OK on success.
4713 * If OK is returned and len_change is not NULL, *len_change
4714 * is set to the change in the buffer length.
4715 */
4716 static int
4717InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4718{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004719 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004720 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004721 tabpage_T *save_curtab = NULL;
4722
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004723 // First of all, we check the type of the supplied Python object.
4724 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004725 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004726 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004727 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004728
4729 if (str == NULL)
4730 return FAIL;
4731
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004733 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004734 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004736 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004737 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004739 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004740 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004741 // Only adjust marks if we managed to switch to a window that
4742 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004743 appended_lines_mark((linenr_T)n, 1L);
4744
4745 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004746 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004747 update_screen(VALID);
4748
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004749 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004750 return FAIL;
4751
4752 if (len_change)
4753 *len_change = 1;
4754
4755 return OK;
4756 }
4757 else if (PyList_Check(lines))
4758 {
4759 PyInt i;
4760 PyInt size = PyList_Size(lines);
4761 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004762
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004763 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004764 if (array == NULL)
4765 {
4766 PyErr_NoMemory();
4767 return FAIL;
4768 }
4769
4770 for (i = 0; i < size; ++i)
4771 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004772 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004773
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004774 if (!(line = PyList_GetItem(lines, i)) ||
4775 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004776 {
4777 while (i)
4778 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004779 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004780 return FAIL;
4781 }
4782 }
4783
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004784 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004785 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004786 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004787
4788 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004789 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004790 else
4791 {
4792 for (i = 0; i < size; ++i)
4793 {
4794 if (ml_append((linenr_T)(n + i),
4795 (char_u *)array[i], 0, FALSE) == FAIL)
4796 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004797 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004798
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004799 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004800 while (i < size)
4801 vim_free(array[i++]);
4802
4803 break;
4804 }
4805 vim_free(array[i]);
4806 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004807 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004808 // Only adjust marks if we managed to switch to a window that
4809 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004810 appended_lines_mark((linenr_T)n, (long)i);
4811 }
4812
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004813 // Free the array of lines. All of its contents have now
4814 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004815 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004816 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004818 update_screen(VALID);
4819
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004820 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004821 return FAIL;
4822
4823 if (len_change)
4824 *len_change = size;
4825
4826 return OK;
4827 }
4828 else
4829 {
4830 PyErr_BadArgument();
4831 return FAIL;
4832 }
4833}
4834
4835/*
4836 * Common routines for buffers and line ranges
4837 * -------------------------------------------
4838 */
4839
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004840typedef struct
4841{
4842 PyObject_HEAD
4843 buf_T *buf;
4844} BufferObject;
4845
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004846 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004847CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004848{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004849 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004850 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004851 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004852 return -1;
4853 }
4854
4855 return 0;
4856}
4857
4858 static PyObject *
4859RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4860{
4861 if (CheckBuffer(self))
4862 return NULL;
4863
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004864 if (end == -1)
4865 end = self->buf->b_ml.ml_line_count;
4866
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004867 if (n < 0)
4868 n += end - start + 1;
4869
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004870 if (n < 0 || n > end - start)
4871 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004872 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004873 return NULL;
4874 }
4875
4876 return GetBufferLine(self->buf, n+start);
4877}
4878
4879 static PyObject *
4880RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4881{
4882 PyInt size;
4883
4884 if (CheckBuffer(self))
4885 return NULL;
4886
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004887 if (end == -1)
4888 end = self->buf->b_ml.ml_line_count;
4889
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004890 size = end - start + 1;
4891
4892 if (lo < 0)
4893 lo = 0;
4894 else if (lo > size)
4895 lo = size;
4896 if (hi < 0)
4897 hi = 0;
4898 if (hi < lo)
4899 hi = lo;
4900 else if (hi > size)
4901 hi = size;
4902
4903 return GetBufferLineList(self->buf, lo+start, hi+start);
4904}
4905
4906 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004907RBAsItem(
4908 BufferObject *self,
4909 PyInt n,
4910 PyObject *valObject,
4911 PyInt start,
4912 PyInt end,
4913 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004914{
4915 PyInt len_change;
4916
4917 if (CheckBuffer(self))
4918 return -1;
4919
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004920 if (end == -1)
4921 end = self->buf->b_ml.ml_line_count;
4922
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004923 if (n < 0)
4924 n += end - start + 1;
4925
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004926 if (n < 0 || n > end - start)
4927 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004928 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004929 return -1;
4930 }
4931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004932 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004933 return -1;
4934
4935 if (new_end)
4936 *new_end = end + len_change;
4937
4938 return 0;
4939}
4940
Bram Moolenaar19e60942011-06-19 00:27:51 +02004941 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004942RBAsSlice(
4943 BufferObject *self,
4944 PyInt lo,
4945 PyInt hi,
4946 PyObject *valObject,
4947 PyInt start,
4948 PyInt end,
4949 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004950{
4951 PyInt size;
4952 PyInt len_change;
4953
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004954 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004955 if (CheckBuffer(self))
4956 return -1;
4957
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004958 if (end == -1)
4959 end = self->buf->b_ml.ml_line_count;
4960
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004961 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004962 size = end - start + 1;
4963
4964 if (lo < 0)
4965 lo = 0;
4966 else if (lo > size)
4967 lo = size;
4968 if (hi < 0)
4969 hi = 0;
4970 if (hi < lo)
4971 hi = lo;
4972 else if (hi > size)
4973 hi = size;
4974
4975 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004976 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004977 return -1;
4978
4979 if (new_end)
4980 *new_end = end + len_change;
4981
4982 return 0;
4983}
4984
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004985
4986 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004987RBAppend(
4988 BufferObject *self,
4989 PyObject *args,
4990 PyInt start,
4991 PyInt end,
4992 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004993{
4994 PyObject *lines;
4995 PyInt len_change;
4996 PyInt max;
4997 PyInt n;
4998
4999 if (CheckBuffer(self))
5000 return NULL;
5001
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005002 if (end == -1)
5003 end = self->buf->b_ml.ml_line_count;
5004
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005005 max = n = end - start + 1;
5006
5007 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5008 return NULL;
5009
5010 if (n < 0 || n > max)
5011 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005012 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005013 return NULL;
5014 }
5015
5016 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5017 return NULL;
5018
5019 if (new_end)
5020 *new_end = end + len_change;
5021
5022 Py_INCREF(Py_None);
5023 return Py_None;
5024}
5025
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005026// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005027
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005028static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005029static PySequenceMethods RangeAsSeq;
5030static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005031
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005032typedef struct
5033{
5034 PyObject_HEAD
5035 BufferObject *buf;
5036 PyInt start;
5037 PyInt end;
5038} RangeObject;
5039
5040 static PyObject *
5041RangeNew(buf_T *buf, PyInt start, PyInt end)
5042{
5043 BufferObject *bufr;
5044 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005045 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005046 if (self == NULL)
5047 return NULL;
5048
5049 bufr = (BufferObject *)BufferNew(buf);
5050 if (bufr == NULL)
5051 {
5052 Py_DECREF(self);
5053 return NULL;
5054 }
5055 Py_INCREF(bufr);
5056
5057 self->buf = bufr;
5058 self->start = start;
5059 self->end = end;
5060
5061 return (PyObject *)(self);
5062}
5063
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005064 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005066{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005067 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005068 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005069 PyObject_GC_Del((void *)(self));
5070}
5071
5072 static int
5073RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5074{
5075 Py_VISIT(((PyObject *)(self->buf)));
5076 return 0;
5077}
5078
5079 static int
5080RangeClear(RangeObject *self)
5081{
5082 Py_CLEAR(self->buf);
5083 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005084}
5085
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005087RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005089 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005091 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005092
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094}
5095
5096 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005097RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005098{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005099 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100}
5101
5102 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005103RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005105 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106}
5107
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005108static char *RangeAttrs[] = {
5109 "start", "end",
5110 NULL
5111};
5112
5113 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005114RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005115{
5116 return ObjectDir(self, RangeAttrs);
5117}
5118
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005120RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123}
5124
5125 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005126RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005127{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005129 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5130 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005131 else
5132 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005133 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005134
5135 if (name == NULL)
5136 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005137
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005138 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005139 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005140 }
5141}
5142
5143static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005144 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005145 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005146 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5147 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005148};
5149
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005150static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005151static PySequenceMethods BufferAsSeq;
5152static PyMappingMethods BufferAsMapping;
5153
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005154 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005155BufferNew(buf_T *buf)
5156{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005157 /*
5158 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005159 * If we add a "b_python*_ref" field to the buf_T structure,
5160 * then we can get at it in buf_freeall() in vim. We then
5161 * need to create only ONE Python object per buffer - if
5162 * we try to create a second, just INCREF the existing one
5163 * and return it. The (single) Python object referring to
5164 * the buffer is stored in "b_python*_ref".
5165 * Question: what to do on a buf_freeall(). We'll probably
5166 * have to either delete the Python object (DECREF it to
5167 * zero - a bad idea, as it leaves dangling refs!) or
5168 * set the buf_T * value to an invalid value (-1?), which
5169 * means we need checks in all access functions... Bah.
5170 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005171 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005172 * b_python_ref and b_python3_ref fields respectively.
5173 */
5174
5175 BufferObject *self;
5176
5177 if (BUF_PYTHON_REF(buf) != NULL)
5178 {
5179 self = BUF_PYTHON_REF(buf);
5180 Py_INCREF(self);
5181 }
5182 else
5183 {
5184 self = PyObject_NEW(BufferObject, &BufferType);
5185 if (self == NULL)
5186 return NULL;
5187 self->buf = buf;
5188 BUF_PYTHON_REF(buf) = self;
5189 }
5190
5191 return (PyObject *)(self);
5192}
5193
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005194 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005196{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005197 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5198 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005199
5200 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005201}
5202
Bram Moolenaar971db462013-05-12 18:44:48 +02005203 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005204BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005205{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005206 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005207 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005208 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005209
Bram Moolenaard6e39182013-05-21 18:30:34 +02005210 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005211}
5212
5213 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005214BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005215{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005216 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005217}
5218
5219 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005220BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005221{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005222 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005223}
5224
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005225static char *BufferAttrs[] = {
5226 "name", "number", "vars", "options", "valid",
5227 NULL
5228};
5229
5230 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005231BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005232{
5233 return ObjectDir(self, BufferAttrs);
5234}
5235
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005236 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005237BufferAttrValid(BufferObject *self, char *name)
5238{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005239 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005240
5241 if (strcmp(name, "valid") != 0)
5242 return NULL;
5243
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005244 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5245 Py_INCREF(ret);
5246 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005247}
5248
5249 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005250BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005251{
5252 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005253 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005254 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005255 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005256 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005257 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005258 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005259 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005260 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5261 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005262 else if (strcmp(name, "__members__") == 0)
5263 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005264 else
5265 return NULL;
5266}
5267
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005268 static int
5269BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5270{
5271 if (CheckBuffer(self))
5272 return -1;
5273
5274 if (strcmp(name, "name") == 0)
5275 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005276 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005277 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005278 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005279 PyObject *todecref;
5280
5281 if (!(val = StringToChars(valObject, &todecref)))
5282 return -1;
5283
5284 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005285 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005286 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005287 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005288 aucmd_restbuf(&aco);
5289 Py_XDECREF(todecref);
5290 if (VimTryEnd())
5291 return -1;
5292
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005293 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005294 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005295 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005296 return -1;
5297 }
5298 return 0;
5299 }
5300 else
5301 {
5302 PyErr_SetString(PyExc_AttributeError, name);
5303 return -1;
5304 }
5305}
5306
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005307 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005308BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005309{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311}
5312
5313 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005314BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315{
5316 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005317 char_u *pmark;
5318 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005319 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005320 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321
Bram Moolenaard6e39182013-05-21 18:30:34 +02005322 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005323 return NULL;
5324
Bram Moolenaar389a1792013-06-23 13:00:44 +02005325 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005326 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005327
Bram Moolenaar389a1792013-06-23 13:00:44 +02005328 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005329 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005330 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005331 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005332 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005333 return NULL;
5334 }
5335
5336 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005337
5338 Py_XDECREF(todecref);
5339
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005340 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005341 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005342 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005343 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005344 if (VimTryEnd())
5345 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005346
5347 if (posp == NULL)
5348 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005349 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005350 return NULL;
5351 }
5352
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005353 if (posp->lnum <= 0)
5354 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005355 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005356 Py_INCREF(Py_None);
5357 return Py_None;
5358 }
5359
5360 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5361}
5362
5363 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005364BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005365{
5366 PyInt start;
5367 PyInt end;
5368
Bram Moolenaard6e39182013-05-21 18:30:34 +02005369 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005370 return NULL;
5371
5372 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5373 return NULL;
5374
Bram Moolenaard6e39182013-05-21 18:30:34 +02005375 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005376}
5377
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005378 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005379BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005380{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005381 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005382 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005383 else
5384 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005385 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005386
5387 if (name == NULL)
5388 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005389
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005390 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005391 }
5392}
5393
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005394static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005395 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005396 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005397 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005398 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005399 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5400 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005401};
5402
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005403/*
5404 * Buffer list object - Implementation
5405 */
5406
5407static PyTypeObject BufMapType;
5408
5409typedef struct
5410{
5411 PyObject_HEAD
5412} BufMapObject;
5413
5414 static PyInt
5415BufMapLength(PyObject *self UNUSED)
5416{
5417 buf_T *b = firstbuf;
5418 PyInt n = 0;
5419
5420 while (b)
5421 {
5422 ++n;
5423 b = b->b_next;
5424 }
5425
5426 return n;
5427}
5428
5429 static PyObject *
5430BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5431{
5432 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005433 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005434
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005435 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005436 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005437
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005438 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005439
5440 if (b)
5441 return BufferNew(b);
5442 else
5443 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005444 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005445 return NULL;
5446 }
5447}
5448
5449 static void
5450BufMapIterDestruct(PyObject *buffer)
5451{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005452 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005453 if (buffer)
5454 {
5455 Py_DECREF(buffer);
5456 }
5457}
5458
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005459 static int
5460BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5461{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005462 if (buffer)
5463 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005464 return 0;
5465}
5466
5467 static int
5468BufMapIterClear(PyObject **buffer)
5469{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005470 if (*buffer)
5471 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005472 return 0;
5473}
5474
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005475 static PyObject *
5476BufMapIterNext(PyObject **buffer)
5477{
5478 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005479 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005480
5481 if (!*buffer)
5482 return NULL;
5483
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005484 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005485
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005486 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005487 {
5488 *buffer = NULL;
5489 return NULL;
5490 }
5491
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005493 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005494 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005495 return NULL;
5496 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005497 // Do not increment reference: we no longer hold it (decref), but whoever
5498 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005499 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005500}
5501
5502 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005503BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005504{
5505 PyObject *buffer;
5506
5507 buffer = BufferNew(firstbuf);
5508 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005509 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005510 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5511 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005512}
5513
5514static PyMappingMethods BufMapAsMapping = {
5515 (lenfunc) BufMapLength,
5516 (binaryfunc) BufMapItem,
5517 (objobjargproc) 0,
5518};
5519
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005520// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005521
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005522static char *CurrentAttrs[] = {
5523 "buffer", "window", "line", "range", "tabpage",
5524 NULL
5525};
5526
5527 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005528CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005529{
5530 return ObjectDir(self, CurrentAttrs);
5531}
5532
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005533 static PyObject *
5534CurrentGetattr(PyObject *self UNUSED, char *name)
5535{
5536 if (strcmp(name, "buffer") == 0)
5537 return (PyObject *)BufferNew(curbuf);
5538 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005539 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005540 else if (strcmp(name, "tabpage") == 0)
5541 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005542 else if (strcmp(name, "line") == 0)
5543 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5544 else if (strcmp(name, "range") == 0)
5545 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005546 else if (strcmp(name, "__members__") == 0)
5547 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005548 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005549#if PY_MAJOR_VERSION < 3
5550 return Py_FindMethod(WindowMethods, self, name);
5551#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005552 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005553#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005554}
5555
5556 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005557CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005558{
5559 if (strcmp(name, "line") == 0)
5560 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005561 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5562 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005563 return -1;
5564
5565 return 0;
5566 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005567 else if (strcmp(name, "buffer") == 0)
5568 {
5569 int count;
5570
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005572 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005573 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005574 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005575 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005576 return -1;
5577 }
5578
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005579 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005580 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005582
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005583 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005584 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5585 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005586 if (VimTryEnd())
5587 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005588 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005589 return -1;
5590 }
5591
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005592 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005593 }
5594 else if (strcmp(name, "window") == 0)
5595 {
5596 int count;
5597
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005598 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005599 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005600 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005601 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005602 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005603 return -1;
5604 }
5605
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005606 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005607 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005608 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005609
5610 if (!count)
5611 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005612 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005613 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005614 return -1;
5615 }
5616
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005617 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005618 win_goto(((WindowObject *)(valObject))->win);
5619 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005621 if (VimTryEnd())
5622 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005623 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005624 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005625 return -1;
5626 }
5627
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005628 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005629 }
5630 else if (strcmp(name, "tabpage") == 0)
5631 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005632 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005633 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005634 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005635 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005636 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005637 return -1;
5638 }
5639
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005640 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005641 return -1;
5642
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005643 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005644 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5645 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005646 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005647 if (VimTryEnd())
5648 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005649 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005650 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005651 return -1;
5652 }
5653
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005654 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005655 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005656 else
5657 {
5658 PyErr_SetString(PyExc_AttributeError, name);
5659 return -1;
5660 }
5661}
5662
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005663static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005664 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005665 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5666 { NULL, NULL, 0, NULL}
5667};
5668
Bram Moolenaardb913952012-06-29 12:54:53 +02005669 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005670init_range_cmd(exarg_T *eap)
5671{
5672 RangeStart = eap->line1;
5673 RangeEnd = eap->line2;
5674}
5675
5676 static void
5677init_range_eval(typval_T *rettv UNUSED)
5678{
5679 RangeStart = (PyInt) curwin->w_cursor.lnum;
5680 RangeEnd = RangeStart;
5681}
5682
5683 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005684run_cmd(const char *cmd, void *arg UNUSED
5685#ifdef PY_CAN_RECURSE
5686 , PyGILState_STATE *pygilstate UNUSED
5687#endif
5688 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005689{
Bram Moolenaar41009372013-07-01 22:03:04 +02005690 PyObject *run_ret;
5691 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5692 if (run_ret != NULL)
5693 {
5694 Py_DECREF(run_ret);
5695 }
5696 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5697 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005698 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005699 PyErr_Clear();
5700 }
5701 else
5702 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005703}
5704
5705static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5706static int code_hdr_len = 30;
5707
5708 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005709run_do(const char *cmd, void *arg UNUSED
5710#ifdef PY_CAN_RECURSE
5711 , PyGILState_STATE *pygilstate
5712#endif
5713 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005714{
5715 PyInt lnum;
5716 size_t len;
5717 char *code;
5718 int status;
5719 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005720 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005721 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005722
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005723 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005725 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005726 return;
5727 }
5728
5729 len = code_hdr_len + STRLEN(cmd);
5730 code = PyMem_New(char, len + 1);
5731 memcpy(code, code_hdr, code_hdr_len);
5732 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005733 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5734 status = -1;
5735 if (run_ret != NULL)
5736 {
5737 status = 0;
5738 Py_DECREF(run_ret);
5739 }
5740 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5741 {
5742 PyMem_Free(code);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005743 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005744 PyErr_Clear();
5745 return;
5746 }
5747 else
5748 PyErr_PrintEx(1);
5749
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005750 PyMem_Free(code);
5751
5752 if (status)
5753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005754 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005755 return;
5756 }
5757
5758 status = 0;
5759 pymain = PyImport_AddModule("__main__");
5760 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005761#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005762 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005763#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764
5765 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5766 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005767 PyObject *line;
5768 PyObject *linenr;
5769 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005770
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005771#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005772 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005773#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005774 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005775 if (lnum > curbuf->b_ml.ml_line_count
5776 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005777 goto err;
5778 if (!(linenr = PyInt_FromLong((long) lnum)))
5779 {
5780 Py_DECREF(line);
5781 goto err;
5782 }
5783 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5784 Py_DECREF(line);
5785 Py_DECREF(linenr);
5786 if (!ret)
5787 goto err;
5788
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005789 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005790 if (curbuf != was_curbuf)
5791 {
5792 Py_XDECREF(ret);
5793 goto err;
5794 }
5795
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005796 if (ret != Py_None)
5797 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005798 {
5799 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005800 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005801 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005802
5803 Py_XDECREF(ret);
5804 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005805#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005807#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005808 }
5809 goto out;
5810err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005811#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005812 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005813#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005814 PyErr_PrintEx(0);
5815 PythonIO_Flush();
5816 status = 1;
5817out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005818#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005819 if (!status)
5820 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005821#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005822 Py_DECREF(pyfunc);
5823 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5824 if (status)
5825 return;
5826 check_cursor();
5827 update_curbuf(NOT_VALID);
5828}
5829
5830 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005831run_eval(const char *cmd, typval_T *rettv
5832#ifdef PY_CAN_RECURSE
5833 , PyGILState_STATE *pygilstate UNUSED
5834#endif
5835 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005836{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005837 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005838
Bram Moolenaar41009372013-07-01 22:03:04 +02005839 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005840 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005841 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005842 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005843 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005844 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005845 PyErr_Clear();
5846 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005847 else
5848 {
5849 if (PyErr_Occurred() && !msg_silent)
5850 PyErr_PrintEx(0);
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005851 emsg(_(e_eval_did_not_return_valid_python_object));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005852 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005853 }
5854 else
5855 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005856 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005857 emsg(_(e_failed_to_convert_returned_python_object_to_vim_value));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005858 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005859 }
5860 PyErr_Clear();
5861}
5862
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005864set_ref_in_py(const int copyID)
5865{
5866 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005867 list_T *ll;
5868 int i;
5869 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005870 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005871
5872 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005873 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005874 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005875 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5876 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005877 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005878
5879 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005880 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005881 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005882 {
5883 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005884 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005885 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005886 }
5887
Bram Moolenaar8110a092016-04-14 15:56:09 +02005888 if (lastfunc != NULL)
5889 {
5890 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5891 {
5892 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005893 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005894 if (func->argc)
5895 for (i = 0; !abort && i < func->argc; ++i)
5896 abort = abort
5897 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5898 }
5899 }
5900
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005901 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005902}
5903
5904 static int
5905set_string_copy(char_u *str, typval_T *tv)
5906{
5907 tv->vval.v_string = vim_strsave(str);
5908 if (tv->vval.v_string == NULL)
5909 {
5910 PyErr_NoMemory();
5911 return -1;
5912 }
5913 return 0;
5914}
5915
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005917pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005919 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920 char_u *key;
5921 dictitem_T *di;
5922 PyObject *keyObject;
5923 PyObject *valObject;
5924 Py_ssize_t iter = 0;
5925
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005926 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005927 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005928
5929 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005930 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005931
5932 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5933 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005934 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005935
Bram Moolenaara03e6312013-05-29 22:49:26 +02005936 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005937 {
5938 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005940 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005942 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005943 {
5944 dict_unref(dict);
5945 return -1;
5946 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005947
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005948 if (*key == NUL)
5949 {
5950 dict_unref(dict);
5951 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005952 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005953 return -1;
5954 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005955
5956 di = dictitem_alloc(key);
5957
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005958 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959
5960 if (di == NULL)
5961 {
5962 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005963 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005964 return -1;
5965 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005967 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968 {
5969 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005970 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971 return -1;
5972 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005973
5974 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005976 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005977 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005978 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005979 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005980 return -1;
5981 }
5982 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005983
5984 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985 return 0;
5986}
5987
5988 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005989pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005990{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005991 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005992 char_u *key;
5993 dictitem_T *di;
5994 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005995 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005996 PyObject *keyObject;
5997 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005999 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006000 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006001
6002 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006003 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006004
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006005 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006006 {
6007 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006008 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006009 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006010
6011 if (!(iterator = PyObject_GetIter(list)))
6012 {
6013 dict_unref(dict);
6014 Py_DECREF(list);
6015 return -1;
6016 }
6017 Py_DECREF(list);
6018
6019 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006020 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006021 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006022
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006024 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006025 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006026 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006027 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006028 return -1;
6029 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006030
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006031 if (*key == NUL)
6032 {
6033 Py_DECREF(keyObject);
6034 Py_DECREF(iterator);
6035 Py_XDECREF(todecref);
6036 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006037 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006038 return -1;
6039 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006040
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006041 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006043 Py_DECREF(keyObject);
6044 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006045 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006046 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006047 return -1;
6048 }
6049
6050 di = dictitem_alloc(key);
6051
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006052 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006053 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006054
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006055 if (di == NULL)
6056 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006057 Py_DECREF(iterator);
6058 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006059 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006060 PyErr_NoMemory();
6061 return -1;
6062 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006063
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006064 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006065 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006066 Py_DECREF(iterator);
6067 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006069 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006070 return -1;
6071 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006072
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006073 Py_DECREF(valObject);
6074
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006075 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006077 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006078 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 dictitem_free(di);
6080 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006081 return -1;
6082 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006083 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006084 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006085 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006086 return 0;
6087}
6088
6089 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006090pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006091{
6092 list_T *l;
6093
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006094 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006095 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006096
6097 tv->v_type = VAR_LIST;
6098 tv->vval.v_list = l;
6099
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006100 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006101 {
6102 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006103 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006104 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006105
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006106 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006107 return 0;
6108}
6109
Bram Moolenaardb913952012-06-29 12:54:53 +02006110typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6111
6112 static int
6113convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006114 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006115{
6116 PyObject *capsule;
6117 char hexBuf[sizeof(void *) * 2 + 3];
6118
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006119 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006120
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006121#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006122 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006123#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006124 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006125#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006126 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006127 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006128#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006129 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006130#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006131 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006132#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006133 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6134 {
6135 Py_DECREF(capsule);
6136 tv->v_type = VAR_UNKNOWN;
6137 return -1;
6138 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006139
6140 Py_DECREF(capsule);
6141
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006142 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006143 {
6144 tv->v_type = VAR_UNKNOWN;
6145 return -1;
6146 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006147 // As we are not using copy_tv which increments reference count we must
6148 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006149 if (tv->v_type == VAR_DICT)
6150 ++tv->vval.v_dict->dv_refcount;
6151 else if (tv->v_type == VAR_LIST)
6152 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006153 }
6154 else
6155 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006156 typval_T *v;
6157
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006158#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006159 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006160#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006161 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006162#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006163 copy_tv(v, tv);
6164 }
6165 return 0;
6166}
6167
6168 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006169ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6170{
6171 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006172 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006173
6174 if (!(lookup_dict = PyDict_New()))
6175 return -1;
6176
6177 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6178 {
6179 tv->v_type = VAR_DICT;
6180 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6181 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006182 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006183 }
6184 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006185 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006186 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006187 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006188 else
6189 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006190 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006191 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006192 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006193 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006194 }
6195 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006196 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006197}
6198
6199 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006200ConvertFromPySequence(PyObject *obj, typval_T *tv)
6201{
6202 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006203 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006204
6205 if (!(lookup_dict = PyDict_New()))
6206 return -1;
6207
6208 if (PyType_IsSubtype(obj->ob_type, &ListType))
6209 {
6210 tv->v_type = VAR_LIST;
6211 tv->vval.v_list = (((ListObject *)(obj))->list);
6212 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006213 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006214 }
6215 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006216 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006217 else
6218 {
6219 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006220 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006221 Py_TYPE_NAME(obj));
6222 ret = -1;
6223 }
6224 Py_DECREF(lookup_dict);
6225 return ret;
6226}
6227
6228 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006229ConvertFromPyObject(PyObject *obj, typval_T *tv)
6230{
6231 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006232 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006233
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006234 if (!(lookup_dict = PyDict_New()))
6235 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006236 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006237 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006238 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006239}
6240
6241 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006242_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006243{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006244 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006245 {
6246 tv->v_type = VAR_DICT;
6247 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6248 ++tv->vval.v_dict->dv_refcount;
6249 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006250 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 {
6252 tv->v_type = VAR_LIST;
6253 tv->vval.v_list = (((ListObject *)(obj))->list);
6254 ++tv->vval.v_list->lv_refcount;
6255 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006256 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006258 FunctionObject *func = (FunctionObject *) obj;
6259 if (func->self != NULL || func->argv != NULL)
6260 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006261 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6262
Bram Moolenaar8110a092016-04-14 15:56:09 +02006263 set_partial(func, pt, TRUE);
6264 tv->vval.v_partial = pt;
6265 tv->v_type = VAR_PARTIAL;
6266 }
6267 else
6268 {
6269 if (set_string_copy(func->name, tv) == -1)
6270 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006271
Bram Moolenaar8110a092016-04-14 15:56:09 +02006272 tv->v_type = VAR_FUNC;
6273 }
6274 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006275 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 else if (PyBytes_Check(obj))
6277 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006278 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006280 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006281 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006282 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 return -1;
6284
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006285 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006286 return -1;
6287
6288 tv->v_type = VAR_STRING;
6289 }
6290 else if (PyUnicode_Check(obj))
6291 {
6292 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006293 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006294
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006295 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006296 if (bytes == NULL)
6297 return -1;
6298
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006299 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006300 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006301 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006302 return -1;
6303
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006304 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006305 {
6306 Py_XDECREF(bytes);
6307 return -1;
6308 }
6309 Py_XDECREF(bytes);
6310
6311 tv->v_type = VAR_STRING;
6312 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006313#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006314 else if (PyInt_Check(obj))
6315 {
6316 tv->v_type = VAR_NUMBER;
6317 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006318 if (PyErr_Occurred())
6319 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006320 }
6321#endif
6322 else if (PyLong_Check(obj))
6323 {
6324 tv->v_type = VAR_NUMBER;
6325 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006326 if (PyErr_Occurred())
6327 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006328 }
6329 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006330 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006331#ifdef FEAT_FLOAT
6332 else if (PyFloat_Check(obj))
6333 {
6334 tv->v_type = VAR_FLOAT;
6335 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6336 }
6337#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006338 else if (PyObject_HasAttrString(obj, "keys"))
6339 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006340 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006341 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006342 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006343 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006344 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006345 else if (PyNumber_Check(obj))
6346 {
6347 PyObject *num;
6348
6349 if (!(num = PyNumber_Long(obj)))
6350 return -1;
6351
6352 tv->v_type = VAR_NUMBER;
6353 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6354
6355 Py_DECREF(num);
6356 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006357 else if (obj == Py_None)
6358 {
6359 tv->v_type = VAR_SPECIAL;
6360 tv->vval.v_number = VVAL_NONE;
6361 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006362 else
6363 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006364 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006365 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006366 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006367 return -1;
6368 }
6369 return 0;
6370}
6371
6372 static PyObject *
6373ConvertToPyObject(typval_T *tv)
6374{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006375 typval_T *argv;
6376 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006377 if (tv == NULL)
6378 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006379 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006380 return NULL;
6381 }
6382 switch (tv->v_type)
6383 {
6384 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006385 return PyBytes_FromString(tv->vval.v_string == NULL
6386 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006387 case VAR_NUMBER:
6388 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006389 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006390#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006391 return PyFloat_FromDouble((double) tv->vval.v_float);
6392#endif
6393 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006394 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006396 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006397 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006398 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006399 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006400 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006401 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006402 if (tv->vval.v_partial->pt_argc)
6403 {
6404 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6405 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6406 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6407 }
6408 else
6409 argv = NULL;
6410 if (tv->vval.v_partial->pt_dict != NULL)
6411 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006412 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006413 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006414 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006415 tv->vval.v_partial->pt_dict,
6416 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006417 case VAR_BLOB:
6418 return PyBytes_FromStringAndSize(
6419 (char*) tv->vval.v_blob->bv_ga.ga_data,
6420 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006421 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006422 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006424 case VAR_CHANNEL:
6425 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006426 case VAR_INSTR:
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 Py_INCREF(Py_None);
6428 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006429 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006430 case VAR_SPECIAL:
6431 switch (tv->vval.v_number)
6432 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006433 case VVAL_FALSE: return ALWAYS_FALSE;
6434 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006435 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006436 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006437 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006438 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006439 return NULL;
6440 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006441 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006442}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006443
6444typedef struct
6445{
6446 PyObject_HEAD
6447} CurrentObject;
6448static PyTypeObject CurrentType;
6449
6450 static void
6451init_structs(void)
6452{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006453 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006454 OutputType.tp_name = "vim.message";
6455 OutputType.tp_basicsize = sizeof(OutputObject);
6456 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6457 OutputType.tp_doc = "vim message object";
6458 OutputType.tp_methods = OutputMethods;
6459#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006460 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6461 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006462 OutputType.tp_alloc = call_PyType_GenericAlloc;
6463 OutputType.tp_new = call_PyType_GenericNew;
6464 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006465 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006466#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006467 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6468 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006469 // Disabled, because this causes a crash in test86
6470 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006471#endif
6472
Bram Moolenaara80faa82020-04-12 19:37:17 +02006473 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006474 IterType.tp_name = "vim.iter";
6475 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006476 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006477 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006478 IterType.tp_iter = (getiterfunc)IterIter;
6479 IterType.tp_iternext = (iternextfunc)IterNext;
6480 IterType.tp_dealloc = (destructor)IterDestructor;
6481 IterType.tp_traverse = (traverseproc)IterTraverse;
6482 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006483
Bram Moolenaara80faa82020-04-12 19:37:17 +02006484 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006485 BufferType.tp_name = "vim.buffer";
6486 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006487 BufferType.tp_dealloc = (destructor)BufferDestructor;
6488 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006489 BufferType.tp_as_sequence = &BufferAsSeq;
6490 BufferType.tp_as_mapping = &BufferAsMapping;
6491 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6492 BufferType.tp_doc = "vim buffer object";
6493 BufferType.tp_methods = BufferMethods;
6494#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006495 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006496 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006497 BufferType.tp_alloc = call_PyType_GenericAlloc;
6498 BufferType.tp_new = call_PyType_GenericNew;
6499 BufferType.tp_free = call_PyObject_Free;
6500#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006501 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006502 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006503#endif
6504
Bram Moolenaara80faa82020-04-12 19:37:17 +02006505 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006506 WindowType.tp_name = "vim.window";
6507 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006508 WindowType.tp_dealloc = (destructor)WindowDestructor;
6509 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006510 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006511 WindowType.tp_doc = "vim Window object";
6512 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006513 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6514 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006515#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006516 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6517 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006518 WindowType.tp_alloc = call_PyType_GenericAlloc;
6519 WindowType.tp_new = call_PyType_GenericNew;
6520 WindowType.tp_free = call_PyObject_Free;
6521#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006522 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6523 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006524#endif
6525
Bram Moolenaara80faa82020-04-12 19:37:17 +02006526 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006527 TabPageType.tp_name = "vim.tabpage";
6528 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006529 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6530 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006531 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6532 TabPageType.tp_doc = "vim tab page object";
6533 TabPageType.tp_methods = TabPageMethods;
6534#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006535 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006536 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6537 TabPageType.tp_new = call_PyType_GenericNew;
6538 TabPageType.tp_free = call_PyObject_Free;
6539#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006540 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006541#endif
6542
Bram Moolenaara80faa82020-04-12 19:37:17 +02006543 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006544 BufMapType.tp_name = "vim.bufferlist";
6545 BufMapType.tp_basicsize = sizeof(BufMapObject);
6546 BufMapType.tp_as_mapping = &BufMapAsMapping;
6547 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006548 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006549 BufferType.tp_doc = "vim buffer list";
6550
Bram Moolenaara80faa82020-04-12 19:37:17 +02006551 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006552 WinListType.tp_name = "vim.windowlist";
6553 WinListType.tp_basicsize = sizeof(WinListType);
6554 WinListType.tp_as_sequence = &WinListAsSeq;
6555 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6556 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006557 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006558
Bram Moolenaara80faa82020-04-12 19:37:17 +02006559 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006560 TabListType.tp_name = "vim.tabpagelist";
6561 TabListType.tp_basicsize = sizeof(TabListType);
6562 TabListType.tp_as_sequence = &TabListAsSeq;
6563 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6564 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565
Bram Moolenaara80faa82020-04-12 19:37:17 +02006566 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006567 RangeType.tp_name = "vim.range";
6568 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006569 RangeType.tp_dealloc = (destructor)RangeDestructor;
6570 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006571 RangeType.tp_as_sequence = &RangeAsSeq;
6572 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006573 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006574 RangeType.tp_doc = "vim Range object";
6575 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006576 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6577 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006578#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006579 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006580 RangeType.tp_alloc = call_PyType_GenericAlloc;
6581 RangeType.tp_new = call_PyType_GenericNew;
6582 RangeType.tp_free = call_PyObject_Free;
6583#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006584 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006585#endif
6586
Bram Moolenaara80faa82020-04-12 19:37:17 +02006587 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588 CurrentType.tp_name = "vim.currentdata";
6589 CurrentType.tp_basicsize = sizeof(CurrentObject);
6590 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6591 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006592 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006593#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006594 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6595 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006596#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006597 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6598 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006599#endif
6600
Bram Moolenaara80faa82020-04-12 19:37:17 +02006601 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006602 DictionaryType.tp_name = "vim.dictionary";
6603 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006604 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006605 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006606 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006607 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006608 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006609 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006610 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6611 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6612 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006613#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006614 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6615 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006616#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006617 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6618 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619#endif
6620
Bram Moolenaara80faa82020-04-12 19:37:17 +02006621 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006622 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006623 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006624 ListType.tp_basicsize = sizeof(ListObject);
6625 ListType.tp_as_sequence = &ListAsSeq;
6626 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006627 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006628 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006629 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006630 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006631 ListType.tp_new = (newfunc)ListConstructor;
6632 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006633#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006634 ListType.tp_getattro = (getattrofunc)ListGetattro;
6635 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006636#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006637 ListType.tp_getattr = (getattrfunc)ListGetattr;
6638 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006639#endif
6640
Bram Moolenaara80faa82020-04-12 19:37:17 +02006641 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006642 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006643 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006644 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6645 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006646 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006647 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006648 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006649 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006650 FunctionType.tp_new = (newfunc)FunctionConstructor;
6651 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006652#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006653 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006654#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006655 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006656#endif
6657
Bram Moolenaara80faa82020-04-12 19:37:17 +02006658 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006659 OptionsType.tp_name = "vim.options";
6660 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006661 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006662 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006663 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006664 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006665 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006666 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6667 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6668 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006669
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006670#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006671 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006672 LoaderType.tp_name = "vim.Loader";
6673 LoaderType.tp_basicsize = sizeof(LoaderObject);
6674 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6675 LoaderType.tp_doc = "vim message object";
6676 LoaderType.tp_methods = LoaderMethods;
6677 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006678#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006679
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006680#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006681 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006682 vimmodule.m_name = "vim";
6683 vimmodule.m_doc = "Vim Python interface\n";
6684 vimmodule.m_size = -1;
6685 vimmodule.m_methods = VimMethods;
6686#endif
6687}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006688
6689#define PYTYPE_READY(type) \
6690 if (PyType_Ready(&type)) \
6691 return -1;
6692
6693 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006694init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006695{
6696 PYTYPE_READY(IterType);
6697 PYTYPE_READY(BufferType);
6698 PYTYPE_READY(RangeType);
6699 PYTYPE_READY(WindowType);
6700 PYTYPE_READY(TabPageType);
6701 PYTYPE_READY(BufMapType);
6702 PYTYPE_READY(WinListType);
6703 PYTYPE_READY(TabListType);
6704 PYTYPE_READY(CurrentType);
6705 PYTYPE_READY(DictionaryType);
6706 PYTYPE_READY(ListType);
6707 PYTYPE_READY(FunctionType);
6708 PYTYPE_READY(OptionsType);
6709 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006710#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006711 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006712#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006713 return 0;
6714}
6715
6716 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006717init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006718{
6719 PyObject *path;
6720 PyObject *path_hook;
6721 PyObject *path_hooks;
6722
6723 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6724 return -1;
6725
6726 if (!(path_hooks = PySys_GetObject("path_hooks")))
6727 {
6728 PyErr_Clear();
6729 path_hooks = PyList_New(1);
6730 PyList_SET_ITEM(path_hooks, 0, path_hook);
6731 if (PySys_SetObject("path_hooks", path_hooks))
6732 {
6733 Py_DECREF(path_hooks);
6734 return -1;
6735 }
6736 Py_DECREF(path_hooks);
6737 }
6738 else if (PyList_Check(path_hooks))
6739 {
6740 if (PyList_Append(path_hooks, path_hook))
6741 {
6742 Py_DECREF(path_hook);
6743 return -1;
6744 }
6745 Py_DECREF(path_hook);
6746 }
6747 else
6748 {
6749 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006750 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006751 "You should now do the following:\n"
6752 "- append vim.path_hook to sys.path_hooks\n"
6753 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006754 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006755 Py_DECREF(path_hook);
6756 return 0;
6757 }
6758
6759 if (!(path = PySys_GetObject("path")))
6760 {
6761 PyErr_Clear();
6762 path = PyList_New(1);
6763 Py_INCREF(vim_special_path_object);
6764 PyList_SET_ITEM(path, 0, vim_special_path_object);
6765 if (PySys_SetObject("path", path))
6766 {
6767 Py_DECREF(path);
6768 return -1;
6769 }
6770 Py_DECREF(path);
6771 }
6772 else if (PyList_Check(path))
6773 {
6774 if (PyList_Append(path, vim_special_path_object))
6775 return -1;
6776 }
6777 else
6778 {
6779 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006780 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006781 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006782 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006783 }
6784
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006785 return 0;
6786}
6787
6788static BufMapObject TheBufferMap =
6789{
6790 PyObject_HEAD_INIT(&BufMapType)
6791};
6792
6793static WinListObject TheWindowList =
6794{
6795 PyObject_HEAD_INIT(&WinListType)
6796 NULL
6797};
6798
6799static CurrentObject TheCurrent =
6800{
6801 PyObject_HEAD_INIT(&CurrentType)
6802};
6803
6804static TabListObject TheTabPageList =
6805{
6806 PyObject_HEAD_INIT(&TabListType)
6807};
6808
6809static struct numeric_constant {
6810 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006811 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006812} numeric_constants[] = {
6813 {"VAR_LOCKED", VAR_LOCKED},
6814 {"VAR_FIXED", VAR_FIXED},
6815 {"VAR_SCOPE", VAR_SCOPE},
6816 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6817};
6818
6819static struct object_constant {
6820 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006821 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006822} object_constants[] = {
6823 {"buffers", (PyObject *)(void *)&TheBufferMap},
6824 {"windows", (PyObject *)(void *)&TheWindowList},
6825 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6826 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006827
6828 {"Buffer", (PyObject *)&BufferType},
6829 {"Range", (PyObject *)&RangeType},
6830 {"Window", (PyObject *)&WindowType},
6831 {"TabPage", (PyObject *)&TabPageType},
6832 {"Dictionary", (PyObject *)&DictionaryType},
6833 {"List", (PyObject *)&ListType},
6834 {"Function", (PyObject *)&FunctionType},
6835 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006836#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006837 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006838#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006839};
6840
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006841#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006842 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006843 return -1;
6844
6845#define ADD_CHECKED_OBJECT(m, name, obj) \
6846 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006847 PyObject *valObject = obj; \
6848 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006849 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006850 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006851 }
6852
6853 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006854populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006855{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006856 int i;
6857 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006858 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006859 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006860#if PY_VERSION_HEX >= 0x030700f0
6861 PyObject *dict;
6862 PyObject *cls;
6863#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006864
6865 for (i = 0; i < (int)(sizeof(numeric_constants)
6866 / sizeof(struct numeric_constant));
6867 ++i)
6868 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006869 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006870
6871 for (i = 0; i < (int)(sizeof(object_constants)
6872 / sizeof(struct object_constant));
6873 ++i)
6874 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006875 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006876
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006877 valObject = object_constants[i].valObject;
6878 Py_INCREF(valObject);
6879 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006880 }
6881
6882 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6883 return -1;
6884 ADD_OBJECT(m, "error", VimError);
6885
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006886 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6887 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006888 ADD_CHECKED_OBJECT(m, "options",
6889 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006890
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006891 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006892 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006893 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894
Bram Moolenaar22081f42016-06-01 20:38:34 +02006895#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006896 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006897 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006898#else
6899 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6900 return -1;
6901#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006902 ADD_OBJECT(m, "_getcwd", py_getcwd)
6903
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006904 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006905 return -1;
6906 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006907 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006908 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006909 if (PyObject_SetAttrString(other_module, "chdir", attr))
6910 {
6911 Py_DECREF(attr);
6912 return -1;
6913 }
6914 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006915
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006916 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006917 {
6918 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006919 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006920 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006921 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6922 {
6923 Py_DECREF(attr);
6924 return -1;
6925 }
6926 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006927 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006928 else
6929 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006930
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006931 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6932 return -1;
6933
6934 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6935
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006936#if PY_VERSION_HEX >= 0x030700f0
6937 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6938 return -1;
6939
6940 dict = PyModule_GetDict(imp);
6941
6942 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6943 {
6944 Py_DECREF(imp);
6945 return -1;
6946 }
6947
6948 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6949 {
6950 Py_DECREF(imp);
6951 return -1;
6952 }
6953
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006954 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6955 {
6956 // find_module() is deprecated, this may stop working in some later
6957 // version.
6958 ADD_OBJECT(m, "_find_module", py_find_module);
6959 }
6960
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006961 Py_DECREF(imp);
6962
6963 ADD_OBJECT(m, "_find_spec", py_find_spec);
6964#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006965 if (!(imp = PyImport_ImportModule("imp")))
6966 return -1;
6967
6968 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6969 {
6970 Py_DECREF(imp);
6971 return -1;
6972 }
6973
6974 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6975 {
6976 Py_DECREF(py_find_module);
6977 Py_DECREF(imp);
6978 return -1;
6979 }
6980
6981 Py_DECREF(imp);
6982
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006983 ADD_OBJECT(m, "_find_module", py_find_module);
6984 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006985#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006986
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006987 return 0;
6988}