blob: 0cde18d0512b50f0296cab4bcd8c6ae145ad195d [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
kylo2529dac9b12022-03-27 20:05:17 +010033#define Py_TYPE_NAME(obj) ((obj)->ob_type->tp_name == NULL \
Bram Moolenaarc476e522013-06-23 13:46:40 +020034 ? "(NULL)" \
kylo2529dac9b12022-03-27 20:05:17 +010035 : (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 Moolenaara4d158b2022-08-14 14:17:45 +0100700 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701
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 Moolenaarca8a4df2010-07-31 19:54:14 +0200764 else if (our_tv->v_type == VAR_FLOAT)
765 {
766 char buf[NUMBUFLEN];
767
768 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200769 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 else if (our_tv->v_type == VAR_LIST)
772 {
773 list_T *list = our_tv->vval.v_list;
774 listitem_T *curr;
775
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 if (list == NULL)
777 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200779 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 return NULL;
781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200783 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 return NULL;
786 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200788 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200789 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200790 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200791 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200792 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200794 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200796 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200797 {
798 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200799 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 return NULL;
801 }
802 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803 }
804 }
805 else if (our_tv->v_type == VAR_DICT)
806 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200807
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100808 hashtab_T *ht;
809 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200810 hashitem_T *hi;
811 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100812
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200813 if (our_tv->vval.v_dict == NULL)
814 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100815 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200816
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200817 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200818 return NULL;
819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200820 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200821 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200822 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200823 return NULL;
824 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200825
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100826 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200827 for (hi = ht->ht_array; todo > 0; ++hi)
828 {
829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200830 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200831 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200833 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200834 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200835 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200837 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200838 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200840 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200841 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200842 Py_DECREF(newObj);
843 return NULL;
844 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200845 }
846 }
847 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100848 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100849 {
850 if (our_tv->vval.v_number == VVAL_FALSE)
851 {
852 ret = Py_False;
853 Py_INCREF(ret);
854 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100855 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100856 {
857 ret = Py_True;
858 Py_INCREF(ret);
859 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100860 return ret;
861 }
862 else if (our_tv->v_type == VAR_SPECIAL)
863 {
864 Py_INCREF(Py_None);
865 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100866 return ret;
867 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100868 else if (our_tv->v_type == VAR_BLOB)
869 ret = PyBytes_FromStringAndSize(
870 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
871 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200872 else
873 {
874 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200875 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200876 }
877
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200878 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200879}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200880
881 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200882VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200883{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200884 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200886 PyObject *string;
887 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200888 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200889 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200890
Bram Moolenaar389a1792013-06-23 13:00:44 +0200891 if (!PyArg_ParseTuple(args, "O", &string))
892 return NULL;
893
894 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200895 return NULL;
896
897 Py_BEGIN_ALLOW_THREADS
898 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200899 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200900 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200901 Python_Release_Vim();
902 Py_END_ALLOW_THREADS
903
Bram Moolenaar389a1792013-06-23 13:00:44 +0200904 Py_XDECREF(todecref);
905
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200906 if (VimTryEnd())
907 return NULL;
908
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200909 if (our_tv == NULL)
910 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200911 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200912 return NULL;
913 }
914
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100915 // Convert the Vim type into a Python type. Create a dictionary that's
916 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200917 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200918 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200919 else
920 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200921 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200922 Py_DECREF(lookup_dict);
923 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200924
925
926 Py_BEGIN_ALLOW_THREADS
927 Python_Lock_Vim();
928 free_tv(our_tv);
929 Python_Release_Vim();
930 Py_END_ALLOW_THREADS
931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200932 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200933}
934
Bram Moolenaardb913952012-06-29 12:54:53 +0200935static PyObject *ConvertToPyObject(typval_T *);
936
937 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200938VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200939{
Bram Moolenaardb913952012-06-29 12:54:53 +0200940 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200941 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200942 char_u *expr;
943 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200944
Bram Moolenaar389a1792013-06-23 13:00:44 +0200945 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200946 return NULL;
947
948 Py_BEGIN_ALLOW_THREADS
949 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200950 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200951 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200952 Python_Release_Vim();
953 Py_END_ALLOW_THREADS
954
Bram Moolenaar389a1792013-06-23 13:00:44 +0200955 Py_XDECREF(todecref);
956
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200957 if (VimTryEnd())
958 return NULL;
959
Bram Moolenaardb913952012-06-29 12:54:53 +0200960 if (our_tv == NULL)
961 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200962 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200963 return NULL;
964 }
965
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200966 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200967 Py_BEGIN_ALLOW_THREADS
968 Python_Lock_Vim();
969 free_tv(our_tv);
970 Python_Release_Vim();
971 Py_END_ALLOW_THREADS
972
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200973 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200974}
975
976 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200977VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200978{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200979 char_u *str;
980 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200981 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200982
Bram Moolenaar389a1792013-06-23 13:00:44 +0200983 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200984 return NULL;
985
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200986 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200987
988 Py_XDECREF(todecref);
989
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200990 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200991}
992
Bram Moolenaarf4258302013-06-02 18:20:17 +0200993 static PyObject *
994_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
995{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200996 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200997 PyObject *newwd;
998 PyObject *todecref;
999 char_u *new_dir;
1000
Bram Moolenaard4209d22013-06-05 20:34:15 +02001001 if (_chdir == NULL)
1002 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001003 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001004 return NULL;
1005
1006 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1007 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001008 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001009 return NULL;
1010 }
1011
1012 if (!(new_dir = StringToChars(newwd, &todecref)))
1013 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001014 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001015 Py_DECREF(newwd);
1016 return NULL;
1017 }
1018
1019 VimTryStart();
1020
1021 if (vim_chdir(new_dir))
1022 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001023 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001024 Py_DECREF(newwd);
1025 Py_XDECREF(todecref);
1026
1027 if (VimTryEnd())
1028 return NULL;
1029
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001030 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001031 return NULL;
1032 }
1033
1034 Py_DECREF(newwd);
1035 Py_XDECREF(todecref);
1036
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001037 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001038
1039 if (VimTryEnd())
1040 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001041 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001042 return NULL;
1043 }
1044
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001045 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001046}
1047
1048 static PyObject *
1049VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1050{
1051 return _VimChdir(py_chdir, args, kwargs);
1052}
1053
1054 static PyObject *
1055VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1056{
1057 return _VimChdir(py_fchdir, args, kwargs);
1058}
1059
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001060typedef struct {
1061 PyObject *callable;
1062 PyObject *result;
1063} map_rtp_data;
1064
1065 static void
1066map_rtp_callback(char_u *path, void *_data)
1067{
1068 void **data = (void **) _data;
1069 PyObject *pathObject;
1070 map_rtp_data *mr_data = *((map_rtp_data **) data);
1071
Bram Moolenaar41009372013-07-01 22:03:04 +02001072 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001073 {
1074 *data = NULL;
1075 return;
1076 }
1077
1078 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1079 pathObject, NULL);
1080
1081 Py_DECREF(pathObject);
1082
1083 if (!mr_data->result || mr_data->result != Py_None)
1084 *data = NULL;
1085 else
1086 {
1087 Py_DECREF(mr_data->result);
1088 mr_data->result = NULL;
1089 }
1090}
1091
1092 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001093VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001094{
1095 map_rtp_data data;
1096
Bram Moolenaar389a1792013-06-23 13:00:44 +02001097 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001098 data.result = NULL;
1099
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001100 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001101
1102 if (data.result == NULL)
1103 {
1104 if (PyErr_Occurred())
1105 return NULL;
1106 else
1107 {
1108 Py_INCREF(Py_None);
1109 return Py_None;
1110 }
1111 }
1112 return data.result;
1113}
1114
1115/*
1116 * _vim_runtimepath_ special path implementation.
1117 */
1118
1119 static void
1120map_finder_callback(char_u *path, void *_data)
1121{
1122 void **data = (void **) _data;
1123 PyObject *list = *((PyObject **) data);
1124 PyObject *pathObject1, *pathObject2;
1125 char *pathbuf;
1126 size_t pathlen;
1127
1128 pathlen = STRLEN(path);
1129
1130#if PY_MAJOR_VERSION < 3
1131# define PY_MAIN_DIR_STRING "python2"
1132#else
1133# define PY_MAIN_DIR_STRING "python3"
1134#endif
1135#define PY_ALTERNATE_DIR_STRING "pythonx"
1136
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001137#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001138 if (!(pathbuf = PyMem_New(char,
1139 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1140 {
1141 PyErr_NoMemory();
1142 *data = NULL;
1143 return;
1144 }
1145
1146 mch_memmove(pathbuf, path, pathlen + 1);
1147 add_pathsep((char_u *) pathbuf);
1148
1149 pathlen = STRLEN(pathbuf);
1150 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1151 PYTHONX_STRING_LENGTH + 1);
1152
1153 if (!(pathObject1 = PyString_FromString(pathbuf)))
1154 {
1155 *data = NULL;
1156 PyMem_Free(pathbuf);
1157 return;
1158 }
1159
1160 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1161 PYTHONX_STRING_LENGTH + 1);
1162
1163 if (!(pathObject2 = PyString_FromString(pathbuf)))
1164 {
1165 Py_DECREF(pathObject1);
1166 PyMem_Free(pathbuf);
1167 *data = NULL;
1168 return;
1169 }
1170
1171 PyMem_Free(pathbuf);
1172
1173 if (PyList_Append(list, pathObject1)
1174 || PyList_Append(list, pathObject2))
1175 *data = NULL;
1176
1177 Py_DECREF(pathObject1);
1178 Py_DECREF(pathObject2);
1179}
1180
1181 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001182Vim_GetPaths(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001183{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001184 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001185
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001186 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001187 return NULL;
1188
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001189 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001190
1191 if (PyErr_Occurred())
1192 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001193 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001194 return NULL;
1195 }
1196
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001197 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001198}
1199
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001200#if PY_VERSION_HEX >= 0x030700f0
1201 static PyObject *
1202FinderFindSpec(PyObject *self, PyObject *args)
1203{
1204 char *fullname;
1205 PyObject *paths;
1206 PyObject *target = Py_None;
1207 PyObject *spec;
1208
1209 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1210 return NULL;
1211
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001212 if (!(paths = Vim_GetPaths(self, NULL)))
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001213 return NULL;
1214
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001215 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001216
1217 Py_DECREF(paths);
1218
1219 if (!spec)
1220 {
1221 if (PyErr_Occurred())
1222 return NULL;
1223
1224 Py_INCREF(Py_None);
1225 return Py_None;
1226 }
1227
1228 return spec;
1229}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001230
1231 static PyObject *
1232FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1233{
1234 // Apparently returning None works.
1235 Py_INCREF(Py_None);
1236 return Py_None;
1237}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001238#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001239 static PyObject *
1240call_load_module(char *name, int len, PyObject *find_module_result)
1241{
1242 PyObject *fd, *pathname, *description;
1243
Bram Moolenaarc476e522013-06-23 13:46:40 +02001244 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001245 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001246 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001247 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001248 Py_TYPE_NAME(find_module_result));
1249 return NULL;
1250 }
1251 if (PyTuple_GET_SIZE(find_module_result) != 3)
1252 {
1253 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001254 N_("expected 3-tuple as imp.find_module() result, but got "
1255 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001256 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001257 return NULL;
1258 }
1259
1260 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1261 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1262 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1263 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001264 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001265 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001266 return NULL;
1267 }
1268
1269 return PyObject_CallFunction(py_load_module,
1270 "s#OOO", name, len, fd, pathname, description);
1271}
1272
1273 static PyObject *
1274find_module(char *fullname, char *tail, PyObject *new_path)
1275{
1276 PyObject *find_module_result;
1277 PyObject *module;
1278 char *dot;
1279
Bram Moolenaar41009372013-07-01 22:03:04 +02001280 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001281 {
1282 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001283 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001284 * first component
1285 */
1286 PyObject *newest_path;
1287 int partlen = (int) (dot - 1 - tail);
1288
1289 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1290 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001291 {
1292 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1293 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001294 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001295 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001296
1297 if (!(module = call_load_module(
1298 fullname,
1299 ((int) (tail - fullname)) + partlen,
1300 find_module_result)))
1301 {
1302 Py_DECREF(find_module_result);
1303 return NULL;
1304 }
1305
1306 Py_DECREF(find_module_result);
1307
1308 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1309 {
1310 Py_DECREF(module);
1311 return NULL;
1312 }
1313
1314 Py_DECREF(module);
1315
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001316 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001317
1318 Py_DECREF(newest_path);
1319
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001320 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001321 }
1322 else
1323 {
1324 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1325 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001326 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001327 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1328 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001329 return NULL;
1330 }
1331
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001332 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001333 }
1334}
1335
1336 static PyObject *
1337FinderFindModule(PyObject *self, PyObject *args)
1338{
1339 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001340 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001341 PyObject *new_path;
1342 LoaderObject *loader;
1343
1344 if (!PyArg_ParseTuple(args, "s", &fullname))
1345 return NULL;
1346
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001347 if (!(new_path = Vim_GetPaths(self, NULL)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001348 return NULL;
1349
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001350 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001351
1352 Py_DECREF(new_path);
1353
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001354 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001355 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001356 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001357 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001358
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001359 Py_INCREF(Py_None);
1360 return Py_None;
1361 }
1362
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001363 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001364 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001365 Py_DECREF(result);
1366 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001367 return NULL;
1368 }
1369
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001370 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1371 {
1372 vim_free(fullname);
1373 Py_DECREF(result);
1374 return NULL;
1375 }
1376
1377 loader->fullname = fullname;
1378 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001379
1380 return (PyObject *) loader;
1381}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001382#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001383
1384 static PyObject *
1385VimPathHook(PyObject *self UNUSED, PyObject *args)
1386{
1387 char *path;
1388
1389 if (PyArg_ParseTuple(args, "s", &path)
1390 && STRCMP(path, vim_special_path) == 0)
1391 {
1392 Py_INCREF(vim_module);
1393 return vim_module;
1394 }
1395
1396 PyErr_Clear();
1397 PyErr_SetNone(PyExc_ImportError);
1398 return NULL;
1399}
1400
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001401/*
1402 * Vim module - Definitions
1403 */
1404
1405static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001406 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001407 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001408 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001409 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001410 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001411 {"chdir", (PyCFunction)(void *)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1412 {"fchdir", (PyCFunction)(void *)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001413 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001414#if PY_VERSION_HEX >= 0x030700f0
1415 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001416#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001417 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001418 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1419 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1420 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001421};
1422
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001423/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001424 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001425 */
1426
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001427static PyTypeObject IterType;
1428
1429typedef PyObject *(*nextfun)(void **);
1430typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001431typedef int (*traversefun)(void *, visitproc, void *);
1432typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001433
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001434// Main purpose of this object is removing the need for do python
1435// initialization (i.e. PyType_Ready and setting type attributes) for a big
1436// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001437typedef struct
1438{
1439 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001440 void *cur;
1441 nextfun next;
1442 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001443 traversefun traverse;
1444 clearfun clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001445 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001446} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001447
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001448 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001449IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001450 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001451{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001452 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001453
Bram Moolenaar774267b2013-05-21 20:51:59 +02001454 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001455 self->cur = start;
1456 self->next = next;
1457 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001458 self->traverse = traverse;
1459 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001460 self->iter_object = iter_object;
1461
1462 if (iter_object)
1463 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001464
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001465 return (PyObject *)(self);
1466}
1467
1468 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001469IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001470{
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001471 if (self->iter_object)
1472 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001473 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001474 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001475 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001476}
1477
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001478 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001479IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001480{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001481 if (self->traverse != NULL)
1482 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001483 else
1484 return 0;
1485}
1486
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001487// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001488#ifdef clear
1489# undef clear
1490#endif
1491
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001492 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001493IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001494{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001495 if (self->clear != NULL)
1496 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001497 else
1498 return 0;
1499}
1500
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001501 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001503{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001504 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001505}
1506
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001507 static PyObject *
1508IterIter(PyObject *self)
1509{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001510 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001511 return self;
1512}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001513
Bram Moolenaardb913952012-06-29 12:54:53 +02001514typedef struct pylinkedlist_S {
1515 struct pylinkedlist_S *pll_next;
1516 struct pylinkedlist_S *pll_prev;
1517 PyObject *pll_obj;
1518} pylinkedlist_T;
1519
1520static pylinkedlist_T *lastdict = NULL;
1521static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001522static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001523
1524 static void
1525pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1526{
1527 if (ref->pll_prev == NULL)
1528 {
1529 if (ref->pll_next == NULL)
1530 {
1531 *last = NULL;
1532 return;
1533 }
1534 }
1535 else
1536 ref->pll_prev->pll_next = ref->pll_next;
1537
1538 if (ref->pll_next == NULL)
1539 *last = ref->pll_prev;
1540 else
1541 ref->pll_next->pll_prev = ref->pll_prev;
1542}
1543
1544 static void
1545pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1546{
1547 if (*last == NULL)
1548 ref->pll_prev = NULL;
1549 else
1550 {
1551 (*last)->pll_next = ref;
1552 ref->pll_prev = *last;
1553 }
1554 ref->pll_next = NULL;
1555 ref->pll_obj = self;
1556 *last = ref;
1557}
1558
1559static PyTypeObject DictionaryType;
1560
1561typedef struct
1562{
1563 PyObject_HEAD
1564 dict_T *dict;
1565 pylinkedlist_T ref;
1566} DictionaryObject;
1567
Bram Moolenaara9922d62013-05-30 13:01:18 +02001568static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1569
1570#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1571
Bram Moolenaardb913952012-06-29 12:54:53 +02001572 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001573DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001574{
1575 DictionaryObject *self;
1576
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001578 if (self == NULL)
1579 return NULL;
1580 self->dict = dict;
1581 ++dict->dv_refcount;
1582
1583 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1584
1585 return (PyObject *)(self);
1586}
1587
Bram Moolenaara9922d62013-05-30 13:01:18 +02001588 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001589py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001591 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594 {
1595 PyErr_NoMemory();
1596 return NULL;
1597 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001598 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001599
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001600 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601}
1602
1603 static PyObject *
1604DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1605{
1606 DictionaryObject *self;
1607 dict_T *dict;
1608
1609 if (!(dict = py_dict_alloc()))
1610 return NULL;
1611
1612 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1613
1614 --dict->dv_refcount;
1615
1616 if (kwargs || PyTuple_Size(args))
1617 {
1618 PyObject *tmp;
1619 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1620 {
1621 Py_DECREF(self);
1622 return NULL;
1623 }
1624
1625 Py_DECREF(tmp);
1626 }
1627
1628 return (PyObject *)(self);
1629}
1630
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001631 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001632DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001633{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001634 pyll_remove(&self->ref, &lastdict);
1635 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001636
1637 DESTRUCTOR_FINISH(self);
1638}
1639
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001640static char *DictionaryAttrs[] = {
1641 "locked", "scope",
1642 NULL
1643};
1644
1645 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001646DictionaryDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001647{
1648 return ObjectDir(self, DictionaryAttrs);
1649}
1650
Bram Moolenaardb913952012-06-29 12:54:53 +02001651 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001652DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001653{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001654 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001655 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001656 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001657 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001658 return -1;
1659 }
1660
1661 if (strcmp(name, "locked") == 0)
1662 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001663 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001664 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001665 PyErr_SET_STRING(PyExc_TypeError,
1666 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001667 return -1;
1668 }
1669 else
1670 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001671 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001672 if (istrue == -1)
1673 return -1;
1674 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001675 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001676 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001677 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001678 }
1679 return 0;
1680 }
1681 else
1682 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001683 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001684 return -1;
1685 }
1686}
1687
1688 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001689DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001690{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001691 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001692}
1693
Bram Moolenaara9922d62013-05-30 13:01:18 +02001694#define DICT_FLAG_HAS_DEFAULT 0x01
1695#define DICT_FLAG_POP 0x02
1696#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001697#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001698#define DICT_FLAG_RETURN_PAIR 0x10
1699
Bram Moolenaardb913952012-06-29 12:54:53 +02001700 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001702{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001703 PyObject *keyObject;
1704 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001705 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001706 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001707 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001708 dict_T *dict = self->dict;
1709 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001710 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001711
Bram Moolenaara9922d62013-05-30 13:01:18 +02001712 if (flags & DICT_FLAG_HAS_DEFAULT)
1713 {
1714 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1715 return NULL;
1716 }
1717 else
1718 keyObject = args;
1719
1720 if (flags & DICT_FLAG_RETURN_BOOL)
1721 defObject = Py_False;
1722
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001723 if (!(key = StringToChars(keyObject, &todecref)))
1724 return NULL;
1725
1726 if (*key == NUL)
1727 {
1728 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001729 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001730 return NULL;
1731 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001732
Bram Moolenaara9922d62013-05-30 13:01:18 +02001733 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001734
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001735 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001736
Bram Moolenaara9922d62013-05-30 13:01:18 +02001737 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001738 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001739 if (defObject)
1740 {
1741 Py_INCREF(defObject);
1742 return defObject;
1743 }
1744 else
1745 {
1746 PyErr_SetObject(PyExc_KeyError, keyObject);
1747 return NULL;
1748 }
1749 }
1750 else if (flags & DICT_FLAG_RETURN_BOOL)
1751 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001752 ret = Py_True;
1753 Py_INCREF(ret);
1754 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001755 }
1756
1757 di = dict_lookup(hi);
1758
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001759 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001760 return NULL;
1761
1762 if (flags & DICT_FLAG_POP)
1763 {
1764 if (dict->dv_lock)
1765 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001766 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001767 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001768 return NULL;
1769 }
1770
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001771 hash_remove(&dict->dv_hashtab, hi, "Python remove variable");
Bram Moolenaara9922d62013-05-30 13:01:18 +02001772 dictitem_free(di);
1773 }
1774
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001775 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001776}
1777
1778 static PyObject *
1779DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1780{
1781 return _DictionaryItem(self, keyObject, 0);
1782}
1783
1784 static int
1785DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1786{
1787 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001788 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001789
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001790 if (rObj == NULL)
1791 return -1;
1792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001793 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001794
Bram Moolenaardee2e312013-06-23 16:35:47 +02001795 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001797 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798}
1799
1800typedef struct
1801{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001802 int dii_changed;
1803 hashtab_T *dii_ht;
1804 hashitem_T *dii_hi;
1805 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001806} dictiterinfo_T;
1807
1808 static PyObject *
1809DictionaryIterNext(dictiterinfo_T **dii)
1810{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001811 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001812
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001813 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001814 return NULL;
1815
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001816 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001817 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001818 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001819 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001820 return NULL;
1821 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001822
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001823 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
1824 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001825
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001826 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001827
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001828 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 return NULL;
1830
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001831 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001832}
1833
1834 static PyObject *
1835DictionaryIter(DictionaryObject *self)
1836{
1837 dictiterinfo_T *dii;
1838 hashtab_T *ht;
1839
1840 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1841 {
1842 PyErr_NoMemory();
1843 return NULL;
1844 }
1845
1846 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001847 dii->dii_changed = ht->ht_changed;
1848 dii->dii_ht = ht;
1849 dii->dii_hi = ht->ht_array;
1850 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001851
1852 return IterNew(dii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001853 (destructorfun)(void *) PyMem_Free, (nextfun) DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001854 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001855}
1856
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001857 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001858DictionaryAssItem(
1859 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001860{
1861 char_u *key;
1862 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001863 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001864 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001865 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001867 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001868 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001869 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 return -1;
1871 }
1872
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001873 if (!(key = StringToChars(keyObject, &todecref)))
1874 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001875
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001876 if (*key == NUL)
1877 {
1878 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001879 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001880 return -1;
1881 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001882
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001883 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001884
1885 if (valObject == NULL)
1886 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001887 hashitem_T *hi;
1888
Bram Moolenaardb913952012-06-29 12:54:53 +02001889 if (di == NULL)
1890 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001891 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001892 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 return -1;
1894 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001895 hi = hash_find(&dict->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001896 hash_remove(&dict->dv_hashtab, hi, "Python remove item");
Bram Moolenaardb913952012-06-29 12:54:53 +02001897 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001898 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 return 0;
1900 }
1901
1902 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001903 {
1904 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001905 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001906 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001907
1908 if (di == NULL)
1909 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001910 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001912 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001913 PyErr_NoMemory();
1914 return -1;
1915 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001916 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001917
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001918 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001919 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001920 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001921 RAISE_KEY_ADD_FAIL(key);
1922 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001923 return -1;
1924 }
1925 }
1926 else
1927 clear_tv(&di->di_tv);
1928
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001929 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001930
1931 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001932 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001933 return 0;
1934}
1935
Bram Moolenaara9922d62013-05-30 13:01:18 +02001936typedef PyObject *(*hi_to_py)(hashitem_T *);
1937
Bram Moolenaardb913952012-06-29 12:54:53 +02001938 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001940{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001941 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 long_u todo = dict->dv_hashtab.ht_used;
1943 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001944 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001945 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001946 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001947
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001948 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001949 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1950 {
1951 if (!HASHITEM_EMPTY(hi))
1952 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001953 if (!(newObj = hiconvert(hi)))
1954 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001955 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001956 return NULL;
1957 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001958 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001959 --todo;
1960 ++i;
1961 }
1962 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001963 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001964}
1965
Bram Moolenaara9922d62013-05-30 13:01:18 +02001966 static PyObject *
1967dict_key(hashitem_T *hi)
1968{
1969 return PyBytes_FromString((char *)(hi->hi_key));
1970}
1971
1972 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001973DictionaryListKeys(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001974{
1975 return DictionaryListObjects(self, dict_key);
1976}
1977
1978 static PyObject *
1979dict_val(hashitem_T *hi)
1980{
1981 dictitem_T *di;
1982
1983 di = dict_lookup(hi);
1984 return ConvertToPyObject(&di->di_tv);
1985}
1986
1987 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001988DictionaryListValues(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001989{
1990 return DictionaryListObjects(self, dict_val);
1991}
1992
1993 static PyObject *
1994dict_item(hashitem_T *hi)
1995{
1996 PyObject *keyObject;
1997 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001998 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001999
2000 if (!(keyObject = dict_key(hi)))
2001 return NULL;
2002
2003 if (!(valObject = dict_val(hi)))
2004 {
2005 Py_DECREF(keyObject);
2006 return NULL;
2007 }
2008
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002009 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010
2011 Py_DECREF(keyObject);
2012 Py_DECREF(valObject);
2013
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002014 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002015}
2016
2017 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002018DictionaryListItems(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002019{
2020 return DictionaryListObjects(self, dict_item);
2021}
2022
2023 static PyObject *
2024DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2025{
2026 dict_T *dict = self->dict;
2027
2028 if (dict->dv_lock)
2029 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002030 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002031 return NULL;
2032 }
2033
2034 if (kwargs)
2035 {
2036 typval_T tv;
2037
2038 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2039 return NULL;
2040
2041 VimTryStart();
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002042 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force", NULL);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002043 clear_tv(&tv);
2044 if (VimTryEnd())
2045 return NULL;
2046 }
2047 else
2048 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002049 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002050
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002051 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002052 return NULL;
2053
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002054 if (obj == NULL)
2055 {
2056 Py_INCREF(Py_None);
2057 return Py_None;
2058 }
2059
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002060 if (PyObject_HasAttrString(obj, "keys"))
2061 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002062 else
2063 {
2064 PyObject *iterator;
2065 PyObject *item;
2066
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002067 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002068 return NULL;
2069
2070 while ((item = PyIter_Next(iterator)))
2071 {
2072 PyObject *fast;
2073 PyObject *keyObject;
2074 PyObject *valObject;
2075 PyObject *todecref;
2076 char_u *key;
2077 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002078 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002079
2080 if (!(fast = PySequence_Fast(item, "")))
2081 {
2082 Py_DECREF(iterator);
2083 Py_DECREF(item);
2084 return NULL;
2085 }
2086
2087 Py_DECREF(item);
2088
2089 if (PySequence_Fast_GET_SIZE(fast) != 2)
2090 {
2091 Py_DECREF(iterator);
2092 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002093 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002094 N_("expected sequence element of size 2, "
2095 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002096 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002097 return NULL;
2098 }
2099
2100 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2101
2102 if (!(key = StringToChars(keyObject, &todecref)))
2103 {
2104 Py_DECREF(iterator);
2105 Py_DECREF(fast);
2106 return NULL;
2107 }
2108
2109 di = dictitem_alloc(key);
2110
2111 Py_XDECREF(todecref);
2112
2113 if (di == NULL)
2114 {
2115 Py_DECREF(fast);
2116 Py_DECREF(iterator);
2117 PyErr_NoMemory();
2118 return NULL;
2119 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002120 di->di_tv.v_type = VAR_UNKNOWN;
2121
2122 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2123
2124 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2125 {
2126 Py_DECREF(iterator);
2127 Py_DECREF(fast);
2128 dictitem_free(di);
2129 return NULL;
2130 }
2131
2132 Py_DECREF(fast);
2133
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002134 hi = hash_find(&dict->dv_hashtab, di->di_key);
2135 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002136 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002137 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002138 Py_DECREF(iterator);
2139 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002140 return NULL;
2141 }
2142 }
2143
2144 Py_DECREF(iterator);
2145
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002146 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002147 if (PyErr_Occurred())
2148 return NULL;
2149 }
2150 }
2151 Py_INCREF(Py_None);
2152 return Py_None;
2153}
2154
2155 static PyObject *
2156DictionaryGet(DictionaryObject *self, PyObject *args)
2157{
2158 return _DictionaryItem(self, args,
2159 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2160}
2161
2162 static PyObject *
2163DictionaryPop(DictionaryObject *self, PyObject *args)
2164{
2165 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2166}
2167
2168 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002169DictionaryPopItem(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002170{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002171 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002172 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002173 PyObject *valObject;
2174 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002175
Bram Moolenaarde71b562013-06-02 17:41:54 +02002176 if (self->dict->dv_hashtab.ht_used == 0)
2177 {
2178 PyErr_SetNone(PyExc_KeyError);
2179 return NULL;
2180 }
2181
2182 hi = self->dict->dv_hashtab.ht_array;
2183 while (HASHITEM_EMPTY(hi))
2184 ++hi;
2185
2186 di = dict_lookup(hi);
2187
2188 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002189 return NULL;
2190
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002191 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002192 {
2193 Py_DECREF(valObject);
2194 return NULL;
2195 }
2196
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002197 hash_remove(&self->dict->dv_hashtab, hi, "Python pop item");
Bram Moolenaarde71b562013-06-02 17:41:54 +02002198 dictitem_free(di);
2199
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002200 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002201}
2202
2203 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002204DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002205{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002206 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2207}
2208
2209static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002210 0, // sq_length
2211 0, // sq_concat
2212 0, // sq_repeat
2213 0, // sq_item
2214 0, // sq_slice
2215 0, // sq_ass_item
2216 0, // sq_ass_slice
2217 (objobjproc) DictionaryContains, // sq_contains
2218 0, // sq_inplace_concat
2219 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002220};
2221
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002222static PyMappingMethods DictionaryAsMapping = {
2223 (lenfunc) DictionaryLength,
2224 (binaryfunc) DictionaryItem,
2225 (objobjargproc) DictionaryAssItem,
2226};
2227
Bram Moolenaardb913952012-06-29 12:54:53 +02002228static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002229 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002230 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2231 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002232 {"update", (PyCFunction)(void *)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002233 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2234 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002235 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002236 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002237 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2238 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002239};
2240
2241static PyTypeObject ListType;
2242
2243typedef struct
2244{
2245 PyObject_HEAD
2246 list_T *list;
2247 pylinkedlist_T ref;
2248} ListObject;
2249
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002250#define NEW_LIST(list) ListNew(&ListType, list)
2251
Bram Moolenaardb913952012-06-29 12:54:53 +02002252 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002253ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002254{
2255 ListObject *self;
2256
Bram Moolenaarab589462020-07-06 21:03:06 +02002257 if (list == NULL)
2258 return NULL;
2259
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002260 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002261 if (self == NULL)
2262 return NULL;
2263 self->list = list;
2264 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002265 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002266
2267 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2268
2269 return (PyObject *)(self);
2270}
2271
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002272 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002273py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002274{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002275 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002277 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002278 {
2279 PyErr_NoMemory();
2280 return NULL;
2281 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002282 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002283
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002284 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002285}
2286
2287 static int
2288list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2289{
2290 PyObject *iterator;
2291 PyObject *item;
2292 listitem_T *li;
2293
2294 if (!(iterator = PyObject_GetIter(obj)))
2295 return -1;
2296
2297 while ((item = PyIter_Next(iterator)))
2298 {
2299 if (!(li = listitem_alloc()))
2300 {
2301 PyErr_NoMemory();
2302 Py_DECREF(item);
2303 Py_DECREF(iterator);
2304 return -1;
2305 }
2306 li->li_tv.v_lock = 0;
2307 li->li_tv.v_type = VAR_UNKNOWN;
2308
2309 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2310 {
2311 Py_DECREF(item);
2312 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002314 return -1;
2315 }
2316
2317 Py_DECREF(item);
2318
2319 list_append(l, li);
2320 }
2321
2322 Py_DECREF(iterator);
2323
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002324 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002325 if (PyErr_Occurred())
2326 return -1;
2327
2328 return 0;
2329}
2330
2331 static PyObject *
2332ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2333{
2334 list_T *list;
2335 PyObject *obj = NULL;
2336
2337 if (kwargs)
2338 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002339 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002340 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002341 return NULL;
2342 }
2343
2344 if (!PyArg_ParseTuple(args, "|O", &obj))
2345 return NULL;
2346
2347 if (!(list = py_list_alloc()))
2348 return NULL;
2349
2350 if (obj)
2351 {
2352 PyObject *lookup_dict;
2353
2354 if (!(lookup_dict = PyDict_New()))
2355 {
2356 list_unref(list);
2357 return NULL;
2358 }
2359
2360 if (list_py_concat(list, obj, lookup_dict) == -1)
2361 {
2362 Py_DECREF(lookup_dict);
2363 list_unref(list);
2364 return NULL;
2365 }
2366
2367 Py_DECREF(lookup_dict);
2368 }
2369
2370 return ListNew(subtype, list);
2371}
2372
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002373 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002374ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002375{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376 pyll_remove(&self->ref, &lastlist);
2377 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002378
2379 DESTRUCTOR_FINISH(self);
2380}
2381
Bram Moolenaardb913952012-06-29 12:54:53 +02002382 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002383ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002384{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002385 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002386}
2387
2388 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002389ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002390{
2391 listitem_T *li;
2392
Bram Moolenaard6e39182013-05-21 18:30:34 +02002393 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002394 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002395 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002396 return NULL;
2397 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002398 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002399 if (li == NULL)
2400 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002401 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002402 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002403 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002404 return NULL;
2405 }
2406 return ConvertToPyObject(&li->li_tv);
2407}
2408
Bram Moolenaardb913952012-06-29 12:54:53 +02002409 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002410ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2411 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002412{
2413 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002414 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002415
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002416 if (step == 0)
2417 {
2418 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2419 return NULL;
2420 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002421
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002422 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002423 if (list == NULL)
2424 return NULL;
2425
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002426 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002427 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002428 PyObject *item;
2429
2430 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002431 if (item == NULL)
2432 {
2433 Py_DECREF(list);
2434 return NULL;
2435 }
2436
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002437 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002438 }
2439
2440 return list;
2441}
2442
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002443 static PyObject *
2444ListItem(ListObject *self, PyObject* idx)
2445{
2446#if PY_MAJOR_VERSION < 3
2447 if (PyInt_Check(idx))
2448 {
2449 long _idx = PyInt_AsLong(idx);
2450 return ListIndex(self, _idx);
2451 }
2452 else
2453#endif
2454 if (PyLong_Check(idx))
2455 {
2456 long _idx = PyLong_AsLong(idx);
2457 return ListIndex(self, _idx);
2458 }
2459 else if (PySlice_Check(idx))
2460 {
2461 Py_ssize_t start, stop, step, slicelen;
2462
Bram Moolenaar922a4662014-03-30 16:11:43 +02002463 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002464 &start, &stop, &step, &slicelen) < 0)
2465 return NULL;
2466 return ListSlice(self, start, step, slicelen);
2467 }
2468 else
2469 {
2470 RAISE_INVALID_INDEX_TYPE(idx);
2471 return NULL;
2472 }
2473}
2474
2475 static void
2476list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2477 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2478{
2479 while (numreplaced--)
2480 {
2481 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2482 listitem_remove(l, lis[slicelen + numreplaced]);
2483 }
2484 while (numadded--)
2485 {
2486 listitem_T *next;
2487
2488 next = lastaddedli->li_prev;
2489 listitem_remove(l, lastaddedli);
2490 lastaddedli = next;
2491 }
2492}
2493
2494 static int
2495ListAssSlice(ListObject *self, Py_ssize_t first,
2496 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2497{
2498 PyObject *iterator;
2499 PyObject *item;
2500 listitem_T *li;
2501 listitem_T *lastaddedli = NULL;
2502 listitem_T *next;
2503 typval_T v;
2504 list_T *l = self->list;
2505 PyInt i;
2506 PyInt j;
2507 PyInt numreplaced = 0;
2508 PyInt numadded = 0;
2509 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002510 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002511
2512 size = ListLength(self);
2513
2514 if (l->lv_lock)
2515 {
2516 RAISE_LOCKED_LIST;
2517 return -1;
2518 }
2519
2520 if (step == 0)
2521 {
2522 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2523 return -1;
2524 }
2525
2526 if (step != 1 && slicelen == 0)
2527 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002528 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002529 int ret = 0;
2530
2531 if (obj == NULL)
2532 return 0;
2533
2534 if (!(iterator = PyObject_GetIter(obj)))
2535 return -1;
2536
2537 if ((item = PyIter_Next(iterator)))
2538 {
2539 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002540 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002541 "to extended slice"), 0);
2542 Py_DECREF(item);
2543 ret = -1;
2544 }
2545 Py_DECREF(iterator);
2546 return ret;
2547 }
2548
2549 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002550 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002551 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2552 {
2553 PyErr_NoMemory();
2554 return -1;
2555 }
2556
2557 if (first == size)
2558 li = NULL;
2559 else
2560 {
2561 li = list_find(l, (long) first);
2562 if (li == NULL)
2563 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002564 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002565 (int)first);
2566 if (obj != NULL)
2567 PyMem_Free(lis);
2568 return -1;
2569 }
2570 i = slicelen;
2571 while (i-- && li != NULL)
2572 {
2573 j = step;
2574 next = li;
2575 if (step > 0)
2576 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2577 else
2578 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2579
2580 if (obj == NULL)
2581 listitem_remove(l, li);
2582 else
2583 lis[slicelen - i - 1] = li;
2584
2585 li = next;
2586 }
2587 if (li == NULL && i != -1)
2588 {
2589 PyErr_SET_VIM(N_("internal error: not enough list items"));
2590 if (obj != NULL)
2591 PyMem_Free(lis);
2592 return -1;
2593 }
2594 }
2595
2596 if (obj == NULL)
2597 return 0;
2598
2599 if (!(iterator = PyObject_GetIter(obj)))
2600 {
2601 PyMem_Free(lis);
2602 return -1;
2603 }
2604
2605 i = 0;
2606 while ((item = PyIter_Next(iterator)))
2607 {
2608 if (ConvertFromPyObject(item, &v) == -1)
2609 {
2610 Py_DECREF(iterator);
2611 Py_DECREF(item);
2612 PyMem_Free(lis);
2613 return -1;
2614 }
2615 Py_DECREF(item);
2616 if (list_insert_tv(l, &v, numreplaced < slicelen
2617 ? lis[numreplaced]
2618 : li) == FAIL)
2619 {
2620 clear_tv(&v);
2621 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2622 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2623 PyMem_Free(lis);
2624 return -1;
2625 }
2626 if (numreplaced < slicelen)
2627 {
2628 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002629 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002630 numreplaced++;
2631 }
2632 else
2633 {
2634 if (li)
2635 lastaddedli = li->li_prev;
2636 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002637 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002638 numadded++;
2639 }
2640 clear_tv(&v);
2641 if (step != 1 && i >= slicelen)
2642 {
2643 Py_DECREF(iterator);
2644 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002645 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002646 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002647 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2648 PyMem_Free(lis);
2649 return -1;
2650 }
2651 ++i;
2652 }
2653 Py_DECREF(iterator);
2654
2655 if (step != 1 && i != slicelen)
2656 {
2657 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002658 N_("attempt to assign sequence of size %d to extended slice "
2659 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002660 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2661 PyMem_Free(lis);
2662 return -1;
2663 }
2664
2665 if (PyErr_Occurred())
2666 {
2667 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2668 PyMem_Free(lis);
2669 return -1;
2670 }
2671
2672 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002674 if (step == 1)
2675 for (i = numreplaced; i < slicelen; i++)
2676 listitem_remove(l, lis[i]);
2677
2678 PyMem_Free(lis);
2679
2680 return 0;
2681}
2682
2683 static int
2684ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2685{
2686 typval_T tv;
2687 list_T *l = self->list;
2688 listitem_T *li;
2689 Py_ssize_t length = ListLength(self);
2690
2691 if (l->lv_lock)
2692 {
2693 RAISE_LOCKED_LIST;
2694 return -1;
2695 }
2696 if (index > length || (index == length && obj == NULL))
2697 {
2698 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2699 return -1;
2700 }
2701
2702 if (obj == NULL)
2703 {
2704 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002705 if (li == NULL)
2706 {
2707 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2708 "list item %d"), (int) index);
2709 return -1;
2710 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002711 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002712 clear_tv(&li->li_tv);
2713 vim_free(li);
2714 return 0;
2715 }
2716
2717 if (ConvertFromPyObject(obj, &tv) == -1)
2718 return -1;
2719
2720 if (index == length)
2721 {
2722 if (list_append_tv(l, &tv) == FAIL)
2723 {
2724 clear_tv(&tv);
2725 PyErr_SET_VIM(N_("failed to add item to list"));
2726 return -1;
2727 }
2728 }
2729 else
2730 {
2731 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002732 if (li == NULL)
2733 {
2734 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2735 "list item %d"), (int) index);
2736 return -1;
2737 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002738 clear_tv(&li->li_tv);
2739 copy_tv(&tv, &li->li_tv);
2740 clear_tv(&tv);
2741 }
2742 return 0;
2743}
2744
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002745 static int
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002746ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2747{
2748#if PY_MAJOR_VERSION < 3
2749 if (PyInt_Check(idx))
2750 {
2751 long _idx = PyInt_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002752 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002753 }
2754 else
2755#endif
2756 if (PyLong_Check(idx))
2757 {
2758 long _idx = PyLong_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002759 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002760 }
2761 else if (PySlice_Check(idx))
2762 {
2763 Py_ssize_t start, stop, step, slicelen;
2764
Bram Moolenaar922a4662014-03-30 16:11:43 +02002765 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002766 &start, &stop, &step, &slicelen) < 0)
2767 return -1;
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002768 return (int)ListAssSlice(self, start, step, slicelen,
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002769 obj);
2770 }
2771 else
2772 {
2773 RAISE_INVALID_INDEX_TYPE(idx);
2774 return -1;
2775 }
2776}
2777
2778 static PyObject *
2779ListConcatInPlace(ListObject *self, PyObject *obj)
2780{
2781 list_T *l = self->list;
2782 PyObject *lookup_dict;
2783
2784 if (l->lv_lock)
2785 {
2786 RAISE_LOCKED_LIST;
2787 return NULL;
2788 }
2789
2790 if (!(lookup_dict = PyDict_New()))
2791 return NULL;
2792
2793 if (list_py_concat(l, obj, lookup_dict) == -1)
2794 {
2795 Py_DECREF(lookup_dict);
2796 return NULL;
2797 }
2798 Py_DECREF(lookup_dict);
2799
2800 Py_INCREF(self);
2801 return (PyObject *)(self);
2802}
2803
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002804typedef struct
2805{
2806 listwatch_T lw;
2807 list_T *list;
2808} listiterinfo_T;
2809
2810 static void
2811ListIterDestruct(listiterinfo_T *lii)
2812{
2813 list_rem_watch(lii->list, &lii->lw);
Bram Moolenaar21578272021-02-21 19:12:47 +01002814 list_unref(lii->list);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002815 PyMem_Free(lii);
2816}
2817
2818 static PyObject *
2819ListIterNext(listiterinfo_T **lii)
2820{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002821 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002822
2823 if (!((*lii)->lw.lw_item))
2824 return NULL;
2825
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002826 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002827 return NULL;
2828
2829 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2830
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002831 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002832}
2833
2834 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002835ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002836{
2837 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002838 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002839
2840 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2841 {
2842 PyErr_NoMemory();
2843 return NULL;
2844 }
2845
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002846 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002847 list_add_watch(l, &lii->lw);
2848 lii->lw.lw_item = l->lv_first;
2849 lii->list = l;
Bram Moolenaar21578272021-02-21 19:12:47 +01002850 ++l->lv_refcount;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002851
2852 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002853 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002854 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002855}
2856
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002857static char *ListAttrs[] = {
2858 "locked",
2859 NULL
2860};
2861
2862 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002863ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002864{
2865 return ObjectDir(self, ListAttrs);
2866}
2867
Bram Moolenaar66b79852012-09-21 14:00:35 +02002868 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002869ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002872 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002873 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002874 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002875 return -1;
2876 }
2877
2878 if (strcmp(name, "locked") == 0)
2879 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002880 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002881 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002882 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002883 return -1;
2884 }
2885 else
2886 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002887 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002888 if (istrue == -1)
2889 return -1;
2890 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002891 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002892 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002893 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002894 }
2895 return 0;
2896 }
2897 else
2898 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002899 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002900 return -1;
2901 }
2902}
2903
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002904static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002905 (lenfunc) ListLength, // sq_length, len(x)
2906 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2907 0, // RangeRepeat, sq_repeat, x*n
2908 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2909 0, // was_sq_slice, x[i:j]
2910 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2911 0, // was_sq_ass_slice, x[i:j]=v
2912 0, // sq_contains
2913 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2914 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002915};
2916
2917static PyMappingMethods ListAsMapping = {
2918 /* mp_length */ (lenfunc) ListLength,
2919 /* mp_subscript */ (binaryfunc) ListItem,
2920 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2921};
2922
Bram Moolenaardb913952012-06-29 12:54:53 +02002923static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002924 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2925 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2926 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002927};
2928
2929typedef struct
2930{
2931 PyObject_HEAD
2932 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002933 int argc;
2934 typval_T *argv;
2935 dict_T *self;
2936 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002937 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002938} FunctionObject;
2939
2940static PyTypeObject FunctionType;
2941
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002942#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2943 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002944
Bram Moolenaardb913952012-06-29 12:54:53 +02002945 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002946FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002947 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002948{
2949 FunctionObject *self;
2950
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002951 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002952 if (self == NULL)
2953 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002954
2955 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002956 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002957 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002958 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002959 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002960 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002961 return NULL;
2962 }
2963 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002964 }
2965 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002966 {
2967 char_u *p;
2968
2969 if ((p = get_expanded_name(name,
2970 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002971 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002972 PyErr_FORMAT(PyExc_ValueError,
2973 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002974 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002975 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002976
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002977 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2978 {
2979 char_u *np;
2980 size_t len = STRLEN(p) + 1;
2981
Bram Moolenaar51e14382019-05-25 20:21:28 +02002982 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002983 {
2984 vim_free(p);
2985 return NULL;
2986 }
2987 mch_memmove(np, "<SNR>", 5);
2988 mch_memmove(np + 5, p + 3, len - 3);
2989 vim_free(p);
2990 self->name = np;
2991 }
2992 else
2993 self->name = p;
2994 }
2995
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002996 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002997 self->argc = argc;
2998 self->argv = argv;
2999 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003000 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003001
3002 if (self->argv || self->self)
3003 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3004
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003005 return (PyObject *)(self);
3006}
3007
3008 static PyObject *
3009FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3010{
3011 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003012 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003013 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003014 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003015 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003016 typval_T selfdicttv;
3017 typval_T argstv;
3018 list_T *argslist = NULL;
3019 dict_T *selfdict = NULL;
3020 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003021 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003022 typval_T *argv = NULL;
3023 typval_T *curtv;
3024 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003025
Bram Moolenaar8110a092016-04-14 15:56:09 +02003026 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003027 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003028 selfdictObject = PyDict_GetItemString(kwargs, "self");
3029 if (selfdictObject != NULL)
3030 {
3031 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3032 return NULL;
3033 selfdict = selfdicttv.vval.v_dict;
3034 }
3035 argsObject = PyDict_GetItemString(kwargs, "args");
3036 if (argsObject != NULL)
3037 {
3038 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3039 {
3040 dict_unref(selfdict);
3041 return NULL;
3042 }
3043 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003044 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003045
3046 argc = argslist->lv_len;
3047 if (argc != 0)
3048 {
3049 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003050 if (argv == NULL)
3051 {
3052 PyErr_NoMemory();
3053 dict_unref(selfdict);
3054 list_unref(argslist);
3055 return NULL;
3056 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003057 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003058 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003059 copy_tv(&li->li_tv, curtv++);
3060 }
3061 list_unref(argslist);
3062 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003063 if (selfdict != NULL)
3064 {
3065 auto_rebind = FALSE;
3066 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3067 if (autoRebindObject != NULL)
3068 {
3069 auto_rebind = PyObject_IsTrue(autoRebindObject);
3070 if (auto_rebind == -1)
3071 {
3072 dict_unref(selfdict);
3073 list_unref(argslist);
3074 return NULL;
3075 }
3076 }
3077 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003078 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003079
Bram Moolenaar389a1792013-06-23 13:00:44 +02003080 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003081 {
3082 dict_unref(selfdict);
3083 while (argc--)
3084 clear_tv(&argv[argc]);
3085 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003086 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003087 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003088
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003089 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003090
Bram Moolenaar389a1792013-06-23 13:00:44 +02003091 PyMem_Free(name);
3092
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003093 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003094}
3095
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003096 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003097FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003098{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003099 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003100 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003101 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003102 for (i = 0; i < self->argc; ++i)
3103 clear_tv(&self->argv[i]);
3104 PyMem_Free(self->argv);
3105 dict_unref(self->self);
3106 if (self->argv || self->self)
3107 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003108
3109 DESTRUCTOR_FINISH(self);
3110}
3111
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003112static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003113 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003114 NULL
3115};
3116
3117 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003118FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003119{
3120 return ObjectDir(self, FunctionAttrs);
3121}
3122
Bram Moolenaardb913952012-06-29 12:54:53 +02003123 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003124FunctionAttr(FunctionObject *self, char *name)
3125{
3126 list_T *list;
3127 int i;
3128 if (strcmp(name, "name") == 0)
3129 return PyString_FromString((char *)(self->name));
3130 else if (strcmp(name, "args") == 0)
3131 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003132 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003133 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003134
Bram Moolenaar8110a092016-04-14 15:56:09 +02003135 for (i = 0; i < self->argc; ++i)
3136 list_append_tv(list, &self->argv[i]);
3137 return NEW_LIST(list);
3138 }
3139 else if (strcmp(name, "self") == 0)
3140 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003141 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003142 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003143 else if (strcmp(name, "auto_rebind") == 0)
3144 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003145 ? ALWAYS_TRUE
3146 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003147 else if (strcmp(name, "__members__") == 0)
3148 return ObjectDir(NULL, FunctionAttrs);
3149 return NULL;
3150}
3151
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003152/*
3153 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003154 *
3155 * "exported" should be set to true when it is needed to construct a partial
3156 * that may be stored in a variable (i.e. may be freed by Vim).
3157 */
3158 static void
3159set_partial(FunctionObject *self, partial_T *pt, int exported)
3160{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003161 int i;
3162
3163 pt->pt_name = self->name;
3164 if (self->argv)
3165 {
3166 pt->pt_argc = self->argc;
3167 if (exported)
3168 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003169 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003170 for (i = 0; i < pt->pt_argc; ++i)
3171 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3172 }
3173 else
3174 pt->pt_argv = self->argv;
3175 }
3176 else
3177 {
3178 pt->pt_argc = 0;
3179 pt->pt_argv = NULL;
3180 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003181 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003182 pt->pt_dict = self->self;
3183 if (exported && self->self)
3184 ++pt->pt_dict->dv_refcount;
3185 if (exported)
3186 pt->pt_name = vim_strsave(pt->pt_name);
3187 pt->pt_refcount = 1;
3188}
3189
3190 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003192{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003193 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003194 typval_T args;
3195 typval_T selfdicttv;
3196 typval_T rettv;
3197 dict_T *selfdict = NULL;
3198 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003199 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003200 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003201 partial_T pt;
3202 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003203
Bram Moolenaar8110a092016-04-14 15:56:09 +02003204 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003205 return NULL;
3206
3207 if (kwargs != NULL)
3208 {
3209 selfdictObject = PyDict_GetItemString(kwargs, "self");
3210 if (selfdictObject != NULL)
3211 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003212 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003213 {
3214 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003215 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003216 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003217 selfdict = selfdicttv.vval.v_dict;
3218 }
3219 }
3220
Bram Moolenaar8110a092016-04-14 15:56:09 +02003221 if (self->argv || self->self)
3222 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003223 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003224 set_partial(self, &pt, FALSE);
3225 pt_ptr = &pt;
3226 }
3227
Bram Moolenaar71700b82013-05-15 17:49:05 +02003228 Py_BEGIN_ALLOW_THREADS
3229 Python_Lock_Vim();
3230
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003231 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003232 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003233
3234 Python_Release_Vim();
3235 Py_END_ALLOW_THREADS
3236
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003237 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003238 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003239 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003240 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003241 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003242 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003243 }
3244 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003245 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003246
Bram Moolenaardb913952012-06-29 12:54:53 +02003247 clear_tv(&args);
3248 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003249 if (selfdict != NULL)
3250 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003251
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003252 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003253}
3254
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003255 static PyObject *
3256FunctionRepr(FunctionObject *self)
3257{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003258 PyObject *ret;
3259 garray_T repr_ga;
3260 int i;
3261 char_u *tofree = NULL;
3262 typval_T tv;
3263 char_u numbuf[NUMBUFLEN];
3264
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003265 ga_init2(&repr_ga, sizeof(char), 70);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003266 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3267 if (self->name)
3268 ga_concat(&repr_ga, self->name);
3269 else
3270 ga_concat(&repr_ga, (char_u *)"<NULL>");
3271 ga_append(&repr_ga, '\'');
3272 if (self->argv)
3273 {
3274 ga_concat(&repr_ga, (char_u *)", args=[");
3275 ++emsg_silent;
3276 for (i = 0; i < self->argc; i++)
3277 {
3278 if (i != 0)
3279 ga_concat(&repr_ga, (char_u *)", ");
3280 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3281 get_copyID()));
3282 vim_free(tofree);
3283 }
3284 --emsg_silent;
3285 ga_append(&repr_ga, ']');
3286 }
3287 if (self->self)
3288 {
3289 ga_concat(&repr_ga, (char_u *)", self=");
3290 tv.v_type = VAR_DICT;
3291 tv.vval.v_dict = self->self;
3292 ++emsg_silent;
3293 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3294 --emsg_silent;
3295 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003296 if (self->auto_rebind)
3297 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003298 }
3299 ga_append(&repr_ga, '>');
3300 ret = PyString_FromString((char *)repr_ga.ga_data);
3301 ga_clear(&repr_ga);
3302 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003303}
3304
Bram Moolenaardb913952012-06-29 12:54:53 +02003305static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003306 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3307 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003308};
3309
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003310/*
3311 * Options object
3312 */
3313
3314static PyTypeObject OptionsType;
3315
3316typedef int (*checkfun)(void *);
3317
3318typedef struct
3319{
3320 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003321 int opt_type;
3322 void *from;
3323 checkfun Check;
3324 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003325} OptionsObject;
3326
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003327 static int
3328dummy_check(void *arg UNUSED)
3329{
3330 return 0;
3331}
3332
3333 static PyObject *
3334OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3335{
3336 OptionsObject *self;
3337
Bram Moolenaar774267b2013-05-21 20:51:59 +02003338 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003339 if (self == NULL)
3340 return NULL;
3341
3342 self->opt_type = opt_type;
3343 self->from = from;
3344 self->Check = Check;
3345 self->fromObj = fromObj;
3346 if (fromObj)
3347 Py_INCREF(fromObj);
3348
3349 return (PyObject *)(self);
3350}
3351
3352 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003353OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003354{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003355 PyObject_GC_UnTrack((void *)(self));
3356 Py_XDECREF(self->fromObj);
3357 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003358}
3359
3360 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003361OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003362{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003364 return 0;
3365}
3366
3367 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003368OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003369{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003371 return 0;
3372}
3373
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003374 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003375OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003376{
3377 char_u *key;
3378 int flags;
3379 long numval;
3380 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003381 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003382
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003383 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003384 return NULL;
3385
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003386 if (!(key = StringToChars(keyObject, &todecref)))
3387 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003388
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003389 if (*key == NUL)
3390 {
3391 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003392 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003393 return NULL;
3394 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003395
3396 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003397 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003398
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003399 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003400
3401 if (flags == 0)
3402 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003403 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003404 return NULL;
3405 }
3406
3407 if (flags & SOPT_UNSET)
3408 {
3409 Py_INCREF(Py_None);
3410 return Py_None;
3411 }
3412 else if (flags & SOPT_BOOL)
3413 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003414 PyObject *ret;
3415 ret = numval ? Py_True : Py_False;
3416 Py_INCREF(ret);
3417 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003418 }
3419 else if (flags & SOPT_NUM)
3420 return PyInt_FromLong(numval);
3421 else if (flags & SOPT_STRING)
3422 {
3423 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003424 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003425 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003426 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003427 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003428 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003429 else
3430 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003431 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003432 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003433 return NULL;
3434 }
3435 }
3436 else
3437 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003438 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003439 return NULL;
3440 }
3441}
3442
3443 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003444OptionsContains(OptionsObject *self, PyObject *keyObject)
3445{
3446 char_u *key;
3447 PyObject *todecref;
3448
3449 if (!(key = StringToChars(keyObject, &todecref)))
3450 return -1;
3451
3452 if (*key == NUL)
3453 {
3454 Py_XDECREF(todecref);
3455 return 0;
3456 }
3457
3458 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3459 {
3460 Py_XDECREF(todecref);
3461 return 1;
3462 }
3463 else
3464 {
3465 Py_XDECREF(todecref);
3466 return 0;
3467 }
3468}
3469
3470typedef struct
3471{
3472 void *lastoption;
3473 int opt_type;
3474} optiterinfo_T;
3475
3476 static PyObject *
3477OptionsIterNext(optiterinfo_T **oii)
3478{
3479 char_u *name;
3480
3481 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3482 return PyString_FromString((char *)name);
3483
3484 return NULL;
3485}
3486
3487 static PyObject *
3488OptionsIter(OptionsObject *self)
3489{
3490 optiterinfo_T *oii;
3491
3492 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3493 {
3494 PyErr_NoMemory();
3495 return NULL;
3496 }
3497
3498 oii->opt_type = self->opt_type;
3499 oii->lastoption = NULL;
3500
3501 return IterNew(oii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003502 (destructorfun)(void *) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003503 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003504}
3505
3506 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003507set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003508{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003509 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003510
3511 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3512 {
3513 if (VimTryEnd())
3514 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003515 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003516 return FAIL;
3517 }
3518 return OK;
3519}
3520
3521 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003522set_option_value_for(
3523 char_u *key,
3524 int numval,
3525 char_u *stringval,
3526 int opt_flags,
3527 int opt_type,
3528 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003529{
Bram Moolenaar18f47402022-01-06 13:24:51 +00003530 switchwin_T switchwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003531 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003532 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003533
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003534 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003535 switch (opt_type)
3536 {
3537 case SREQ_WIN:
Bram Moolenaar18f47402022-01-06 13:24:51 +00003538 if (switch_win(&switchwin, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003539 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00003541 restore_win(&switchwin, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003542 if (VimTryEnd())
3543 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003544 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003545 return -1;
3546 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003547 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar18f47402022-01-06 13:24:51 +00003548 restore_win(&switchwin, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003549 break;
3550 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003551 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003552 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003553 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003554 break;
3555 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003556 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003557 break;
3558 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003559 if (set_ret == FAIL)
3560 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003561 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003562}
3563
3564 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003565OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003566{
3567 char_u *key;
3568 int flags;
3569 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003570 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003571 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003572
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003573 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003574 return -1;
3575
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003576 if (!(key = StringToChars(keyObject, &todecref)))
3577 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003578
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003579 if (*key == NUL)
3580 {
3581 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003582 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003583 return -1;
3584 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003585
3586 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003587 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003588
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003589 if (flags == 0)
3590 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003591 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003592 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003593 return -1;
3594 }
3595
3596 if (valObject == NULL)
3597 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003598 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003599 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003600 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003601 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003602 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003603 return -1;
3604 }
3605 else if (!(flags & SOPT_GLOBAL))
3606 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003607 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003608 N_("unable to unset option %s "
3609 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003610 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003611 return -1;
3612 }
3613 else
3614 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003615 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003616 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003617 return 0;
3618 }
3619 }
3620
Bram Moolenaard6e39182013-05-21 18:30:34 +02003621 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003622
3623 if (flags & SOPT_BOOL)
3624 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003625 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003626
Bram Moolenaarb983f752013-05-15 16:11:50 +02003627 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003628 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003629 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003630 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003631 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003632 }
3633 else if (flags & SOPT_NUM)
3634 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003635 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003636
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003637 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003638 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003639 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003640 return -1;
3641 }
3642
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003643 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003644 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003645 }
3646 else
3647 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003648 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003649 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003650
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003651 if ((val = StringToChars(valObject, &todecref2)))
3652 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003653 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003654 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003655 Py_XDECREF(todecref2);
3656 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003657 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003658 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003659 }
3660
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003661 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003662
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003663 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003664}
3665
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003666static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003667 0, // sq_length
3668 0, // sq_concat
3669 0, // sq_repeat
3670 0, // sq_item
3671 0, // sq_slice
3672 0, // sq_ass_item
3673 0, // sq_ass_slice
3674 (objobjproc) OptionsContains, // sq_contains
3675 0, // sq_inplace_concat
3676 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003677};
3678
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003679static PyMappingMethods OptionsAsMapping = {
3680 (lenfunc) NULL,
3681 (binaryfunc) OptionsItem,
3682 (objobjargproc) OptionsAssItem,
3683};
3684
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003685// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003686
3687typedef struct
3688{
3689 PyObject_HEAD
3690 tabpage_T *tab;
3691} TabPageObject;
3692
3693static PyObject *WinListNew(TabPageObject *tabObject);
3694
3695static PyTypeObject TabPageType;
3696
3697 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003698CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003699{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003700 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003701 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003702 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003703 return -1;
3704 }
3705
3706 return 0;
3707}
3708
3709 static PyObject *
3710TabPageNew(tabpage_T *tab)
3711{
3712 TabPageObject *self;
3713
3714 if (TAB_PYTHON_REF(tab))
3715 {
3716 self = TAB_PYTHON_REF(tab);
3717 Py_INCREF(self);
3718 }
3719 else
3720 {
3721 self = PyObject_NEW(TabPageObject, &TabPageType);
3722 if (self == NULL)
3723 return NULL;
3724 self->tab = tab;
3725 TAB_PYTHON_REF(tab) = self;
3726 }
3727
3728 return (PyObject *)(self);
3729}
3730
3731 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003732TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003733{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003734 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3735 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003736
3737 DESTRUCTOR_FINISH(self);
3738}
3739
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003740static char *TabPageAttrs[] = {
3741 "windows", "number", "vars", "window", "valid",
3742 NULL
3743};
3744
3745 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003746TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003747{
3748 return ObjectDir(self, TabPageAttrs);
3749}
3750
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003751 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003752TabPageAttrValid(TabPageObject *self, char *name)
3753{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003754 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003755
3756 if (strcmp(name, "valid") != 0)
3757 return NULL;
3758
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003759 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3760 Py_INCREF(ret);
3761 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003762}
3763
3764 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003766{
3767 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003768 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003769 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003770 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003772 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003773 else if (strcmp(name, "window") == 0)
3774 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003775 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003776 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003777 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003778 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003779 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003780 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003781 else if (strcmp(name, "__members__") == 0)
3782 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783 return NULL;
3784}
3785
3786 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003787TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003788{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003789 if (self->tab == INVALID_TABPAGE_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00003790 return PyString_FromFormat("<tabpage object (deleted) at %p>", (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003791 else
3792 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003793 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003794
3795 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003796 return PyString_FromFormat("<tabpage object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00003797 (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003798 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003799 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003800 }
3801}
3802
3803static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003804 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003805 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3806 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003807};
3808
3809/*
3810 * Window list object
3811 */
3812
3813static PyTypeObject TabListType;
3814static PySequenceMethods TabListAsSeq;
3815
3816typedef struct
3817{
3818 PyObject_HEAD
3819} TabListObject;
3820
3821 static PyInt
3822TabListLength(PyObject *self UNUSED)
3823{
3824 tabpage_T *tp = first_tabpage;
3825 PyInt n = 0;
3826
3827 while (tp != NULL)
3828 {
3829 ++n;
3830 tp = tp->tp_next;
3831 }
3832
3833 return n;
3834}
3835
3836 static PyObject *
3837TabListItem(PyObject *self UNUSED, PyInt n)
3838{
3839 tabpage_T *tp;
3840
3841 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3842 if (n == 0)
3843 return TabPageNew(tp);
3844
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003845 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003846 return NULL;
3847}
3848
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003849/*
3850 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003851 */
3852
3853typedef struct
3854{
3855 PyObject_HEAD
3856 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003857 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003858} WindowObject;
3859
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003860static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003861
3862 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003863CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003864{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003865 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003866 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003867 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003868 return -1;
3869 }
3870
3871 return 0;
3872}
3873
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003874 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003875WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003876{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003877 /*
3878 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003879 * If we add a "w_python*_ref" field to the win_T structure,
3880 * then we can get at it in win_free() in vim. We then
3881 * need to create only ONE Python object per window - if
3882 * we try to create a second, just INCREF the existing one
3883 * and return it. The (single) Python object referring to
3884 * the window is stored in "w_python*_ref".
3885 * On a win_free() we set the Python object's win_T* field
3886 * to an invalid value. We trap all uses of a window
3887 * object, and reject them if the win_T* field is invalid.
3888 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003889 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003890 * w_python_ref and w_python3_ref fields respectively.
3891 */
3892
3893 WindowObject *self;
3894
3895 if (WIN_PYTHON_REF(win))
3896 {
3897 self = WIN_PYTHON_REF(win);
3898 Py_INCREF(self);
3899 }
3900 else
3901 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003902 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003903 if (self == NULL)
3904 return NULL;
3905 self->win = win;
3906 WIN_PYTHON_REF(win) = self;
3907 }
3908
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003909 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3910
Bram Moolenaar971db462013-05-12 18:44:48 +02003911 return (PyObject *)(self);
3912}
3913
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003914 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003915WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003916{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003917 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003918 if (self->win && self->win != INVALID_WINDOW_VALUE)
3919 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003920 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003921 PyObject_GC_Del((void *)(self));
3922}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003923
Bram Moolenaar774267b2013-05-21 20:51:59 +02003924 static int
3925WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3926{
3927 Py_VISIT(((PyObject *)(self->tabObject)));
3928 return 0;
3929}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003930
Bram Moolenaar774267b2013-05-21 20:51:59 +02003931 static int
3932WindowClear(WindowObject *self)
3933{
3934 Py_CLEAR(self->tabObject);
3935 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003936}
3937
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003938 static win_T *
3939get_firstwin(TabPageObject *tabObject)
3940{
3941 if (tabObject)
3942 {
3943 if (CheckTabPage(tabObject))
3944 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003945 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003946 else if (tabObject->tab == curtab)
3947 return firstwin;
3948 else
3949 return tabObject->tab->tp_firstwin;
3950 }
3951 else
3952 return firstwin;
3953}
Bram Moolenaare950f992018-06-10 13:55:55 +02003954
3955// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003956static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003957 "buffer",
3958 "cursor",
3959 "height",
3960 "row",
3961 "width",
3962 "col",
3963 "vars",
3964 "options",
3965 "number",
3966 "tabpage",
3967 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003968 NULL
3969};
3970
3971 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003972WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003973{
3974 return ObjectDir(self, WindowAttrs);
3975}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003976
Bram Moolenaar971db462013-05-12 18:44:48 +02003977 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003978WindowAttrValid(WindowObject *self, char *name)
3979{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003980 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003981
3982 if (strcmp(name, "valid") != 0)
3983 return NULL;
3984
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003985 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3986 Py_INCREF(ret);
3987 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003988}
3989
3990 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003991WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003992{
3993 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003994 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003995 else if (strcmp(name, "cursor") == 0)
3996 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003997 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003998
3999 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4000 }
4001 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004002 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004003 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004004 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004005 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004006 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004007 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004008 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004009 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004010 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004011 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004012 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4013 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004014 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004015 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004016 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004017 return NULL;
4018 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004019 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004020 }
4021 else if (strcmp(name, "tabpage") == 0)
4022 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004023 Py_INCREF(self->tabObject);
4024 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004025 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004026 else if (strcmp(name, "__members__") == 0)
4027 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004028 else
4029 return NULL;
4030}
4031
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004032 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004033WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004035 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036 return -1;
4037
4038 if (strcmp(name, "buffer") == 0)
4039 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004040 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041 return -1;
4042 }
4043 else if (strcmp(name, "cursor") == 0)
4044 {
4045 long lnum;
4046 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004047
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004048 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049 return -1;
4050
Bram Moolenaard6e39182013-05-21 18:30:34 +02004051 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004053 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 return -1;
4055 }
4056
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004057 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004058 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 return -1;
4060
Bram Moolenaard6e39182013-05-21 18:30:34 +02004061 self->win->w_cursor.lnum = lnum;
4062 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004063 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004064 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004065 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004066 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004068 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 return 0;
4070 }
4071 else if (strcmp(name, "height") == 0)
4072 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004073 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 win_T *savewin;
4075
Bram Moolenaardee2e312013-06-23 16:35:47 +02004076 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077 return -1;
4078
4079#ifdef FEAT_GUI
4080 need_mouse_correct = TRUE;
4081#endif
4082 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004083 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004084
4085 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004086 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004088 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 return -1;
4090
4091 return 0;
4092 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 else if (strcmp(name, "width") == 0)
4094 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004095 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096 win_T *savewin;
4097
Bram Moolenaardee2e312013-06-23 16:35:47 +02004098 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099 return -1;
4100
4101#ifdef FEAT_GUI
4102 need_mouse_correct = TRUE;
4103#endif
4104 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004105 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004106
4107 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004108 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004110 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 return -1;
4112
4113 return 0;
4114 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115 else
4116 {
4117 PyErr_SetString(PyExc_AttributeError, name);
4118 return -1;
4119 }
4120}
4121
4122 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004123WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004125 if (self->win == INVALID_WINDOW_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00004126 return PyString_FromFormat("<window object (deleted) at %p>", (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127 else
4128 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004129 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130
Bram Moolenaar6d216452013-05-12 19:00:41 +02004131 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004132 return PyString_FromFormat("<window object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00004133 (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004134 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004135 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 }
4137}
4138
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004139static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004140 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004141 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4142 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004143};
4144
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004145/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004146 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004148
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004149static PyTypeObject WinListType;
4150static PySequenceMethods WinListAsSeq;
4151
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004152typedef struct
4153{
4154 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004155 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004156} WinListObject;
4157
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004158 static PyObject *
4159WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004160{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004161 WinListObject *self;
4162
4163 self = PyObject_NEW(WinListObject, &WinListType);
4164 self->tabObject = tabObject;
4165 Py_INCREF(tabObject);
4166
4167 return (PyObject *)(self);
4168}
4169
4170 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004171WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004172{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004173 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004174
4175 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004176 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004177 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004178 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004179
4180 DESTRUCTOR_FINISH(self);
4181}
4182
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004183 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004184WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004185{
4186 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004187 PyInt n = 0;
4188
Bram Moolenaard6e39182013-05-21 18:30:34 +02004189 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004190 return -1;
4191
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192 while (w != NULL)
4193 {
4194 ++n;
4195 w = W_NEXT(w);
4196 }
4197
4198 return n;
4199}
4200
4201 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004202WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004203{
4204 win_T *w;
4205
Bram Moolenaard6e39182013-05-21 18:30:34 +02004206 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004207 return NULL;
4208
4209 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004210 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004211 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004213 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214 return NULL;
4215}
4216
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004217/*
4218 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004219 *
4220 * The result is in allocated memory. All internal nulls are replaced by
4221 * newline characters. It is an error for the string to contain newline
4222 * characters.
4223 *
4224 * On errors, the Python exception data is set, and NULL is returned.
4225 */
4226 static char *
4227StringToLine(PyObject *obj)
4228{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004229 char *str;
4230 char *save;
4231 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004232 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004233 PyInt i;
4234 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004235
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004236 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004237 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004238 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4239 || str == NULL)
4240 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004241 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004242 else if (PyUnicode_Check(obj))
4243 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004244 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4245 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004246 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004247
Bram Moolenaardaa27022013-06-24 22:33:30 +02004248 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004249 || str == NULL)
4250 {
4251 Py_DECREF(bytes);
4252 return NULL;
4253 }
4254 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004255 else
4256 {
4257#if PY_MAJOR_VERSION < 3
4258 PyErr_FORMAT(PyExc_TypeError,
4259 N_("expected str() or unicode() instance, but got %s"),
4260 Py_TYPE_NAME(obj));
4261#else
4262 PyErr_FORMAT(PyExc_TypeError,
4263 N_("expected bytes() or str() instance, but got %s"),
4264 Py_TYPE_NAME(obj));
4265#endif
4266 return NULL;
4267 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004268
4269 /*
4270 * Error checking: String must not contain newlines, as we
4271 * are replacing a single line, and we must replace it with
4272 * a single line.
4273 * A trailing newline is removed, so that append(f.readlines()) works.
4274 */
4275 p = memchr(str, '\n', len);
4276 if (p != NULL)
4277 {
4278 if (p == str + len - 1)
4279 --len;
4280 else
4281 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004282 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004283 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004284 return NULL;
4285 }
4286 }
4287
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004288 /*
4289 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004290 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004291 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004292 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004293 if (save == NULL)
4294 {
4295 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004296 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004297 return NULL;
4298 }
4299
4300 for (i = 0; i < len; ++i)
4301 {
4302 if (str[i] == '\0')
4303 save[i] = '\n';
4304 else
4305 save[i] = str[i];
4306 }
4307
4308 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004309 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004310
4311 return save;
4312}
4313
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004314/*
4315 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004316 * in Vim format (1-based). The line is returned as a Python
4317 * string object.
4318 */
4319 static PyObject *
4320GetBufferLine(buf_T *buf, PyInt n)
4321{
4322 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4323}
4324
4325
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004326/*
4327 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004328 * are in Vim format (1-based). The range is from lo up to, but not
4329 * including, hi. The list is returned as a Python list of string objects.
4330 */
4331 static PyObject *
4332GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4333{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004334 PyInt i;
4335 PyInt n = hi - lo;
4336 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004337
4338 if (list == NULL)
4339 return NULL;
4340
4341 for (i = 0; i < n; ++i)
4342 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004343 linenr_T lnum = (linenr_T)(lo + i);
4344 char *text;
4345 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004346
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004347 if (lnum > buf->b_ml.ml_line_count)
4348 text = "";
4349 else
4350 text = (char *)ml_get_buf(buf, lnum, FALSE);
4351 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004352 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004353 {
4354 Py_DECREF(list);
4355 return NULL;
4356 }
4357
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004358 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004359 }
4360
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004361 // The ownership of the Python list is passed to the caller (ie,
4362 // the caller should Py_DECREF() the object when it is finished
4363 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004364
4365 return list;
4366}
4367
4368/*
4369 * Check if deleting lines made the cursor position invalid.
4370 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4371 * deleted).
4372 */
4373 static void
4374py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4375{
4376 if (curwin->w_cursor.lnum >= lo)
4377 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004378 // Adjust the cursor position if it's in/after the changed
4379 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004380 if (curwin->w_cursor.lnum >= hi)
4381 {
4382 curwin->w_cursor.lnum += extra;
4383 check_cursor_col();
4384 }
4385 else if (extra < 0)
4386 {
4387 curwin->w_cursor.lnum = lo;
4388 check_cursor();
4389 }
4390 else
4391 check_cursor_col();
4392 changed_cline_bef_curs();
4393 }
4394 invalidate_botline();
4395}
4396
Bram Moolenaar19e60942011-06-19 00:27:51 +02004397/*
4398 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004399 * in Vim format (1-based). The replacement line is given as
4400 * a Python string object. The object is checked for validity
4401 * and correct format. Errors are returned as a value of FAIL.
4402 * The return value is OK on success.
4403 * If OK is returned and len_change is not NULL, *len_change
4404 * is set to the change in the buffer length.
4405 */
4406 static int
4407SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4408{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004409 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004410 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004411
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004412 // First of all, we check the type of the supplied Python object.
4413 // There are three cases:
4414 // 1. NULL, or None - this is a deletion.
4415 // 2. A string - this is a replacement.
4416 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004417 if (line == Py_None || line == NULL)
4418 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004419 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004420 switchwin.sw_curwin = NULL;
4421 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004423 VimTryStart();
4424
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004426 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004427 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004428 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 else
4430 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00004431 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004432 || save_curbuf.br_buf == NULL))
4433 // Using an existing window for the buffer, adjust the cursor
4434 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004436 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004437 // Only adjust marks if we managed to switch to a window that
4438 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004439 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440 }
4441
Bram Moolenaar18f47402022-01-06 13:24:51 +00004442 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004444 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 return FAIL;
4446
4447 if (len_change)
4448 *len_change = -1;
4449
4450 return OK;
4451 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004452 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004454 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004455
4456 if (save == NULL)
4457 return FAIL;
4458
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004459 VimTryStart();
4460
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004461 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004462 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004463 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004464
4465 if (u_savesub((linenr_T)n) == FAIL)
4466 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004467 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004468 vim_free(save);
4469 }
4470 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4471 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004472 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004473 vim_free(save);
4474 }
4475 else
4476 changed_bytes((linenr_T)n, 0);
4477
Bram Moolenaar18f47402022-01-06 13:24:51 +00004478 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004479
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004480 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004481 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004482 check_cursor_col();
4483
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004484 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004485 return FAIL;
4486
4487 if (len_change)
4488 *len_change = 0;
4489
4490 return OK;
4491 }
4492 else
4493 {
4494 PyErr_BadArgument();
4495 return FAIL;
4496 }
4497}
4498
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004499/*
4500 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004501 * Vim format (1-based). The range is from lo up to, but not including, hi.
4502 * The replacement lines are given as a Python list of string objects. The
4503 * list is checked for validity and correct format. Errors are returned as a
4504 * value of FAIL. The return value is OK on success.
4505 * If OK is returned and len_change is not NULL, *len_change
4506 * is set to the change in the buffer length.
4507 */
4508 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004509SetBufferLineList(
4510 buf_T *buf,
4511 PyInt lo,
4512 PyInt hi,
4513 PyObject *list,
4514 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004515{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004516 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004517 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004518
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004519 // First of all, we check the type of the supplied Python object.
4520 // There are three cases:
4521 // 1. NULL, or None - this is a deletion.
4522 // 2. A list - this is a replacement.
4523 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004524 if (list == Py_None || list == NULL)
4525 {
4526 PyInt i;
4527 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004528
4529 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004530 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004531 switchwin.sw_curwin = NULL;
4532 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004533
4534 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004535 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004536 else
4537 {
4538 for (i = 0; i < n; ++i)
4539 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004540 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004542 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004543 break;
4544 }
4545 }
Bram Moolenaar18f47402022-01-06 13:24:51 +00004546 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004547 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004548 // Using an existing window for the buffer, adjust the cursor
4549 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004550 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004551 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004552 // Only adjust marks if we managed to switch to a window that
4553 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004554 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004555 }
4556
Bram Moolenaar18f47402022-01-06 13:24:51 +00004557 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004559 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004560 return FAIL;
4561
4562 if (len_change)
4563 *len_change = -n;
4564
4565 return OK;
4566 }
4567 else if (PyList_Check(list))
4568 {
4569 PyInt i;
4570 PyInt new_len = PyList_Size(list);
4571 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004572 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004573 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004574
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004575 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004576 array = NULL;
4577 else
4578 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004579 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004580 if (array == NULL)
4581 {
4582 PyErr_NoMemory();
4583 return FAIL;
4584 }
4585 }
4586
4587 for (i = 0; i < new_len; ++i)
4588 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004589 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004590
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004591 if (!(line = PyList_GetItem(list, i)) ||
4592 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004593 {
4594 while (i)
4595 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004596 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597 return FAIL;
4598 }
4599 }
4600
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004601 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004602 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004603
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004604 // START of region without "return". Must call restore_buffer()!
Bram Moolenaar18f47402022-01-06 13:24:51 +00004605 switchwin.sw_curwin = NULL;
4606 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004607
4608 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004609 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004610
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004611 // If the size of the range is reducing (ie, new_len < old_len) we
4612 // need to delete some old_len. We do this at the start, by
4613 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004614 if (!PyErr_Occurred())
4615 {
4616 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004617 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004618 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004619 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004620 break;
4621 }
4622 extra -= i;
4623 }
4624
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004625 // For as long as possible, replace the existing old_len with the
4626 // new old_len. This is a more efficient operation, as it requires
4627 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004628 if (!PyErr_Occurred())
4629 {
4630 for (i = 0; i < old_len && i < new_len; ++i)
4631 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4632 == FAIL)
4633 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004634 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004635 break;
4636 }
4637 }
4638 else
4639 i = 0;
4640
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004641 // Now we may need to insert the remaining new old_len. If we do, we
4642 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004643 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004644 if (!PyErr_Occurred())
4645 {
4646 while (i < new_len)
4647 {
4648 if (ml_append((linenr_T)(lo + i - 1),
4649 (char_u *)array[i], 0, FALSE) == FAIL)
4650 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004651 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004652 break;
4653 }
4654 vim_free(array[i]);
4655 ++i;
4656 ++extra;
4657 }
4658 }
4659
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004660 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004661 while (i < new_len)
4662 {
4663 vim_free(array[i]);
4664 ++i;
4665 }
4666
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004667 // Free the array of old_len. All of its contents have now
4668 // been dealt with (either freed, or the responsibility passed
4669 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004670 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004671
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004672 // Adjust marks. Invalidate any which lie in the
4673 // changed range, and move any in the remainder of the buffer.
4674 // Only adjust marks if we managed to switch to a window that holds
4675 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004676 if (save_curbuf.br_buf == NULL)
Bram Moolenaar37233f62022-05-22 12:23:48 +01004677 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004678 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004679 (long)MAXLNUM, (long)extra);
Bram Moolenaar37233f62022-05-22 12:23:48 +01004680 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4681 }
Bram Moolenaar19e60942011-06-19 00:27:51 +02004682
Bram Moolenaar18f47402022-01-06 13:24:51 +00004683 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004684 || 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 Moolenaar18f47402022-01-06 13:24:51 +00004690 restore_win_for_buf(&switchwin, &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 Moolenaar18f47402022-01-06 13:24:51 +00004720 switchwin_T switchwin;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004721
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004722 // First of all, we check the type of the supplied Python object.
4723 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004724 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004725 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004726 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727
4728 if (str == NULL)
4729 return FAIL;
4730
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004732 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004733 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004734
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004735 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004736 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004737 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004738 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004739 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004740 // Only adjust marks if we managed to switch to a window that
4741 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742 appended_lines_mark((linenr_T)n, 1L);
4743
4744 vim_free(str);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004745 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004746 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004747
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004748 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 return FAIL;
4750
4751 if (len_change)
4752 *len_change = 1;
4753
4754 return OK;
4755 }
4756 else if (PyList_Check(lines))
4757 {
4758 PyInt i;
4759 PyInt size = PyList_Size(lines);
4760 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004761
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004762 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763 if (array == NULL)
4764 {
4765 PyErr_NoMemory();
4766 return FAIL;
4767 }
4768
4769 for (i = 0; i < size; ++i)
4770 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004771 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004772
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004773 if (!(line = PyList_GetItem(lines, i)) ||
4774 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004775 {
4776 while (i)
4777 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004778 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779 return FAIL;
4780 }
4781 }
4782
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004784 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004785 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786
4787 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004788 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004789 else
4790 {
4791 for (i = 0; i < size; ++i)
4792 {
4793 if (ml_append((linenr_T)(n + i),
4794 (char_u *)array[i], 0, FALSE) == FAIL)
4795 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004796 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004797
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004798 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004799 while (i < size)
4800 vim_free(array[i++]);
4801
4802 break;
4803 }
4804 vim_free(array[i]);
4805 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004806 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004807 // Only adjust marks if we managed to switch to a window that
4808 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004809 appended_lines_mark((linenr_T)n, (long)i);
4810 }
4811
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004812 // Free the array of lines. All of its contents have now
4813 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004814 PyMem_Free(array);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004815 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004816
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004817 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004818
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004819 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004820 return FAIL;
4821
4822 if (len_change)
4823 *len_change = size;
4824
4825 return OK;
4826 }
4827 else
4828 {
4829 PyErr_BadArgument();
4830 return FAIL;
4831 }
4832}
4833
4834/*
4835 * Common routines for buffers and line ranges
4836 * -------------------------------------------
4837 */
4838
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004839typedef struct
4840{
4841 PyObject_HEAD
4842 buf_T *buf;
4843} BufferObject;
4844
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004845 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004846CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004847{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004848 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004849 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004850 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004851 return -1;
4852 }
4853
4854 return 0;
4855}
4856
4857 static PyObject *
4858RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4859{
4860 if (CheckBuffer(self))
4861 return NULL;
4862
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004863 if (end == -1)
4864 end = self->buf->b_ml.ml_line_count;
4865
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004866 if (n < 0)
4867 n += end - start + 1;
4868
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004869 if (n < 0 || n > end - start)
4870 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004871 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004872 return NULL;
4873 }
4874
4875 return GetBufferLine(self->buf, n+start);
4876}
4877
4878 static PyObject *
4879RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4880{
4881 PyInt size;
4882
4883 if (CheckBuffer(self))
4884 return NULL;
4885
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004886 if (end == -1)
4887 end = self->buf->b_ml.ml_line_count;
4888
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004889 size = end - start + 1;
4890
4891 if (lo < 0)
4892 lo = 0;
4893 else if (lo > size)
4894 lo = size;
4895 if (hi < 0)
4896 hi = 0;
4897 if (hi < lo)
4898 hi = lo;
4899 else if (hi > size)
4900 hi = size;
4901
4902 return GetBufferLineList(self->buf, lo+start, hi+start);
4903}
4904
4905 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004906RBAsItem(
4907 BufferObject *self,
4908 PyInt n,
4909 PyObject *valObject,
4910 PyInt start,
4911 PyInt end,
4912 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004913{
4914 PyInt len_change;
4915
4916 if (CheckBuffer(self))
4917 return -1;
4918
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004919 if (end == -1)
4920 end = self->buf->b_ml.ml_line_count;
4921
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004922 if (n < 0)
4923 n += end - start + 1;
4924
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004925 if (n < 0 || n > end - start)
4926 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004927 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004928 return -1;
4929 }
4930
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004931 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004932 return -1;
4933
4934 if (new_end)
4935 *new_end = end + len_change;
4936
4937 return 0;
4938}
4939
Bram Moolenaar19e60942011-06-19 00:27:51 +02004940 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004941RBAsSlice(
4942 BufferObject *self,
4943 PyInt lo,
4944 PyInt hi,
4945 PyObject *valObject,
4946 PyInt start,
4947 PyInt end,
4948 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004949{
4950 PyInt size;
4951 PyInt len_change;
4952
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004953 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004954 if (CheckBuffer(self))
4955 return -1;
4956
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004957 if (end == -1)
4958 end = self->buf->b_ml.ml_line_count;
4959
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004960 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004961 size = end - start + 1;
4962
4963 if (lo < 0)
4964 lo = 0;
4965 else if (lo > size)
4966 lo = size;
4967 if (hi < 0)
4968 hi = 0;
4969 if (hi < lo)
4970 hi = lo;
4971 else if (hi > size)
4972 hi = size;
4973
4974 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004975 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004976 return -1;
4977
4978 if (new_end)
4979 *new_end = end + len_change;
4980
4981 return 0;
4982}
4983
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004984
4985 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004986RBAppend(
4987 BufferObject *self,
4988 PyObject *args,
4989 PyInt start,
4990 PyInt end,
4991 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992{
4993 PyObject *lines;
4994 PyInt len_change;
4995 PyInt max;
4996 PyInt n;
4997
4998 if (CheckBuffer(self))
4999 return NULL;
5000
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005001 if (end == -1)
5002 end = self->buf->b_ml.ml_line_count;
5003
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005004 max = n = end - start + 1;
5005
5006 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5007 return NULL;
5008
5009 if (n < 0 || n > max)
5010 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005011 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005012 return NULL;
5013 }
5014
5015 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5016 return NULL;
5017
5018 if (new_end)
5019 *new_end = end + len_change;
5020
5021 Py_INCREF(Py_None);
5022 return Py_None;
5023}
5024
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005025// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005026
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005027static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005028static PySequenceMethods RangeAsSeq;
5029static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005031typedef struct
5032{
5033 PyObject_HEAD
5034 BufferObject *buf;
5035 PyInt start;
5036 PyInt end;
5037} RangeObject;
5038
5039 static PyObject *
5040RangeNew(buf_T *buf, PyInt start, PyInt end)
5041{
5042 BufferObject *bufr;
5043 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005044 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005045 if (self == NULL)
5046 return NULL;
5047
5048 bufr = (BufferObject *)BufferNew(buf);
5049 if (bufr == NULL)
5050 {
5051 Py_DECREF(self);
5052 return NULL;
5053 }
5054 Py_INCREF(bufr);
5055
5056 self->buf = bufr;
5057 self->start = start;
5058 self->end = end;
5059
5060 return (PyObject *)(self);
5061}
5062
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005063 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005064RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005065{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005066 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005067 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005068 PyObject_GC_Del((void *)(self));
5069}
5070
5071 static int
5072RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5073{
5074 Py_VISIT(((PyObject *)(self->buf)));
5075 return 0;
5076}
5077
5078 static int
5079RangeClear(RangeObject *self)
5080{
5081 Py_CLEAR(self->buf);
5082 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005083}
5084
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005085 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005088 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005090 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093}
5094
5095 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005098 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005099}
5100
5101 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005104 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005105}
5106
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005107static char *RangeAttrs[] = {
5108 "start", "end",
5109 NULL
5110};
5111
5112 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005113RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005114{
5115 return ObjectDir(self, RangeAttrs);
5116}
5117
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005118 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005119RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005120{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122}
5123
5124 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005125RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005126{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005127 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005128 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00005129 (void *)self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005130 else
5131 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005132 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005133
5134 if (name == NULL)
5135 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005137 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005138 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139 }
5140}
5141
5142static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005143 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005144 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005145 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5146 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005147};
5148
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005149static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005150static PySequenceMethods BufferAsSeq;
5151static PyMappingMethods BufferAsMapping;
5152
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005153 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005154BufferNew(buf_T *buf)
5155{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005156 /*
5157 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005158 * If we add a "b_python*_ref" field to the buf_T structure,
5159 * then we can get at it in buf_freeall() in vim. We then
5160 * need to create only ONE Python object per buffer - if
5161 * we try to create a second, just INCREF the existing one
5162 * and return it. The (single) Python object referring to
5163 * the buffer is stored in "b_python*_ref".
5164 * Question: what to do on a buf_freeall(). We'll probably
5165 * have to either delete the Python object (DECREF it to
5166 * zero - a bad idea, as it leaves dangling refs!) or
5167 * set the buf_T * value to an invalid value (-1?), which
5168 * means we need checks in all access functions... Bah.
5169 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005170 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005171 * b_python_ref and b_python3_ref fields respectively.
5172 */
5173
5174 BufferObject *self;
5175
5176 if (BUF_PYTHON_REF(buf) != NULL)
5177 {
5178 self = BUF_PYTHON_REF(buf);
5179 Py_INCREF(self);
5180 }
5181 else
5182 {
5183 self = PyObject_NEW(BufferObject, &BufferType);
5184 if (self == NULL)
5185 return NULL;
5186 self->buf = buf;
5187 BUF_PYTHON_REF(buf) = self;
5188 }
5189
5190 return (PyObject *)(self);
5191}
5192
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005193 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005194BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005195{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005196 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5197 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005198
5199 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005200}
5201
Bram Moolenaar971db462013-05-12 18:44:48 +02005202 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005203BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005204{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005205 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005206 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005207 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005208
Bram Moolenaard6e39182013-05-21 18:30:34 +02005209 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005210}
5211
5212 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005213BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005214{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005216}
5217
5218 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005219BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005220{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005221 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005222}
5223
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005224static char *BufferAttrs[] = {
5225 "name", "number", "vars", "options", "valid",
5226 NULL
5227};
5228
5229 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005230BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005231{
5232 return ObjectDir(self, BufferAttrs);
5233}
5234
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005235 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005236BufferAttrValid(BufferObject *self, char *name)
5237{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005238 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005239
5240 if (strcmp(name, "valid") != 0)
5241 return NULL;
5242
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005243 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5244 Py_INCREF(ret);
5245 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005246}
5247
5248 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005249BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005250{
5251 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005252 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005253 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005254 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005255 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005256 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005257 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005259 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5260 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005261 else if (strcmp(name, "__members__") == 0)
5262 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005263 else
5264 return NULL;
5265}
5266
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005267 static int
5268BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5269{
5270 if (CheckBuffer(self))
5271 return -1;
5272
5273 if (strcmp(name, "name") == 0)
5274 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005275 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005276 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005277 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005278 PyObject *todecref;
5279
5280 if (!(val = StringToChars(valObject, &todecref)))
5281 return -1;
5282
5283 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005284 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005285 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00005286 if (curbuf == self->buf)
5287 {
5288 ren_ret = rename_buffer(val);
5289 aucmd_restbuf(&aco);
5290 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005291 Py_XDECREF(todecref);
5292 if (VimTryEnd())
5293 return -1;
5294
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005295 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005296 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005297 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005298 return -1;
5299 }
5300 return 0;
5301 }
5302 else
5303 {
5304 PyErr_SetString(PyExc_AttributeError, name);
5305 return -1;
5306 }
5307}
5308
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005309 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005310BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005312 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005313}
5314
5315 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005316BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005317{
5318 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005319 char_u *pmark;
5320 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005321 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005322 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005323
Bram Moolenaard6e39182013-05-21 18:30:34 +02005324 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005325 return NULL;
5326
Bram Moolenaar389a1792013-06-23 13:00:44 +02005327 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005328 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005329
Bram Moolenaar389a1792013-06-23 13:00:44 +02005330 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005331 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005332 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005333 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005334 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005335 return NULL;
5336 }
5337
5338 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005339
5340 Py_XDECREF(todecref);
5341
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005342 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005343 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005344 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005345 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005346 if (VimTryEnd())
5347 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005348
5349 if (posp == NULL)
5350 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005351 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005352 return NULL;
5353 }
5354
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005355 if (posp->lnum <= 0)
5356 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005357 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005358 Py_INCREF(Py_None);
5359 return Py_None;
5360 }
5361
5362 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5363}
5364
5365 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005366BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005367{
5368 PyInt start;
5369 PyInt end;
5370
Bram Moolenaard6e39182013-05-21 18:30:34 +02005371 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005372 return NULL;
5373
5374 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5375 return NULL;
5376
Bram Moolenaard6e39182013-05-21 18:30:34 +02005377 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005378}
5379
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005380 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005381BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005382{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005383 if (self->buf == INVALID_BUFFER_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00005384 return PyString_FromFormat("<buffer object (deleted) at %p>", (void *)self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005385 else
5386 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005387 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005388
5389 if (name == NULL)
5390 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005391
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005392 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005393 }
5394}
5395
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005396static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005397 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005398 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005399 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005400 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005401 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5402 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005403};
5404
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005405/*
5406 * Buffer list object - Implementation
5407 */
5408
5409static PyTypeObject BufMapType;
5410
5411typedef struct
5412{
5413 PyObject_HEAD
5414} BufMapObject;
5415
5416 static PyInt
5417BufMapLength(PyObject *self UNUSED)
5418{
5419 buf_T *b = firstbuf;
5420 PyInt n = 0;
5421
5422 while (b)
5423 {
5424 ++n;
5425 b = b->b_next;
5426 }
5427
5428 return n;
5429}
5430
5431 static PyObject *
5432BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5433{
5434 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005435 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005436
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005437 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005438 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005439
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005440 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005441
5442 if (b)
5443 return BufferNew(b);
5444 else
5445 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005446 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005447 return NULL;
5448 }
5449}
5450
5451 static void
5452BufMapIterDestruct(PyObject *buffer)
5453{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005454 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005455 if (buffer)
5456 {
5457 Py_DECREF(buffer);
5458 }
5459}
5460
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005461 static int
5462BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5463{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005464 if (buffer)
5465 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005466 return 0;
5467}
5468
5469 static int
5470BufMapIterClear(PyObject **buffer)
5471{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005472 if (*buffer)
5473 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005474 return 0;
5475}
5476
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005477 static PyObject *
5478BufMapIterNext(PyObject **buffer)
5479{
5480 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005481 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005482
5483 if (!*buffer)
5484 return NULL;
5485
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005486 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005487
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005488 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005489 {
5490 *buffer = NULL;
5491 return NULL;
5492 }
5493
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005494 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005495 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005496 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005497 return NULL;
5498 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005499 // Do not increment reference: we no longer hold it (decref), but whoever
5500 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005501 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005502}
5503
5504 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005505BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005506{
5507 PyObject *buffer;
5508
5509 buffer = BufferNew(firstbuf);
5510 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005511 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005512 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5513 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005514}
5515
5516static PyMappingMethods BufMapAsMapping = {
5517 (lenfunc) BufMapLength,
5518 (binaryfunc) BufMapItem,
5519 (objobjargproc) 0,
5520};
5521
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005522// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005523
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005524static char *CurrentAttrs[] = {
5525 "buffer", "window", "line", "range", "tabpage",
5526 NULL
5527};
5528
5529 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005530CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005531{
5532 return ObjectDir(self, CurrentAttrs);
5533}
5534
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005535 static PyObject *
5536CurrentGetattr(PyObject *self UNUSED, char *name)
5537{
5538 if (strcmp(name, "buffer") == 0)
5539 return (PyObject *)BufferNew(curbuf);
5540 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005541 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005542 else if (strcmp(name, "tabpage") == 0)
5543 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005544 else if (strcmp(name, "line") == 0)
5545 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5546 else if (strcmp(name, "range") == 0)
5547 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005548 else if (strcmp(name, "__members__") == 0)
5549 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005550 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005551#if PY_MAJOR_VERSION < 3
5552 return Py_FindMethod(WindowMethods, self, name);
5553#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005554 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005555#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005556}
5557
5558 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005559CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005560{
5561 if (strcmp(name, "line") == 0)
5562 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5564 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005565 return -1;
5566
5567 return 0;
5568 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005569 else if (strcmp(name, "buffer") == 0)
5570 {
5571 int count;
5572
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005574 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005575 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005576 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005578 return -1;
5579 }
5580
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005583 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005584
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005585 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005586 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5587 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005588 if (VimTryEnd())
5589 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005590 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005591 return -1;
5592 }
5593
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005594 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005595 }
5596 else if (strcmp(name, "window") == 0)
5597 {
5598 int count;
5599
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005600 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005601 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005602 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005603 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005604 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005605 return -1;
5606 }
5607
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005608 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005609 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005610 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005611
5612 if (!count)
5613 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005614 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005615 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005616 return -1;
5617 }
5618
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005619 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005620 win_goto(((WindowObject *)(valObject))->win);
5621 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005622 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005623 if (VimTryEnd())
5624 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005625 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005626 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005627 return -1;
5628 }
5629
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005630 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005631 }
5632 else if (strcmp(name, "tabpage") == 0)
5633 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005634 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005635 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005636 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005637 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005638 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005639 return -1;
5640 }
5641
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005642 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005643 return -1;
5644
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005645 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005646 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5647 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005648 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005649 if (VimTryEnd())
5650 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005651 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005652 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005653 return -1;
5654 }
5655
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005656 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005657 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005658 else
5659 {
5660 PyErr_SetString(PyExc_AttributeError, name);
5661 return -1;
5662 }
5663}
5664
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005665static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005666 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005667 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5668 { NULL, NULL, 0, NULL}
5669};
5670
Bram Moolenaardb913952012-06-29 12:54:53 +02005671 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005672init_range_cmd(exarg_T *eap)
5673{
5674 RangeStart = eap->line1;
5675 RangeEnd = eap->line2;
5676}
5677
5678 static void
5679init_range_eval(typval_T *rettv UNUSED)
5680{
5681 RangeStart = (PyInt) curwin->w_cursor.lnum;
5682 RangeEnd = RangeStart;
5683}
5684
5685 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005686run_cmd(const char *cmd, void *arg UNUSED
5687#ifdef PY_CAN_RECURSE
5688 , PyGILState_STATE *pygilstate UNUSED
5689#endif
5690 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691{
Bram Moolenaar41009372013-07-01 22:03:04 +02005692 PyObject *run_ret;
5693 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5694 if (run_ret != NULL)
5695 {
5696 Py_DECREF(run_ret);
5697 }
5698 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5699 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005700 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005701 PyErr_Clear();
5702 }
5703 else
5704 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005705}
5706
5707static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5708static int code_hdr_len = 30;
5709
5710 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005711run_do(const char *cmd, void *arg UNUSED
5712#ifdef PY_CAN_RECURSE
5713 , PyGILState_STATE *pygilstate
5714#endif
5715 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005716{
5717 PyInt lnum;
5718 size_t len;
5719 char *code;
5720 int status;
5721 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005722 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005723 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005724
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005725 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005727 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005728 return;
5729 }
5730
5731 len = code_hdr_len + STRLEN(cmd);
5732 code = PyMem_New(char, len + 1);
5733 memcpy(code, code_hdr, code_hdr_len);
5734 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005735 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5736 status = -1;
5737 if (run_ret != NULL)
5738 {
5739 status = 0;
5740 Py_DECREF(run_ret);
5741 }
5742 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5743 {
5744 PyMem_Free(code);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005745 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005746 PyErr_Clear();
5747 return;
5748 }
5749 else
5750 PyErr_PrintEx(1);
5751
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005752 PyMem_Free(code);
5753
5754 if (status)
5755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005756 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005757 return;
5758 }
5759
5760 status = 0;
5761 pymain = PyImport_AddModule("__main__");
5762 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005763#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005765#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005766
5767 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5768 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005769 PyObject *line;
5770 PyObject *linenr;
5771 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005772
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005773#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005774 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005775#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005776 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005777 if (lnum > curbuf->b_ml.ml_line_count
5778 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005779 goto err;
5780 if (!(linenr = PyInt_FromLong((long) lnum)))
5781 {
5782 Py_DECREF(line);
5783 goto err;
5784 }
5785 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5786 Py_DECREF(line);
5787 Py_DECREF(linenr);
5788 if (!ret)
5789 goto err;
5790
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005791 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005792 if (curbuf != was_curbuf)
5793 {
5794 Py_XDECREF(ret);
5795 goto err;
5796 }
5797
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005798 if (ret != Py_None)
5799 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005800 {
5801 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005802 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005803 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005804
5805 Py_XDECREF(ret);
5806 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005807#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005808 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005809#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005810 }
5811 goto out;
5812err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005813#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005814 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005815#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005816 PyErr_PrintEx(0);
5817 PythonIO_Flush();
5818 status = 1;
5819out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005820#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005821 if (!status)
5822 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005823#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005824 Py_DECREF(pyfunc);
5825 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5826 if (status)
5827 return;
5828 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005829 update_curbuf(UPD_NOT_VALID);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005830}
5831
5832 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005833run_eval(const char *cmd, typval_T *rettv
5834#ifdef PY_CAN_RECURSE
5835 , PyGILState_STATE *pygilstate UNUSED
5836#endif
5837 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005838{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005839 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005840
Bram Moolenaar41009372013-07-01 22:03:04 +02005841 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005842 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005843 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005844 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005845 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005846 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005847 PyErr_Clear();
5848 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005849 else
5850 {
5851 if (PyErr_Occurred() && !msg_silent)
5852 PyErr_PrintEx(0);
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005853 emsg(_(e_eval_did_not_return_valid_python_object));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005854 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005855 }
5856 else
5857 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005858 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005859 emsg(_(e_failed_to_convert_returned_python_object_to_vim_value));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005860 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005861 }
5862 PyErr_Clear();
5863}
5864
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005865 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005866set_ref_in_py(const int copyID)
5867{
5868 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005869 list_T *ll;
5870 int i;
5871 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005872 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005873
5874 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005876 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005877 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5878 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005879 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005880
5881 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005882 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005883 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005884 {
5885 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005886 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005887 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005888 }
5889
Bram Moolenaar8110a092016-04-14 15:56:09 +02005890 if (lastfunc != NULL)
5891 {
5892 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5893 {
5894 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005895 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005896 if (func->argc)
5897 for (i = 0; !abort && i < func->argc; ++i)
5898 abort = abort
5899 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5900 }
5901 }
5902
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005903 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005904}
5905
5906 static int
5907set_string_copy(char_u *str, typval_T *tv)
5908{
5909 tv->vval.v_string = vim_strsave(str);
5910 if (tv->vval.v_string == NULL)
5911 {
5912 PyErr_NoMemory();
5913 return -1;
5914 }
5915 return 0;
5916}
5917
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005919pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005921 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922 char_u *key;
5923 dictitem_T *di;
5924 PyObject *keyObject;
5925 PyObject *valObject;
5926 Py_ssize_t iter = 0;
5927
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005928 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005929 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930
5931 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005932 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005933
5934 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5935 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005936 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937
Bram Moolenaara03e6312013-05-29 22:49:26 +02005938 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005939 {
5940 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005942 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005943
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005944 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005945 {
5946 dict_unref(dict);
5947 return -1;
5948 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005949
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005950 if (*key == NUL)
5951 {
5952 dict_unref(dict);
5953 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005954 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005955 return -1;
5956 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005957
5958 di = dictitem_alloc(key);
5959
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005960 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961
5962 if (di == NULL)
5963 {
5964 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005965 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return -1;
5967 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005969 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 {
5971 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005972 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 return -1;
5974 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005975
5976 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005978 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005979 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005980 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005982 return -1;
5983 }
5984 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985
5986 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005987 return 0;
5988}
5989
5990 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005991pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005992{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005993 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005994 char_u *key;
5995 dictitem_T *di;
5996 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005997 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998 PyObject *keyObject;
5999 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006000
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006001 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006002 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006003
6004 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006005 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006006
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006007 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006008 {
6009 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006010 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006011 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006012
6013 if (!(iterator = PyObject_GetIter(list)))
6014 {
6015 dict_unref(dict);
6016 Py_DECREF(list);
6017 return -1;
6018 }
6019 Py_DECREF(list);
6020
6021 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006022 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006023 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006024
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006025 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006026 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006027 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006028 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006029 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006030 return -1;
6031 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006032
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006033 if (*key == NUL)
6034 {
6035 Py_DECREF(keyObject);
6036 Py_DECREF(iterator);
6037 Py_XDECREF(todecref);
6038 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006039 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006040 return -1;
6041 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006043 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006044 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006045 Py_DECREF(keyObject);
6046 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006047 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006048 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006049 return -1;
6050 }
6051
6052 di = dictitem_alloc(key);
6053
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006054 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006055 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006056
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006057 if (di == NULL)
6058 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006059 Py_DECREF(iterator);
6060 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006061 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006062 PyErr_NoMemory();
6063 return -1;
6064 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006065
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006066 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006067 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006068 Py_DECREF(iterator);
6069 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006070 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006071 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006072 return -1;
6073 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006074
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006075 Py_DECREF(valObject);
6076
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006077 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006078 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006079 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006080 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006081 dictitem_free(di);
6082 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006083 return -1;
6084 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006085 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006086 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006087 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006088 return 0;
6089}
6090
6091 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006092pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006093{
6094 list_T *l;
6095
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006096 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006097 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006098
6099 tv->v_type = VAR_LIST;
6100 tv->vval.v_list = l;
6101
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006102 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006103 {
6104 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006105 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006106 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006107
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006108 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006109 return 0;
6110}
6111
Bram Moolenaardb913952012-06-29 12:54:53 +02006112typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6113
6114 static int
6115convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006116 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006117{
6118 PyObject *capsule;
6119 char hexBuf[sizeof(void *) * 2 + 3];
6120
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006121 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006122
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006123#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006124 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006125#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006126 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006127#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006128 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006129 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006130#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006131 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006132#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006133 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006134#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006135 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6136 {
6137 Py_DECREF(capsule);
6138 tv->v_type = VAR_UNKNOWN;
6139 return -1;
6140 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006141
6142 Py_DECREF(capsule);
6143
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006144 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006145 {
6146 tv->v_type = VAR_UNKNOWN;
6147 return -1;
6148 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006149 // As we are not using copy_tv which increments reference count we must
6150 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006151 if (tv->v_type == VAR_DICT)
6152 ++tv->vval.v_dict->dv_refcount;
6153 else if (tv->v_type == VAR_LIST)
6154 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006155 }
6156 else
6157 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006158 typval_T *v;
6159
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006160#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006161 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006162#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006163 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006164#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 copy_tv(v, tv);
6166 }
6167 return 0;
6168}
6169
6170 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006171ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6172{
6173 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006174 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006175
6176 if (!(lookup_dict = PyDict_New()))
6177 return -1;
6178
6179 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6180 {
6181 tv->v_type = VAR_DICT;
6182 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6183 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006184 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006185 }
6186 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006187 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006188 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006189 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006190 else
6191 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006192 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006193 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006194 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006195 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006196 }
6197 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006198 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006199}
6200
6201 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006202ConvertFromPySequence(PyObject *obj, typval_T *tv)
6203{
6204 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006205 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006206
6207 if (!(lookup_dict = PyDict_New()))
6208 return -1;
6209
6210 if (PyType_IsSubtype(obj->ob_type, &ListType))
6211 {
6212 tv->v_type = VAR_LIST;
6213 tv->vval.v_list = (((ListObject *)(obj))->list);
6214 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006215 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006216 }
6217 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006218 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006219 else
6220 {
6221 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006222 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006223 Py_TYPE_NAME(obj));
6224 ret = -1;
6225 }
6226 Py_DECREF(lookup_dict);
6227 return ret;
6228}
6229
6230 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006231ConvertFromPyObject(PyObject *obj, typval_T *tv)
6232{
6233 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006234 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006235
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006236 if (!(lookup_dict = PyDict_New()))
6237 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006238 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006239 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006240 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006241}
6242
6243 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006244_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006245{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006246 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006247 {
6248 tv->v_type = VAR_DICT;
6249 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6250 ++tv->vval.v_dict->dv_refcount;
6251 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006252 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006253 {
6254 tv->v_type = VAR_LIST;
6255 tv->vval.v_list = (((ListObject *)(obj))->list);
6256 ++tv->vval.v_list->lv_refcount;
6257 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006258 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006260 FunctionObject *func = (FunctionObject *) obj;
6261 if (func->self != NULL || func->argv != NULL)
6262 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006263 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6264
Bram Moolenaar8110a092016-04-14 15:56:09 +02006265 set_partial(func, pt, TRUE);
6266 tv->vval.v_partial = pt;
6267 tv->v_type = VAR_PARTIAL;
6268 }
6269 else
6270 {
6271 if (set_string_copy(func->name, tv) == -1)
6272 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006273
Bram Moolenaar8110a092016-04-14 15:56:09 +02006274 tv->v_type = VAR_FUNC;
6275 }
6276 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006277 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006278 else if (PyBytes_Check(obj))
6279 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006280 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006282 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006283 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006284 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 return -1;
6286
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006287 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006288 return -1;
6289
6290 tv->v_type = VAR_STRING;
6291 }
6292 else if (PyUnicode_Check(obj))
6293 {
6294 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006295 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006296
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006297 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006298 if (bytes == NULL)
6299 return -1;
6300
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006301 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006302 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006303 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 return -1;
6305
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006306 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006307 {
6308 Py_XDECREF(bytes);
6309 return -1;
6310 }
6311 Py_XDECREF(bytes);
6312
6313 tv->v_type = VAR_STRING;
6314 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006315#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006316 else if (PyInt_Check(obj))
6317 {
6318 tv->v_type = VAR_NUMBER;
6319 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006320 if (PyErr_Occurred())
6321 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006322 }
6323#endif
6324 else if (PyLong_Check(obj))
6325 {
6326 tv->v_type = VAR_NUMBER;
6327 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006328 if (PyErr_Occurred())
6329 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006330 }
6331 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006332 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006333 else if (PyFloat_Check(obj))
6334 {
6335 tv->v_type = VAR_FLOAT;
6336 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6337 }
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:
6390 return PyFloat_FromDouble((double) tv->vval.v_float);
Bram Moolenaardb913952012-06-29 12:54:53 +02006391 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006392 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006393 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006394 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006396 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006397 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006398 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006399 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006400 if (tv->vval.v_partial->pt_argc)
6401 {
6402 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6403 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6404 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6405 }
6406 else
6407 argv = NULL;
6408 if (tv->vval.v_partial->pt_dict != NULL)
6409 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006410 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006411 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006412 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006413 tv->vval.v_partial->pt_dict,
6414 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006415 case VAR_BLOB:
6416 return PyBytes_FromStringAndSize(
6417 (char*) tv->vval.v_blob->bv_ga.ga_data,
6418 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006419 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006420 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006422 case VAR_CHANNEL:
6423 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006424 case VAR_INSTR:
Bram Moolenaardb913952012-06-29 12:54:53 +02006425 Py_INCREF(Py_None);
6426 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006427 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006428 case VAR_SPECIAL:
6429 switch (tv->vval.v_number)
6430 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006431 case VVAL_FALSE: return ALWAYS_FALSE;
6432 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006433 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006434 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006435 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006436 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006437 return NULL;
6438 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006439 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006440}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006441
6442typedef struct
6443{
6444 PyObject_HEAD
6445} CurrentObject;
6446static PyTypeObject CurrentType;
6447
6448 static void
6449init_structs(void)
6450{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006451 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006452 OutputType.tp_name = "vim.message";
6453 OutputType.tp_basicsize = sizeof(OutputObject);
6454 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6455 OutputType.tp_doc = "vim message object";
6456 OutputType.tp_methods = OutputMethods;
6457#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006458 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6459 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006460 OutputType.tp_alloc = call_PyType_GenericAlloc;
6461 OutputType.tp_new = call_PyType_GenericNew;
6462 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006463 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006464#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006465 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6466 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006467 // Disabled, because this causes a crash in test86
6468 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006469#endif
6470
Bram Moolenaara80faa82020-04-12 19:37:17 +02006471 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006472 IterType.tp_name = "vim.iter";
6473 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006474 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006475 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006476 IterType.tp_iter = (getiterfunc)IterIter;
6477 IterType.tp_iternext = (iternextfunc)IterNext;
6478 IterType.tp_dealloc = (destructor)IterDestructor;
6479 IterType.tp_traverse = (traverseproc)IterTraverse;
6480 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006481
Bram Moolenaara80faa82020-04-12 19:37:17 +02006482 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483 BufferType.tp_name = "vim.buffer";
6484 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006485 BufferType.tp_dealloc = (destructor)BufferDestructor;
6486 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006487 BufferType.tp_as_sequence = &BufferAsSeq;
6488 BufferType.tp_as_mapping = &BufferAsMapping;
6489 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6490 BufferType.tp_doc = "vim buffer object";
6491 BufferType.tp_methods = BufferMethods;
6492#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006493 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006494 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006495 BufferType.tp_alloc = call_PyType_GenericAlloc;
6496 BufferType.tp_new = call_PyType_GenericNew;
6497 BufferType.tp_free = call_PyObject_Free;
6498#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006500 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006501#endif
6502
Bram Moolenaara80faa82020-04-12 19:37:17 +02006503 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504 WindowType.tp_name = "vim.window";
6505 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006506 WindowType.tp_dealloc = (destructor)WindowDestructor;
6507 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006508 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006509 WindowType.tp_doc = "vim Window object";
6510 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006511 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6512 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006513#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006514 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6515 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006516 WindowType.tp_alloc = call_PyType_GenericAlloc;
6517 WindowType.tp_new = call_PyType_GenericNew;
6518 WindowType.tp_free = call_PyObject_Free;
6519#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006520 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6521 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006522#endif
6523
Bram Moolenaara80faa82020-04-12 19:37:17 +02006524 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006525 TabPageType.tp_name = "vim.tabpage";
6526 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006527 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6528 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006529 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6530 TabPageType.tp_doc = "vim tab page object";
6531 TabPageType.tp_methods = TabPageMethods;
6532#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006533 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006534 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6535 TabPageType.tp_new = call_PyType_GenericNew;
6536 TabPageType.tp_free = call_PyObject_Free;
6537#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006538 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006539#endif
6540
Bram Moolenaara80faa82020-04-12 19:37:17 +02006541 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006542 BufMapType.tp_name = "vim.bufferlist";
6543 BufMapType.tp_basicsize = sizeof(BufMapObject);
6544 BufMapType.tp_as_mapping = &BufMapAsMapping;
6545 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006546 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006547 BufferType.tp_doc = "vim buffer list";
6548
Bram Moolenaara80faa82020-04-12 19:37:17 +02006549 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006550 WinListType.tp_name = "vim.windowlist";
6551 WinListType.tp_basicsize = sizeof(WinListType);
6552 WinListType.tp_as_sequence = &WinListAsSeq;
6553 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6554 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006555 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006556
Bram Moolenaara80faa82020-04-12 19:37:17 +02006557 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006558 TabListType.tp_name = "vim.tabpagelist";
6559 TabListType.tp_basicsize = sizeof(TabListType);
6560 TabListType.tp_as_sequence = &TabListAsSeq;
6561 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6562 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006563
Bram Moolenaara80faa82020-04-12 19:37:17 +02006564 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565 RangeType.tp_name = "vim.range";
6566 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006567 RangeType.tp_dealloc = (destructor)RangeDestructor;
6568 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006569 RangeType.tp_as_sequence = &RangeAsSeq;
6570 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006571 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572 RangeType.tp_doc = "vim Range object";
6573 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006574 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6575 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006576#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006577 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006578 RangeType.tp_alloc = call_PyType_GenericAlloc;
6579 RangeType.tp_new = call_PyType_GenericNew;
6580 RangeType.tp_free = call_PyObject_Free;
6581#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006582 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006583#endif
6584
Bram Moolenaara80faa82020-04-12 19:37:17 +02006585 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006586 CurrentType.tp_name = "vim.currentdata";
6587 CurrentType.tp_basicsize = sizeof(CurrentObject);
6588 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6589 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006590 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006591#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006592 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6593 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006594#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006595 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6596 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006597#endif
6598
Bram Moolenaara80faa82020-04-12 19:37:17 +02006599 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600 DictionaryType.tp_name = "vim.dictionary";
6601 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006602 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006603 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006604 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006605 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006606 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006607 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006608 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6609 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6610 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006611#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006612 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6613 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006614#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006615 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6616 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006617#endif
6618
Bram Moolenaara80faa82020-04-12 19:37:17 +02006619 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006620 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006621 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006622 ListType.tp_basicsize = sizeof(ListObject);
6623 ListType.tp_as_sequence = &ListAsSeq;
6624 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006625 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006626 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006627 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006628 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006629 ListType.tp_new = (newfunc)ListConstructor;
6630 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006631#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006632 ListType.tp_getattro = (getattrofunc)ListGetattro;
6633 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006634#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006635 ListType.tp_getattr = (getattrfunc)ListGetattr;
6636 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006637#endif
6638
Bram Moolenaara80faa82020-04-12 19:37:17 +02006639 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006640 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006641 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006642 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6643 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006644 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006645 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006646 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006647 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006648 FunctionType.tp_new = (newfunc)FunctionConstructor;
6649 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006650#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006651 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006652#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006653 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006654#endif
6655
Bram Moolenaara80faa82020-04-12 19:37:17 +02006656 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006657 OptionsType.tp_name = "vim.options";
6658 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006659 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006660 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006661 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006662 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006663 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006664 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6665 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6666 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006667
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006668#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006669 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006670 LoaderType.tp_name = "vim.Loader";
6671 LoaderType.tp_basicsize = sizeof(LoaderObject);
6672 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6673 LoaderType.tp_doc = "vim message object";
6674 LoaderType.tp_methods = LoaderMethods;
6675 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006676#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006677
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006678#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006679 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006680 vimmodule.m_name = "vim";
6681 vimmodule.m_doc = "Vim Python interface\n";
6682 vimmodule.m_size = -1;
6683 vimmodule.m_methods = VimMethods;
6684#endif
6685}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006686
6687#define PYTYPE_READY(type) \
kylo2529dac9b12022-03-27 20:05:17 +01006688 if (PyType_Ready(&(type))) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006689 return -1;
6690
6691 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006692init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006693{
6694 PYTYPE_READY(IterType);
6695 PYTYPE_READY(BufferType);
6696 PYTYPE_READY(RangeType);
6697 PYTYPE_READY(WindowType);
6698 PYTYPE_READY(TabPageType);
6699 PYTYPE_READY(BufMapType);
6700 PYTYPE_READY(WinListType);
6701 PYTYPE_READY(TabListType);
6702 PYTYPE_READY(CurrentType);
6703 PYTYPE_READY(DictionaryType);
6704 PYTYPE_READY(ListType);
6705 PYTYPE_READY(FunctionType);
6706 PYTYPE_READY(OptionsType);
6707 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006708#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006709 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006710#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006711 return 0;
6712}
6713
6714 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006715init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006716{
6717 PyObject *path;
6718 PyObject *path_hook;
6719 PyObject *path_hooks;
6720
6721 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6722 return -1;
6723
6724 if (!(path_hooks = PySys_GetObject("path_hooks")))
6725 {
6726 PyErr_Clear();
6727 path_hooks = PyList_New(1);
6728 PyList_SET_ITEM(path_hooks, 0, path_hook);
6729 if (PySys_SetObject("path_hooks", path_hooks))
6730 {
6731 Py_DECREF(path_hooks);
6732 return -1;
6733 }
6734 Py_DECREF(path_hooks);
6735 }
6736 else if (PyList_Check(path_hooks))
6737 {
6738 if (PyList_Append(path_hooks, path_hook))
6739 {
6740 Py_DECREF(path_hook);
6741 return -1;
6742 }
6743 Py_DECREF(path_hook);
6744 }
6745 else
6746 {
6747 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006748 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006749 "You should now do the following:\n"
6750 "- append vim.path_hook to sys.path_hooks\n"
6751 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006752 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006753 Py_DECREF(path_hook);
6754 return 0;
6755 }
6756
6757 if (!(path = PySys_GetObject("path")))
6758 {
6759 PyErr_Clear();
6760 path = PyList_New(1);
6761 Py_INCREF(vim_special_path_object);
6762 PyList_SET_ITEM(path, 0, vim_special_path_object);
6763 if (PySys_SetObject("path", path))
6764 {
6765 Py_DECREF(path);
6766 return -1;
6767 }
6768 Py_DECREF(path);
6769 }
6770 else if (PyList_Check(path))
6771 {
6772 if (PyList_Append(path, vim_special_path_object))
6773 return -1;
6774 }
6775 else
6776 {
6777 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006778 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006779 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006780 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006781 }
6782
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006783 return 0;
6784}
6785
6786static BufMapObject TheBufferMap =
6787{
6788 PyObject_HEAD_INIT(&BufMapType)
6789};
6790
6791static WinListObject TheWindowList =
6792{
6793 PyObject_HEAD_INIT(&WinListType)
6794 NULL
6795};
6796
6797static CurrentObject TheCurrent =
6798{
6799 PyObject_HEAD_INIT(&CurrentType)
6800};
6801
6802static TabListObject TheTabPageList =
6803{
6804 PyObject_HEAD_INIT(&TabListType)
6805};
6806
6807static struct numeric_constant {
6808 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006809 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006810} numeric_constants[] = {
6811 {"VAR_LOCKED", VAR_LOCKED},
6812 {"VAR_FIXED", VAR_FIXED},
6813 {"VAR_SCOPE", VAR_SCOPE},
6814 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6815};
6816
6817static struct object_constant {
6818 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006819 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006820} object_constants[] = {
6821 {"buffers", (PyObject *)(void *)&TheBufferMap},
6822 {"windows", (PyObject *)(void *)&TheWindowList},
6823 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6824 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006825
6826 {"Buffer", (PyObject *)&BufferType},
6827 {"Range", (PyObject *)&RangeType},
6828 {"Window", (PyObject *)&WindowType},
6829 {"TabPage", (PyObject *)&TabPageType},
6830 {"Dictionary", (PyObject *)&DictionaryType},
6831 {"List", (PyObject *)&ListType},
6832 {"Function", (PyObject *)&FunctionType},
6833 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006834#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006835 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006836#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006837};
6838
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006839#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006840 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006841 return -1;
6842
6843#define ADD_CHECKED_OBJECT(m, name, obj) \
6844 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006845 PyObject *valObject = obj; \
6846 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006847 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006848 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006849 }
6850
6851 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006852populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006853{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006854 int i;
6855 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006856 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006857 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006858#if PY_VERSION_HEX >= 0x030700f0
6859 PyObject *dict;
6860 PyObject *cls;
6861#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006862
6863 for (i = 0; i < (int)(sizeof(numeric_constants)
6864 / sizeof(struct numeric_constant));
6865 ++i)
6866 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006867 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006868
6869 for (i = 0; i < (int)(sizeof(object_constants)
6870 / sizeof(struct object_constant));
6871 ++i)
6872 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006873 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006874
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006875 valObject = object_constants[i].valObject;
6876 Py_INCREF(valObject);
6877 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006878 }
6879
6880 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6881 return -1;
6882 ADD_OBJECT(m, "error", VimError);
6883
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006884 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6885 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006886 ADD_CHECKED_OBJECT(m, "options",
6887 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006889 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006890 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006891 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006892
Bram Moolenaar22081f42016-06-01 20:38:34 +02006893#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006894 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006895 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006896#else
6897 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6898 return -1;
6899#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006900 ADD_OBJECT(m, "_getcwd", py_getcwd)
6901
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006902 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006903 return -1;
6904 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006905 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006906 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006907 if (PyObject_SetAttrString(other_module, "chdir", attr))
6908 {
6909 Py_DECREF(attr);
6910 return -1;
6911 }
6912 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006913
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006914 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006915 {
6916 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006917 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006918 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006919 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6920 {
6921 Py_DECREF(attr);
6922 return -1;
6923 }
6924 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006925 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006926 else
6927 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006928
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006929 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6930 return -1;
6931
6932 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6933
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006934#if PY_VERSION_HEX >= 0x030700f0
6935 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6936 return -1;
6937
6938 dict = PyModule_GetDict(imp);
6939
6940 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6941 {
6942 Py_DECREF(imp);
6943 return -1;
6944 }
6945
6946 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6947 {
6948 Py_DECREF(imp);
6949 return -1;
6950 }
6951
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006952 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6953 {
6954 // find_module() is deprecated, this may stop working in some later
6955 // version.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01006956 ADD_OBJECT(m, "_find_module", py_find_module);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006957 }
6958
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006959 Py_DECREF(imp);
6960
6961 ADD_OBJECT(m, "_find_spec", py_find_spec);
6962#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006963 if (!(imp = PyImport_ImportModule("imp")))
6964 return -1;
6965
6966 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6967 {
6968 Py_DECREF(imp);
6969 return -1;
6970 }
6971
6972 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6973 {
6974 Py_DECREF(py_find_module);
6975 Py_DECREF(imp);
6976 return -1;
6977 }
6978
6979 Py_DECREF(imp);
6980
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006981 ADD_OBJECT(m, "_find_module", py_find_module);
6982 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006983#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006984
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006985 return 0;
6986}