blob: 0d0a5cef29716288911e32846ee32f871b548ddf [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 Moolenaarca8a4df2010-07-31 19:54:14 +0200700 update_screen(VALID);
701
702 Python_Release_Vim();
703 Py_END_ALLOW_THREADS
704
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200705 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200706 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200711 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713}
714
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715/*
716 * Function to translate a typval_T into a PyObject; this will recursively
717 * translate lists/dictionaries into their Python equivalents.
718 *
719 * The depth parameter is to avoid infinite recursion, set it to 1 when
720 * you call VimToPython.
721 */
722 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200723VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200724{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200727 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100729 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730 if (depth > 100)
731 {
732 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200733 ret = Py_None;
734 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735 }
736
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100737 // Check if we run into a recursive loop. The item must be in lookup_dict
738 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200739 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
740 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
741 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200742 sprintf(ptrBuf, "%p",
743 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
744 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200745
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200747 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 Py_INCREF(ret);
749 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750 }
751 }
752
753 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200754 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200755 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200756 else if (our_tv->v_type == VAR_NUMBER)
757 {
758 char buf[NUMBUFLEN];
759
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100760 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200762 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100764#ifdef FEAT_FLOAT
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 else if (our_tv->v_type == VAR_FLOAT)
766 {
767 char buf[NUMBUFLEN];
768
769 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200770 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771 }
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100772#endif
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 else if (our_tv->v_type == VAR_LIST)
774 {
775 list_T *list = our_tv->vval.v_list;
776 listitem_T *curr;
777
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200778 if (list == NULL)
779 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200781 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200782 return NULL;
783
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200785 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200786 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200787 return NULL;
788 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200789
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200790 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200791 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200792 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200793 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200798 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200799 {
800 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200801 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200802 return NULL;
803 }
804 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805 }
806 }
807 else if (our_tv->v_type == VAR_DICT)
808 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200809
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100810 hashtab_T *ht;
811 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200812 hashitem_T *hi;
813 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100814
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200815 if (our_tv->vval.v_dict == NULL)
816 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100817 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200818
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200819 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200820 return NULL;
821
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200822 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200823 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200824 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200825 return NULL;
826 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100828 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200829 for (hi = ht->ht_array; todo > 0; ++hi)
830 {
831 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200833 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200834
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200835 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200836 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200837 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200839 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200841 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200842 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200843 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200844 Py_DECREF(newObj);
845 return NULL;
846 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200847 }
848 }
849 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100850 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100851 {
852 if (our_tv->vval.v_number == VVAL_FALSE)
853 {
854 ret = Py_False;
855 Py_INCREF(ret);
856 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100857 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100858 {
859 ret = Py_True;
860 Py_INCREF(ret);
861 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100862 return ret;
863 }
864 else if (our_tv->v_type == VAR_SPECIAL)
865 {
866 Py_INCREF(Py_None);
867 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100868 return ret;
869 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100870 else if (our_tv->v_type == VAR_BLOB)
871 ret = PyBytes_FromStringAndSize(
872 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
873 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 else
875 {
876 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200877 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878 }
879
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200880 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200881}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200882
883 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200884VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200886 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200887 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200888 PyObject *string;
889 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200890 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200891 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200892
Bram Moolenaar389a1792013-06-23 13:00:44 +0200893 if (!PyArg_ParseTuple(args, "O", &string))
894 return NULL;
895
896 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200897 return NULL;
898
899 Py_BEGIN_ALLOW_THREADS
900 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200901 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200903 Python_Release_Vim();
904 Py_END_ALLOW_THREADS
905
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906 Py_XDECREF(todecref);
907
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200908 if (VimTryEnd())
909 return NULL;
910
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200911 if (our_tv == NULL)
912 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200913 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200914 return NULL;
915 }
916
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100917 // Convert the Vim type into a Python type. Create a dictionary that's
918 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200919 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200920 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200921 else
922 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200923 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200924 Py_DECREF(lookup_dict);
925 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200926
927
928 Py_BEGIN_ALLOW_THREADS
929 Python_Lock_Vim();
930 free_tv(our_tv);
931 Python_Release_Vim();
932 Py_END_ALLOW_THREADS
933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200934 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200935}
936
Bram Moolenaardb913952012-06-29 12:54:53 +0200937static PyObject *ConvertToPyObject(typval_T *);
938
939 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200941{
Bram Moolenaardb913952012-06-29 12:54:53 +0200942 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200943 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200944 char_u *expr;
945 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200946
Bram Moolenaar389a1792013-06-23 13:00:44 +0200947 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200948 return NULL;
949
950 Py_BEGIN_ALLOW_THREADS
951 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200952 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200954 Python_Release_Vim();
955 Py_END_ALLOW_THREADS
956
Bram Moolenaar389a1792013-06-23 13:00:44 +0200957 Py_XDECREF(todecref);
958
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200959 if (VimTryEnd())
960 return NULL;
961
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 if (our_tv == NULL)
963 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200964 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200965 return NULL;
966 }
967
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200969 Py_BEGIN_ALLOW_THREADS
970 Python_Lock_Vim();
971 free_tv(our_tv);
972 Python_Release_Vim();
973 Py_END_ALLOW_THREADS
974
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200975 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200976}
977
978 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200979VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200980{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200981 char_u *str;
982 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200983 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200984
Bram Moolenaar389a1792013-06-23 13:00:44 +0200985 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200986 return NULL;
987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200988 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +0200989
990 Py_XDECREF(todecref);
991
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200992 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200993}
994
Bram Moolenaarf4258302013-06-02 18:20:17 +0200995 static PyObject *
996_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
997{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200998 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200999 PyObject *newwd;
1000 PyObject *todecref;
1001 char_u *new_dir;
1002
Bram Moolenaard4209d22013-06-05 20:34:15 +02001003 if (_chdir == NULL)
1004 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001005 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001006 return NULL;
1007
1008 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1009 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001010 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001011 return NULL;
1012 }
1013
1014 if (!(new_dir = StringToChars(newwd, &todecref)))
1015 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001016 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001017 Py_DECREF(newwd);
1018 return NULL;
1019 }
1020
1021 VimTryStart();
1022
1023 if (vim_chdir(new_dir))
1024 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001025 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001026 Py_DECREF(newwd);
1027 Py_XDECREF(todecref);
1028
1029 if (VimTryEnd())
1030 return NULL;
1031
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001032 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001033 return NULL;
1034 }
1035
1036 Py_DECREF(newwd);
1037 Py_XDECREF(todecref);
1038
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001039 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001040
1041 if (VimTryEnd())
1042 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001043 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001044 return NULL;
1045 }
1046
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001047 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001048}
1049
1050 static PyObject *
1051VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1052{
1053 return _VimChdir(py_chdir, args, kwargs);
1054}
1055
1056 static PyObject *
1057VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1058{
1059 return _VimChdir(py_fchdir, args, kwargs);
1060}
1061
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001062typedef struct {
1063 PyObject *callable;
1064 PyObject *result;
1065} map_rtp_data;
1066
1067 static void
1068map_rtp_callback(char_u *path, void *_data)
1069{
1070 void **data = (void **) _data;
1071 PyObject *pathObject;
1072 map_rtp_data *mr_data = *((map_rtp_data **) data);
1073
Bram Moolenaar41009372013-07-01 22:03:04 +02001074 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001075 {
1076 *data = NULL;
1077 return;
1078 }
1079
1080 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1081 pathObject, NULL);
1082
1083 Py_DECREF(pathObject);
1084
1085 if (!mr_data->result || mr_data->result != Py_None)
1086 *data = NULL;
1087 else
1088 {
1089 Py_DECREF(mr_data->result);
1090 mr_data->result = NULL;
1091 }
1092}
1093
1094 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001095VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001096{
1097 map_rtp_data data;
1098
Bram Moolenaar389a1792013-06-23 13:00:44 +02001099 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001100 data.result = NULL;
1101
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001102 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001103
1104 if (data.result == NULL)
1105 {
1106 if (PyErr_Occurred())
1107 return NULL;
1108 else
1109 {
1110 Py_INCREF(Py_None);
1111 return Py_None;
1112 }
1113 }
1114 return data.result;
1115}
1116
1117/*
1118 * _vim_runtimepath_ special path implementation.
1119 */
1120
1121 static void
1122map_finder_callback(char_u *path, void *_data)
1123{
1124 void **data = (void **) _data;
1125 PyObject *list = *((PyObject **) data);
1126 PyObject *pathObject1, *pathObject2;
1127 char *pathbuf;
1128 size_t pathlen;
1129
1130 pathlen = STRLEN(path);
1131
1132#if PY_MAJOR_VERSION < 3
1133# define PY_MAIN_DIR_STRING "python2"
1134#else
1135# define PY_MAIN_DIR_STRING "python3"
1136#endif
1137#define PY_ALTERNATE_DIR_STRING "pythonx"
1138
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001139#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001140 if (!(pathbuf = PyMem_New(char,
1141 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1142 {
1143 PyErr_NoMemory();
1144 *data = NULL;
1145 return;
1146 }
1147
1148 mch_memmove(pathbuf, path, pathlen + 1);
1149 add_pathsep((char_u *) pathbuf);
1150
1151 pathlen = STRLEN(pathbuf);
1152 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1153 PYTHONX_STRING_LENGTH + 1);
1154
1155 if (!(pathObject1 = PyString_FromString(pathbuf)))
1156 {
1157 *data = NULL;
1158 PyMem_Free(pathbuf);
1159 return;
1160 }
1161
1162 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1163 PYTHONX_STRING_LENGTH + 1);
1164
1165 if (!(pathObject2 = PyString_FromString(pathbuf)))
1166 {
1167 Py_DECREF(pathObject1);
1168 PyMem_Free(pathbuf);
1169 *data = NULL;
1170 return;
1171 }
1172
1173 PyMem_Free(pathbuf);
1174
1175 if (PyList_Append(list, pathObject1)
1176 || PyList_Append(list, pathObject2))
1177 *data = NULL;
1178
1179 Py_DECREF(pathObject1);
1180 Py_DECREF(pathObject2);
1181}
1182
1183 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001184Vim_GetPaths(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001185{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001186 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001187
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001188 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001189 return NULL;
1190
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001191 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001192
1193 if (PyErr_Occurred())
1194 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001195 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001196 return NULL;
1197 }
1198
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001199 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001200}
1201
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001202#if PY_VERSION_HEX >= 0x030700f0
1203 static PyObject *
1204FinderFindSpec(PyObject *self, PyObject *args)
1205{
1206 char *fullname;
1207 PyObject *paths;
1208 PyObject *target = Py_None;
1209 PyObject *spec;
1210
1211 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1212 return NULL;
1213
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001214 if (!(paths = Vim_GetPaths(self, NULL)))
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001215 return NULL;
1216
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001217 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001218
1219 Py_DECREF(paths);
1220
1221 if (!spec)
1222 {
1223 if (PyErr_Occurred())
1224 return NULL;
1225
1226 Py_INCREF(Py_None);
1227 return Py_None;
1228 }
1229
1230 return spec;
1231}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001232
1233 static PyObject *
1234FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1235{
1236 // Apparently returning None works.
1237 Py_INCREF(Py_None);
1238 return Py_None;
1239}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001240#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001241 static PyObject *
1242call_load_module(char *name, int len, PyObject *find_module_result)
1243{
1244 PyObject *fd, *pathname, *description;
1245
Bram Moolenaarc476e522013-06-23 13:46:40 +02001246 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001247 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001248 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001249 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001250 Py_TYPE_NAME(find_module_result));
1251 return NULL;
1252 }
1253 if (PyTuple_GET_SIZE(find_module_result) != 3)
1254 {
1255 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001256 N_("expected 3-tuple as imp.find_module() result, but got "
1257 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001258 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001259 return NULL;
1260 }
1261
1262 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1263 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1264 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1265 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001266 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001267 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001268 return NULL;
1269 }
1270
1271 return PyObject_CallFunction(py_load_module,
1272 "s#OOO", name, len, fd, pathname, description);
1273}
1274
1275 static PyObject *
1276find_module(char *fullname, char *tail, PyObject *new_path)
1277{
1278 PyObject *find_module_result;
1279 PyObject *module;
1280 char *dot;
1281
Bram Moolenaar41009372013-07-01 22:03:04 +02001282 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001283 {
1284 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001285 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001286 * first component
1287 */
1288 PyObject *newest_path;
1289 int partlen = (int) (dot - 1 - tail);
1290
1291 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1292 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001293 {
1294 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1295 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001296 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001297 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001298
1299 if (!(module = call_load_module(
1300 fullname,
1301 ((int) (tail - fullname)) + partlen,
1302 find_module_result)))
1303 {
1304 Py_DECREF(find_module_result);
1305 return NULL;
1306 }
1307
1308 Py_DECREF(find_module_result);
1309
1310 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1311 {
1312 Py_DECREF(module);
1313 return NULL;
1314 }
1315
1316 Py_DECREF(module);
1317
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001318 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001319
1320 Py_DECREF(newest_path);
1321
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001322 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001323 }
1324 else
1325 {
1326 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1327 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001328 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001329 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1330 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001331 return NULL;
1332 }
1333
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001334 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001335 }
1336}
1337
1338 static PyObject *
1339FinderFindModule(PyObject *self, PyObject *args)
1340{
1341 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001342 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001343 PyObject *new_path;
1344 LoaderObject *loader;
1345
1346 if (!PyArg_ParseTuple(args, "s", &fullname))
1347 return NULL;
1348
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001349 if (!(new_path = Vim_GetPaths(self, NULL)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001350 return NULL;
1351
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001352 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001353
1354 Py_DECREF(new_path);
1355
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001356 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001357 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001358 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001359 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001360
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001361 Py_INCREF(Py_None);
1362 return Py_None;
1363 }
1364
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001365 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001366 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001367 Py_DECREF(result);
1368 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001369 return NULL;
1370 }
1371
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001372 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1373 {
1374 vim_free(fullname);
1375 Py_DECREF(result);
1376 return NULL;
1377 }
1378
1379 loader->fullname = fullname;
1380 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001381
1382 return (PyObject *) loader;
1383}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001384#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001385
1386 static PyObject *
1387VimPathHook(PyObject *self UNUSED, PyObject *args)
1388{
1389 char *path;
1390
1391 if (PyArg_ParseTuple(args, "s", &path)
1392 && STRCMP(path, vim_special_path) == 0)
1393 {
1394 Py_INCREF(vim_module);
1395 return vim_module;
1396 }
1397
1398 PyErr_Clear();
1399 PyErr_SetNone(PyExc_ImportError);
1400 return NULL;
1401}
1402
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001403/*
1404 * Vim module - Definitions
1405 */
1406
1407static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001408 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001409 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001410 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001411 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001412 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001413 {"chdir", (PyCFunction)(void *)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1414 {"fchdir", (PyCFunction)(void *)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001415 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001416#if PY_VERSION_HEX >= 0x030700f0
1417 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001418#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001419 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001420 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1421 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1422 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001423};
1424
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001425/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001426 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001427 */
1428
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001429static PyTypeObject IterType;
1430
1431typedef PyObject *(*nextfun)(void **);
1432typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001433typedef int (*traversefun)(void *, visitproc, void *);
1434typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001435
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001436// Main purpose of this object is removing the need for do python
1437// initialization (i.e. PyType_Ready and setting type attributes) for a big
1438// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001439typedef struct
1440{
1441 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001442 void *cur;
1443 nextfun next;
1444 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001445 traversefun traverse;
1446 clearfun clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001447 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001448} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001449
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001450 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001451IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001452 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001453{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001454 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001455
Bram Moolenaar774267b2013-05-21 20:51:59 +02001456 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001457 self->cur = start;
1458 self->next = next;
1459 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001460 self->traverse = traverse;
1461 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001462 self->iter_object = iter_object;
1463
1464 if (iter_object)
1465 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001466
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001467 return (PyObject *)(self);
1468}
1469
1470 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001471IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001472{
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001473 if (self->iter_object)
1474 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001475 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001476 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001477 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001478}
1479
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001480 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001481IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001482{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001483 if (self->traverse != NULL)
1484 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001485 else
1486 return 0;
1487}
1488
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001489// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001490#ifdef clear
1491# undef clear
1492#endif
1493
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001494 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001495IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001496{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001497 if (self->clear != NULL)
1498 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001499 else
1500 return 0;
1501}
1502
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001503 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001504IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001505{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001506 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001507}
1508
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001509 static PyObject *
1510IterIter(PyObject *self)
1511{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001512 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001513 return self;
1514}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001515
Bram Moolenaardb913952012-06-29 12:54:53 +02001516typedef struct pylinkedlist_S {
1517 struct pylinkedlist_S *pll_next;
1518 struct pylinkedlist_S *pll_prev;
1519 PyObject *pll_obj;
1520} pylinkedlist_T;
1521
1522static pylinkedlist_T *lastdict = NULL;
1523static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001524static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001525
1526 static void
1527pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1528{
1529 if (ref->pll_prev == NULL)
1530 {
1531 if (ref->pll_next == NULL)
1532 {
1533 *last = NULL;
1534 return;
1535 }
1536 }
1537 else
1538 ref->pll_prev->pll_next = ref->pll_next;
1539
1540 if (ref->pll_next == NULL)
1541 *last = ref->pll_prev;
1542 else
1543 ref->pll_next->pll_prev = ref->pll_prev;
1544}
1545
1546 static void
1547pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1548{
1549 if (*last == NULL)
1550 ref->pll_prev = NULL;
1551 else
1552 {
1553 (*last)->pll_next = ref;
1554 ref->pll_prev = *last;
1555 }
1556 ref->pll_next = NULL;
1557 ref->pll_obj = self;
1558 *last = ref;
1559}
1560
1561static PyTypeObject DictionaryType;
1562
1563typedef struct
1564{
1565 PyObject_HEAD
1566 dict_T *dict;
1567 pylinkedlist_T ref;
1568} DictionaryObject;
1569
Bram Moolenaara9922d62013-05-30 13:01:18 +02001570static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1571
1572#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1573
Bram Moolenaardb913952012-06-29 12:54:53 +02001574 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001575DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001576{
1577 DictionaryObject *self;
1578
Bram Moolenaara9922d62013-05-30 13:01:18 +02001579 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001580 if (self == NULL)
1581 return NULL;
1582 self->dict = dict;
1583 ++dict->dv_refcount;
1584
1585 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1586
1587 return (PyObject *)(self);
1588}
1589
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001591py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001592{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001594
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001595 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001596 {
1597 PyErr_NoMemory();
1598 return NULL;
1599 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001600 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001602 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001603}
1604
1605 static PyObject *
1606DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1607{
1608 DictionaryObject *self;
1609 dict_T *dict;
1610
1611 if (!(dict = py_dict_alloc()))
1612 return NULL;
1613
1614 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1615
1616 --dict->dv_refcount;
1617
1618 if (kwargs || PyTuple_Size(args))
1619 {
1620 PyObject *tmp;
1621 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1622 {
1623 Py_DECREF(self);
1624 return NULL;
1625 }
1626
1627 Py_DECREF(tmp);
1628 }
1629
1630 return (PyObject *)(self);
1631}
1632
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001633 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001634DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001635{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001636 pyll_remove(&self->ref, &lastdict);
1637 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001638
1639 DESTRUCTOR_FINISH(self);
1640}
1641
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001642static char *DictionaryAttrs[] = {
1643 "locked", "scope",
1644 NULL
1645};
1646
1647 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001648DictionaryDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001649{
1650 return ObjectDir(self, DictionaryAttrs);
1651}
1652
Bram Moolenaardb913952012-06-29 12:54:53 +02001653 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001654DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001655{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001656 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001657 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001658 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001659 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001660 return -1;
1661 }
1662
1663 if (strcmp(name, "locked") == 0)
1664 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001665 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001666 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001667 PyErr_SET_STRING(PyExc_TypeError,
1668 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001669 return -1;
1670 }
1671 else
1672 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001673 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001674 if (istrue == -1)
1675 return -1;
1676 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001677 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001678 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001679 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001680 }
1681 return 0;
1682 }
1683 else
1684 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001685 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001686 return -1;
1687 }
1688}
1689
1690 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001691DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001692{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001693 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001694}
1695
Bram Moolenaara9922d62013-05-30 13:01:18 +02001696#define DICT_FLAG_HAS_DEFAULT 0x01
1697#define DICT_FLAG_POP 0x02
1698#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001699#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001700#define DICT_FLAG_RETURN_PAIR 0x10
1701
Bram Moolenaardb913952012-06-29 12:54:53 +02001702 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001703_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001704{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705 PyObject *keyObject;
1706 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001707 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001708 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001709 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001710 dict_T *dict = self->dict;
1711 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001712 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001713
Bram Moolenaara9922d62013-05-30 13:01:18 +02001714 if (flags & DICT_FLAG_HAS_DEFAULT)
1715 {
1716 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1717 return NULL;
1718 }
1719 else
1720 keyObject = args;
1721
1722 if (flags & DICT_FLAG_RETURN_BOOL)
1723 defObject = Py_False;
1724
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001725 if (!(key = StringToChars(keyObject, &todecref)))
1726 return NULL;
1727
1728 if (*key == NUL)
1729 {
1730 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001731 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001732 return NULL;
1733 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001734
Bram Moolenaara9922d62013-05-30 13:01:18 +02001735 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001736
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001737 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001738
Bram Moolenaara9922d62013-05-30 13:01:18 +02001739 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001740 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001741 if (defObject)
1742 {
1743 Py_INCREF(defObject);
1744 return defObject;
1745 }
1746 else
1747 {
1748 PyErr_SetObject(PyExc_KeyError, keyObject);
1749 return NULL;
1750 }
1751 }
1752 else if (flags & DICT_FLAG_RETURN_BOOL)
1753 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001754 ret = Py_True;
1755 Py_INCREF(ret);
1756 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001757 }
1758
1759 di = dict_lookup(hi);
1760
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001761 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001762 return NULL;
1763
1764 if (flags & DICT_FLAG_POP)
1765 {
1766 if (dict->dv_lock)
1767 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001768 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001769 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001770 return NULL;
1771 }
1772
1773 hash_remove(&dict->dv_hashtab, hi);
1774 dictitem_free(di);
1775 }
1776
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001777 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778}
1779
1780 static PyObject *
1781DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1782{
1783 return _DictionaryItem(self, keyObject, 0);
1784}
1785
1786 static int
1787DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1788{
1789 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001790 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001791
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001792 if (rObj == NULL)
1793 return -1;
1794
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001795 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796
Bram Moolenaardee2e312013-06-23 16:35:47 +02001797 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001799 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001800}
1801
1802typedef struct
1803{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001804 int dii_changed;
1805 hashtab_T *dii_ht;
1806 hashitem_T *dii_hi;
1807 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001808} dictiterinfo_T;
1809
1810 static PyObject *
1811DictionaryIterNext(dictiterinfo_T **dii)
1812{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001813 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001814
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001815 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816 return NULL;
1817
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001818 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001819 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001820 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001821 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001822 return NULL;
1823 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001824
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001825 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
1826 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001827
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001828 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001830 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001831 return NULL;
1832
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001833 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001834}
1835
1836 static PyObject *
1837DictionaryIter(DictionaryObject *self)
1838{
1839 dictiterinfo_T *dii;
1840 hashtab_T *ht;
1841
1842 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1843 {
1844 PyErr_NoMemory();
1845 return NULL;
1846 }
1847
1848 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02001849 dii->dii_changed = ht->ht_changed;
1850 dii->dii_ht = ht;
1851 dii->dii_hi = ht->ht_array;
1852 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001853
1854 return IterNew(dii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001855 (destructorfun)(void *) PyMem_Free, (nextfun) DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001856 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02001857}
1858
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001859 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001860DictionaryAssItem(
1861 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001862{
1863 char_u *key;
1864 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001865 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001867 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001868
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001869 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001871 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001872 return -1;
1873 }
1874
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001875 if (!(key = StringToChars(keyObject, &todecref)))
1876 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001877
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001878 if (*key == NUL)
1879 {
1880 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001881 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001882 return -1;
1883 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001884
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001885 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001886
1887 if (valObject == NULL)
1888 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001889 hashitem_T *hi;
1890
Bram Moolenaardb913952012-06-29 12:54:53 +02001891 if (di == NULL)
1892 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001893 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001894 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 return -1;
1896 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001897 hi = hash_find(&dict->dv_hashtab, di->di_key);
1898 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001900 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001901 return 0;
1902 }
1903
1904 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001905 {
1906 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001908 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001909
1910 if (di == NULL)
1911 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001912 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001913 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001914 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001915 PyErr_NoMemory();
1916 return -1;
1917 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001918 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001919
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001920 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001921 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001922 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001923 RAISE_KEY_ADD_FAIL(key);
1924 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001925 return -1;
1926 }
1927 }
1928 else
1929 clear_tv(&di->di_tv);
1930
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001931 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001932
1933 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001934 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001935 return 0;
1936}
1937
Bram Moolenaara9922d62013-05-30 13:01:18 +02001938typedef PyObject *(*hi_to_py)(hashitem_T *);
1939
Bram Moolenaardb913952012-06-29 12:54:53 +02001940 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001941DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001942{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001943 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001944 long_u todo = dict->dv_hashtab.ht_used;
1945 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001946 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001947 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001948 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001949
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001950 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001951 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1952 {
1953 if (!HASHITEM_EMPTY(hi))
1954 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001955 if (!(newObj = hiconvert(hi)))
1956 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001957 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001958 return NULL;
1959 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001960 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001961 --todo;
1962 ++i;
1963 }
1964 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001965 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001966}
1967
Bram Moolenaara9922d62013-05-30 13:01:18 +02001968 static PyObject *
1969dict_key(hashitem_T *hi)
1970{
1971 return PyBytes_FromString((char *)(hi->hi_key));
1972}
1973
1974 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001975DictionaryListKeys(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001976{
1977 return DictionaryListObjects(self, dict_key);
1978}
1979
1980 static PyObject *
1981dict_val(hashitem_T *hi)
1982{
1983 dictitem_T *di;
1984
1985 di = dict_lookup(hi);
1986 return ConvertToPyObject(&di->di_tv);
1987}
1988
1989 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001990DictionaryListValues(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001991{
1992 return DictionaryListObjects(self, dict_val);
1993}
1994
1995 static PyObject *
1996dict_item(hashitem_T *hi)
1997{
1998 PyObject *keyObject;
1999 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002000 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002001
2002 if (!(keyObject = dict_key(hi)))
2003 return NULL;
2004
2005 if (!(valObject = dict_val(hi)))
2006 {
2007 Py_DECREF(keyObject);
2008 return NULL;
2009 }
2010
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002011 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012
2013 Py_DECREF(keyObject);
2014 Py_DECREF(valObject);
2015
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002016 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002017}
2018
2019 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002020DictionaryListItems(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002021{
2022 return DictionaryListObjects(self, dict_item);
2023}
2024
2025 static PyObject *
2026DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2027{
2028 dict_T *dict = self->dict;
2029
2030 if (dict->dv_lock)
2031 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002032 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002033 return NULL;
2034 }
2035
2036 if (kwargs)
2037 {
2038 typval_T tv;
2039
2040 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2041 return NULL;
2042
2043 VimTryStart();
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002044 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force", NULL);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045 clear_tv(&tv);
2046 if (VimTryEnd())
2047 return NULL;
2048 }
2049 else
2050 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002051 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002052
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002053 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002054 return NULL;
2055
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002056 if (obj == NULL)
2057 {
2058 Py_INCREF(Py_None);
2059 return Py_None;
2060 }
2061
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002062 if (PyObject_HasAttrString(obj, "keys"))
2063 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002064 else
2065 {
2066 PyObject *iterator;
2067 PyObject *item;
2068
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002069 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002070 return NULL;
2071
2072 while ((item = PyIter_Next(iterator)))
2073 {
2074 PyObject *fast;
2075 PyObject *keyObject;
2076 PyObject *valObject;
2077 PyObject *todecref;
2078 char_u *key;
2079 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002080 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002081
2082 if (!(fast = PySequence_Fast(item, "")))
2083 {
2084 Py_DECREF(iterator);
2085 Py_DECREF(item);
2086 return NULL;
2087 }
2088
2089 Py_DECREF(item);
2090
2091 if (PySequence_Fast_GET_SIZE(fast) != 2)
2092 {
2093 Py_DECREF(iterator);
2094 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002095 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002096 N_("expected sequence element of size 2, "
2097 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002098 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002099 return NULL;
2100 }
2101
2102 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2103
2104 if (!(key = StringToChars(keyObject, &todecref)))
2105 {
2106 Py_DECREF(iterator);
2107 Py_DECREF(fast);
2108 return NULL;
2109 }
2110
2111 di = dictitem_alloc(key);
2112
2113 Py_XDECREF(todecref);
2114
2115 if (di == NULL)
2116 {
2117 Py_DECREF(fast);
2118 Py_DECREF(iterator);
2119 PyErr_NoMemory();
2120 return NULL;
2121 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002122 di->di_tv.v_type = VAR_UNKNOWN;
2123
2124 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2125
2126 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2127 {
2128 Py_DECREF(iterator);
2129 Py_DECREF(fast);
2130 dictitem_free(di);
2131 return NULL;
2132 }
2133
2134 Py_DECREF(fast);
2135
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002136 hi = hash_find(&dict->dv_hashtab, di->di_key);
2137 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002138 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002139 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002140 Py_DECREF(iterator);
2141 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002142 return NULL;
2143 }
2144 }
2145
2146 Py_DECREF(iterator);
2147
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002148 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002149 if (PyErr_Occurred())
2150 return NULL;
2151 }
2152 }
2153 Py_INCREF(Py_None);
2154 return Py_None;
2155}
2156
2157 static PyObject *
2158DictionaryGet(DictionaryObject *self, PyObject *args)
2159{
2160 return _DictionaryItem(self, args,
2161 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2162}
2163
2164 static PyObject *
2165DictionaryPop(DictionaryObject *self, PyObject *args)
2166{
2167 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2168}
2169
2170 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002171DictionaryPopItem(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002172{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002173 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002174 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002175 PyObject *valObject;
2176 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002177
Bram Moolenaarde71b562013-06-02 17:41:54 +02002178 if (self->dict->dv_hashtab.ht_used == 0)
2179 {
2180 PyErr_SetNone(PyExc_KeyError);
2181 return NULL;
2182 }
2183
2184 hi = self->dict->dv_hashtab.ht_array;
2185 while (HASHITEM_EMPTY(hi))
2186 ++hi;
2187
2188 di = dict_lookup(hi);
2189
2190 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002191 return NULL;
2192
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002193 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002194 {
2195 Py_DECREF(valObject);
2196 return NULL;
2197 }
2198
2199 hash_remove(&self->dict->dv_hashtab, hi);
2200 dictitem_free(di);
2201
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002202 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002203}
2204
2205 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002206DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002207{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002208 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2209}
2210
2211static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002212 0, // sq_length
2213 0, // sq_concat
2214 0, // sq_repeat
2215 0, // sq_item
2216 0, // sq_slice
2217 0, // sq_ass_item
2218 0, // sq_ass_slice
2219 (objobjproc) DictionaryContains, // sq_contains
2220 0, // sq_inplace_concat
2221 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002222};
2223
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002224static PyMappingMethods DictionaryAsMapping = {
2225 (lenfunc) DictionaryLength,
2226 (binaryfunc) DictionaryItem,
2227 (objobjargproc) DictionaryAssItem,
2228};
2229
Bram Moolenaardb913952012-06-29 12:54:53 +02002230static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002231 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002232 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2233 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002234 {"update", (PyCFunction)(void *)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002235 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2236 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002237 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002238 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002239 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2240 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002241};
2242
2243static PyTypeObject ListType;
2244
2245typedef struct
2246{
2247 PyObject_HEAD
2248 list_T *list;
2249 pylinkedlist_T ref;
2250} ListObject;
2251
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002252#define NEW_LIST(list) ListNew(&ListType, list)
2253
Bram Moolenaardb913952012-06-29 12:54:53 +02002254 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002255ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002256{
2257 ListObject *self;
2258
Bram Moolenaarab589462020-07-06 21:03:06 +02002259 if (list == NULL)
2260 return NULL;
2261
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002262 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002263 if (self == NULL)
2264 return NULL;
2265 self->list = list;
2266 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002267 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002268
2269 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2270
2271 return (PyObject *)(self);
2272}
2273
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002274 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002275py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002276{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002277 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002279 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002280 {
2281 PyErr_NoMemory();
2282 return NULL;
2283 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002284 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002286 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002287}
2288
2289 static int
2290list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2291{
2292 PyObject *iterator;
2293 PyObject *item;
2294 listitem_T *li;
2295
2296 if (!(iterator = PyObject_GetIter(obj)))
2297 return -1;
2298
2299 while ((item = PyIter_Next(iterator)))
2300 {
2301 if (!(li = listitem_alloc()))
2302 {
2303 PyErr_NoMemory();
2304 Py_DECREF(item);
2305 Py_DECREF(iterator);
2306 return -1;
2307 }
2308 li->li_tv.v_lock = 0;
2309 li->li_tv.v_type = VAR_UNKNOWN;
2310
2311 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2312 {
2313 Py_DECREF(item);
2314 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002316 return -1;
2317 }
2318
2319 Py_DECREF(item);
2320
2321 list_append(l, li);
2322 }
2323
2324 Py_DECREF(iterator);
2325
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002326 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002327 if (PyErr_Occurred())
2328 return -1;
2329
2330 return 0;
2331}
2332
2333 static PyObject *
2334ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2335{
2336 list_T *list;
2337 PyObject *obj = NULL;
2338
2339 if (kwargs)
2340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002341 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002342 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002343 return NULL;
2344 }
2345
2346 if (!PyArg_ParseTuple(args, "|O", &obj))
2347 return NULL;
2348
2349 if (!(list = py_list_alloc()))
2350 return NULL;
2351
2352 if (obj)
2353 {
2354 PyObject *lookup_dict;
2355
2356 if (!(lookup_dict = PyDict_New()))
2357 {
2358 list_unref(list);
2359 return NULL;
2360 }
2361
2362 if (list_py_concat(list, obj, lookup_dict) == -1)
2363 {
2364 Py_DECREF(lookup_dict);
2365 list_unref(list);
2366 return NULL;
2367 }
2368
2369 Py_DECREF(lookup_dict);
2370 }
2371
2372 return ListNew(subtype, list);
2373}
2374
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002375 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002376ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002377{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002378 pyll_remove(&self->ref, &lastlist);
2379 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002380
2381 DESTRUCTOR_FINISH(self);
2382}
2383
Bram Moolenaardb913952012-06-29 12:54:53 +02002384 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002385ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002386{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002387 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002388}
2389
2390 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002391ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002392{
2393 listitem_T *li;
2394
Bram Moolenaard6e39182013-05-21 18:30:34 +02002395 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002396 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002397 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 return NULL;
2399 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002400 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002401 if (li == NULL)
2402 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002403 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002404 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002405 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002406 return NULL;
2407 }
2408 return ConvertToPyObject(&li->li_tv);
2409}
2410
Bram Moolenaardb913952012-06-29 12:54:53 +02002411 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002412ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2413 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002414{
2415 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002417
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002418 if (step == 0)
2419 {
2420 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2421 return NULL;
2422 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002423
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002424 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002425 if (list == NULL)
2426 return NULL;
2427
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002428 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002429 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002430 PyObject *item;
2431
2432 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002433 if (item == NULL)
2434 {
2435 Py_DECREF(list);
2436 return NULL;
2437 }
2438
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002439 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002440 }
2441
2442 return list;
2443}
2444
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002445 static PyObject *
2446ListItem(ListObject *self, PyObject* idx)
2447{
2448#if PY_MAJOR_VERSION < 3
2449 if (PyInt_Check(idx))
2450 {
2451 long _idx = PyInt_AsLong(idx);
2452 return ListIndex(self, _idx);
2453 }
2454 else
2455#endif
2456 if (PyLong_Check(idx))
2457 {
2458 long _idx = PyLong_AsLong(idx);
2459 return ListIndex(self, _idx);
2460 }
2461 else if (PySlice_Check(idx))
2462 {
2463 Py_ssize_t start, stop, step, slicelen;
2464
Bram Moolenaar922a4662014-03-30 16:11:43 +02002465 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002466 &start, &stop, &step, &slicelen) < 0)
2467 return NULL;
2468 return ListSlice(self, start, step, slicelen);
2469 }
2470 else
2471 {
2472 RAISE_INVALID_INDEX_TYPE(idx);
2473 return NULL;
2474 }
2475}
2476
2477 static void
2478list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2479 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2480{
2481 while (numreplaced--)
2482 {
2483 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2484 listitem_remove(l, lis[slicelen + numreplaced]);
2485 }
2486 while (numadded--)
2487 {
2488 listitem_T *next;
2489
2490 next = lastaddedli->li_prev;
2491 listitem_remove(l, lastaddedli);
2492 lastaddedli = next;
2493 }
2494}
2495
2496 static int
2497ListAssSlice(ListObject *self, Py_ssize_t first,
2498 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2499{
2500 PyObject *iterator;
2501 PyObject *item;
2502 listitem_T *li;
2503 listitem_T *lastaddedli = NULL;
2504 listitem_T *next;
2505 typval_T v;
2506 list_T *l = self->list;
2507 PyInt i;
2508 PyInt j;
2509 PyInt numreplaced = 0;
2510 PyInt numadded = 0;
2511 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002512 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002513
2514 size = ListLength(self);
2515
2516 if (l->lv_lock)
2517 {
2518 RAISE_LOCKED_LIST;
2519 return -1;
2520 }
2521
2522 if (step == 0)
2523 {
2524 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2525 return -1;
2526 }
2527
2528 if (step != 1 && slicelen == 0)
2529 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002530 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002531 int ret = 0;
2532
2533 if (obj == NULL)
2534 return 0;
2535
2536 if (!(iterator = PyObject_GetIter(obj)))
2537 return -1;
2538
2539 if ((item = PyIter_Next(iterator)))
2540 {
2541 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002542 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002543 "to extended slice"), 0);
2544 Py_DECREF(item);
2545 ret = -1;
2546 }
2547 Py_DECREF(iterator);
2548 return ret;
2549 }
2550
2551 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002552 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002553 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2554 {
2555 PyErr_NoMemory();
2556 return -1;
2557 }
2558
2559 if (first == size)
2560 li = NULL;
2561 else
2562 {
2563 li = list_find(l, (long) first);
2564 if (li == NULL)
2565 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002566 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002567 (int)first);
2568 if (obj != NULL)
2569 PyMem_Free(lis);
2570 return -1;
2571 }
2572 i = slicelen;
2573 while (i-- && li != NULL)
2574 {
2575 j = step;
2576 next = li;
2577 if (step > 0)
2578 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2579 else
2580 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2581
2582 if (obj == NULL)
2583 listitem_remove(l, li);
2584 else
2585 lis[slicelen - i - 1] = li;
2586
2587 li = next;
2588 }
2589 if (li == NULL && i != -1)
2590 {
2591 PyErr_SET_VIM(N_("internal error: not enough list items"));
2592 if (obj != NULL)
2593 PyMem_Free(lis);
2594 return -1;
2595 }
2596 }
2597
2598 if (obj == NULL)
2599 return 0;
2600
2601 if (!(iterator = PyObject_GetIter(obj)))
2602 {
2603 PyMem_Free(lis);
2604 return -1;
2605 }
2606
2607 i = 0;
2608 while ((item = PyIter_Next(iterator)))
2609 {
2610 if (ConvertFromPyObject(item, &v) == -1)
2611 {
2612 Py_DECREF(iterator);
2613 Py_DECREF(item);
2614 PyMem_Free(lis);
2615 return -1;
2616 }
2617 Py_DECREF(item);
2618 if (list_insert_tv(l, &v, numreplaced < slicelen
2619 ? lis[numreplaced]
2620 : li) == FAIL)
2621 {
2622 clear_tv(&v);
2623 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2624 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2625 PyMem_Free(lis);
2626 return -1;
2627 }
2628 if (numreplaced < slicelen)
2629 {
2630 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002631 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002632 numreplaced++;
2633 }
2634 else
2635 {
2636 if (li)
2637 lastaddedli = li->li_prev;
2638 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002639 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002640 numadded++;
2641 }
2642 clear_tv(&v);
2643 if (step != 1 && i >= slicelen)
2644 {
2645 Py_DECREF(iterator);
2646 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002647 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002648 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002649 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2650 PyMem_Free(lis);
2651 return -1;
2652 }
2653 ++i;
2654 }
2655 Py_DECREF(iterator);
2656
2657 if (step != 1 && i != slicelen)
2658 {
2659 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002660 N_("attempt to assign sequence of size %d to extended slice "
2661 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002662 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2663 PyMem_Free(lis);
2664 return -1;
2665 }
2666
2667 if (PyErr_Occurred())
2668 {
2669 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2670 PyMem_Free(lis);
2671 return -1;
2672 }
2673
2674 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002676 if (step == 1)
2677 for (i = numreplaced; i < slicelen; i++)
2678 listitem_remove(l, lis[i]);
2679
2680 PyMem_Free(lis);
2681
2682 return 0;
2683}
2684
2685 static int
2686ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2687{
2688 typval_T tv;
2689 list_T *l = self->list;
2690 listitem_T *li;
2691 Py_ssize_t length = ListLength(self);
2692
2693 if (l->lv_lock)
2694 {
2695 RAISE_LOCKED_LIST;
2696 return -1;
2697 }
2698 if (index > length || (index == length && obj == NULL))
2699 {
2700 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2701 return -1;
2702 }
2703
2704 if (obj == NULL)
2705 {
2706 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002707 if (li == NULL)
2708 {
2709 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2710 "list item %d"), (int) index);
2711 return -1;
2712 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002713 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002714 clear_tv(&li->li_tv);
2715 vim_free(li);
2716 return 0;
2717 }
2718
2719 if (ConvertFromPyObject(obj, &tv) == -1)
2720 return -1;
2721
2722 if (index == length)
2723 {
2724 if (list_append_tv(l, &tv) == FAIL)
2725 {
2726 clear_tv(&tv);
2727 PyErr_SET_VIM(N_("failed to add item to list"));
2728 return -1;
2729 }
2730 }
2731 else
2732 {
2733 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002734 if (li == NULL)
2735 {
2736 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2737 "list item %d"), (int) index);
2738 return -1;
2739 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002740 clear_tv(&li->li_tv);
2741 copy_tv(&tv, &li->li_tv);
2742 clear_tv(&tv);
2743 }
2744 return 0;
2745}
2746
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002747 static int
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002748ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2749{
2750#if PY_MAJOR_VERSION < 3
2751 if (PyInt_Check(idx))
2752 {
2753 long _idx = PyInt_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002754 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002755 }
2756 else
2757#endif
2758 if (PyLong_Check(idx))
2759 {
2760 long _idx = PyLong_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002761 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002762 }
2763 else if (PySlice_Check(idx))
2764 {
2765 Py_ssize_t start, stop, step, slicelen;
2766
Bram Moolenaar922a4662014-03-30 16:11:43 +02002767 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002768 &start, &stop, &step, &slicelen) < 0)
2769 return -1;
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002770 return (int)ListAssSlice(self, start, step, slicelen,
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002771 obj);
2772 }
2773 else
2774 {
2775 RAISE_INVALID_INDEX_TYPE(idx);
2776 return -1;
2777 }
2778}
2779
2780 static PyObject *
2781ListConcatInPlace(ListObject *self, PyObject *obj)
2782{
2783 list_T *l = self->list;
2784 PyObject *lookup_dict;
2785
2786 if (l->lv_lock)
2787 {
2788 RAISE_LOCKED_LIST;
2789 return NULL;
2790 }
2791
2792 if (!(lookup_dict = PyDict_New()))
2793 return NULL;
2794
2795 if (list_py_concat(l, obj, lookup_dict) == -1)
2796 {
2797 Py_DECREF(lookup_dict);
2798 return NULL;
2799 }
2800 Py_DECREF(lookup_dict);
2801
2802 Py_INCREF(self);
2803 return (PyObject *)(self);
2804}
2805
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002806typedef struct
2807{
2808 listwatch_T lw;
2809 list_T *list;
2810} listiterinfo_T;
2811
2812 static void
2813ListIterDestruct(listiterinfo_T *lii)
2814{
2815 list_rem_watch(lii->list, &lii->lw);
Bram Moolenaar21578272021-02-21 19:12:47 +01002816 list_unref(lii->list);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002817 PyMem_Free(lii);
2818}
2819
2820 static PyObject *
2821ListIterNext(listiterinfo_T **lii)
2822{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002823 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002824
2825 if (!((*lii)->lw.lw_item))
2826 return NULL;
2827
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002828 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002829 return NULL;
2830
2831 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2832
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002833 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002834}
2835
2836 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002837ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002838{
2839 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002840 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002841
2842 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2843 {
2844 PyErr_NoMemory();
2845 return NULL;
2846 }
2847
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002848 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002849 list_add_watch(l, &lii->lw);
2850 lii->lw.lw_item = l->lv_first;
2851 lii->list = l;
Bram Moolenaar21578272021-02-21 19:12:47 +01002852 ++l->lv_refcount;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002853
2854 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002855 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002856 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002857}
2858
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002859static char *ListAttrs[] = {
2860 "locked",
2861 NULL
2862};
2863
2864 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002865ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002866{
2867 return ObjectDir(self, ListAttrs);
2868}
2869
Bram Moolenaar66b79852012-09-21 14:00:35 +02002870 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002872{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002873 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002874 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002875 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002876 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002877 return -1;
2878 }
2879
2880 if (strcmp(name, "locked") == 0)
2881 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002882 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002883 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002884 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002885 return -1;
2886 }
2887 else
2888 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002889 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002890 if (istrue == -1)
2891 return -1;
2892 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002893 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002894 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002896 }
2897 return 0;
2898 }
2899 else
2900 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002901 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002902 return -1;
2903 }
2904}
2905
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002906static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002907 (lenfunc) ListLength, // sq_length, len(x)
2908 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2909 0, // RangeRepeat, sq_repeat, x*n
2910 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2911 0, // was_sq_slice, x[i:j]
2912 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2913 0, // was_sq_ass_slice, x[i:j]=v
2914 0, // sq_contains
2915 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2916 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002917};
2918
2919static PyMappingMethods ListAsMapping = {
2920 /* mp_length */ (lenfunc) ListLength,
2921 /* mp_subscript */ (binaryfunc) ListItem,
2922 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2923};
2924
Bram Moolenaardb913952012-06-29 12:54:53 +02002925static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002926 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2927 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2928 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002929};
2930
2931typedef struct
2932{
2933 PyObject_HEAD
2934 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002935 int argc;
2936 typval_T *argv;
2937 dict_T *self;
2938 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002939 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002940} FunctionObject;
2941
2942static PyTypeObject FunctionType;
2943
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002944#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2945 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002946
Bram Moolenaardb913952012-06-29 12:54:53 +02002947 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002948FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002949 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002950{
2951 FunctionObject *self;
2952
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002953 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002954 if (self == NULL)
2955 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002956
2957 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002958 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02002959 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002960 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002961 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002962 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002963 return NULL;
2964 }
2965 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002966 }
2967 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002968 {
2969 char_u *p;
2970
2971 if ((p = get_expanded_name(name,
2972 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002973 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002974 PyErr_FORMAT(PyExc_ValueError,
2975 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002976 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002977 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002978
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002979 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2980 {
2981 char_u *np;
2982 size_t len = STRLEN(p) + 1;
2983
Bram Moolenaar51e14382019-05-25 20:21:28 +02002984 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002985 {
2986 vim_free(p);
2987 return NULL;
2988 }
2989 mch_memmove(np, "<SNR>", 5);
2990 mch_memmove(np + 5, p + 3, len - 3);
2991 vim_free(p);
2992 self->name = np;
2993 }
2994 else
2995 self->name = p;
2996 }
2997
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002998 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002999 self->argc = argc;
3000 self->argv = argv;
3001 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003002 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003003
3004 if (self->argv || self->self)
3005 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3006
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003007 return (PyObject *)(self);
3008}
3009
3010 static PyObject *
3011FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3012{
3013 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003014 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003015 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003016 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003017 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003018 typval_T selfdicttv;
3019 typval_T argstv;
3020 list_T *argslist = NULL;
3021 dict_T *selfdict = NULL;
3022 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003023 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003024 typval_T *argv = NULL;
3025 typval_T *curtv;
3026 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003027
Bram Moolenaar8110a092016-04-14 15:56:09 +02003028 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003029 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003030 selfdictObject = PyDict_GetItemString(kwargs, "self");
3031 if (selfdictObject != NULL)
3032 {
3033 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3034 return NULL;
3035 selfdict = selfdicttv.vval.v_dict;
3036 }
3037 argsObject = PyDict_GetItemString(kwargs, "args");
3038 if (argsObject != NULL)
3039 {
3040 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3041 {
3042 dict_unref(selfdict);
3043 return NULL;
3044 }
3045 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003046 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003047
3048 argc = argslist->lv_len;
3049 if (argc != 0)
3050 {
3051 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003052 if (argv == NULL)
3053 {
3054 PyErr_NoMemory();
3055 dict_unref(selfdict);
3056 list_unref(argslist);
3057 return NULL;
3058 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003059 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003060 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003061 copy_tv(&li->li_tv, curtv++);
3062 }
3063 list_unref(argslist);
3064 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003065 if (selfdict != NULL)
3066 {
3067 auto_rebind = FALSE;
3068 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3069 if (autoRebindObject != NULL)
3070 {
3071 auto_rebind = PyObject_IsTrue(autoRebindObject);
3072 if (auto_rebind == -1)
3073 {
3074 dict_unref(selfdict);
3075 list_unref(argslist);
3076 return NULL;
3077 }
3078 }
3079 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003080 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003081
Bram Moolenaar389a1792013-06-23 13:00:44 +02003082 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003083 {
3084 dict_unref(selfdict);
3085 while (argc--)
3086 clear_tv(&argv[argc]);
3087 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003088 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003089 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003090
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003091 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003092
Bram Moolenaar389a1792013-06-23 13:00:44 +02003093 PyMem_Free(name);
3094
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003095 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003096}
3097
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003098 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003099FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003100{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003101 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003102 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003103 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003104 for (i = 0; i < self->argc; ++i)
3105 clear_tv(&self->argv[i]);
3106 PyMem_Free(self->argv);
3107 dict_unref(self->self);
3108 if (self->argv || self->self)
3109 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003110
3111 DESTRUCTOR_FINISH(self);
3112}
3113
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003114static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003115 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003116 NULL
3117};
3118
3119 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003120FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003121{
3122 return ObjectDir(self, FunctionAttrs);
3123}
3124
Bram Moolenaardb913952012-06-29 12:54:53 +02003125 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003126FunctionAttr(FunctionObject *self, char *name)
3127{
3128 list_T *list;
3129 int i;
3130 if (strcmp(name, "name") == 0)
3131 return PyString_FromString((char *)(self->name));
3132 else if (strcmp(name, "args") == 0)
3133 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003134 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003135 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003136
Bram Moolenaar8110a092016-04-14 15:56:09 +02003137 for (i = 0; i < self->argc; ++i)
3138 list_append_tv(list, &self->argv[i]);
3139 return NEW_LIST(list);
3140 }
3141 else if (strcmp(name, "self") == 0)
3142 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003143 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003144 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003145 else if (strcmp(name, "auto_rebind") == 0)
3146 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003147 ? ALWAYS_TRUE
3148 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003149 else if (strcmp(name, "__members__") == 0)
3150 return ObjectDir(NULL, FunctionAttrs);
3151 return NULL;
3152}
3153
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003154/*
3155 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003156 *
3157 * "exported" should be set to true when it is needed to construct a partial
3158 * that may be stored in a variable (i.e. may be freed by Vim).
3159 */
3160 static void
3161set_partial(FunctionObject *self, partial_T *pt, int exported)
3162{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003163 int i;
3164
3165 pt->pt_name = self->name;
3166 if (self->argv)
3167 {
3168 pt->pt_argc = self->argc;
3169 if (exported)
3170 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003171 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003172 for (i = 0; i < pt->pt_argc; ++i)
3173 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3174 }
3175 else
3176 pt->pt_argv = self->argv;
3177 }
3178 else
3179 {
3180 pt->pt_argc = 0;
3181 pt->pt_argv = NULL;
3182 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003183 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003184 pt->pt_dict = self->self;
3185 if (exported && self->self)
3186 ++pt->pt_dict->dv_refcount;
3187 if (exported)
3188 pt->pt_name = vim_strsave(pt->pt_name);
3189 pt->pt_refcount = 1;
3190}
3191
3192 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003193FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003194{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003195 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003196 typval_T args;
3197 typval_T selfdicttv;
3198 typval_T rettv;
3199 dict_T *selfdict = NULL;
3200 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003201 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003202 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003203 partial_T pt;
3204 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003205
Bram Moolenaar8110a092016-04-14 15:56:09 +02003206 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003207 return NULL;
3208
3209 if (kwargs != NULL)
3210 {
3211 selfdictObject = PyDict_GetItemString(kwargs, "self");
3212 if (selfdictObject != NULL)
3213 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003214 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003215 {
3216 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003217 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003218 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003219 selfdict = selfdicttv.vval.v_dict;
3220 }
3221 }
3222
Bram Moolenaar8110a092016-04-14 15:56:09 +02003223 if (self->argv || self->self)
3224 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003225 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003226 set_partial(self, &pt, FALSE);
3227 pt_ptr = &pt;
3228 }
3229
Bram Moolenaar71700b82013-05-15 17:49:05 +02003230 Py_BEGIN_ALLOW_THREADS
3231 Python_Lock_Vim();
3232
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003233 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003234 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003235
3236 Python_Release_Vim();
3237 Py_END_ALLOW_THREADS
3238
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003239 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003240 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003241 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003242 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003244 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003245 }
3246 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003247 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003248
Bram Moolenaardb913952012-06-29 12:54:53 +02003249 clear_tv(&args);
3250 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003251 if (selfdict != NULL)
3252 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003253
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003254 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003255}
3256
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003257 static PyObject *
3258FunctionRepr(FunctionObject *self)
3259{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003260 PyObject *ret;
3261 garray_T repr_ga;
3262 int i;
3263 char_u *tofree = NULL;
3264 typval_T tv;
3265 char_u numbuf[NUMBUFLEN];
3266
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003267 ga_init2(&repr_ga, sizeof(char), 70);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003268 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3269 if (self->name)
3270 ga_concat(&repr_ga, self->name);
3271 else
3272 ga_concat(&repr_ga, (char_u *)"<NULL>");
3273 ga_append(&repr_ga, '\'');
3274 if (self->argv)
3275 {
3276 ga_concat(&repr_ga, (char_u *)", args=[");
3277 ++emsg_silent;
3278 for (i = 0; i < self->argc; i++)
3279 {
3280 if (i != 0)
3281 ga_concat(&repr_ga, (char_u *)", ");
3282 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3283 get_copyID()));
3284 vim_free(tofree);
3285 }
3286 --emsg_silent;
3287 ga_append(&repr_ga, ']');
3288 }
3289 if (self->self)
3290 {
3291 ga_concat(&repr_ga, (char_u *)", self=");
3292 tv.v_type = VAR_DICT;
3293 tv.vval.v_dict = self->self;
3294 ++emsg_silent;
3295 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3296 --emsg_silent;
3297 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003298 if (self->auto_rebind)
3299 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003300 }
3301 ga_append(&repr_ga, '>');
3302 ret = PyString_FromString((char *)repr_ga.ga_data);
3303 ga_clear(&repr_ga);
3304 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003305}
3306
Bram Moolenaardb913952012-06-29 12:54:53 +02003307static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003308 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3309 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003310};
3311
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003312/*
3313 * Options object
3314 */
3315
3316static PyTypeObject OptionsType;
3317
3318typedef int (*checkfun)(void *);
3319
3320typedef struct
3321{
3322 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003323 int opt_type;
3324 void *from;
3325 checkfun Check;
3326 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003327} OptionsObject;
3328
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003329 static int
3330dummy_check(void *arg UNUSED)
3331{
3332 return 0;
3333}
3334
3335 static PyObject *
3336OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3337{
3338 OptionsObject *self;
3339
Bram Moolenaar774267b2013-05-21 20:51:59 +02003340 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003341 if (self == NULL)
3342 return NULL;
3343
3344 self->opt_type = opt_type;
3345 self->from = from;
3346 self->Check = Check;
3347 self->fromObj = fromObj;
3348 if (fromObj)
3349 Py_INCREF(fromObj);
3350
3351 return (PyObject *)(self);
3352}
3353
3354 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003355OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003356{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003357 PyObject_GC_UnTrack((void *)(self));
3358 Py_XDECREF(self->fromObj);
3359 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003360}
3361
3362 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003364{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003365 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003366 return 0;
3367}
3368
3369 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003371{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003373 return 0;
3374}
3375
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003376 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003377OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003378{
3379 char_u *key;
3380 int flags;
3381 long numval;
3382 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003383 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003384
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003385 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386 return NULL;
3387
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003388 if (!(key = StringToChars(keyObject, &todecref)))
3389 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003390
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003391 if (*key == NUL)
3392 {
3393 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003394 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003395 return NULL;
3396 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003397
3398 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003399 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003400
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003401 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402
3403 if (flags == 0)
3404 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003405 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003406 return NULL;
3407 }
3408
3409 if (flags & SOPT_UNSET)
3410 {
3411 Py_INCREF(Py_None);
3412 return Py_None;
3413 }
3414 else if (flags & SOPT_BOOL)
3415 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003416 PyObject *ret;
3417 ret = numval ? Py_True : Py_False;
3418 Py_INCREF(ret);
3419 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003420 }
3421 else if (flags & SOPT_NUM)
3422 return PyInt_FromLong(numval);
3423 else if (flags & SOPT_STRING)
3424 {
3425 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003426 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003427 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003428 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003429 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003430 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003431 else
3432 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003433 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003434 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003435 return NULL;
3436 }
3437 }
3438 else
3439 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003440 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003441 return NULL;
3442 }
3443}
3444
3445 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003446OptionsContains(OptionsObject *self, PyObject *keyObject)
3447{
3448 char_u *key;
3449 PyObject *todecref;
3450
3451 if (!(key = StringToChars(keyObject, &todecref)))
3452 return -1;
3453
3454 if (*key == NUL)
3455 {
3456 Py_XDECREF(todecref);
3457 return 0;
3458 }
3459
3460 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3461 {
3462 Py_XDECREF(todecref);
3463 return 1;
3464 }
3465 else
3466 {
3467 Py_XDECREF(todecref);
3468 return 0;
3469 }
3470}
3471
3472typedef struct
3473{
3474 void *lastoption;
3475 int opt_type;
3476} optiterinfo_T;
3477
3478 static PyObject *
3479OptionsIterNext(optiterinfo_T **oii)
3480{
3481 char_u *name;
3482
3483 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3484 return PyString_FromString((char *)name);
3485
3486 return NULL;
3487}
3488
3489 static PyObject *
3490OptionsIter(OptionsObject *self)
3491{
3492 optiterinfo_T *oii;
3493
3494 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3495 {
3496 PyErr_NoMemory();
3497 return NULL;
3498 }
3499
3500 oii->opt_type = self->opt_type;
3501 oii->lastoption = NULL;
3502
3503 return IterNew(oii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003504 (destructorfun)(void *) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003505 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003506}
3507
3508 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003509set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003510{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003511 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003512
3513 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3514 {
3515 if (VimTryEnd())
3516 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003517 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003518 return FAIL;
3519 }
3520 return OK;
3521}
3522
3523 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003524set_option_value_for(
3525 char_u *key,
3526 int numval,
3527 char_u *stringval,
3528 int opt_flags,
3529 int opt_type,
3530 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003531{
Bram Moolenaar18f47402022-01-06 13:24:51 +00003532 switchwin_T switchwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003533 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003534 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003535
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003536 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003537 switch (opt_type)
3538 {
3539 case SREQ_WIN:
Bram Moolenaar18f47402022-01-06 13:24:51 +00003540 if (switch_win(&switchwin, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003541 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003542 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00003543 restore_win(&switchwin, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003544 if (VimTryEnd())
3545 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003546 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003547 return -1;
3548 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003549 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar18f47402022-01-06 13:24:51 +00003550 restore_win(&switchwin, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003551 break;
3552 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003553 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003554 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003555 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003556 break;
3557 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003558 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003559 break;
3560 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003561 if (set_ret == FAIL)
3562 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003563 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003564}
3565
3566 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003567OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003568{
3569 char_u *key;
3570 int flags;
3571 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003572 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003573 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003574
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003575 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003576 return -1;
3577
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003578 if (!(key = StringToChars(keyObject, &todecref)))
3579 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003580
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003581 if (*key == NUL)
3582 {
3583 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003584 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003585 return -1;
3586 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003587
3588 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003589 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003590
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003591 if (flags == 0)
3592 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003593 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003594 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003595 return -1;
3596 }
3597
3598 if (valObject == NULL)
3599 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003600 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003601 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003602 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003603 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003604 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003605 return -1;
3606 }
3607 else if (!(flags & SOPT_GLOBAL))
3608 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003609 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003610 N_("unable to unset option %s "
3611 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003612 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003613 return -1;
3614 }
3615 else
3616 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003617 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003618 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003619 return 0;
3620 }
3621 }
3622
Bram Moolenaard6e39182013-05-21 18:30:34 +02003623 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003624
3625 if (flags & SOPT_BOOL)
3626 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003627 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003628
Bram Moolenaarb983f752013-05-15 16:11:50 +02003629 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003630 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003631 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003632 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003633 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003634 }
3635 else if (flags & SOPT_NUM)
3636 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003637 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003638
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003639 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003640 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003641 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003642 return -1;
3643 }
3644
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003645 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003646 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003647 }
3648 else
3649 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003650 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003651 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003652
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003653 if ((val = StringToChars(valObject, &todecref2)))
3654 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003655 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003656 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003657 Py_XDECREF(todecref2);
3658 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003659 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003660 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003661 }
3662
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003663 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003664
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003665 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003666}
3667
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003668static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003669 0, // sq_length
3670 0, // sq_concat
3671 0, // sq_repeat
3672 0, // sq_item
3673 0, // sq_slice
3674 0, // sq_ass_item
3675 0, // sq_ass_slice
3676 (objobjproc) OptionsContains, // sq_contains
3677 0, // sq_inplace_concat
3678 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003679};
3680
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003681static PyMappingMethods OptionsAsMapping = {
3682 (lenfunc) NULL,
3683 (binaryfunc) OptionsItem,
3684 (objobjargproc) OptionsAssItem,
3685};
3686
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003687// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003688
3689typedef struct
3690{
3691 PyObject_HEAD
3692 tabpage_T *tab;
3693} TabPageObject;
3694
3695static PyObject *WinListNew(TabPageObject *tabObject);
3696
3697static PyTypeObject TabPageType;
3698
3699 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003700CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003701{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003702 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003703 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003704 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003705 return -1;
3706 }
3707
3708 return 0;
3709}
3710
3711 static PyObject *
3712TabPageNew(tabpage_T *tab)
3713{
3714 TabPageObject *self;
3715
3716 if (TAB_PYTHON_REF(tab))
3717 {
3718 self = TAB_PYTHON_REF(tab);
3719 Py_INCREF(self);
3720 }
3721 else
3722 {
3723 self = PyObject_NEW(TabPageObject, &TabPageType);
3724 if (self == NULL)
3725 return NULL;
3726 self->tab = tab;
3727 TAB_PYTHON_REF(tab) = self;
3728 }
3729
3730 return (PyObject *)(self);
3731}
3732
3733 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003734TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003735{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003736 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3737 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003738
3739 DESTRUCTOR_FINISH(self);
3740}
3741
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003742static char *TabPageAttrs[] = {
3743 "windows", "number", "vars", "window", "valid",
3744 NULL
3745};
3746
3747 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003748TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003749{
3750 return ObjectDir(self, TabPageAttrs);
3751}
3752
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003753 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003754TabPageAttrValid(TabPageObject *self, char *name)
3755{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003756 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003757
3758 if (strcmp(name, "valid") != 0)
3759 return NULL;
3760
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003761 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3762 Py_INCREF(ret);
3763 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003764}
3765
3766 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003767TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003768{
3769 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003770 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003772 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003773 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003774 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003775 else if (strcmp(name, "window") == 0)
3776 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003777 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02003778 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003779 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003780 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003781 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003782 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003783 else if (strcmp(name, "__members__") == 0)
3784 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003785 return NULL;
3786}
3787
3788 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003789TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003790{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003791 if (self->tab == INVALID_TABPAGE_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00003792 return PyString_FromFormat("<tabpage object (deleted) at %p>", (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003793 else
3794 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003795 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003796
3797 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003798 return PyString_FromFormat("<tabpage object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00003799 (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003800 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003801 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003802 }
3803}
3804
3805static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003806 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003807 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3808 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003809};
3810
3811/*
3812 * Window list object
3813 */
3814
3815static PyTypeObject TabListType;
3816static PySequenceMethods TabListAsSeq;
3817
3818typedef struct
3819{
3820 PyObject_HEAD
3821} TabListObject;
3822
3823 static PyInt
3824TabListLength(PyObject *self UNUSED)
3825{
3826 tabpage_T *tp = first_tabpage;
3827 PyInt n = 0;
3828
3829 while (tp != NULL)
3830 {
3831 ++n;
3832 tp = tp->tp_next;
3833 }
3834
3835 return n;
3836}
3837
3838 static PyObject *
3839TabListItem(PyObject *self UNUSED, PyInt n)
3840{
3841 tabpage_T *tp;
3842
3843 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3844 if (n == 0)
3845 return TabPageNew(tp);
3846
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003847 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003848 return NULL;
3849}
3850
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003851/*
3852 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003853 */
3854
3855typedef struct
3856{
3857 PyObject_HEAD
3858 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003859 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003860} WindowObject;
3861
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003862static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003863
3864 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003865CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003866{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003867 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003868 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003869 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003870 return -1;
3871 }
3872
3873 return 0;
3874}
3875
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003876 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003877WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003878{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003879 /*
3880 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02003881 * If we add a "w_python*_ref" field to the win_T structure,
3882 * then we can get at it in win_free() in vim. We then
3883 * need to create only ONE Python object per window - if
3884 * we try to create a second, just INCREF the existing one
3885 * and return it. The (single) Python object referring to
3886 * the window is stored in "w_python*_ref".
3887 * On a win_free() we set the Python object's win_T* field
3888 * to an invalid value. We trap all uses of a window
3889 * object, and reject them if the win_T* field is invalid.
3890 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003891 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003892 * w_python_ref and w_python3_ref fields respectively.
3893 */
3894
3895 WindowObject *self;
3896
3897 if (WIN_PYTHON_REF(win))
3898 {
3899 self = WIN_PYTHON_REF(win);
3900 Py_INCREF(self);
3901 }
3902 else
3903 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003904 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003905 if (self == NULL)
3906 return NULL;
3907 self->win = win;
3908 WIN_PYTHON_REF(win) = self;
3909 }
3910
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003911 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3912
Bram Moolenaar971db462013-05-12 18:44:48 +02003913 return (PyObject *)(self);
3914}
3915
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003916 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003917WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003918{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003919 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003920 if (self->win && self->win != INVALID_WINDOW_VALUE)
3921 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02003922 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02003923 PyObject_GC_Del((void *)(self));
3924}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003925
Bram Moolenaar774267b2013-05-21 20:51:59 +02003926 static int
3927WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3928{
3929 Py_VISIT(((PyObject *)(self->tabObject)));
3930 return 0;
3931}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003932
Bram Moolenaar774267b2013-05-21 20:51:59 +02003933 static int
3934WindowClear(WindowObject *self)
3935{
3936 Py_CLEAR(self->tabObject);
3937 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003938}
3939
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003940 static win_T *
3941get_firstwin(TabPageObject *tabObject)
3942{
3943 if (tabObject)
3944 {
3945 if (CheckTabPage(tabObject))
3946 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003947 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003948 else if (tabObject->tab == curtab)
3949 return firstwin;
3950 else
3951 return tabObject->tab->tp_firstwin;
3952 }
3953 else
3954 return firstwin;
3955}
Bram Moolenaare950f992018-06-10 13:55:55 +02003956
3957// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003958static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003959 "buffer",
3960 "cursor",
3961 "height",
3962 "row",
3963 "width",
3964 "col",
3965 "vars",
3966 "options",
3967 "number",
3968 "tabpage",
3969 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003970 NULL
3971};
3972
3973 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003974WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003975{
3976 return ObjectDir(self, WindowAttrs);
3977}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003978
Bram Moolenaar971db462013-05-12 18:44:48 +02003979 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003980WindowAttrValid(WindowObject *self, char *name)
3981{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003982 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003983
3984 if (strcmp(name, "valid") != 0)
3985 return NULL;
3986
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003987 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3988 Py_INCREF(ret);
3989 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003990}
3991
3992 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003993WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003994{
3995 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003997 else if (strcmp(name, "cursor") == 0)
3998 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004000
4001 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4002 }
4003 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004004 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004005 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004006 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004007 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004008 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004009 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004010 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004011 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004012 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004013 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004014 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4015 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004016 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004017 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004018 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004019 return NULL;
4020 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004021 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004022 }
4023 else if (strcmp(name, "tabpage") == 0)
4024 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004025 Py_INCREF(self->tabObject);
4026 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004027 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004028 else if (strcmp(name, "__members__") == 0)
4029 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004030 else
4031 return NULL;
4032}
4033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004035WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004037 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 return -1;
4039
4040 if (strcmp(name, "buffer") == 0)
4041 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004042 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043 return -1;
4044 }
4045 else if (strcmp(name, "cursor") == 0)
4046 {
4047 long lnum;
4048 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004049
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004050 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 return -1;
4052
Bram Moolenaard6e39182013-05-21 18:30:34 +02004053 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004055 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056 return -1;
4057 }
4058
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004059 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004060 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061 return -1;
4062
Bram Moolenaard6e39182013-05-21 18:30:34 +02004063 self->win->w_cursor.lnum = lnum;
4064 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004065 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004066 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004067 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004068 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004070 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071 return 0;
4072 }
4073 else if (strcmp(name, "height") == 0)
4074 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004075 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 win_T *savewin;
4077
Bram Moolenaardee2e312013-06-23 16:35:47 +02004078 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 return -1;
4080
4081#ifdef FEAT_GUI
4082 need_mouse_correct = TRUE;
4083#endif
4084 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004085 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004086
4087 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004088 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004090 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 return -1;
4092
4093 return 0;
4094 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 else if (strcmp(name, "width") == 0)
4096 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004097 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 win_T *savewin;
4099
Bram Moolenaardee2e312013-06-23 16:35:47 +02004100 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 return -1;
4102
4103#ifdef FEAT_GUI
4104 need_mouse_correct = TRUE;
4105#endif
4106 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004107 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004108
4109 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004110 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004112 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 return -1;
4114
4115 return 0;
4116 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004117 else
4118 {
4119 PyErr_SetString(PyExc_AttributeError, name);
4120 return -1;
4121 }
4122}
4123
4124 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004125WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004127 if (self->win == INVALID_WINDOW_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00004128 return PyString_FromFormat("<window object (deleted) at %p>", (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129 else
4130 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004131 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004132
Bram Moolenaar6d216452013-05-12 19:00:41 +02004133 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004134 return PyString_FromFormat("<window object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00004135 (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004137 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138 }
4139}
4140
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004141static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004142 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004143 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4144 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004145};
4146
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004148 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004149 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004150
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004151static PyTypeObject WinListType;
4152static PySequenceMethods WinListAsSeq;
4153
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004154typedef struct
4155{
4156 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004157 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004158} WinListObject;
4159
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004160 static PyObject *
4161WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004162{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004163 WinListObject *self;
4164
4165 self = PyObject_NEW(WinListObject, &WinListType);
4166 self->tabObject = tabObject;
4167 Py_INCREF(tabObject);
4168
4169 return (PyObject *)(self);
4170}
4171
4172 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004173WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004174{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004175 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004176
4177 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004178 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004179 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004180 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004181
4182 DESTRUCTOR_FINISH(self);
4183}
4184
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004185 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004186WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004187{
4188 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004189 PyInt n = 0;
4190
Bram Moolenaard6e39182013-05-21 18:30:34 +02004191 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004192 return -1;
4193
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004194 while (w != NULL)
4195 {
4196 ++n;
4197 w = W_NEXT(w);
4198 }
4199
4200 return n;
4201}
4202
4203 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004204WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004205{
4206 win_T *w;
4207
Bram Moolenaard6e39182013-05-21 18:30:34 +02004208 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004209 return NULL;
4210
4211 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004213 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004215 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 return NULL;
4217}
4218
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004219/*
4220 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004221 *
4222 * The result is in allocated memory. All internal nulls are replaced by
4223 * newline characters. It is an error for the string to contain newline
4224 * characters.
4225 *
4226 * On errors, the Python exception data is set, and NULL is returned.
4227 */
4228 static char *
4229StringToLine(PyObject *obj)
4230{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004231 char *str;
4232 char *save;
4233 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004234 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004235 PyInt i;
4236 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004237
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004238 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004239 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004240 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4241 || str == NULL)
4242 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004243 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004244 else if (PyUnicode_Check(obj))
4245 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004246 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4247 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004248 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004249
Bram Moolenaardaa27022013-06-24 22:33:30 +02004250 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004251 || str == NULL)
4252 {
4253 Py_DECREF(bytes);
4254 return NULL;
4255 }
4256 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004257 else
4258 {
4259#if PY_MAJOR_VERSION < 3
4260 PyErr_FORMAT(PyExc_TypeError,
4261 N_("expected str() or unicode() instance, but got %s"),
4262 Py_TYPE_NAME(obj));
4263#else
4264 PyErr_FORMAT(PyExc_TypeError,
4265 N_("expected bytes() or str() instance, but got %s"),
4266 Py_TYPE_NAME(obj));
4267#endif
4268 return NULL;
4269 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270
4271 /*
4272 * Error checking: String must not contain newlines, as we
4273 * are replacing a single line, and we must replace it with
4274 * a single line.
4275 * A trailing newline is removed, so that append(f.readlines()) works.
4276 */
4277 p = memchr(str, '\n', len);
4278 if (p != NULL)
4279 {
4280 if (p == str + len - 1)
4281 --len;
4282 else
4283 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004284 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004285 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004286 return NULL;
4287 }
4288 }
4289
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004290 /*
4291 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004292 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004293 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004294 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004295 if (save == NULL)
4296 {
4297 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004298 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004299 return NULL;
4300 }
4301
4302 for (i = 0; i < len; ++i)
4303 {
4304 if (str[i] == '\0')
4305 save[i] = '\n';
4306 else
4307 save[i] = str[i];
4308 }
4309
4310 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004311 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004312
4313 return save;
4314}
4315
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004316/*
4317 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004318 * in Vim format (1-based). The line is returned as a Python
4319 * string object.
4320 */
4321 static PyObject *
4322GetBufferLine(buf_T *buf, PyInt n)
4323{
4324 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4325}
4326
4327
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004328/*
4329 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004330 * are in Vim format (1-based). The range is from lo up to, but not
4331 * including, hi. The list is returned as a Python list of string objects.
4332 */
4333 static PyObject *
4334GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4335{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004336 PyInt i;
4337 PyInt n = hi - lo;
4338 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004339
4340 if (list == NULL)
4341 return NULL;
4342
4343 for (i = 0; i < n; ++i)
4344 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004345 linenr_T lnum = (linenr_T)(lo + i);
4346 char *text;
4347 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004348
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004349 if (lnum > buf->b_ml.ml_line_count)
4350 text = "";
4351 else
4352 text = (char *)ml_get_buf(buf, lnum, FALSE);
4353 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004354 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004355 {
4356 Py_DECREF(list);
4357 return NULL;
4358 }
4359
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004360 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004361 }
4362
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004363 // The ownership of the Python list is passed to the caller (ie,
4364 // the caller should Py_DECREF() the object when it is finished
4365 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004366
4367 return list;
4368}
4369
4370/*
4371 * Check if deleting lines made the cursor position invalid.
4372 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4373 * deleted).
4374 */
4375 static void
4376py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4377{
4378 if (curwin->w_cursor.lnum >= lo)
4379 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004380 // Adjust the cursor position if it's in/after the changed
4381 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004382 if (curwin->w_cursor.lnum >= hi)
4383 {
4384 curwin->w_cursor.lnum += extra;
4385 check_cursor_col();
4386 }
4387 else if (extra < 0)
4388 {
4389 curwin->w_cursor.lnum = lo;
4390 check_cursor();
4391 }
4392 else
4393 check_cursor_col();
4394 changed_cline_bef_curs();
4395 }
4396 invalidate_botline();
4397}
4398
Bram Moolenaar19e60942011-06-19 00:27:51 +02004399/*
4400 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 * in Vim format (1-based). The replacement line is given as
4402 * a Python string object. The object is checked for validity
4403 * and correct format. Errors are returned as a value of FAIL.
4404 * The return value is OK on success.
4405 * If OK is returned and len_change is not NULL, *len_change
4406 * is set to the change in the buffer length.
4407 */
4408 static int
4409SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4410{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004411 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004412 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004413
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004414 // First of all, we check the type of the supplied Python object.
4415 // There are three cases:
4416 // 1. NULL, or None - this is a deletion.
4417 // 2. A string - this is a replacement.
4418 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004419 if (line == Py_None || line == NULL)
4420 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004422 switchwin.sw_curwin = NULL;
4423 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004424
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004425 VimTryStart();
4426
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004427 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004428 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004429 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004430 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431 else
4432 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00004433 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004434 || save_curbuf.br_buf == NULL))
4435 // Using an existing window for the buffer, adjust the cursor
4436 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004437 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004438 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004439 // Only adjust marks if we managed to switch to a window that
4440 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004441 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004442 }
4443
Bram Moolenaar18f47402022-01-06 13:24:51 +00004444 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004446 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004447 return FAIL;
4448
4449 if (len_change)
4450 *len_change = -1;
4451
4452 return OK;
4453 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004454 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004455 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004456 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004457
4458 if (save == NULL)
4459 return FAIL;
4460
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004461 VimTryStart();
4462
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004463 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004464 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004465 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004466
4467 if (u_savesub((linenr_T)n) == FAIL)
4468 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004469 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004470 vim_free(save);
4471 }
4472 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4473 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004474 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004475 vim_free(save);
4476 }
4477 else
4478 changed_bytes((linenr_T)n, 0);
4479
Bram Moolenaar18f47402022-01-06 13:24:51 +00004480 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004481
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004482 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004483 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004484 check_cursor_col();
4485
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004486 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004487 return FAIL;
4488
4489 if (len_change)
4490 *len_change = 0;
4491
4492 return OK;
4493 }
4494 else
4495 {
4496 PyErr_BadArgument();
4497 return FAIL;
4498 }
4499}
4500
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004501/*
4502 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004503 * Vim format (1-based). The range is from lo up to, but not including, hi.
4504 * The replacement lines are given as a Python list of string objects. The
4505 * list is checked for validity and correct format. Errors are returned as a
4506 * value of FAIL. The return value is OK on success.
4507 * If OK is returned and len_change is not NULL, *len_change
4508 * is set to the change in the buffer length.
4509 */
4510 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004511SetBufferLineList(
4512 buf_T *buf,
4513 PyInt lo,
4514 PyInt hi,
4515 PyObject *list,
4516 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004517{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004518 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004519 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004520
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004521 // First of all, we check the type of the supplied Python object.
4522 // There are three cases:
4523 // 1. NULL, or None - this is a deletion.
4524 // 2. A list - this is a replacement.
4525 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004526 if (list == Py_None || list == NULL)
4527 {
4528 PyInt i;
4529 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004530
4531 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004532 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004533 switchwin.sw_curwin = NULL;
4534 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004535
4536 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004537 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004538 else
4539 {
4540 for (i = 0; i < n; ++i)
4541 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004542 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004543 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004544 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004545 break;
4546 }
4547 }
Bram Moolenaar18f47402022-01-06 13:24:51 +00004548 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004549 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004550 // Using an existing window for the buffer, adjust the cursor
4551 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004552 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004553 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004554 // Only adjust marks if we managed to switch to a window that
4555 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004556 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004557 }
4558
Bram Moolenaar18f47402022-01-06 13:24:51 +00004559 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004560
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004561 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004562 return FAIL;
4563
4564 if (len_change)
4565 *len_change = -n;
4566
4567 return OK;
4568 }
4569 else if (PyList_Check(list))
4570 {
4571 PyInt i;
4572 PyInt new_len = PyList_Size(list);
4573 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004574 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004575 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004576
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004577 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004578 array = NULL;
4579 else
4580 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004581 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004582 if (array == NULL)
4583 {
4584 PyErr_NoMemory();
4585 return FAIL;
4586 }
4587 }
4588
4589 for (i = 0; i < new_len; ++i)
4590 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004591 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004592
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004593 if (!(line = PyList_GetItem(list, i)) ||
4594 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004595 {
4596 while (i)
4597 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004598 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004599 return FAIL;
4600 }
4601 }
4602
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004603 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004604 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004605
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004606 // START of region without "return". Must call restore_buffer()!
Bram Moolenaar18f47402022-01-06 13:24:51 +00004607 switchwin.sw_curwin = NULL;
4608 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004609
4610 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004611 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004612
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004613 // If the size of the range is reducing (ie, new_len < old_len) we
4614 // need to delete some old_len. We do this at the start, by
4615 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004616 if (!PyErr_Occurred())
4617 {
4618 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004619 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004620 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004621 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 break;
4623 }
4624 extra -= i;
4625 }
4626
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004627 // For as long as possible, replace the existing old_len with the
4628 // new old_len. This is a more efficient operation, as it requires
4629 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004630 if (!PyErr_Occurred())
4631 {
4632 for (i = 0; i < old_len && i < new_len; ++i)
4633 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4634 == FAIL)
4635 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004636 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004637 break;
4638 }
4639 }
4640 else
4641 i = 0;
4642
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004643 // Now we may need to insert the remaining new old_len. If we do, we
4644 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004645 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004646 if (!PyErr_Occurred())
4647 {
4648 while (i < new_len)
4649 {
4650 if (ml_append((linenr_T)(lo + i - 1),
4651 (char_u *)array[i], 0, FALSE) == FAIL)
4652 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004653 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004654 break;
4655 }
4656 vim_free(array[i]);
4657 ++i;
4658 ++extra;
4659 }
4660 }
4661
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004662 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004663 while (i < new_len)
4664 {
4665 vim_free(array[i]);
4666 ++i;
4667 }
4668
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004669 // Free the array of old_len. All of its contents have now
4670 // been dealt with (either freed, or the responsibility passed
4671 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004672 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004673
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004674 // Adjust marks. Invalidate any which lie in the
4675 // changed range, and move any in the remainder of the buffer.
4676 // Only adjust marks if we managed to switch to a window that holds
4677 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004678 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004679 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004680 (long)MAXLNUM, (long)extra);
4681 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4682
Bram Moolenaar18f47402022-01-06 13:24:51 +00004683 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004684 || save_curbuf.br_buf == NULL))
4685 // Using an existing window for the buffer, adjust the cursor
4686 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004687 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4688
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004689 // END of region without "return".
Bram Moolenaar18f47402022-01-06 13:24:51 +00004690 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004691
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004692 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004693 return FAIL;
4694
4695 if (len_change)
4696 *len_change = new_len - old_len;
4697
4698 return OK;
4699 }
4700 else
4701 {
4702 PyErr_BadArgument();
4703 return FAIL;
4704 }
4705}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004707/*
4708 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004709 * The line number is in Vim format (1-based). The lines to be inserted are
4710 * given as a Python list of string objects or as a single string. The lines
4711 * to be added are checked for validity and correct format. Errors are
4712 * returned as a value of FAIL. The return value is OK on success.
4713 * If OK is returned and len_change is not NULL, *len_change
4714 * is set to the change in the buffer length.
4715 */
4716 static int
4717InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4718{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004719 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004720 switchwin_T switchwin;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004721
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004722 // First of all, we check the type of the supplied Python object.
4723 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004724 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004725 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004726 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727
4728 if (str == NULL)
4729 return FAIL;
4730
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004732 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004733 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004734
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004735 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004736 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004737 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004738 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004739 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004740 // Only adjust marks if we managed to switch to a window that
4741 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742 appended_lines_mark((linenr_T)n, 1L);
4743
4744 vim_free(str);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004745 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004746 update_screen(VALID);
4747
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004748 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 return FAIL;
4750
4751 if (len_change)
4752 *len_change = 1;
4753
4754 return OK;
4755 }
4756 else if (PyList_Check(lines))
4757 {
4758 PyInt i;
4759 PyInt size = PyList_Size(lines);
4760 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004761
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004762 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763 if (array == NULL)
4764 {
4765 PyErr_NoMemory();
4766 return FAIL;
4767 }
4768
4769 for (i = 0; i < size; ++i)
4770 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004771 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004772
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004773 if (!(line = PyList_GetItem(lines, i)) ||
4774 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004775 {
4776 while (i)
4777 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004778 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779 return FAIL;
4780 }
4781 }
4782
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004784 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004785 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786
4787 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004788 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004789 else
4790 {
4791 for (i = 0; i < size; ++i)
4792 {
4793 if (ml_append((linenr_T)(n + i),
4794 (char_u *)array[i], 0, FALSE) == FAIL)
4795 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004796 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004797
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004798 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004799 while (i < size)
4800 vim_free(array[i++]);
4801
4802 break;
4803 }
4804 vim_free(array[i]);
4805 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004806 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004807 // Only adjust marks if we managed to switch to a window that
4808 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004809 appended_lines_mark((linenr_T)n, (long)i);
4810 }
4811
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004812 // Free the array of lines. All of its contents have now
4813 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004814 PyMem_Free(array);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004815 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004816
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817 update_screen(VALID);
4818
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004819 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004820 return FAIL;
4821
4822 if (len_change)
4823 *len_change = size;
4824
4825 return OK;
4826 }
4827 else
4828 {
4829 PyErr_BadArgument();
4830 return FAIL;
4831 }
4832}
4833
4834/*
4835 * Common routines for buffers and line ranges
4836 * -------------------------------------------
4837 */
4838
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004839typedef struct
4840{
4841 PyObject_HEAD
4842 buf_T *buf;
4843} BufferObject;
4844
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004845 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004846CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004847{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004848 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004849 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004850 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004851 return -1;
4852 }
4853
4854 return 0;
4855}
4856
4857 static PyObject *
4858RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4859{
4860 if (CheckBuffer(self))
4861 return NULL;
4862
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004863 if (end == -1)
4864 end = self->buf->b_ml.ml_line_count;
4865
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004866 if (n < 0)
4867 n += end - start + 1;
4868
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004869 if (n < 0 || n > end - start)
4870 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004871 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004872 return NULL;
4873 }
4874
4875 return GetBufferLine(self->buf, n+start);
4876}
4877
4878 static PyObject *
4879RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4880{
4881 PyInt size;
4882
4883 if (CheckBuffer(self))
4884 return NULL;
4885
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004886 if (end == -1)
4887 end = self->buf->b_ml.ml_line_count;
4888
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004889 size = end - start + 1;
4890
4891 if (lo < 0)
4892 lo = 0;
4893 else if (lo > size)
4894 lo = size;
4895 if (hi < 0)
4896 hi = 0;
4897 if (hi < lo)
4898 hi = lo;
4899 else if (hi > size)
4900 hi = size;
4901
4902 return GetBufferLineList(self->buf, lo+start, hi+start);
4903}
4904
4905 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004906RBAsItem(
4907 BufferObject *self,
4908 PyInt n,
4909 PyObject *valObject,
4910 PyInt start,
4911 PyInt end,
4912 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004913{
4914 PyInt len_change;
4915
4916 if (CheckBuffer(self))
4917 return -1;
4918
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004919 if (end == -1)
4920 end = self->buf->b_ml.ml_line_count;
4921
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004922 if (n < 0)
4923 n += end - start + 1;
4924
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004925 if (n < 0 || n > end - start)
4926 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004927 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004928 return -1;
4929 }
4930
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004931 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004932 return -1;
4933
4934 if (new_end)
4935 *new_end = end + len_change;
4936
4937 return 0;
4938}
4939
Bram Moolenaar19e60942011-06-19 00:27:51 +02004940 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004941RBAsSlice(
4942 BufferObject *self,
4943 PyInt lo,
4944 PyInt hi,
4945 PyObject *valObject,
4946 PyInt start,
4947 PyInt end,
4948 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004949{
4950 PyInt size;
4951 PyInt len_change;
4952
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004953 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004954 if (CheckBuffer(self))
4955 return -1;
4956
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004957 if (end == -1)
4958 end = self->buf->b_ml.ml_line_count;
4959
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004960 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004961 size = end - start + 1;
4962
4963 if (lo < 0)
4964 lo = 0;
4965 else if (lo > size)
4966 lo = size;
4967 if (hi < 0)
4968 hi = 0;
4969 if (hi < lo)
4970 hi = lo;
4971 else if (hi > size)
4972 hi = size;
4973
4974 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004975 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004976 return -1;
4977
4978 if (new_end)
4979 *new_end = end + len_change;
4980
4981 return 0;
4982}
4983
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004984
4985 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004986RBAppend(
4987 BufferObject *self,
4988 PyObject *args,
4989 PyInt start,
4990 PyInt end,
4991 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992{
4993 PyObject *lines;
4994 PyInt len_change;
4995 PyInt max;
4996 PyInt n;
4997
4998 if (CheckBuffer(self))
4999 return NULL;
5000
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005001 if (end == -1)
5002 end = self->buf->b_ml.ml_line_count;
5003
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005004 max = n = end - start + 1;
5005
5006 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5007 return NULL;
5008
5009 if (n < 0 || n > max)
5010 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005011 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005012 return NULL;
5013 }
5014
5015 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5016 return NULL;
5017
5018 if (new_end)
5019 *new_end = end + len_change;
5020
5021 Py_INCREF(Py_None);
5022 return Py_None;
5023}
5024
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005025// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005026
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005027static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005028static PySequenceMethods RangeAsSeq;
5029static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005031typedef struct
5032{
5033 PyObject_HEAD
5034 BufferObject *buf;
5035 PyInt start;
5036 PyInt end;
5037} RangeObject;
5038
5039 static PyObject *
5040RangeNew(buf_T *buf, PyInt start, PyInt end)
5041{
5042 BufferObject *bufr;
5043 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005044 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005045 if (self == NULL)
5046 return NULL;
5047
5048 bufr = (BufferObject *)BufferNew(buf);
5049 if (bufr == NULL)
5050 {
5051 Py_DECREF(self);
5052 return NULL;
5053 }
5054 Py_INCREF(bufr);
5055
5056 self->buf = bufr;
5057 self->start = start;
5058 self->end = end;
5059
5060 return (PyObject *)(self);
5061}
5062
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005063 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005064RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005065{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005066 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005067 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005068 PyObject_GC_Del((void *)(self));
5069}
5070
5071 static int
5072RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5073{
5074 Py_VISIT(((PyObject *)(self->buf)));
5075 return 0;
5076}
5077
5078 static int
5079RangeClear(RangeObject *self)
5080{
5081 Py_CLEAR(self->buf);
5082 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005083}
5084
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005085 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005088 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005089 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005090 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093}
5094
5095 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005096RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005098 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005099}
5100
5101 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005102RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005104 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005105}
5106
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005107static char *RangeAttrs[] = {
5108 "start", "end",
5109 NULL
5110};
5111
5112 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005113RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005114{
5115 return ObjectDir(self, RangeAttrs);
5116}
5117
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005118 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005119RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005120{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122}
5123
5124 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005125RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005126{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005127 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005128 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00005129 (void *)self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005130 else
5131 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005132 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005133
5134 if (name == NULL)
5135 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005137 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005138 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139 }
5140}
5141
5142static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005143 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005144 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005145 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5146 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005147};
5148
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005149static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005150static PySequenceMethods BufferAsSeq;
5151static PyMappingMethods BufferAsMapping;
5152
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005153 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005154BufferNew(buf_T *buf)
5155{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005156 /*
5157 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005158 * If we add a "b_python*_ref" field to the buf_T structure,
5159 * then we can get at it in buf_freeall() in vim. We then
5160 * need to create only ONE Python object per buffer - if
5161 * we try to create a second, just INCREF the existing one
5162 * and return it. The (single) Python object referring to
5163 * the buffer is stored in "b_python*_ref".
5164 * Question: what to do on a buf_freeall(). We'll probably
5165 * have to either delete the Python object (DECREF it to
5166 * zero - a bad idea, as it leaves dangling refs!) or
5167 * set the buf_T * value to an invalid value (-1?), which
5168 * means we need checks in all access functions... Bah.
5169 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005170 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005171 * b_python_ref and b_python3_ref fields respectively.
5172 */
5173
5174 BufferObject *self;
5175
5176 if (BUF_PYTHON_REF(buf) != NULL)
5177 {
5178 self = BUF_PYTHON_REF(buf);
5179 Py_INCREF(self);
5180 }
5181 else
5182 {
5183 self = PyObject_NEW(BufferObject, &BufferType);
5184 if (self == NULL)
5185 return NULL;
5186 self->buf = buf;
5187 BUF_PYTHON_REF(buf) = self;
5188 }
5189
5190 return (PyObject *)(self);
5191}
5192
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005193 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005194BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005195{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005196 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5197 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005198
5199 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005200}
5201
Bram Moolenaar971db462013-05-12 18:44:48 +02005202 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005203BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005204{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005205 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005206 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005207 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005208
Bram Moolenaard6e39182013-05-21 18:30:34 +02005209 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005210}
5211
5212 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005213BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005214{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005216}
5217
5218 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005219BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005220{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005221 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005222}
5223
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005224static char *BufferAttrs[] = {
5225 "name", "number", "vars", "options", "valid",
5226 NULL
5227};
5228
5229 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005230BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005231{
5232 return ObjectDir(self, BufferAttrs);
5233}
5234
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005235 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005236BufferAttrValid(BufferObject *self, char *name)
5237{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005238 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005239
5240 if (strcmp(name, "valid") != 0)
5241 return NULL;
5242
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005243 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5244 Py_INCREF(ret);
5245 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005246}
5247
5248 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005249BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005250{
5251 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005252 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005253 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005254 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005255 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005256 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005257 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005259 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5260 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005261 else if (strcmp(name, "__members__") == 0)
5262 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005263 else
5264 return NULL;
5265}
5266
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005267 static int
5268BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5269{
5270 if (CheckBuffer(self))
5271 return -1;
5272
5273 if (strcmp(name, "name") == 0)
5274 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005275 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005276 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005277 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005278 PyObject *todecref;
5279
5280 if (!(val = StringToChars(valObject, &todecref)))
5281 return -1;
5282
5283 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005284 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005285 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005286 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005287 aucmd_restbuf(&aco);
5288 Py_XDECREF(todecref);
5289 if (VimTryEnd())
5290 return -1;
5291
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005292 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005293 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005294 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005295 return -1;
5296 }
5297 return 0;
5298 }
5299 else
5300 {
5301 PyErr_SetString(PyExc_AttributeError, name);
5302 return -1;
5303 }
5304}
5305
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005306 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005307BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005308{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005309 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005310}
5311
5312 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005313BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005314{
5315 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005316 char_u *pmark;
5317 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005318 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005319 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005320
Bram Moolenaard6e39182013-05-21 18:30:34 +02005321 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005322 return NULL;
5323
Bram Moolenaar389a1792013-06-23 13:00:44 +02005324 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005325 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005326
Bram Moolenaar389a1792013-06-23 13:00:44 +02005327 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005328 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005329 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005330 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005331 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005332 return NULL;
5333 }
5334
5335 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005336
5337 Py_XDECREF(todecref);
5338
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005339 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005340 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005341 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005342 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005343 if (VimTryEnd())
5344 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005345
5346 if (posp == NULL)
5347 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005348 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005349 return NULL;
5350 }
5351
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005352 if (posp->lnum <= 0)
5353 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005354 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005355 Py_INCREF(Py_None);
5356 return Py_None;
5357 }
5358
5359 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5360}
5361
5362 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005363BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005364{
5365 PyInt start;
5366 PyInt end;
5367
Bram Moolenaard6e39182013-05-21 18:30:34 +02005368 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005369 return NULL;
5370
5371 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5372 return NULL;
5373
Bram Moolenaard6e39182013-05-21 18:30:34 +02005374 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005375}
5376
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005377 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005378BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005379{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005380 if (self->buf == INVALID_BUFFER_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00005381 return PyString_FromFormat("<buffer object (deleted) at %p>", (void *)self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005382 else
5383 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005384 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005385
5386 if (name == NULL)
5387 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005388
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005389 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005390 }
5391}
5392
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005393static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005394 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005395 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005396 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005397 {"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 +02005398 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5399 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005400};
5401
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005402/*
5403 * Buffer list object - Implementation
5404 */
5405
5406static PyTypeObject BufMapType;
5407
5408typedef struct
5409{
5410 PyObject_HEAD
5411} BufMapObject;
5412
5413 static PyInt
5414BufMapLength(PyObject *self UNUSED)
5415{
5416 buf_T *b = firstbuf;
5417 PyInt n = 0;
5418
5419 while (b)
5420 {
5421 ++n;
5422 b = b->b_next;
5423 }
5424
5425 return n;
5426}
5427
5428 static PyObject *
5429BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5430{
5431 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005432 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005433
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005434 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005435 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005436
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005437 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005438
5439 if (b)
5440 return BufferNew(b);
5441 else
5442 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005443 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005444 return NULL;
5445 }
5446}
5447
5448 static void
5449BufMapIterDestruct(PyObject *buffer)
5450{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005451 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005452 if (buffer)
5453 {
5454 Py_DECREF(buffer);
5455 }
5456}
5457
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005458 static int
5459BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5460{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005461 if (buffer)
5462 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005463 return 0;
5464}
5465
5466 static int
5467BufMapIterClear(PyObject **buffer)
5468{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005469 if (*buffer)
5470 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005471 return 0;
5472}
5473
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005474 static PyObject *
5475BufMapIterNext(PyObject **buffer)
5476{
5477 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005478 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005479
5480 if (!*buffer)
5481 return NULL;
5482
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005483 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005484
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005485 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005486 {
5487 *buffer = NULL;
5488 return NULL;
5489 }
5490
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005491 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005492 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005493 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005494 return NULL;
5495 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005496 // Do not increment reference: we no longer hold it (decref), but whoever
5497 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005498 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005499}
5500
5501 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005502BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005503{
5504 PyObject *buffer;
5505
5506 buffer = BufferNew(firstbuf);
5507 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005508 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005509 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5510 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005511}
5512
5513static PyMappingMethods BufMapAsMapping = {
5514 (lenfunc) BufMapLength,
5515 (binaryfunc) BufMapItem,
5516 (objobjargproc) 0,
5517};
5518
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005519// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005520
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005521static char *CurrentAttrs[] = {
5522 "buffer", "window", "line", "range", "tabpage",
5523 NULL
5524};
5525
5526 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005527CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005528{
5529 return ObjectDir(self, CurrentAttrs);
5530}
5531
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005532 static PyObject *
5533CurrentGetattr(PyObject *self UNUSED, char *name)
5534{
5535 if (strcmp(name, "buffer") == 0)
5536 return (PyObject *)BufferNew(curbuf);
5537 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005538 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005539 else if (strcmp(name, "tabpage") == 0)
5540 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005541 else if (strcmp(name, "line") == 0)
5542 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5543 else if (strcmp(name, "range") == 0)
5544 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005545 else if (strcmp(name, "__members__") == 0)
5546 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005547 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005548#if PY_MAJOR_VERSION < 3
5549 return Py_FindMethod(WindowMethods, self, name);
5550#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005551 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005552#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005553}
5554
5555 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005556CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005557{
5558 if (strcmp(name, "line") == 0)
5559 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005560 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5561 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005562 return -1;
5563
5564 return 0;
5565 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005566 else if (strcmp(name, "buffer") == 0)
5567 {
5568 int count;
5569
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005570 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005571 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005572 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005573 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005574 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005575 return -1;
5576 }
5577
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005578 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005579 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005580 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005581
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005582 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005583 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5584 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005585 if (VimTryEnd())
5586 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005587 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005588 return -1;
5589 }
5590
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005591 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005592 }
5593 else if (strcmp(name, "window") == 0)
5594 {
5595 int count;
5596
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005597 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005598 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005599 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005600 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005601 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005602 return -1;
5603 }
5604
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005606 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005607 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005608
5609 if (!count)
5610 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005611 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005612 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005613 return -1;
5614 }
5615
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005616 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005617 win_goto(((WindowObject *)(valObject))->win);
5618 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005619 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005620 if (VimTryEnd())
5621 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005622 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005623 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005624 return -1;
5625 }
5626
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005627 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005628 }
5629 else if (strcmp(name, "tabpage") == 0)
5630 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005631 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005632 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005633 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005634 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005635 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005636 return -1;
5637 }
5638
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005639 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005640 return -1;
5641
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005642 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005643 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5644 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005645 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005646 if (VimTryEnd())
5647 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005648 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005649 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005650 return -1;
5651 }
5652
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005653 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005654 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005655 else
5656 {
5657 PyErr_SetString(PyExc_AttributeError, name);
5658 return -1;
5659 }
5660}
5661
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005662static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005663 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005664 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5665 { NULL, NULL, 0, NULL}
5666};
5667
Bram Moolenaardb913952012-06-29 12:54:53 +02005668 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005669init_range_cmd(exarg_T *eap)
5670{
5671 RangeStart = eap->line1;
5672 RangeEnd = eap->line2;
5673}
5674
5675 static void
5676init_range_eval(typval_T *rettv UNUSED)
5677{
5678 RangeStart = (PyInt) curwin->w_cursor.lnum;
5679 RangeEnd = RangeStart;
5680}
5681
5682 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005683run_cmd(const char *cmd, void *arg UNUSED
5684#ifdef PY_CAN_RECURSE
5685 , PyGILState_STATE *pygilstate UNUSED
5686#endif
5687 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005688{
Bram Moolenaar41009372013-07-01 22:03:04 +02005689 PyObject *run_ret;
5690 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5691 if (run_ret != NULL)
5692 {
5693 Py_DECREF(run_ret);
5694 }
5695 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5696 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005697 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005698 PyErr_Clear();
5699 }
5700 else
5701 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005702}
5703
5704static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5705static int code_hdr_len = 30;
5706
5707 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005708run_do(const char *cmd, void *arg UNUSED
5709#ifdef PY_CAN_RECURSE
5710 , PyGILState_STATE *pygilstate
5711#endif
5712 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005713{
5714 PyInt lnum;
5715 size_t len;
5716 char *code;
5717 int status;
5718 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005719 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005720 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005721
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005722 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005724 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005725 return;
5726 }
5727
5728 len = code_hdr_len + STRLEN(cmd);
5729 code = PyMem_New(char, len + 1);
5730 memcpy(code, code_hdr, code_hdr_len);
5731 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005732 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5733 status = -1;
5734 if (run_ret != NULL)
5735 {
5736 status = 0;
5737 Py_DECREF(run_ret);
5738 }
5739 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5740 {
5741 PyMem_Free(code);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005742 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005743 PyErr_Clear();
5744 return;
5745 }
5746 else
5747 PyErr_PrintEx(1);
5748
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005749 PyMem_Free(code);
5750
5751 if (status)
5752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005753 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005754 return;
5755 }
5756
5757 status = 0;
5758 pymain = PyImport_AddModule("__main__");
5759 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005760#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005761 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005762#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005763
5764 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5765 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005766 PyObject *line;
5767 PyObject *linenr;
5768 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005769
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005770#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005772#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005773 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005774 if (lnum > curbuf->b_ml.ml_line_count
5775 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005776 goto err;
5777 if (!(linenr = PyInt_FromLong((long) lnum)))
5778 {
5779 Py_DECREF(line);
5780 goto err;
5781 }
5782 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5783 Py_DECREF(line);
5784 Py_DECREF(linenr);
5785 if (!ret)
5786 goto err;
5787
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005788 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005789 if (curbuf != was_curbuf)
5790 {
5791 Py_XDECREF(ret);
5792 goto err;
5793 }
5794
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005795 if (ret != Py_None)
5796 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005797 {
5798 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005799 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005800 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005801
5802 Py_XDECREF(ret);
5803 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005804#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005805 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005806#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005807 }
5808 goto out;
5809err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005810#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005811 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005812#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005813 PyErr_PrintEx(0);
5814 PythonIO_Flush();
5815 status = 1;
5816out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005817#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005818 if (!status)
5819 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005820#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005821 Py_DECREF(pyfunc);
5822 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5823 if (status)
5824 return;
5825 check_cursor();
5826 update_curbuf(NOT_VALID);
5827}
5828
5829 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005830run_eval(const char *cmd, typval_T *rettv
5831#ifdef PY_CAN_RECURSE
5832 , PyGILState_STATE *pygilstate UNUSED
5833#endif
5834 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005835{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005836 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005837
Bram Moolenaar41009372013-07-01 22:03:04 +02005838 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005839 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005840 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005841 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005842 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005843 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005844 PyErr_Clear();
5845 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005846 else
5847 {
5848 if (PyErr_Occurred() && !msg_silent)
5849 PyErr_PrintEx(0);
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005850 emsg(_(e_eval_did_not_return_valid_python_object));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005851 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005852 }
5853 else
5854 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005855 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005856 emsg(_(e_failed_to_convert_returned_python_object_to_vim_value));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005857 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005858 }
5859 PyErr_Clear();
5860}
5861
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005862 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005863set_ref_in_py(const int copyID)
5864{
5865 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005866 list_T *ll;
5867 int i;
5868 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005869 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005870
5871 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005872 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005873 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005874 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5875 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005876 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005877
5878 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005879 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005880 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005881 {
5882 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005883 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005884 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005885 }
5886
Bram Moolenaar8110a092016-04-14 15:56:09 +02005887 if (lastfunc != NULL)
5888 {
5889 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5890 {
5891 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005892 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005893 if (func->argc)
5894 for (i = 0; !abort && i < func->argc; ++i)
5895 abort = abort
5896 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5897 }
5898 }
5899
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005900 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005901}
5902
5903 static int
5904set_string_copy(char_u *str, typval_T *tv)
5905{
5906 tv->vval.v_string = vim_strsave(str);
5907 if (tv->vval.v_string == NULL)
5908 {
5909 PyErr_NoMemory();
5910 return -1;
5911 }
5912 return 0;
5913}
5914
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005915 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005916pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005917{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005918 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005919 char_u *key;
5920 dictitem_T *di;
5921 PyObject *keyObject;
5922 PyObject *valObject;
5923 Py_ssize_t iter = 0;
5924
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005925 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005927
5928 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005929 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930
5931 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5932 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005933 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934
Bram Moolenaara03e6312013-05-29 22:49:26 +02005935 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005936 {
5937 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005938 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005939 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005940
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005941 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005942 {
5943 dict_unref(dict);
5944 return -1;
5945 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005946
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005947 if (*key == NUL)
5948 {
5949 dict_unref(dict);
5950 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005951 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005952 return -1;
5953 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005954
5955 di = dictitem_alloc(key);
5956
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005957 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005958
5959 if (di == NULL)
5960 {
5961 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005962 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 return -1;
5964 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005966 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005967 {
5968 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005969 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005970 return -1;
5971 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005972
5973 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005974 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005975 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005976 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005978 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005979 return -1;
5980 }
5981 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005982
5983 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005984 return 0;
5985}
5986
5987 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005988pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005990 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005991 char_u *key;
5992 dictitem_T *di;
5993 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005994 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005995 PyObject *keyObject;
5996 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005997
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005998 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005999 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006000
6001 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006002 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006003
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006004 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006005 {
6006 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006007 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006008 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006009
6010 if (!(iterator = PyObject_GetIter(list)))
6011 {
6012 dict_unref(dict);
6013 Py_DECREF(list);
6014 return -1;
6015 }
6016 Py_DECREF(list);
6017
6018 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006019 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006020 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006021
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006022 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006023 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006024 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006025 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006026 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006027 return -1;
6028 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006029
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006030 if (*key == NUL)
6031 {
6032 Py_DECREF(keyObject);
6033 Py_DECREF(iterator);
6034 Py_XDECREF(todecref);
6035 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006036 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006037 return -1;
6038 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006039
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006040 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006041 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006042 Py_DECREF(keyObject);
6043 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006044 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006045 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006046 return -1;
6047 }
6048
6049 di = dictitem_alloc(key);
6050
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006051 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006052 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006053
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006054 if (di == NULL)
6055 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006056 Py_DECREF(iterator);
6057 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006058 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006059 PyErr_NoMemory();
6060 return -1;
6061 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006062
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006063 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006065 Py_DECREF(iterator);
6066 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006067 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006068 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006069 return -1;
6070 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006071
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006072 Py_DECREF(valObject);
6073
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006074 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006075 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006076 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006077 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006078 dictitem_free(di);
6079 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006080 return -1;
6081 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006082 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006083 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006084 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006085 return 0;
6086}
6087
6088 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006089pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006090{
6091 list_T *l;
6092
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006093 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006094 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006095
6096 tv->v_type = VAR_LIST;
6097 tv->vval.v_list = l;
6098
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006099 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006100 {
6101 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006102 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006103 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006104
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006105 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006106 return 0;
6107}
6108
Bram Moolenaardb913952012-06-29 12:54:53 +02006109typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6110
6111 static int
6112convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006113 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006114{
6115 PyObject *capsule;
6116 char hexBuf[sizeof(void *) * 2 + 3];
6117
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006118 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006119
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006120#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006121 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006122#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006123 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006124#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006125 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006126 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006127#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006128 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006129#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006130 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006131#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006132 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6133 {
6134 Py_DECREF(capsule);
6135 tv->v_type = VAR_UNKNOWN;
6136 return -1;
6137 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006138
6139 Py_DECREF(capsule);
6140
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006141 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006142 {
6143 tv->v_type = VAR_UNKNOWN;
6144 return -1;
6145 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006146 // As we are not using copy_tv which increments reference count we must
6147 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006148 if (tv->v_type == VAR_DICT)
6149 ++tv->vval.v_dict->dv_refcount;
6150 else if (tv->v_type == VAR_LIST)
6151 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006152 }
6153 else
6154 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006155 typval_T *v;
6156
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006157#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006158 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006159#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006160 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006161#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 copy_tv(v, tv);
6163 }
6164 return 0;
6165}
6166
6167 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006168ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6169{
6170 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006171 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006172
6173 if (!(lookup_dict = PyDict_New()))
6174 return -1;
6175
6176 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6177 {
6178 tv->v_type = VAR_DICT;
6179 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6180 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006181 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006182 }
6183 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006184 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006185 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006186 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006187 else
6188 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006189 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006190 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006191 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006192 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006193 }
6194 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006195 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006196}
6197
6198 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006199ConvertFromPySequence(PyObject *obj, typval_T *tv)
6200{
6201 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006202 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006203
6204 if (!(lookup_dict = PyDict_New()))
6205 return -1;
6206
6207 if (PyType_IsSubtype(obj->ob_type, &ListType))
6208 {
6209 tv->v_type = VAR_LIST;
6210 tv->vval.v_list = (((ListObject *)(obj))->list);
6211 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006212 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006213 }
6214 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006215 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006216 else
6217 {
6218 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006219 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006220 Py_TYPE_NAME(obj));
6221 ret = -1;
6222 }
6223 Py_DECREF(lookup_dict);
6224 return ret;
6225}
6226
6227 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006228ConvertFromPyObject(PyObject *obj, typval_T *tv)
6229{
6230 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006231 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006232
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006233 if (!(lookup_dict = PyDict_New()))
6234 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006235 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006236 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006237 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006238}
6239
6240 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006241_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006242{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006243 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006244 {
6245 tv->v_type = VAR_DICT;
6246 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6247 ++tv->vval.v_dict->dv_refcount;
6248 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006249 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006250 {
6251 tv->v_type = VAR_LIST;
6252 tv->vval.v_list = (((ListObject *)(obj))->list);
6253 ++tv->vval.v_list->lv_refcount;
6254 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006255 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006257 FunctionObject *func = (FunctionObject *) obj;
6258 if (func->self != NULL || func->argv != NULL)
6259 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006260 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6261
Bram Moolenaar8110a092016-04-14 15:56:09 +02006262 set_partial(func, pt, TRUE);
6263 tv->vval.v_partial = pt;
6264 tv->v_type = VAR_PARTIAL;
6265 }
6266 else
6267 {
6268 if (set_string_copy(func->name, tv) == -1)
6269 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006270
Bram Moolenaar8110a092016-04-14 15:56:09 +02006271 tv->v_type = VAR_FUNC;
6272 }
6273 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006274 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006275 else if (PyBytes_Check(obj))
6276 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006277 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006279 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006280 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006281 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006282 return -1;
6283
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006284 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 return -1;
6286
6287 tv->v_type = VAR_STRING;
6288 }
6289 else if (PyUnicode_Check(obj))
6290 {
6291 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006292 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006293
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006294 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006295 if (bytes == NULL)
6296 return -1;
6297
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006298 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006299 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006300 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006301 return -1;
6302
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006303 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 {
6305 Py_XDECREF(bytes);
6306 return -1;
6307 }
6308 Py_XDECREF(bytes);
6309
6310 tv->v_type = VAR_STRING;
6311 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006312#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006313 else if (PyInt_Check(obj))
6314 {
6315 tv->v_type = VAR_NUMBER;
6316 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006317 if (PyErr_Occurred())
6318 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006319 }
6320#endif
6321 else if (PyLong_Check(obj))
6322 {
6323 tv->v_type = VAR_NUMBER;
6324 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006325 if (PyErr_Occurred())
6326 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006327 }
6328 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006329 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006330#ifdef FEAT_FLOAT
6331 else if (PyFloat_Check(obj))
6332 {
6333 tv->v_type = VAR_FLOAT;
6334 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6335 }
6336#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006337 else if (PyObject_HasAttrString(obj, "keys"))
6338 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006339 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006340 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006341 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006342 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006343 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006344 else if (PyNumber_Check(obj))
6345 {
6346 PyObject *num;
6347
6348 if (!(num = PyNumber_Long(obj)))
6349 return -1;
6350
6351 tv->v_type = VAR_NUMBER;
6352 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6353
6354 Py_DECREF(num);
6355 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006356 else if (obj == Py_None)
6357 {
6358 tv->v_type = VAR_SPECIAL;
6359 tv->vval.v_number = VVAL_NONE;
6360 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006361 else
6362 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006363 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006364 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006365 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006366 return -1;
6367 }
6368 return 0;
6369}
6370
6371 static PyObject *
6372ConvertToPyObject(typval_T *tv)
6373{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006374 typval_T *argv;
6375 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006376 if (tv == NULL)
6377 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006378 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006379 return NULL;
6380 }
6381 switch (tv->v_type)
6382 {
6383 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006384 return PyBytes_FromString(tv->vval.v_string == NULL
6385 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006386 case VAR_NUMBER:
6387 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006389#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006390 return PyFloat_FromDouble((double) tv->vval.v_float);
6391#endif
6392 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006393 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006394 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006395 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006397 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006398 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006399 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006400 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006401 if (tv->vval.v_partial->pt_argc)
6402 {
6403 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6404 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6405 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6406 }
6407 else
6408 argv = NULL;
6409 if (tv->vval.v_partial->pt_dict != NULL)
6410 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006411 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006412 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006413 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006414 tv->vval.v_partial->pt_dict,
6415 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006416 case VAR_BLOB:
6417 return PyBytes_FromStringAndSize(
6418 (char*) tv->vval.v_blob->bv_ga.ga_data,
6419 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006420 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006421 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006423 case VAR_CHANNEL:
6424 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006425 case VAR_INSTR:
Bram Moolenaardb913952012-06-29 12:54:53 +02006426 Py_INCREF(Py_None);
6427 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006428 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006429 case VAR_SPECIAL:
6430 switch (tv->vval.v_number)
6431 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006432 case VVAL_FALSE: return ALWAYS_FALSE;
6433 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006434 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006435 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006436 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006437 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006438 return NULL;
6439 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006440 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006441}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006442
6443typedef struct
6444{
6445 PyObject_HEAD
6446} CurrentObject;
6447static PyTypeObject CurrentType;
6448
6449 static void
6450init_structs(void)
6451{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006452 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006453 OutputType.tp_name = "vim.message";
6454 OutputType.tp_basicsize = sizeof(OutputObject);
6455 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6456 OutputType.tp_doc = "vim message object";
6457 OutputType.tp_methods = OutputMethods;
6458#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006459 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6460 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006461 OutputType.tp_alloc = call_PyType_GenericAlloc;
6462 OutputType.tp_new = call_PyType_GenericNew;
6463 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006464 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006466 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6467 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006468 // Disabled, because this causes a crash in test86
6469 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006470#endif
6471
Bram Moolenaara80faa82020-04-12 19:37:17 +02006472 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006473 IterType.tp_name = "vim.iter";
6474 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006475 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006476 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006477 IterType.tp_iter = (getiterfunc)IterIter;
6478 IterType.tp_iternext = (iternextfunc)IterNext;
6479 IterType.tp_dealloc = (destructor)IterDestructor;
6480 IterType.tp_traverse = (traverseproc)IterTraverse;
6481 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006482
Bram Moolenaara80faa82020-04-12 19:37:17 +02006483 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006484 BufferType.tp_name = "vim.buffer";
6485 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 BufferType.tp_dealloc = (destructor)BufferDestructor;
6487 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488 BufferType.tp_as_sequence = &BufferAsSeq;
6489 BufferType.tp_as_mapping = &BufferAsMapping;
6490 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6491 BufferType.tp_doc = "vim buffer object";
6492 BufferType.tp_methods = BufferMethods;
6493#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006494 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006495 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006496 BufferType.tp_alloc = call_PyType_GenericAlloc;
6497 BufferType.tp_new = call_PyType_GenericNew;
6498 BufferType.tp_free = call_PyObject_Free;
6499#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006500 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006501 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006502#endif
6503
Bram Moolenaara80faa82020-04-12 19:37:17 +02006504 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006505 WindowType.tp_name = "vim.window";
6506 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006507 WindowType.tp_dealloc = (destructor)WindowDestructor;
6508 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006509 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006510 WindowType.tp_doc = "vim Window object";
6511 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006512 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6513 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006514#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006515 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6516 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006517 WindowType.tp_alloc = call_PyType_GenericAlloc;
6518 WindowType.tp_new = call_PyType_GenericNew;
6519 WindowType.tp_free = call_PyObject_Free;
6520#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6522 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006523#endif
6524
Bram Moolenaara80faa82020-04-12 19:37:17 +02006525 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006526 TabPageType.tp_name = "vim.tabpage";
6527 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006528 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6529 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006530 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6531 TabPageType.tp_doc = "vim tab page object";
6532 TabPageType.tp_methods = TabPageMethods;
6533#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006534 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006535 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6536 TabPageType.tp_new = call_PyType_GenericNew;
6537 TabPageType.tp_free = call_PyObject_Free;
6538#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006539 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006540#endif
6541
Bram Moolenaara80faa82020-04-12 19:37:17 +02006542 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006543 BufMapType.tp_name = "vim.bufferlist";
6544 BufMapType.tp_basicsize = sizeof(BufMapObject);
6545 BufMapType.tp_as_mapping = &BufMapAsMapping;
6546 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006547 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006548 BufferType.tp_doc = "vim buffer list";
6549
Bram Moolenaara80faa82020-04-12 19:37:17 +02006550 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551 WinListType.tp_name = "vim.windowlist";
6552 WinListType.tp_basicsize = sizeof(WinListType);
6553 WinListType.tp_as_sequence = &WinListAsSeq;
6554 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6555 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006556 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006557
Bram Moolenaara80faa82020-04-12 19:37:17 +02006558 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006559 TabListType.tp_name = "vim.tabpagelist";
6560 TabListType.tp_basicsize = sizeof(TabListType);
6561 TabListType.tp_as_sequence = &TabListAsSeq;
6562 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6563 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006564
Bram Moolenaara80faa82020-04-12 19:37:17 +02006565 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006566 RangeType.tp_name = "vim.range";
6567 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006568 RangeType.tp_dealloc = (destructor)RangeDestructor;
6569 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006570 RangeType.tp_as_sequence = &RangeAsSeq;
6571 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006572 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006573 RangeType.tp_doc = "vim Range object";
6574 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006575 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6576 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006577#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006578 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579 RangeType.tp_alloc = call_PyType_GenericAlloc;
6580 RangeType.tp_new = call_PyType_GenericNew;
6581 RangeType.tp_free = call_PyObject_Free;
6582#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006583 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006584#endif
6585
Bram Moolenaara80faa82020-04-12 19:37:17 +02006586 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006587 CurrentType.tp_name = "vim.currentdata";
6588 CurrentType.tp_basicsize = sizeof(CurrentObject);
6589 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6590 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006591 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006592#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006593 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6594 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006595#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006596 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6597 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006598#endif
6599
Bram Moolenaara80faa82020-04-12 19:37:17 +02006600 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006601 DictionaryType.tp_name = "vim.dictionary";
6602 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006603 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006604 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006605 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006606 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006607 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006609 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6610 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6611 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006612#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006613 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6614 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006615#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006616 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6617 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006618#endif
6619
Bram Moolenaara80faa82020-04-12 19:37:17 +02006620 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006621 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006622 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623 ListType.tp_basicsize = sizeof(ListObject);
6624 ListType.tp_as_sequence = &ListAsSeq;
6625 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006626 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006627 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006628 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006629 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006630 ListType.tp_new = (newfunc)ListConstructor;
6631 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006632#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006633 ListType.tp_getattro = (getattrofunc)ListGetattro;
6634 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006635#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006636 ListType.tp_getattr = (getattrfunc)ListGetattr;
6637 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006638#endif
6639
Bram Moolenaara80faa82020-04-12 19:37:17 +02006640 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006641 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006642 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006643 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6644 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006645 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006646 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006647 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006648 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006649 FunctionType.tp_new = (newfunc)FunctionConstructor;
6650 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006651#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006652 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006653#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006654 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006655#endif
6656
Bram Moolenaara80faa82020-04-12 19:37:17 +02006657 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006658 OptionsType.tp_name = "vim.options";
6659 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006660 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006661 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006662 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006663 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006664 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006665 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6666 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6667 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006668
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006669#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006670 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006671 LoaderType.tp_name = "vim.Loader";
6672 LoaderType.tp_basicsize = sizeof(LoaderObject);
6673 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6674 LoaderType.tp_doc = "vim message object";
6675 LoaderType.tp_methods = LoaderMethods;
6676 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006677#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006678
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006679#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006680 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006681 vimmodule.m_name = "vim";
6682 vimmodule.m_doc = "Vim Python interface\n";
6683 vimmodule.m_size = -1;
6684 vimmodule.m_methods = VimMethods;
6685#endif
6686}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006687
6688#define PYTYPE_READY(type) \
kylo2529dac9b12022-03-27 20:05:17 +01006689 if (PyType_Ready(&(type))) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006690 return -1;
6691
6692 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006693init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006694{
6695 PYTYPE_READY(IterType);
6696 PYTYPE_READY(BufferType);
6697 PYTYPE_READY(RangeType);
6698 PYTYPE_READY(WindowType);
6699 PYTYPE_READY(TabPageType);
6700 PYTYPE_READY(BufMapType);
6701 PYTYPE_READY(WinListType);
6702 PYTYPE_READY(TabListType);
6703 PYTYPE_READY(CurrentType);
6704 PYTYPE_READY(DictionaryType);
6705 PYTYPE_READY(ListType);
6706 PYTYPE_READY(FunctionType);
6707 PYTYPE_READY(OptionsType);
6708 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006709#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006710 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006711#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006712 return 0;
6713}
6714
6715 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006716init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006717{
6718 PyObject *path;
6719 PyObject *path_hook;
6720 PyObject *path_hooks;
6721
6722 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6723 return -1;
6724
6725 if (!(path_hooks = PySys_GetObject("path_hooks")))
6726 {
6727 PyErr_Clear();
6728 path_hooks = PyList_New(1);
6729 PyList_SET_ITEM(path_hooks, 0, path_hook);
6730 if (PySys_SetObject("path_hooks", path_hooks))
6731 {
6732 Py_DECREF(path_hooks);
6733 return -1;
6734 }
6735 Py_DECREF(path_hooks);
6736 }
6737 else if (PyList_Check(path_hooks))
6738 {
6739 if (PyList_Append(path_hooks, path_hook))
6740 {
6741 Py_DECREF(path_hook);
6742 return -1;
6743 }
6744 Py_DECREF(path_hook);
6745 }
6746 else
6747 {
6748 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006749 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006750 "You should now do the following:\n"
6751 "- append vim.path_hook to sys.path_hooks\n"
6752 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006753 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006754 Py_DECREF(path_hook);
6755 return 0;
6756 }
6757
6758 if (!(path = PySys_GetObject("path")))
6759 {
6760 PyErr_Clear();
6761 path = PyList_New(1);
6762 Py_INCREF(vim_special_path_object);
6763 PyList_SET_ITEM(path, 0, vim_special_path_object);
6764 if (PySys_SetObject("path", path))
6765 {
6766 Py_DECREF(path);
6767 return -1;
6768 }
6769 Py_DECREF(path);
6770 }
6771 else if (PyList_Check(path))
6772 {
6773 if (PyList_Append(path, vim_special_path_object))
6774 return -1;
6775 }
6776 else
6777 {
6778 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006779 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006780 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006781 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006782 }
6783
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006784 return 0;
6785}
6786
6787static BufMapObject TheBufferMap =
6788{
6789 PyObject_HEAD_INIT(&BufMapType)
6790};
6791
6792static WinListObject TheWindowList =
6793{
6794 PyObject_HEAD_INIT(&WinListType)
6795 NULL
6796};
6797
6798static CurrentObject TheCurrent =
6799{
6800 PyObject_HEAD_INIT(&CurrentType)
6801};
6802
6803static TabListObject TheTabPageList =
6804{
6805 PyObject_HEAD_INIT(&TabListType)
6806};
6807
6808static struct numeric_constant {
6809 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006810 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006811} numeric_constants[] = {
6812 {"VAR_LOCKED", VAR_LOCKED},
6813 {"VAR_FIXED", VAR_FIXED},
6814 {"VAR_SCOPE", VAR_SCOPE},
6815 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6816};
6817
6818static struct object_constant {
6819 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006820 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006821} object_constants[] = {
6822 {"buffers", (PyObject *)(void *)&TheBufferMap},
6823 {"windows", (PyObject *)(void *)&TheWindowList},
6824 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6825 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006826
6827 {"Buffer", (PyObject *)&BufferType},
6828 {"Range", (PyObject *)&RangeType},
6829 {"Window", (PyObject *)&WindowType},
6830 {"TabPage", (PyObject *)&TabPageType},
6831 {"Dictionary", (PyObject *)&DictionaryType},
6832 {"List", (PyObject *)&ListType},
6833 {"Function", (PyObject *)&FunctionType},
6834 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006835#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006836 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006837#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006838};
6839
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006841 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006842 return -1;
6843
6844#define ADD_CHECKED_OBJECT(m, name, obj) \
6845 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006846 PyObject *valObject = obj; \
6847 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006848 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006849 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850 }
6851
6852 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006853populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006854{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006855 int i;
6856 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006857 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006858 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006859#if PY_VERSION_HEX >= 0x030700f0
6860 PyObject *dict;
6861 PyObject *cls;
6862#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006863
6864 for (i = 0; i < (int)(sizeof(numeric_constants)
6865 / sizeof(struct numeric_constant));
6866 ++i)
6867 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006868 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006869
6870 for (i = 0; i < (int)(sizeof(object_constants)
6871 / sizeof(struct object_constant));
6872 ++i)
6873 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006874 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006875
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006876 valObject = object_constants[i].valObject;
6877 Py_INCREF(valObject);
6878 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006879 }
6880
6881 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6882 return -1;
6883 ADD_OBJECT(m, "error", VimError);
6884
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006885 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6886 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006887 ADD_CHECKED_OBJECT(m, "options",
6888 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006890 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006892 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006893
Bram Moolenaar22081f42016-06-01 20:38:34 +02006894#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006895 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006897#else
6898 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6899 return -1;
6900#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006901 ADD_OBJECT(m, "_getcwd", py_getcwd)
6902
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006903 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006904 return -1;
6905 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006906 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006907 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006908 if (PyObject_SetAttrString(other_module, "chdir", attr))
6909 {
6910 Py_DECREF(attr);
6911 return -1;
6912 }
6913 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006914
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006915 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006916 {
6917 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006918 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006919 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006920 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6921 {
6922 Py_DECREF(attr);
6923 return -1;
6924 }
6925 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006926 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006927 else
6928 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006929
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006930 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6931 return -1;
6932
6933 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6934
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006935#if PY_VERSION_HEX >= 0x030700f0
6936 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6937 return -1;
6938
6939 dict = PyModule_GetDict(imp);
6940
6941 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6942 {
6943 Py_DECREF(imp);
6944 return -1;
6945 }
6946
6947 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6948 {
6949 Py_DECREF(imp);
6950 return -1;
6951 }
6952
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006953 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6954 {
6955 // find_module() is deprecated, this may stop working in some later
6956 // version.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01006957 ADD_OBJECT(m, "_find_module", py_find_module);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006958 }
6959
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006960 Py_DECREF(imp);
6961
6962 ADD_OBJECT(m, "_find_spec", py_find_spec);
6963#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006964 if (!(imp = PyImport_ImportModule("imp")))
6965 return -1;
6966
6967 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6968 {
6969 Py_DECREF(imp);
6970 return -1;
6971 }
6972
6973 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6974 {
6975 Py_DECREF(py_find_module);
6976 Py_DECREF(imp);
6977 return -1;
6978 }
6979
6980 Py_DECREF(imp);
6981
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006982 ADD_OBJECT(m, "_find_module", py_find_module);
6983 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006984#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006985
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006986 return 0;
6987}