blob: 110de234fd44f0468af36a4836cc5ecb4cafd3b7 [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 Moolenaar6c87bbb2022-12-10 11:17:11 +00004084 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004085
4086 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004087 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 curwin = savewin;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004089 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004090 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 return -1;
4092
4093 return 0;
4094 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 else if (strcmp(name, "width") == 0)
4096 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004097 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 win_T *savewin;
4099
Bram Moolenaardee2e312013-06-23 16:35:47 +02004100 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 return -1;
4102
4103#ifdef FEAT_GUI
4104 need_mouse_correct = TRUE;
4105#endif
4106 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004107 curwin = self->win;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004108 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004109
4110 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004111 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 curwin = savewin;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004113 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004114 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115 return -1;
4116
4117 return 0;
4118 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 else
4120 {
4121 PyErr_SetString(PyExc_AttributeError, name);
4122 return -1;
4123 }
4124}
4125
4126 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004127WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004128{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004129 if (self->win == INVALID_WINDOW_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00004130 return PyString_FromFormat("<window object (deleted) at %p>", (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 else
4132 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004133 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004134
Bram Moolenaar6d216452013-05-12 19:00:41 +02004135 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004136 return PyString_FromFormat("<window object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00004137 (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004139 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004140 }
4141}
4142
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004143static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004144 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004145 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4146 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004147};
4148
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004149/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004150 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004151 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004152
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004153static PyTypeObject WinListType;
4154static PySequenceMethods WinListAsSeq;
4155
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004156typedef struct
4157{
4158 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004159 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004160} WinListObject;
4161
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004162 static PyObject *
4163WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004165 WinListObject *self;
4166
4167 self = PyObject_NEW(WinListObject, &WinListType);
4168 self->tabObject = tabObject;
4169 Py_INCREF(tabObject);
4170
4171 return (PyObject *)(self);
4172}
4173
4174 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004175WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004176{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004177 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004178
4179 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004180 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004181 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004182 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004183
4184 DESTRUCTOR_FINISH(self);
4185}
4186
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004187 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004188WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004189{
4190 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004191 PyInt n = 0;
4192
Bram Moolenaard6e39182013-05-21 18:30:34 +02004193 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004194 return -1;
4195
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004196 while (w != NULL)
4197 {
4198 ++n;
4199 w = W_NEXT(w);
4200 }
4201
4202 return n;
4203}
4204
4205 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004206WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004207{
4208 win_T *w;
4209
Bram Moolenaard6e39182013-05-21 18:30:34 +02004210 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004211 return NULL;
4212
4213 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004215 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004217 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218 return NULL;
4219}
4220
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004221/*
4222 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004223 *
4224 * The result is in allocated memory. All internal nulls are replaced by
4225 * newline characters. It is an error for the string to contain newline
4226 * characters.
4227 *
4228 * On errors, the Python exception data is set, and NULL is returned.
4229 */
4230 static char *
4231StringToLine(PyObject *obj)
4232{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004233 char *str;
4234 char *save;
4235 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004236 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004237 PyInt i;
4238 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004239
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004240 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004241 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004242 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4243 || str == NULL)
4244 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004245 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004246 else if (PyUnicode_Check(obj))
4247 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004248 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4249 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004250 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004251
Bram Moolenaardaa27022013-06-24 22:33:30 +02004252 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004253 || str == NULL)
4254 {
4255 Py_DECREF(bytes);
4256 return NULL;
4257 }
4258 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004259 else
4260 {
4261#if PY_MAJOR_VERSION < 3
4262 PyErr_FORMAT(PyExc_TypeError,
4263 N_("expected str() or unicode() instance, but got %s"),
4264 Py_TYPE_NAME(obj));
4265#else
4266 PyErr_FORMAT(PyExc_TypeError,
4267 N_("expected bytes() or str() instance, but got %s"),
4268 Py_TYPE_NAME(obj));
4269#endif
4270 return NULL;
4271 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004272
4273 /*
4274 * Error checking: String must not contain newlines, as we
4275 * are replacing a single line, and we must replace it with
4276 * a single line.
4277 * A trailing newline is removed, so that append(f.readlines()) works.
4278 */
4279 p = memchr(str, '\n', len);
4280 if (p != NULL)
4281 {
4282 if (p == str + len - 1)
4283 --len;
4284 else
4285 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004286 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004287 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004288 return NULL;
4289 }
4290 }
4291
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004292 /*
4293 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004294 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004295 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004296 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004297 if (save == NULL)
4298 {
4299 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004300 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004301 return NULL;
4302 }
4303
4304 for (i = 0; i < len; ++i)
4305 {
4306 if (str[i] == '\0')
4307 save[i] = '\n';
4308 else
4309 save[i] = str[i];
4310 }
4311
4312 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004313 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004314
4315 return save;
4316}
4317
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004318/*
4319 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004320 * in Vim format (1-based). The line is returned as a Python
4321 * string object.
4322 */
4323 static PyObject *
4324GetBufferLine(buf_T *buf, PyInt n)
4325{
4326 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4327}
4328
4329
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004330/*
4331 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004332 * are in Vim format (1-based). The range is from lo up to, but not
4333 * including, hi. The list is returned as a Python list of string objects.
4334 */
4335 static PyObject *
4336GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4337{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004338 PyInt i;
4339 PyInt n = hi - lo;
4340 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004341
4342 if (list == NULL)
4343 return NULL;
4344
4345 for (i = 0; i < n; ++i)
4346 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004347 linenr_T lnum = (linenr_T)(lo + i);
4348 char *text;
4349 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004350
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004351 if (lnum > buf->b_ml.ml_line_count)
4352 text = "";
4353 else
4354 text = (char *)ml_get_buf(buf, lnum, FALSE);
4355 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004356 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004357 {
4358 Py_DECREF(list);
4359 return NULL;
4360 }
4361
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004362 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004363 }
4364
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004365 // The ownership of the Python list is passed to the caller (ie,
4366 // the caller should Py_DECREF() the object when it is finished
4367 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004368
4369 return list;
4370}
4371
4372/*
4373 * Check if deleting lines made the cursor position invalid.
4374 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4375 * deleted).
4376 */
4377 static void
4378py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4379{
4380 if (curwin->w_cursor.lnum >= lo)
4381 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004382 // Adjust the cursor position if it's in/after the changed
4383 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004384 if (curwin->w_cursor.lnum >= hi)
4385 {
4386 curwin->w_cursor.lnum += extra;
4387 check_cursor_col();
4388 }
4389 else if (extra < 0)
4390 {
4391 curwin->w_cursor.lnum = lo;
4392 check_cursor();
4393 }
4394 else
4395 check_cursor_col();
4396 changed_cline_bef_curs();
4397 }
4398 invalidate_botline();
4399}
4400
Bram Moolenaar19e60942011-06-19 00:27:51 +02004401/*
4402 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004403 * in Vim format (1-based). The replacement line is given as
4404 * a Python string object. The object is checked for validity
4405 * and correct format. Errors are returned as a value of FAIL.
4406 * The return value is OK on success.
4407 * If OK is returned and len_change is not NULL, *len_change
4408 * is set to the change in the buffer length.
4409 */
4410 static int
4411SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4412{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004413 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004414 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004415
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004416 // First of all, we check the type of the supplied Python object.
4417 // There are three cases:
4418 // 1. NULL, or None - this is a deletion.
4419 // 2. A string - this is a replacement.
4420 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 if (line == Py_None || line == NULL)
4422 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004424 switchwin.sw_curwin = NULL;
4425 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004427 VimTryStart();
4428
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004430 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004431 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004432 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004433 else
4434 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00004435 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004436 || save_curbuf.br_buf == NULL))
4437 // Using an existing window for the buffer, adjust the cursor
4438 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004439 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004440 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004441 // Only adjust marks if we managed to switch to a window that
4442 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004443 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004444 }
4445
Bram Moolenaar18f47402022-01-06 13:24:51 +00004446 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004447
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004448 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004449 return FAIL;
4450
4451 if (len_change)
4452 *len_change = -1;
4453
4454 return OK;
4455 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004456 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004457 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004458 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004459
4460 if (save == NULL)
4461 return FAIL;
4462
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004463 VimTryStart();
4464
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004465 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004466 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004467 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004468
4469 if (u_savesub((linenr_T)n) == FAIL)
4470 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004471 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004472 vim_free(save);
4473 }
4474 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4475 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004476 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004477 vim_free(save);
4478 }
4479 else
4480 changed_bytes((linenr_T)n, 0);
4481
Bram Moolenaar18f47402022-01-06 13:24:51 +00004482 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004483
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004484 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004485 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004486 check_cursor_col();
4487
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004488 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004489 return FAIL;
4490
4491 if (len_change)
4492 *len_change = 0;
4493
4494 return OK;
4495 }
4496 else
4497 {
4498 PyErr_BadArgument();
4499 return FAIL;
4500 }
4501}
4502
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004503/*
4504 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004505 * Vim format (1-based). The range is from lo up to, but not including, hi.
4506 * The replacement lines are given as a Python list of string objects. The
4507 * list is checked for validity and correct format. Errors are returned as a
4508 * value of FAIL. The return value is OK on success.
4509 * If OK is returned and len_change is not NULL, *len_change
4510 * is set to the change in the buffer length.
4511 */
4512 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004513SetBufferLineList(
4514 buf_T *buf,
4515 PyInt lo,
4516 PyInt hi,
4517 PyObject *list,
4518 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004519{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004520 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004521 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004522
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004523 // First of all, we check the type of the supplied Python object.
4524 // There are three cases:
4525 // 1. NULL, or None - this is a deletion.
4526 // 2. A list - this is a replacement.
4527 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004528 if (list == Py_None || list == NULL)
4529 {
4530 PyInt i;
4531 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004532
4533 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004534 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004535 switchwin.sw_curwin = NULL;
4536 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004537
4538 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004539 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004540 else
4541 {
4542 for (i = 0; i < n; ++i)
4543 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004544 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004545 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004546 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004547 break;
4548 }
4549 }
Bram Moolenaar18f47402022-01-06 13:24:51 +00004550 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004551 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004552 // Using an existing window for the buffer, adjust the cursor
4553 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004554 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004555 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004556 // Only adjust marks if we managed to switch to a window that
4557 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004558 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004559 }
4560
Bram Moolenaar18f47402022-01-06 13:24:51 +00004561 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004562
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004563 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004564 return FAIL;
4565
4566 if (len_change)
4567 *len_change = -n;
4568
4569 return OK;
4570 }
4571 else if (PyList_Check(list))
4572 {
4573 PyInt i;
4574 PyInt new_len = PyList_Size(list);
4575 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004576 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004577 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004578
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004579 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004580 array = NULL;
4581 else
4582 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004583 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004584 if (array == NULL)
4585 {
4586 PyErr_NoMemory();
4587 return FAIL;
4588 }
4589 }
4590
4591 for (i = 0; i < new_len; ++i)
4592 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004593 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004594
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004595 if (!(line = PyList_GetItem(list, i)) ||
4596 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597 {
4598 while (i)
4599 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004600 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004601 return FAIL;
4602 }
4603 }
4604
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004605 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004606 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004607
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004608 // START of region without "return". Must call restore_buffer()!
Bram Moolenaar18f47402022-01-06 13:24:51 +00004609 switchwin.sw_curwin = NULL;
4610 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004611
4612 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004613 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004614
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004615 // If the size of the range is reducing (ie, new_len < old_len) we
4616 // need to delete some old_len. We do this at the start, by
4617 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004618 if (!PyErr_Occurred())
4619 {
4620 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004621 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004623 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004624 break;
4625 }
4626 extra -= i;
4627 }
4628
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004629 // For as long as possible, replace the existing old_len with the
4630 // new old_len. This is a more efficient operation, as it requires
4631 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004632 if (!PyErr_Occurred())
4633 {
4634 for (i = 0; i < old_len && i < new_len; ++i)
4635 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4636 == FAIL)
4637 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004638 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004639 break;
4640 }
4641 }
4642 else
4643 i = 0;
4644
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004645 // Now we may need to insert the remaining new old_len. If we do, we
4646 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004647 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004648 if (!PyErr_Occurred())
4649 {
4650 while (i < new_len)
4651 {
4652 if (ml_append((linenr_T)(lo + i - 1),
4653 (char_u *)array[i], 0, FALSE) == FAIL)
4654 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004655 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004656 break;
4657 }
4658 vim_free(array[i]);
4659 ++i;
4660 ++extra;
4661 }
4662 }
4663
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004664 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004665 while (i < new_len)
4666 {
4667 vim_free(array[i]);
4668 ++i;
4669 }
4670
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004671 // Free the array of old_len. All of its contents have now
4672 // been dealt with (either freed, or the responsibility passed
4673 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004674 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004675
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004676 // Adjust marks. Invalidate any which lie in the
4677 // changed range, and move any in the remainder of the buffer.
4678 // Only adjust marks if we managed to switch to a window that holds
4679 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004680 if (save_curbuf.br_buf == NULL)
Bram Moolenaar37233f62022-05-22 12:23:48 +01004681 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004682 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004683 (long)MAXLNUM, (long)extra);
Bram Moolenaar37233f62022-05-22 12:23:48 +01004684 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4685 }
Bram Moolenaar19e60942011-06-19 00:27:51 +02004686
Bram Moolenaar18f47402022-01-06 13:24:51 +00004687 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004688 || save_curbuf.br_buf == NULL))
4689 // Using an existing window for the buffer, adjust the cursor
4690 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004691 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4692
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004693 // END of region without "return".
Bram Moolenaar18f47402022-01-06 13:24:51 +00004694 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004695
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004696 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004697 return FAIL;
4698
4699 if (len_change)
4700 *len_change = new_len - old_len;
4701
4702 return OK;
4703 }
4704 else
4705 {
4706 PyErr_BadArgument();
4707 return FAIL;
4708 }
4709}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004710
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004711/*
4712 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004713 * The line number is in Vim format (1-based). The lines to be inserted are
4714 * given as a Python list of string objects or as a single string. The lines
4715 * to be added are checked for validity and correct format. Errors are
4716 * returned as a value of FAIL. The return value is OK on success.
4717 * If OK is returned and len_change is not NULL, *len_change
4718 * is set to the change in the buffer length.
4719 */
4720 static int
4721InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4722{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004723 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004724 switchwin_T switchwin;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004725
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004726 // First of all, we check the type of the supplied Python object.
4727 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004728 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004729 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004730 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731
4732 if (str == NULL)
4733 return FAIL;
4734
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004736 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004737 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004739 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004740 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004742 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004743 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004744 // Only adjust marks if we managed to switch to a window that
4745 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004746 appended_lines_mark((linenr_T)n, 1L);
4747
4748 vim_free(str);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004749 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004750 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004751
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004752 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004753 return FAIL;
4754
4755 if (len_change)
4756 *len_change = 1;
4757
4758 return OK;
4759 }
4760 else if (PyList_Check(lines))
4761 {
4762 PyInt i;
4763 PyInt size = PyList_Size(lines);
4764 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004765
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004766 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004767 if (array == NULL)
4768 {
4769 PyErr_NoMemory();
4770 return FAIL;
4771 }
4772
4773 for (i = 0; i < size; ++i)
4774 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004775 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004776
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004777 if (!(line = PyList_GetItem(lines, i)) ||
4778 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779 {
4780 while (i)
4781 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004782 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 return FAIL;
4784 }
4785 }
4786
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004787 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004788 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004789 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004790
4791 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004792 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004793 else
4794 {
4795 for (i = 0; i < size; ++i)
4796 {
4797 if (ml_append((linenr_T)(n + i),
4798 (char_u *)array[i], 0, FALSE) == FAIL)
4799 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004800 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004801
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004802 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004803 while (i < size)
4804 vim_free(array[i++]);
4805
4806 break;
4807 }
4808 vim_free(array[i]);
4809 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004810 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004811 // Only adjust marks if we managed to switch to a window that
4812 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004813 appended_lines_mark((linenr_T)n, (long)i);
4814 }
4815
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004816 // Free the array of lines. All of its contents have now
4817 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004818 PyMem_Free(array);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004819 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004820
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004821 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004822
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004823 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004824 return FAIL;
4825
4826 if (len_change)
4827 *len_change = size;
4828
4829 return OK;
4830 }
4831 else
4832 {
4833 PyErr_BadArgument();
4834 return FAIL;
4835 }
4836}
4837
4838/*
4839 * Common routines for buffers and line ranges
4840 * -------------------------------------------
4841 */
4842
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004843typedef struct
4844{
4845 PyObject_HEAD
4846 buf_T *buf;
4847} BufferObject;
4848
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004849 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004850CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004851{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004852 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004853 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004854 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004855 return -1;
4856 }
4857
4858 return 0;
4859}
4860
4861 static PyObject *
4862RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4863{
4864 if (CheckBuffer(self))
4865 return NULL;
4866
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004867 if (end == -1)
4868 end = self->buf->b_ml.ml_line_count;
4869
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004870 if (n < 0)
4871 n += end - start + 1;
4872
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004873 if (n < 0 || n > end - start)
4874 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004875 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004876 return NULL;
4877 }
4878
4879 return GetBufferLine(self->buf, n+start);
4880}
4881
4882 static PyObject *
4883RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4884{
4885 PyInt size;
4886
4887 if (CheckBuffer(self))
4888 return NULL;
4889
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004890 if (end == -1)
4891 end = self->buf->b_ml.ml_line_count;
4892
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004893 size = end - start + 1;
4894
4895 if (lo < 0)
4896 lo = 0;
4897 else if (lo > size)
4898 lo = size;
4899 if (hi < 0)
4900 hi = 0;
4901 if (hi < lo)
4902 hi = lo;
4903 else if (hi > size)
4904 hi = size;
4905
4906 return GetBufferLineList(self->buf, lo+start, hi+start);
4907}
4908
4909 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004910RBAsItem(
4911 BufferObject *self,
4912 PyInt n,
4913 PyObject *valObject,
4914 PyInt start,
4915 PyInt end,
4916 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004917{
4918 PyInt len_change;
4919
4920 if (CheckBuffer(self))
4921 return -1;
4922
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004923 if (end == -1)
4924 end = self->buf->b_ml.ml_line_count;
4925
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004926 if (n < 0)
4927 n += end - start + 1;
4928
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004929 if (n < 0 || n > end - start)
4930 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004931 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004932 return -1;
4933 }
4934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004935 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004936 return -1;
4937
4938 if (new_end)
4939 *new_end = end + len_change;
4940
4941 return 0;
4942}
4943
Bram Moolenaar19e60942011-06-19 00:27:51 +02004944 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004945RBAsSlice(
4946 BufferObject *self,
4947 PyInt lo,
4948 PyInt hi,
4949 PyObject *valObject,
4950 PyInt start,
4951 PyInt end,
4952 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004953{
4954 PyInt size;
4955 PyInt len_change;
4956
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004957 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004958 if (CheckBuffer(self))
4959 return -1;
4960
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004961 if (end == -1)
4962 end = self->buf->b_ml.ml_line_count;
4963
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004964 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004965 size = end - start + 1;
4966
4967 if (lo < 0)
4968 lo = 0;
4969 else if (lo > size)
4970 lo = size;
4971 if (hi < 0)
4972 hi = 0;
4973 if (hi < lo)
4974 hi = lo;
4975 else if (hi > size)
4976 hi = size;
4977
4978 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004979 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004980 return -1;
4981
4982 if (new_end)
4983 *new_end = end + len_change;
4984
4985 return 0;
4986}
4987
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004988
4989 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004990RBAppend(
4991 BufferObject *self,
4992 PyObject *args,
4993 PyInt start,
4994 PyInt end,
4995 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004996{
4997 PyObject *lines;
4998 PyInt len_change;
4999 PyInt max;
5000 PyInt n;
5001
5002 if (CheckBuffer(self))
5003 return NULL;
5004
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005005 if (end == -1)
5006 end = self->buf->b_ml.ml_line_count;
5007
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005008 max = n = end - start + 1;
5009
5010 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5011 return NULL;
5012
5013 if (n < 0 || n > max)
5014 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005015 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005016 return NULL;
5017 }
5018
5019 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5020 return NULL;
5021
5022 if (new_end)
5023 *new_end = end + len_change;
5024
5025 Py_INCREF(Py_None);
5026 return Py_None;
5027}
5028
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005029// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005030
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005031static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005032static PySequenceMethods RangeAsSeq;
5033static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005034
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005035typedef struct
5036{
5037 PyObject_HEAD
5038 BufferObject *buf;
5039 PyInt start;
5040 PyInt end;
5041} RangeObject;
5042
5043 static PyObject *
5044RangeNew(buf_T *buf, PyInt start, PyInt end)
5045{
5046 BufferObject *bufr;
5047 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005048 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005049 if (self == NULL)
5050 return NULL;
5051
5052 bufr = (BufferObject *)BufferNew(buf);
5053 if (bufr == NULL)
5054 {
5055 Py_DECREF(self);
5056 return NULL;
5057 }
5058 Py_INCREF(bufr);
5059
5060 self->buf = bufr;
5061 self->start = start;
5062 self->end = end;
5063
5064 return (PyObject *)(self);
5065}
5066
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005067 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005068RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005069{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005070 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005071 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005072 PyObject_GC_Del((void *)(self));
5073}
5074
5075 static int
5076RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5077{
5078 Py_VISIT(((PyObject *)(self->buf)));
5079 return 0;
5080}
5081
5082 static int
5083RangeClear(RangeObject *self)
5084{
5085 Py_CLEAR(self->buf);
5086 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005087}
5088
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005090RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005092 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005093 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005094 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097}
5098
5099 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005100RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103}
5104
5105 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005106RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005108 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005109}
5110
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005111static char *RangeAttrs[] = {
5112 "start", "end",
5113 NULL
5114};
5115
5116 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005117RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005118{
5119 return ObjectDir(self, RangeAttrs);
5120}
5121
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005123RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005125 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005126}
5127
5128 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005129RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005130{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005131 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005132 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00005133 (void *)self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005134 else
5135 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005136 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005137
5138 if (name == NULL)
5139 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005140
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005141 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005142 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005143 }
5144}
5145
5146static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005147 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005148 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005149 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5150 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005151};
5152
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005153static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005154static PySequenceMethods BufferAsSeq;
5155static PyMappingMethods BufferAsMapping;
5156
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005157 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005158BufferNew(buf_T *buf)
5159{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005160 /*
5161 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005162 * If we add a "b_python*_ref" field to the buf_T structure,
5163 * then we can get at it in buf_freeall() in vim. We then
5164 * need to create only ONE Python object per buffer - if
5165 * we try to create a second, just INCREF the existing one
5166 * and return it. The (single) Python object referring to
5167 * the buffer is stored in "b_python*_ref".
5168 * Question: what to do on a buf_freeall(). We'll probably
5169 * have to either delete the Python object (DECREF it to
5170 * zero - a bad idea, as it leaves dangling refs!) or
5171 * set the buf_T * value to an invalid value (-1?), which
5172 * means we need checks in all access functions... Bah.
5173 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005174 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005175 * b_python_ref and b_python3_ref fields respectively.
5176 */
5177
5178 BufferObject *self;
5179
5180 if (BUF_PYTHON_REF(buf) != NULL)
5181 {
5182 self = BUF_PYTHON_REF(buf);
5183 Py_INCREF(self);
5184 }
5185 else
5186 {
5187 self = PyObject_NEW(BufferObject, &BufferType);
5188 if (self == NULL)
5189 return NULL;
5190 self->buf = buf;
5191 BUF_PYTHON_REF(buf) = self;
5192 }
5193
5194 return (PyObject *)(self);
5195}
5196
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005197 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005198BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005199{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005200 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5201 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005202
5203 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005204}
5205
Bram Moolenaar971db462013-05-12 18:44:48 +02005206 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005207BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005208{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005209 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005210 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005211 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005212
Bram Moolenaard6e39182013-05-21 18:30:34 +02005213 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005214}
5215
5216 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005217BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005218{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005219 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005220}
5221
5222 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005223BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005224{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005225 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005226}
5227
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005228static char *BufferAttrs[] = {
5229 "name", "number", "vars", "options", "valid",
5230 NULL
5231};
5232
5233 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005234BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005235{
5236 return ObjectDir(self, BufferAttrs);
5237}
5238
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005239 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005240BufferAttrValid(BufferObject *self, char *name)
5241{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005242 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005243
5244 if (strcmp(name, "valid") != 0)
5245 return NULL;
5246
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005247 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5248 Py_INCREF(ret);
5249 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005250}
5251
5252 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005253BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005254{
5255 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005256 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005257 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005259 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005260 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005261 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005262 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005263 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5264 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005265 else if (strcmp(name, "__members__") == 0)
5266 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005267 else
5268 return NULL;
5269}
5270
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005271 static int
5272BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5273{
5274 if (CheckBuffer(self))
5275 return -1;
5276
5277 if (strcmp(name, "name") == 0)
5278 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005279 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005280 aco_save_T aco;
Bram Moolenaar37199892022-11-29 13:46:48 +00005281 int ren_ret = OK;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005282 PyObject *todecref;
5283
5284 if (!(val = StringToChars(valObject, &todecref)))
5285 return -1;
5286
5287 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005288 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005289 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00005290 if (curbuf == self->buf)
5291 {
5292 ren_ret = rename_buffer(val);
5293 aucmd_restbuf(&aco);
5294 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005295 Py_XDECREF(todecref);
5296 if (VimTryEnd())
5297 return -1;
5298
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005299 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005300 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005301 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005302 return -1;
5303 }
5304 return 0;
5305 }
5306 else
5307 {
5308 PyErr_SetString(PyExc_AttributeError, name);
5309 return -1;
5310 }
5311}
5312
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005313 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005314BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005316 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005317}
5318
5319 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005320BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321{
5322 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005323 char_u *pmark;
5324 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005325 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005326 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005327
Bram Moolenaard6e39182013-05-21 18:30:34 +02005328 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005329 return NULL;
5330
Bram Moolenaar389a1792013-06-23 13:00:44 +02005331 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005332 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005333
Bram Moolenaar389a1792013-06-23 13:00:44 +02005334 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005335 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005336 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005337 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005338 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005339 return NULL;
5340 }
5341
5342 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005343
5344 Py_XDECREF(todecref);
5345
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005346 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005347 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005348 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005349 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005350 if (VimTryEnd())
5351 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005352
5353 if (posp == NULL)
5354 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005355 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005356 return NULL;
5357 }
5358
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005359 if (posp->lnum <= 0)
5360 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005361 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005362 Py_INCREF(Py_None);
5363 return Py_None;
5364 }
5365
5366 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5367}
5368
5369 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005370BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005371{
5372 PyInt start;
5373 PyInt end;
5374
Bram Moolenaard6e39182013-05-21 18:30:34 +02005375 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005376 return NULL;
5377
5378 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5379 return NULL;
5380
Bram Moolenaard6e39182013-05-21 18:30:34 +02005381 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005382}
5383
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005384 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005385BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005386{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005387 if (self->buf == INVALID_BUFFER_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00005388 return PyString_FromFormat("<buffer object (deleted) at %p>", (void *)self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005389 else
5390 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005391 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005392
5393 if (name == NULL)
5394 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005395
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005396 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005397 }
5398}
5399
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005400static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005401 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005402 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005403 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005404 {"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 +02005405 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5406 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005407};
5408
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005409/*
5410 * Buffer list object - Implementation
5411 */
5412
5413static PyTypeObject BufMapType;
5414
5415typedef struct
5416{
5417 PyObject_HEAD
5418} BufMapObject;
5419
5420 static PyInt
5421BufMapLength(PyObject *self UNUSED)
5422{
5423 buf_T *b = firstbuf;
5424 PyInt n = 0;
5425
5426 while (b)
5427 {
5428 ++n;
5429 b = b->b_next;
5430 }
5431
5432 return n;
5433}
5434
5435 static PyObject *
5436BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5437{
5438 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005439 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005440
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005441 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005442 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005443
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005444 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005445
5446 if (b)
5447 return BufferNew(b);
5448 else
5449 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005450 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005451 return NULL;
5452 }
5453}
5454
5455 static void
5456BufMapIterDestruct(PyObject *buffer)
5457{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005458 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005459 if (buffer)
5460 {
5461 Py_DECREF(buffer);
5462 }
5463}
5464
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005465 static int
5466BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5467{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005468 if (buffer)
5469 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005470 return 0;
5471}
5472
5473 static int
5474BufMapIterClear(PyObject **buffer)
5475{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005476 if (*buffer)
5477 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005478 return 0;
5479}
5480
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005481 static PyObject *
5482BufMapIterNext(PyObject **buffer)
5483{
5484 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005485 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005486
5487 if (!*buffer)
5488 return NULL;
5489
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005490 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005491
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005493 {
5494 *buffer = NULL;
5495 return NULL;
5496 }
5497
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005498 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005499 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005500 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005501 return NULL;
5502 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005503 // Do not increment reference: we no longer hold it (decref), but whoever
5504 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005505 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005506}
5507
5508 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005509BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005510{
5511 PyObject *buffer;
5512
5513 buffer = BufferNew(firstbuf);
5514 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005515 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005516 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5517 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005518}
5519
5520static PyMappingMethods BufMapAsMapping = {
5521 (lenfunc) BufMapLength,
5522 (binaryfunc) BufMapItem,
5523 (objobjargproc) 0,
5524};
5525
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005526// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005527
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005528static char *CurrentAttrs[] = {
5529 "buffer", "window", "line", "range", "tabpage",
5530 NULL
5531};
5532
5533 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005534CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005535{
5536 return ObjectDir(self, CurrentAttrs);
5537}
5538
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005539 static PyObject *
5540CurrentGetattr(PyObject *self UNUSED, char *name)
5541{
5542 if (strcmp(name, "buffer") == 0)
5543 return (PyObject *)BufferNew(curbuf);
5544 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005545 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005546 else if (strcmp(name, "tabpage") == 0)
5547 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005548 else if (strcmp(name, "line") == 0)
5549 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5550 else if (strcmp(name, "range") == 0)
5551 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005552 else if (strcmp(name, "__members__") == 0)
5553 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005554 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005555#if PY_MAJOR_VERSION < 3
5556 return Py_FindMethod(WindowMethods, self, name);
5557#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005558 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005559#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005560}
5561
5562 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005564{
5565 if (strcmp(name, "line") == 0)
5566 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005567 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5568 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005569 return -1;
5570
5571 return 0;
5572 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005573 else if (strcmp(name, "buffer") == 0)
5574 {
5575 int count;
5576
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005578 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005579 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005580 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 return -1;
5583 }
5584
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005585 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005586 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005587 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005588
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005589 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005590 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5591 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005592 if (VimTryEnd())
5593 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005594 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005595 return -1;
5596 }
5597
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005598 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005599 }
5600 else if (strcmp(name, "window") == 0)
5601 {
5602 int count;
5603
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005604 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005605 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005606 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005607 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005608 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005609 return -1;
5610 }
5611
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005612 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005613 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005614 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005615
5616 if (!count)
5617 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005618 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005619 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 return -1;
5621 }
5622
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005623 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005624 win_goto(((WindowObject *)(valObject))->win);
5625 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005626 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005627 if (VimTryEnd())
5628 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005629 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005630 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005631 return -1;
5632 }
5633
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005634 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005635 }
5636 else if (strcmp(name, "tabpage") == 0)
5637 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005638 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005639 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005640 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005641 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005642 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005643 return -1;
5644 }
5645
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005646 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005647 return -1;
5648
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005649 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005650 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5651 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005652 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005653 if (VimTryEnd())
5654 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005655 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005656 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005657 return -1;
5658 }
5659
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005660 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005661 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005662 else
5663 {
5664 PyErr_SetString(PyExc_AttributeError, name);
5665 return -1;
5666 }
5667}
5668
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005669static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005670 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005671 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5672 { NULL, NULL, 0, NULL}
5673};
5674
Bram Moolenaardb913952012-06-29 12:54:53 +02005675 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005676init_range_cmd(exarg_T *eap)
5677{
5678 RangeStart = eap->line1;
5679 RangeEnd = eap->line2;
5680}
5681
5682 static void
5683init_range_eval(typval_T *rettv UNUSED)
5684{
5685 RangeStart = (PyInt) curwin->w_cursor.lnum;
5686 RangeEnd = RangeStart;
5687}
5688
5689 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005690run_cmd(const char *cmd, void *arg UNUSED
5691#ifdef PY_CAN_RECURSE
5692 , PyGILState_STATE *pygilstate UNUSED
5693#endif
5694 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005695{
Bram Moolenaar41009372013-07-01 22:03:04 +02005696 PyObject *run_ret;
5697 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5698 if (run_ret != NULL)
5699 {
5700 Py_DECREF(run_ret);
5701 }
5702 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5703 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005704 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005705 PyErr_Clear();
5706 }
5707 else
5708 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005709}
5710
5711static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5712static int code_hdr_len = 30;
5713
5714 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005715run_do(const char *cmd, void *arg UNUSED
5716#ifdef PY_CAN_RECURSE
5717 , PyGILState_STATE *pygilstate
5718#endif
5719 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005720{
5721 PyInt lnum;
5722 size_t len;
5723 char *code;
5724 int status;
5725 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005726 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005727 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005728
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005729 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005731 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005732 return;
5733 }
5734
5735 len = code_hdr_len + STRLEN(cmd);
5736 code = PyMem_New(char, len + 1);
5737 memcpy(code, code_hdr, code_hdr_len);
5738 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005739 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5740 status = -1;
5741 if (run_ret != NULL)
5742 {
5743 status = 0;
5744 Py_DECREF(run_ret);
5745 }
5746 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5747 {
5748 PyMem_Free(code);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005749 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005750 PyErr_Clear();
5751 return;
5752 }
5753 else
5754 PyErr_PrintEx(1);
5755
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005756 PyMem_Free(code);
5757
5758 if (status)
5759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005760 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005761 return;
5762 }
5763
5764 status = 0;
5765 pymain = PyImport_AddModule("__main__");
5766 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005767#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005768 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005769#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005770
5771 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5772 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005773 PyObject *line;
5774 PyObject *linenr;
5775 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005776
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005777#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005778 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005779#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005780 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005781 if (lnum > curbuf->b_ml.ml_line_count
5782 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005783 goto err;
5784 if (!(linenr = PyInt_FromLong((long) lnum)))
5785 {
5786 Py_DECREF(line);
5787 goto err;
5788 }
5789 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5790 Py_DECREF(line);
5791 Py_DECREF(linenr);
5792 if (!ret)
5793 goto err;
5794
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005795 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005796 if (curbuf != was_curbuf)
5797 {
5798 Py_XDECREF(ret);
5799 goto err;
5800 }
5801
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005802 if (ret != Py_None)
5803 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005804 {
5805 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005807 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005808
5809 Py_XDECREF(ret);
5810 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005811#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005812 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005813#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005814 }
5815 goto out;
5816err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005817#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005818 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005819#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005820 PyErr_PrintEx(0);
5821 PythonIO_Flush();
5822 status = 1;
5823out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005824#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005825 if (!status)
5826 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005827#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005828 Py_DECREF(pyfunc);
5829 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5830 if (status)
5831 return;
5832 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005833 update_curbuf(UPD_NOT_VALID);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005834}
5835
5836 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005837run_eval(const char *cmd, typval_T *rettv
5838#ifdef PY_CAN_RECURSE
5839 , PyGILState_STATE *pygilstate UNUSED
5840#endif
5841 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005842{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005843 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005844
Bram Moolenaar41009372013-07-01 22:03:04 +02005845 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005846 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005847 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005848 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005849 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005850 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005851 PyErr_Clear();
5852 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005853 else
5854 {
5855 if (PyErr_Occurred() && !msg_silent)
5856 PyErr_PrintEx(0);
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005857 emsg(_(e_eval_did_not_return_valid_python_object));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005858 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005859 }
5860 else
5861 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005862 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005863 emsg(_(e_failed_to_convert_returned_python_object_to_vim_value));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005864 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005865 }
5866 PyErr_Clear();
5867}
5868
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005869 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005870set_ref_in_py(const int copyID)
5871{
5872 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005873 list_T *ll;
5874 int i;
5875 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005876 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005877
5878 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005879 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005880 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005881 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5882 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005883 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005884
5885 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005886 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005887 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005888 {
5889 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005890 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005891 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005892 }
5893
Bram Moolenaar8110a092016-04-14 15:56:09 +02005894 if (lastfunc != NULL)
5895 {
5896 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5897 {
5898 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005899 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005900 if (func->argc)
5901 for (i = 0; !abort && i < func->argc; ++i)
5902 abort = abort
5903 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5904 }
5905 }
5906
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005907 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005908}
5909
5910 static int
5911set_string_copy(char_u *str, typval_T *tv)
5912{
5913 tv->vval.v_string = vim_strsave(str);
5914 if (tv->vval.v_string == NULL)
5915 {
5916 PyErr_NoMemory();
5917 return -1;
5918 }
5919 return 0;
5920}
5921
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005923pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005924{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005925 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926 char_u *key;
5927 dictitem_T *di;
5928 PyObject *keyObject;
5929 PyObject *valObject;
5930 Py_ssize_t iter = 0;
5931
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005932 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005933 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934
5935 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005936 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937
5938 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5939 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005940 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941
Bram Moolenaara03e6312013-05-29 22:49:26 +02005942 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005943 {
5944 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005945 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005946 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005947
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005948 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005949 {
5950 dict_unref(dict);
5951 return -1;
5952 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005953
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005954 if (*key == NUL)
5955 {
5956 dict_unref(dict);
5957 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005958 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005959 return -1;
5960 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961
5962 di = dictitem_alloc(key);
5963
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005964 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965
5966 if (di == NULL)
5967 {
5968 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005969 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 return -1;
5971 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005972
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005973 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005974 {
5975 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005976 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 return -1;
5978 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005979
5980 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005982 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005983 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005984 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005986 return -1;
5987 }
5988 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005989
5990 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005991 return 0;
5992}
5993
5994 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005995pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005996{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005997 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005998 char_u *key;
5999 dictitem_T *di;
6000 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006001 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006002 PyObject *keyObject;
6003 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006004
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006005 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006006 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006007
6008 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006009 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006010
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006011 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006012 {
6013 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006014 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006015 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006016
6017 if (!(iterator = PyObject_GetIter(list)))
6018 {
6019 dict_unref(dict);
6020 Py_DECREF(list);
6021 return -1;
6022 }
6023 Py_DECREF(list);
6024
6025 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006026 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006027 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006028
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006029 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006030 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006031 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006032 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006033 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006034 return -1;
6035 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006036
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006037 if (*key == NUL)
6038 {
6039 Py_DECREF(keyObject);
6040 Py_DECREF(iterator);
6041 Py_XDECREF(todecref);
6042 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006043 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006044 return -1;
6045 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006046
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006047 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006048 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006049 Py_DECREF(keyObject);
6050 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006051 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006052 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006053 return -1;
6054 }
6055
6056 di = dictitem_alloc(key);
6057
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006058 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006059 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 if (di == NULL)
6062 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006063 Py_DECREF(iterator);
6064 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006065 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006066 PyErr_NoMemory();
6067 return -1;
6068 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006069
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006070 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006072 Py_DECREF(iterator);
6073 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006074 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006075 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076 return -1;
6077 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006078
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006079 Py_DECREF(valObject);
6080
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006081 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006082 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006083 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006084 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006085 dictitem_free(di);
6086 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006087 return -1;
6088 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006089 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006090 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006091 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006092 return 0;
6093}
6094
6095 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006096pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006097{
6098 list_T *l;
6099
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006100 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006101 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006102
6103 tv->v_type = VAR_LIST;
6104 tv->vval.v_list = l;
6105
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006106 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006107 {
6108 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006109 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006110 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006111
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006112 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006113 return 0;
6114}
6115
Bram Moolenaardb913952012-06-29 12:54:53 +02006116typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6117
6118 static int
6119convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006120 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006121{
6122 PyObject *capsule;
6123 char hexBuf[sizeof(void *) * 2 + 3];
6124
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006125 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006126
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006127#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006128 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006129#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006130 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006131#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006132 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006133 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006134#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006135 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006136#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006137 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006138#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006139 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6140 {
6141 Py_DECREF(capsule);
6142 tv->v_type = VAR_UNKNOWN;
6143 return -1;
6144 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006145
6146 Py_DECREF(capsule);
6147
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006148 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006149 {
6150 tv->v_type = VAR_UNKNOWN;
6151 return -1;
6152 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006153 // As we are not using copy_tv which increments reference count we must
6154 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006155 if (tv->v_type == VAR_DICT)
6156 ++tv->vval.v_dict->dv_refcount;
6157 else if (tv->v_type == VAR_LIST)
6158 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006159 }
6160 else
6161 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006162 typval_T *v;
6163
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006164#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006165 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006166#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006167 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006168#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006169 copy_tv(v, tv);
6170 }
6171 return 0;
6172}
6173
6174 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006175ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6176{
6177 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006178 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006179
6180 if (!(lookup_dict = PyDict_New()))
6181 return -1;
6182
6183 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6184 {
6185 tv->v_type = VAR_DICT;
6186 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6187 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006188 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006189 }
6190 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006191 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006192 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006193 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006194 else
6195 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006196 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006197 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006198 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006199 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006200 }
6201 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006202 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006203}
6204
6205 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006206ConvertFromPySequence(PyObject *obj, typval_T *tv)
6207{
6208 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006209 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006210
6211 if (!(lookup_dict = PyDict_New()))
6212 return -1;
6213
6214 if (PyType_IsSubtype(obj->ob_type, &ListType))
6215 {
6216 tv->v_type = VAR_LIST;
6217 tv->vval.v_list = (((ListObject *)(obj))->list);
6218 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006219 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006220 }
6221 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006222 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006223 else
6224 {
6225 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006226 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006227 Py_TYPE_NAME(obj));
6228 ret = -1;
6229 }
6230 Py_DECREF(lookup_dict);
6231 return ret;
6232}
6233
6234 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006235ConvertFromPyObject(PyObject *obj, typval_T *tv)
6236{
6237 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006238 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006239
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006240 if (!(lookup_dict = PyDict_New()))
6241 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006242 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006243 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006244 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006245}
6246
6247 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006248_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006249{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006250 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 {
6252 tv->v_type = VAR_DICT;
6253 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6254 ++tv->vval.v_dict->dv_refcount;
6255 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006256 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 {
6258 tv->v_type = VAR_LIST;
6259 tv->vval.v_list = (((ListObject *)(obj))->list);
6260 ++tv->vval.v_list->lv_refcount;
6261 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006262 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006263 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006264 FunctionObject *func = (FunctionObject *) obj;
6265 if (func->self != NULL || func->argv != NULL)
6266 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006267 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6268
Bram Moolenaar8110a092016-04-14 15:56:09 +02006269 set_partial(func, pt, TRUE);
6270 tv->vval.v_partial = pt;
6271 tv->v_type = VAR_PARTIAL;
6272 }
6273 else
6274 {
6275 if (set_string_copy(func->name, tv) == -1)
6276 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006277
Bram Moolenaar8110a092016-04-14 15:56:09 +02006278 tv->v_type = VAR_FUNC;
6279 }
6280 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006281 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006282 else if (PyBytes_Check(obj))
6283 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006284 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006286 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006287 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006288 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 return -1;
6290
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006291 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 return -1;
6293
6294 tv->v_type = VAR_STRING;
6295 }
6296 else if (PyUnicode_Check(obj))
6297 {
6298 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006299 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006300
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006301 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006302 if (bytes == NULL)
6303 return -1;
6304
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006305 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006306 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006307 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006308 return -1;
6309
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006310 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006311 {
6312 Py_XDECREF(bytes);
6313 return -1;
6314 }
6315 Py_XDECREF(bytes);
6316
6317 tv->v_type = VAR_STRING;
6318 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006319#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006320 else if (PyInt_Check(obj))
6321 {
6322 tv->v_type = VAR_NUMBER;
6323 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006324 if (PyErr_Occurred())
6325 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006326 }
6327#endif
6328 else if (PyLong_Check(obj))
6329 {
6330 tv->v_type = VAR_NUMBER;
6331 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006332 if (PyErr_Occurred())
6333 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006334 }
6335 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006336 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006337 else if (PyFloat_Check(obj))
6338 {
6339 tv->v_type = VAR_FLOAT;
6340 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6341 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006342 else if (PyObject_HasAttrString(obj, "keys"))
6343 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006344 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006345 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006346 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006347 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006348 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006349 else if (PyNumber_Check(obj))
6350 {
6351 PyObject *num;
6352
6353 if (!(num = PyNumber_Long(obj)))
6354 return -1;
6355
6356 tv->v_type = VAR_NUMBER;
6357 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6358
6359 Py_DECREF(num);
6360 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006361 else if (obj == Py_None)
6362 {
6363 tv->v_type = VAR_SPECIAL;
6364 tv->vval.v_number = VVAL_NONE;
6365 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006366 else
6367 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006368 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006369 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006370 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006371 return -1;
6372 }
6373 return 0;
6374}
6375
6376 static PyObject *
6377ConvertToPyObject(typval_T *tv)
6378{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006379 typval_T *argv;
6380 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006381 if (tv == NULL)
6382 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006383 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006384 return NULL;
6385 }
6386 switch (tv->v_type)
6387 {
6388 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006389 return PyBytes_FromString(tv->vval.v_string == NULL
6390 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006391 case VAR_NUMBER:
6392 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006393 case VAR_FLOAT:
6394 return PyFloat_FromDouble((double) tv->vval.v_float);
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006396 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006397 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006398 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006399 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006400 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006401 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006402 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006403 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006404 if (tv->vval.v_partial->pt_argc)
6405 {
6406 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6407 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6408 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6409 }
6410 else
6411 argv = NULL;
6412 if (tv->vval.v_partial->pt_dict != NULL)
6413 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006414 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006415 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006416 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006417 tv->vval.v_partial->pt_dict,
6418 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006419 case VAR_BLOB:
6420 return PyBytes_FromStringAndSize(
6421 (char*) tv->vval.v_blob->bv_ga.ga_data,
6422 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006423 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006424 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006426 case VAR_CHANNEL:
6427 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006428 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006429 case VAR_CLASS:
6430 case VAR_OBJECT:
Bram Moolenaardb913952012-06-29 12:54:53 +02006431 Py_INCREF(Py_None);
6432 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006433 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006434 case VAR_SPECIAL:
6435 switch (tv->vval.v_number)
6436 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006437 case VVAL_FALSE: return ALWAYS_FALSE;
6438 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006439 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006440 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006441 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006442 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006443 return NULL;
6444 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006445 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006446}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006447
6448typedef struct
6449{
6450 PyObject_HEAD
6451} CurrentObject;
6452static PyTypeObject CurrentType;
6453
6454 static void
6455init_structs(void)
6456{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006457 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006458 OutputType.tp_name = "vim.message";
6459 OutputType.tp_basicsize = sizeof(OutputObject);
6460 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6461 OutputType.tp_doc = "vim message object";
6462 OutputType.tp_methods = OutputMethods;
6463#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006464 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6465 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006466 OutputType.tp_alloc = call_PyType_GenericAlloc;
6467 OutputType.tp_new = call_PyType_GenericNew;
6468 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006469 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006470#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006471 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6472 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006473 // Disabled, because this causes a crash in test86
6474 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006475#endif
6476
Bram Moolenaara80faa82020-04-12 19:37:17 +02006477 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006478 IterType.tp_name = "vim.iter";
6479 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006480 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006481 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006482 IterType.tp_iter = (getiterfunc)IterIter;
6483 IterType.tp_iternext = (iternextfunc)IterNext;
6484 IterType.tp_dealloc = (destructor)IterDestructor;
6485 IterType.tp_traverse = (traverseproc)IterTraverse;
6486 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006487
Bram Moolenaara80faa82020-04-12 19:37:17 +02006488 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006489 BufferType.tp_name = "vim.buffer";
6490 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006491 BufferType.tp_dealloc = (destructor)BufferDestructor;
6492 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006493 BufferType.tp_as_sequence = &BufferAsSeq;
6494 BufferType.tp_as_mapping = &BufferAsMapping;
6495 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6496 BufferType.tp_doc = "vim buffer object";
6497 BufferType.tp_methods = BufferMethods;
6498#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006499 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006500 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006501 BufferType.tp_alloc = call_PyType_GenericAlloc;
6502 BufferType.tp_new = call_PyType_GenericNew;
6503 BufferType.tp_free = call_PyObject_Free;
6504#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006506 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006507#endif
6508
Bram Moolenaara80faa82020-04-12 19:37:17 +02006509 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006510 WindowType.tp_name = "vim.window";
6511 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006512 WindowType.tp_dealloc = (destructor)WindowDestructor;
6513 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006514 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006515 WindowType.tp_doc = "vim Window object";
6516 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006517 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6518 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006519#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006520 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6521 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006522 WindowType.tp_alloc = call_PyType_GenericAlloc;
6523 WindowType.tp_new = call_PyType_GenericNew;
6524 WindowType.tp_free = call_PyObject_Free;
6525#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006526 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6527 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006528#endif
6529
Bram Moolenaara80faa82020-04-12 19:37:17 +02006530 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006531 TabPageType.tp_name = "vim.tabpage";
6532 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006533 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6534 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006535 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6536 TabPageType.tp_doc = "vim tab page object";
6537 TabPageType.tp_methods = TabPageMethods;
6538#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006539 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006540 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6541 TabPageType.tp_new = call_PyType_GenericNew;
6542 TabPageType.tp_free = call_PyObject_Free;
6543#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006544 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006545#endif
6546
Bram Moolenaara80faa82020-04-12 19:37:17 +02006547 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006548 BufMapType.tp_name = "vim.bufferlist";
6549 BufMapType.tp_basicsize = sizeof(BufMapObject);
6550 BufMapType.tp_as_mapping = &BufMapAsMapping;
6551 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006552 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006553 BufferType.tp_doc = "vim buffer list";
6554
Bram Moolenaara80faa82020-04-12 19:37:17 +02006555 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006556 WinListType.tp_name = "vim.windowlist";
6557 WinListType.tp_basicsize = sizeof(WinListType);
6558 WinListType.tp_as_sequence = &WinListAsSeq;
6559 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6560 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006561 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006562
Bram Moolenaara80faa82020-04-12 19:37:17 +02006563 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006564 TabListType.tp_name = "vim.tabpagelist";
6565 TabListType.tp_basicsize = sizeof(TabListType);
6566 TabListType.tp_as_sequence = &TabListAsSeq;
6567 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6568 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006569
Bram Moolenaara80faa82020-04-12 19:37:17 +02006570 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006571 RangeType.tp_name = "vim.range";
6572 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006573 RangeType.tp_dealloc = (destructor)RangeDestructor;
6574 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006575 RangeType.tp_as_sequence = &RangeAsSeq;
6576 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006577 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006578 RangeType.tp_doc = "vim Range object";
6579 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006580 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6581 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006582#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006583 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006584 RangeType.tp_alloc = call_PyType_GenericAlloc;
6585 RangeType.tp_new = call_PyType_GenericNew;
6586 RangeType.tp_free = call_PyObject_Free;
6587#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006588 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006589#endif
6590
Bram Moolenaara80faa82020-04-12 19:37:17 +02006591 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592 CurrentType.tp_name = "vim.currentdata";
6593 CurrentType.tp_basicsize = sizeof(CurrentObject);
6594 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6595 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006596 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006597#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006598 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6599 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006601 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6602 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006603#endif
6604
Bram Moolenaara80faa82020-04-12 19:37:17 +02006605 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006606 DictionaryType.tp_name = "vim.dictionary";
6607 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006608 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006609 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006610 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006611 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006612 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006613 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006614 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6615 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6616 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006617#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006618 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6619 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006620#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006621 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6622 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623#endif
6624
Bram Moolenaara80faa82020-04-12 19:37:17 +02006625 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006626 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006627 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006628 ListType.tp_basicsize = sizeof(ListObject);
6629 ListType.tp_as_sequence = &ListAsSeq;
6630 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006631 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006632 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006633 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006634 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006635 ListType.tp_new = (newfunc)ListConstructor;
6636 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006637#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006638 ListType.tp_getattro = (getattrofunc)ListGetattro;
6639 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006640#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006641 ListType.tp_getattr = (getattrfunc)ListGetattr;
6642 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006643#endif
6644
Bram Moolenaara80faa82020-04-12 19:37:17 +02006645 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006646 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006647 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006648 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6649 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006650 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006651 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006652 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006653 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006654 FunctionType.tp_new = (newfunc)FunctionConstructor;
6655 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006656#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006657 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006658#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006659 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006660#endif
6661
Bram Moolenaara80faa82020-04-12 19:37:17 +02006662 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006663 OptionsType.tp_name = "vim.options";
6664 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006665 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006666 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006667 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006668 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006669 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006670 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6671 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6672 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006673
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006674#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006675 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006676 LoaderType.tp_name = "vim.Loader";
6677 LoaderType.tp_basicsize = sizeof(LoaderObject);
6678 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6679 LoaderType.tp_doc = "vim message object";
6680 LoaderType.tp_methods = LoaderMethods;
6681 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006682#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006683
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006684#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006685 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006686 vimmodule.m_name = "vim";
6687 vimmodule.m_doc = "Vim Python interface\n";
6688 vimmodule.m_size = -1;
6689 vimmodule.m_methods = VimMethods;
6690#endif
6691}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006692
6693#define PYTYPE_READY(type) \
kylo2529dac9b12022-03-27 20:05:17 +01006694 if (PyType_Ready(&(type))) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006695 return -1;
6696
6697 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006698init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006699{
6700 PYTYPE_READY(IterType);
6701 PYTYPE_READY(BufferType);
6702 PYTYPE_READY(RangeType);
6703 PYTYPE_READY(WindowType);
6704 PYTYPE_READY(TabPageType);
6705 PYTYPE_READY(BufMapType);
6706 PYTYPE_READY(WinListType);
6707 PYTYPE_READY(TabListType);
6708 PYTYPE_READY(CurrentType);
6709 PYTYPE_READY(DictionaryType);
6710 PYTYPE_READY(ListType);
6711 PYTYPE_READY(FunctionType);
6712 PYTYPE_READY(OptionsType);
6713 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006714#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006715 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006716#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006717 return 0;
6718}
6719
6720 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006721init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006722{
6723 PyObject *path;
6724 PyObject *path_hook;
6725 PyObject *path_hooks;
6726
6727 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6728 return -1;
6729
6730 if (!(path_hooks = PySys_GetObject("path_hooks")))
6731 {
6732 PyErr_Clear();
6733 path_hooks = PyList_New(1);
6734 PyList_SET_ITEM(path_hooks, 0, path_hook);
6735 if (PySys_SetObject("path_hooks", path_hooks))
6736 {
6737 Py_DECREF(path_hooks);
6738 return -1;
6739 }
6740 Py_DECREF(path_hooks);
6741 }
6742 else if (PyList_Check(path_hooks))
6743 {
6744 if (PyList_Append(path_hooks, path_hook))
6745 {
6746 Py_DECREF(path_hook);
6747 return -1;
6748 }
6749 Py_DECREF(path_hook);
6750 }
6751 else
6752 {
6753 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006754 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006755 "You should now do the following:\n"
6756 "- append vim.path_hook to sys.path_hooks\n"
6757 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006758 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006759 Py_DECREF(path_hook);
6760 return 0;
6761 }
6762
6763 if (!(path = PySys_GetObject("path")))
6764 {
6765 PyErr_Clear();
6766 path = PyList_New(1);
6767 Py_INCREF(vim_special_path_object);
6768 PyList_SET_ITEM(path, 0, vim_special_path_object);
6769 if (PySys_SetObject("path", path))
6770 {
6771 Py_DECREF(path);
6772 return -1;
6773 }
6774 Py_DECREF(path);
6775 }
6776 else if (PyList_Check(path))
6777 {
6778 if (PyList_Append(path, vim_special_path_object))
6779 return -1;
6780 }
6781 else
6782 {
6783 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006784 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006785 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006786 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006787 }
6788
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006789 return 0;
6790}
6791
6792static BufMapObject TheBufferMap =
6793{
6794 PyObject_HEAD_INIT(&BufMapType)
6795};
6796
6797static WinListObject TheWindowList =
6798{
6799 PyObject_HEAD_INIT(&WinListType)
6800 NULL
6801};
6802
6803static CurrentObject TheCurrent =
6804{
6805 PyObject_HEAD_INIT(&CurrentType)
6806};
6807
6808static TabListObject TheTabPageList =
6809{
6810 PyObject_HEAD_INIT(&TabListType)
6811};
6812
6813static struct numeric_constant {
6814 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006815 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006816} numeric_constants[] = {
6817 {"VAR_LOCKED", VAR_LOCKED},
6818 {"VAR_FIXED", VAR_FIXED},
6819 {"VAR_SCOPE", VAR_SCOPE},
6820 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6821};
6822
6823static struct object_constant {
6824 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006825 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006826} object_constants[] = {
6827 {"buffers", (PyObject *)(void *)&TheBufferMap},
6828 {"windows", (PyObject *)(void *)&TheWindowList},
6829 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6830 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006831
6832 {"Buffer", (PyObject *)&BufferType},
6833 {"Range", (PyObject *)&RangeType},
6834 {"Window", (PyObject *)&WindowType},
6835 {"TabPage", (PyObject *)&TabPageType},
6836 {"Dictionary", (PyObject *)&DictionaryType},
6837 {"List", (PyObject *)&ListType},
6838 {"Function", (PyObject *)&FunctionType},
6839 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006840#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006841 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006842#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006843};
6844
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006845#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006846 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006847 return -1;
6848
6849#define ADD_CHECKED_OBJECT(m, name, obj) \
6850 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006851 PyObject *valObject = obj; \
6852 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006853 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006854 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006855 }
6856
6857 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006858populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006859{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006860 int i;
6861 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006862 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006863 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006864#if PY_VERSION_HEX >= 0x030700f0
6865 PyObject *dict;
6866 PyObject *cls;
6867#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006868
6869 for (i = 0; i < (int)(sizeof(numeric_constants)
6870 / sizeof(struct numeric_constant));
6871 ++i)
6872 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006873 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006874
6875 for (i = 0; i < (int)(sizeof(object_constants)
6876 / sizeof(struct object_constant));
6877 ++i)
6878 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006879 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006881 valObject = object_constants[i].valObject;
6882 Py_INCREF(valObject);
6883 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006884 }
6885
6886 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6887 return -1;
6888 ADD_OBJECT(m, "error", VimError);
6889
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006890 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6891 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006892 ADD_CHECKED_OBJECT(m, "options",
6893 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006895 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006897 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006898
Bram Moolenaar22081f42016-06-01 20:38:34 +02006899#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006900 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006901 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006902#else
6903 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6904 return -1;
6905#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006906 ADD_OBJECT(m, "_getcwd", py_getcwd)
6907
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006908 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006909 return -1;
6910 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006911 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006912 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006913 if (PyObject_SetAttrString(other_module, "chdir", attr))
6914 {
6915 Py_DECREF(attr);
6916 return -1;
6917 }
6918 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006919
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006920 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006921 {
6922 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006923 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006924 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006925 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6926 {
6927 Py_DECREF(attr);
6928 return -1;
6929 }
6930 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006931 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006932 else
6933 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006934
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006935 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6936 return -1;
6937
6938 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6939
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006940#if PY_VERSION_HEX >= 0x030700f0
6941 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6942 return -1;
6943
6944 dict = PyModule_GetDict(imp);
6945
6946 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6947 {
6948 Py_DECREF(imp);
6949 return -1;
6950 }
6951
6952 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6953 {
6954 Py_DECREF(imp);
6955 return -1;
6956 }
6957
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006958 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6959 {
6960 // find_module() is deprecated, this may stop working in some later
6961 // version.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01006962 ADD_OBJECT(m, "_find_module", py_find_module);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006963 }
6964
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006965 Py_DECREF(imp);
6966
6967 ADD_OBJECT(m, "_find_spec", py_find_spec);
6968#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006969 if (!(imp = PyImport_ImportModule("imp")))
6970 return -1;
6971
6972 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6973 {
6974 Py_DECREF(imp);
6975 return -1;
6976 }
6977
6978 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6979 {
6980 Py_DECREF(py_find_module);
6981 Py_DECREF(imp);
6982 return -1;
6983 }
6984
6985 Py_DECREF(imp);
6986
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006987 ADD_OBJECT(m, "_find_module", py_find_module);
6988 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006989#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006990
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006991 return 0;
6992}