blob: 240cc966ae629f819576e9e84c6a9a1c017e8a50 [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 Moolenaar37233f62022-05-22 12:23:48 +01004679 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004680 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004681 (long)MAXLNUM, (long)extra);
Bram Moolenaar37233f62022-05-22 12:23:48 +01004682 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4683 }
Bram Moolenaar19e60942011-06-19 00:27:51 +02004684
Bram Moolenaar18f47402022-01-06 13:24:51 +00004685 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004686 || save_curbuf.br_buf == NULL))
4687 // Using an existing window for the buffer, adjust the cursor
4688 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004689 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4690
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004691 // END of region without "return".
Bram Moolenaar18f47402022-01-06 13:24:51 +00004692 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004693
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004694 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004695 return FAIL;
4696
4697 if (len_change)
4698 *len_change = new_len - old_len;
4699
4700 return OK;
4701 }
4702 else
4703 {
4704 PyErr_BadArgument();
4705 return FAIL;
4706 }
4707}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004709/*
4710 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711 * The line number is in Vim format (1-based). The lines to be inserted are
4712 * given as a Python list of string objects or as a single string. The lines
4713 * to be added are checked for validity and correct format. Errors are
4714 * returned as a value of FAIL. The return value is OK on success.
4715 * If OK is returned and len_change is not NULL, *len_change
4716 * is set to the change in the buffer length.
4717 */
4718 static int
4719InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4720{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004721 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004722 switchwin_T switchwin;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004723
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004724 // First of all, we check the type of the supplied Python object.
4725 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004726 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004728 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004729
4730 if (str == NULL)
4731 return FAIL;
4732
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004734 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004735 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004736
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004737 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004738 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004739 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004740 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004741 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004742 // Only adjust marks if we managed to switch to a window that
4743 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004744 appended_lines_mark((linenr_T)n, 1L);
4745
4746 vim_free(str);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004747 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004748 update_screen(VALID);
4749
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004750 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004751 return FAIL;
4752
4753 if (len_change)
4754 *len_change = 1;
4755
4756 return OK;
4757 }
4758 else if (PyList_Check(lines))
4759 {
4760 PyInt i;
4761 PyInt size = PyList_Size(lines);
4762 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004764 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004765 if (array == NULL)
4766 {
4767 PyErr_NoMemory();
4768 return FAIL;
4769 }
4770
4771 for (i = 0; i < size; ++i)
4772 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004773 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004774
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004775 if (!(line = PyList_GetItem(lines, i)) ||
4776 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004777 {
4778 while (i)
4779 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004780 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004781 return FAIL;
4782 }
4783 }
4784
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004785 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004786 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004787 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004788
4789 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004790 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004791 else
4792 {
4793 for (i = 0; i < size; ++i)
4794 {
4795 if (ml_append((linenr_T)(n + i),
4796 (char_u *)array[i], 0, FALSE) == FAIL)
4797 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004798 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004799
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004800 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004801 while (i < size)
4802 vim_free(array[i++]);
4803
4804 break;
4805 }
4806 vim_free(array[i]);
4807 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004808 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004809 // Only adjust marks if we managed to switch to a window that
4810 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004811 appended_lines_mark((linenr_T)n, (long)i);
4812 }
4813
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004814 // Free the array of lines. All of its contents have now
4815 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004816 PyMem_Free(array);
Bram Moolenaar18f47402022-01-06 13:24:51 +00004817 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004818
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004819 update_screen(VALID);
4820
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004821 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004822 return FAIL;
4823
4824 if (len_change)
4825 *len_change = size;
4826
4827 return OK;
4828 }
4829 else
4830 {
4831 PyErr_BadArgument();
4832 return FAIL;
4833 }
4834}
4835
4836/*
4837 * Common routines for buffers and line ranges
4838 * -------------------------------------------
4839 */
4840
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004841typedef struct
4842{
4843 PyObject_HEAD
4844 buf_T *buf;
4845} BufferObject;
4846
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004847 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004848CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004849{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004850 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004851 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004852 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004853 return -1;
4854 }
4855
4856 return 0;
4857}
4858
4859 static PyObject *
4860RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4861{
4862 if (CheckBuffer(self))
4863 return NULL;
4864
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004865 if (end == -1)
4866 end = self->buf->b_ml.ml_line_count;
4867
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004868 if (n < 0)
4869 n += end - start + 1;
4870
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004871 if (n < 0 || n > end - start)
4872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004873 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004874 return NULL;
4875 }
4876
4877 return GetBufferLine(self->buf, n+start);
4878}
4879
4880 static PyObject *
4881RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4882{
4883 PyInt size;
4884
4885 if (CheckBuffer(self))
4886 return NULL;
4887
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004888 if (end == -1)
4889 end = self->buf->b_ml.ml_line_count;
4890
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004891 size = end - start + 1;
4892
4893 if (lo < 0)
4894 lo = 0;
4895 else if (lo > size)
4896 lo = size;
4897 if (hi < 0)
4898 hi = 0;
4899 if (hi < lo)
4900 hi = lo;
4901 else if (hi > size)
4902 hi = size;
4903
4904 return GetBufferLineList(self->buf, lo+start, hi+start);
4905}
4906
4907 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004908RBAsItem(
4909 BufferObject *self,
4910 PyInt n,
4911 PyObject *valObject,
4912 PyInt start,
4913 PyInt end,
4914 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004915{
4916 PyInt len_change;
4917
4918 if (CheckBuffer(self))
4919 return -1;
4920
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004921 if (end == -1)
4922 end = self->buf->b_ml.ml_line_count;
4923
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004924 if (n < 0)
4925 n += end - start + 1;
4926
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004927 if (n < 0 || n > end - start)
4928 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004929 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004930 return -1;
4931 }
4932
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004933 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004934 return -1;
4935
4936 if (new_end)
4937 *new_end = end + len_change;
4938
4939 return 0;
4940}
4941
Bram Moolenaar19e60942011-06-19 00:27:51 +02004942 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004943RBAsSlice(
4944 BufferObject *self,
4945 PyInt lo,
4946 PyInt hi,
4947 PyObject *valObject,
4948 PyInt start,
4949 PyInt end,
4950 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004951{
4952 PyInt size;
4953 PyInt len_change;
4954
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004955 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02004956 if (CheckBuffer(self))
4957 return -1;
4958
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004959 if (end == -1)
4960 end = self->buf->b_ml.ml_line_count;
4961
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004962 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02004963 size = end - start + 1;
4964
4965 if (lo < 0)
4966 lo = 0;
4967 else if (lo > size)
4968 lo = size;
4969 if (hi < 0)
4970 hi = 0;
4971 if (hi < lo)
4972 hi = lo;
4973 else if (hi > size)
4974 hi = size;
4975
4976 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004977 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004978 return -1;
4979
4980 if (new_end)
4981 *new_end = end + len_change;
4982
4983 return 0;
4984}
4985
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004986
4987 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004988RBAppend(
4989 BufferObject *self,
4990 PyObject *args,
4991 PyInt start,
4992 PyInt end,
4993 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004994{
4995 PyObject *lines;
4996 PyInt len_change;
4997 PyInt max;
4998 PyInt n;
4999
5000 if (CheckBuffer(self))
5001 return NULL;
5002
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005003 if (end == -1)
5004 end = self->buf->b_ml.ml_line_count;
5005
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005006 max = n = end - start + 1;
5007
5008 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5009 return NULL;
5010
5011 if (n < 0 || n > max)
5012 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005013 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005014 return NULL;
5015 }
5016
5017 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5018 return NULL;
5019
5020 if (new_end)
5021 *new_end = end + len_change;
5022
5023 Py_INCREF(Py_None);
5024 return Py_None;
5025}
5026
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005027// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005028
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005029static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005030static PySequenceMethods RangeAsSeq;
5031static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005032
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005033typedef struct
5034{
5035 PyObject_HEAD
5036 BufferObject *buf;
5037 PyInt start;
5038 PyInt end;
5039} RangeObject;
5040
5041 static PyObject *
5042RangeNew(buf_T *buf, PyInt start, PyInt end)
5043{
5044 BufferObject *bufr;
5045 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005046 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005047 if (self == NULL)
5048 return NULL;
5049
5050 bufr = (BufferObject *)BufferNew(buf);
5051 if (bufr == NULL)
5052 {
5053 Py_DECREF(self);
5054 return NULL;
5055 }
5056 Py_INCREF(bufr);
5057
5058 self->buf = bufr;
5059 self->start = start;
5060 self->end = end;
5061
5062 return (PyObject *)(self);
5063}
5064
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005065 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005067{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005068 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005069 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005070 PyObject_GC_Del((void *)(self));
5071}
5072
5073 static int
5074RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5075{
5076 Py_VISIT(((PyObject *)(self->buf)));
5077 return 0;
5078}
5079
5080 static int
5081RangeClear(RangeObject *self)
5082{
5083 Py_CLEAR(self->buf);
5084 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005085}
5086
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005090 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005091 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005092 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095}
5096
5097 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005098RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005099{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005100 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101}
5102
5103 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005104RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005105{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005106 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005107}
5108
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005109static char *RangeAttrs[] = {
5110 "start", "end",
5111 NULL
5112};
5113
5114 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005115RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005116{
5117 return ObjectDir(self, RangeAttrs);
5118}
5119
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005120 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005123 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124}
5125
5126 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005127RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005128{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005129 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005130 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00005131 (void *)self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005132 else
5133 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005134 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005135
5136 if (name == NULL)
5137 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005138
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005139 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005140 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005141 }
5142}
5143
5144static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005145 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005146 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005147 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5148 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005149};
5150
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005151static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005152static PySequenceMethods BufferAsSeq;
5153static PyMappingMethods BufferAsMapping;
5154
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005155 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005156BufferNew(buf_T *buf)
5157{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005158 /*
5159 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005160 * If we add a "b_python*_ref" field to the buf_T structure,
5161 * then we can get at it in buf_freeall() in vim. We then
5162 * need to create only ONE Python object per buffer - if
5163 * we try to create a second, just INCREF the existing one
5164 * and return it. The (single) Python object referring to
5165 * the buffer is stored in "b_python*_ref".
5166 * Question: what to do on a buf_freeall(). We'll probably
5167 * have to either delete the Python object (DECREF it to
5168 * zero - a bad idea, as it leaves dangling refs!) or
5169 * set the buf_T * value to an invalid value (-1?), which
5170 * means we need checks in all access functions... Bah.
5171 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005172 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005173 * b_python_ref and b_python3_ref fields respectively.
5174 */
5175
5176 BufferObject *self;
5177
5178 if (BUF_PYTHON_REF(buf) != NULL)
5179 {
5180 self = BUF_PYTHON_REF(buf);
5181 Py_INCREF(self);
5182 }
5183 else
5184 {
5185 self = PyObject_NEW(BufferObject, &BufferType);
5186 if (self == NULL)
5187 return NULL;
5188 self->buf = buf;
5189 BUF_PYTHON_REF(buf) = self;
5190 }
5191
5192 return (PyObject *)(self);
5193}
5194
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005195 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005196BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005197{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005198 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5199 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005200
5201 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005202}
5203
Bram Moolenaar971db462013-05-12 18:44:48 +02005204 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005205BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005206{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005207 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005208 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005209 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005210
Bram Moolenaard6e39182013-05-21 18:30:34 +02005211 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005212}
5213
5214 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005216{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005217 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005218}
5219
5220 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005221BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005222{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005223 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005224}
5225
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005226static char *BufferAttrs[] = {
5227 "name", "number", "vars", "options", "valid",
5228 NULL
5229};
5230
5231 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005232BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005233{
5234 return ObjectDir(self, BufferAttrs);
5235}
5236
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005237 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005238BufferAttrValid(BufferObject *self, char *name)
5239{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005240 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005241
5242 if (strcmp(name, "valid") != 0)
5243 return NULL;
5244
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005245 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5246 Py_INCREF(ret);
5247 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005248}
5249
5250 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005251BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005252{
5253 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005254 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005255 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005256 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005257 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005259 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005260 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005261 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5262 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005263 else if (strcmp(name, "__members__") == 0)
5264 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005265 else
5266 return NULL;
5267}
5268
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005269 static int
5270BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5271{
5272 if (CheckBuffer(self))
5273 return -1;
5274
5275 if (strcmp(name, "name") == 0)
5276 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005277 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005278 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005279 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005280 PyObject *todecref;
5281
5282 if (!(val = StringToChars(valObject, &todecref)))
5283 return -1;
5284
5285 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005286 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005287 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005288 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005289 aucmd_restbuf(&aco);
5290 Py_XDECREF(todecref);
5291 if (VimTryEnd())
5292 return -1;
5293
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005294 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005295 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005296 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005297 return -1;
5298 }
5299 return 0;
5300 }
5301 else
5302 {
5303 PyErr_SetString(PyExc_AttributeError, name);
5304 return -1;
5305 }
5306}
5307
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005308 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005309BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005310{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005311 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005312}
5313
5314 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005315BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005316{
5317 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005318 char_u *pmark;
5319 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005320 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005321 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005322
Bram Moolenaard6e39182013-05-21 18:30:34 +02005323 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005324 return NULL;
5325
Bram Moolenaar389a1792013-06-23 13:00:44 +02005326 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005327 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005328
Bram Moolenaar389a1792013-06-23 13:00:44 +02005329 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005330 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005331 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005332 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005333 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005334 return NULL;
5335 }
5336
5337 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005338
5339 Py_XDECREF(todecref);
5340
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005341 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005342 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005343 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005344 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005345 if (VimTryEnd())
5346 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005347
5348 if (posp == NULL)
5349 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005350 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005351 return NULL;
5352 }
5353
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005354 if (posp->lnum <= 0)
5355 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005356 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005357 Py_INCREF(Py_None);
5358 return Py_None;
5359 }
5360
5361 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5362}
5363
5364 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005365BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005366{
5367 PyInt start;
5368 PyInt end;
5369
Bram Moolenaard6e39182013-05-21 18:30:34 +02005370 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005371 return NULL;
5372
5373 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5374 return NULL;
5375
Bram Moolenaard6e39182013-05-21 18:30:34 +02005376 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005377}
5378
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005379 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005380BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005381{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005382 if (self->buf == INVALID_BUFFER_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00005383 return PyString_FromFormat("<buffer object (deleted) at %p>", (void *)self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005384 else
5385 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005386 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005387
5388 if (name == NULL)
5389 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005390
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005391 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005392 }
5393}
5394
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005395static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005396 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005397 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005398 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005399 {"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 +02005400 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5401 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005402};
5403
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005404/*
5405 * Buffer list object - Implementation
5406 */
5407
5408static PyTypeObject BufMapType;
5409
5410typedef struct
5411{
5412 PyObject_HEAD
5413} BufMapObject;
5414
5415 static PyInt
5416BufMapLength(PyObject *self UNUSED)
5417{
5418 buf_T *b = firstbuf;
5419 PyInt n = 0;
5420
5421 while (b)
5422 {
5423 ++n;
5424 b = b->b_next;
5425 }
5426
5427 return n;
5428}
5429
5430 static PyObject *
5431BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5432{
5433 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005434 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005435
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005436 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005437 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005438
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005439 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005440
5441 if (b)
5442 return BufferNew(b);
5443 else
5444 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005445 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005446 return NULL;
5447 }
5448}
5449
5450 static void
5451BufMapIterDestruct(PyObject *buffer)
5452{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005453 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005454 if (buffer)
5455 {
5456 Py_DECREF(buffer);
5457 }
5458}
5459
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005460 static int
5461BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5462{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005463 if (buffer)
5464 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005465 return 0;
5466}
5467
5468 static int
5469BufMapIterClear(PyObject **buffer)
5470{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005471 if (*buffer)
5472 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005473 return 0;
5474}
5475
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005476 static PyObject *
5477BufMapIterNext(PyObject **buffer)
5478{
5479 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005480 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005481
5482 if (!*buffer)
5483 return NULL;
5484
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005485 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005486
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005487 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005488 {
5489 *buffer = NULL;
5490 return NULL;
5491 }
5492
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005493 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005494 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005495 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005496 return NULL;
5497 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005498 // Do not increment reference: we no longer hold it (decref), but whoever
5499 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005500 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005501}
5502
5503 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005504BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005505{
5506 PyObject *buffer;
5507
5508 buffer = BufferNew(firstbuf);
5509 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005510 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005511 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5512 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005513}
5514
5515static PyMappingMethods BufMapAsMapping = {
5516 (lenfunc) BufMapLength,
5517 (binaryfunc) BufMapItem,
5518 (objobjargproc) 0,
5519};
5520
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005521// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005522
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005523static char *CurrentAttrs[] = {
5524 "buffer", "window", "line", "range", "tabpage",
5525 NULL
5526};
5527
5528 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005529CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005530{
5531 return ObjectDir(self, CurrentAttrs);
5532}
5533
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005534 static PyObject *
5535CurrentGetattr(PyObject *self UNUSED, char *name)
5536{
5537 if (strcmp(name, "buffer") == 0)
5538 return (PyObject *)BufferNew(curbuf);
5539 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005540 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005541 else if (strcmp(name, "tabpage") == 0)
5542 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005543 else if (strcmp(name, "line") == 0)
5544 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5545 else if (strcmp(name, "range") == 0)
5546 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005547 else if (strcmp(name, "__members__") == 0)
5548 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005549 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005550#if PY_MAJOR_VERSION < 3
5551 return Py_FindMethod(WindowMethods, self, name);
5552#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005553 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005554#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005555}
5556
5557 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005558CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005559{
5560 if (strcmp(name, "line") == 0)
5561 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005562 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5563 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005564 return -1;
5565
5566 return 0;
5567 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005568 else if (strcmp(name, "buffer") == 0)
5569 {
5570 int count;
5571
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005572 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005573 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005574 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005575 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005576 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005577 return -1;
5578 }
5579
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005580 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005581 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005582 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005583
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005584 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005585 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5586 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005587 if (VimTryEnd())
5588 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005589 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005590 return -1;
5591 }
5592
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005593 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005594 }
5595 else if (strcmp(name, "window") == 0)
5596 {
5597 int count;
5598
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005599 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005600 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005601 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005602 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005603 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005604 return -1;
5605 }
5606
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005607 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005608 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005609 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005610
5611 if (!count)
5612 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005613 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005614 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005615 return -1;
5616 }
5617
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005618 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005619 win_goto(((WindowObject *)(valObject))->win);
5620 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005621 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005622 if (VimTryEnd())
5623 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005624 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005625 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005626 return -1;
5627 }
5628
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005629 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005630 }
5631 else if (strcmp(name, "tabpage") == 0)
5632 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005633 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005634 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005635 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005636 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005637 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005638 return -1;
5639 }
5640
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005641 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005642 return -1;
5643
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005644 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005645 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5646 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005647 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005648 if (VimTryEnd())
5649 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005650 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005651 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005652 return -1;
5653 }
5654
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005655 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005656 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005657 else
5658 {
5659 PyErr_SetString(PyExc_AttributeError, name);
5660 return -1;
5661 }
5662}
5663
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005664static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005665 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005666 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5667 { NULL, NULL, 0, NULL}
5668};
5669
Bram Moolenaardb913952012-06-29 12:54:53 +02005670 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005671init_range_cmd(exarg_T *eap)
5672{
5673 RangeStart = eap->line1;
5674 RangeEnd = eap->line2;
5675}
5676
5677 static void
5678init_range_eval(typval_T *rettv UNUSED)
5679{
5680 RangeStart = (PyInt) curwin->w_cursor.lnum;
5681 RangeEnd = RangeStart;
5682}
5683
5684 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005685run_cmd(const char *cmd, void *arg UNUSED
5686#ifdef PY_CAN_RECURSE
5687 , PyGILState_STATE *pygilstate UNUSED
5688#endif
5689 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005690{
Bram Moolenaar41009372013-07-01 22:03:04 +02005691 PyObject *run_ret;
5692 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5693 if (run_ret != NULL)
5694 {
5695 Py_DECREF(run_ret);
5696 }
5697 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5698 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005699 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005700 PyErr_Clear();
5701 }
5702 else
5703 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005704}
5705
5706static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5707static int code_hdr_len = 30;
5708
5709 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005710run_do(const char *cmd, void *arg UNUSED
5711#ifdef PY_CAN_RECURSE
5712 , PyGILState_STATE *pygilstate
5713#endif
5714 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005715{
5716 PyInt lnum;
5717 size_t len;
5718 char *code;
5719 int status;
5720 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005721 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005722 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005723
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005724 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005726 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005727 return;
5728 }
5729
5730 len = code_hdr_len + STRLEN(cmd);
5731 code = PyMem_New(char, len + 1);
5732 memcpy(code, code_hdr, code_hdr_len);
5733 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005734 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5735 status = -1;
5736 if (run_ret != NULL)
5737 {
5738 status = 0;
5739 Py_DECREF(run_ret);
5740 }
5741 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5742 {
5743 PyMem_Free(code);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005744 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005745 PyErr_Clear();
5746 return;
5747 }
5748 else
5749 PyErr_PrintEx(1);
5750
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005751 PyMem_Free(code);
5752
5753 if (status)
5754 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005755 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005756 return;
5757 }
5758
5759 status = 0;
5760 pymain = PyImport_AddModule("__main__");
5761 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005762#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005763 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005764#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005765
5766 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5767 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005768 PyObject *line;
5769 PyObject *linenr;
5770 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005772#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005773 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005774#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005775 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005776 if (lnum > curbuf->b_ml.ml_line_count
5777 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005778 goto err;
5779 if (!(linenr = PyInt_FromLong((long) lnum)))
5780 {
5781 Py_DECREF(line);
5782 goto err;
5783 }
5784 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5785 Py_DECREF(line);
5786 Py_DECREF(linenr);
5787 if (!ret)
5788 goto err;
5789
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005790 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01005791 if (curbuf != was_curbuf)
5792 {
5793 Py_XDECREF(ret);
5794 goto err;
5795 }
5796
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005797 if (ret != Py_None)
5798 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005799 {
5800 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005801 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005802 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005803
5804 Py_XDECREF(ret);
5805 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005806#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005807 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005808#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005809 }
5810 goto out;
5811err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005812#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005813 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005814#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005815 PyErr_PrintEx(0);
5816 PythonIO_Flush();
5817 status = 1;
5818out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005819#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005820 if (!status)
5821 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005822#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005823 Py_DECREF(pyfunc);
5824 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5825 if (status)
5826 return;
5827 check_cursor();
5828 update_curbuf(NOT_VALID);
5829}
5830
5831 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005832run_eval(const char *cmd, typval_T *rettv
5833#ifdef PY_CAN_RECURSE
5834 , PyGILState_STATE *pygilstate UNUSED
5835#endif
5836 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005837{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005838 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005839
Bram Moolenaar41009372013-07-01 22:03:04 +02005840 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005841 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005842 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005843 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005844 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005845 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005846 PyErr_Clear();
5847 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005848 else
5849 {
5850 if (PyErr_Occurred() && !msg_silent)
5851 PyErr_PrintEx(0);
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005852 emsg(_(e_eval_did_not_return_valid_python_object));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005853 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005854 }
5855 else
5856 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005857 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005858 emsg(_(e_failed_to_convert_returned_python_object_to_vim_value));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005859 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005860 }
5861 PyErr_Clear();
5862}
5863
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005864 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005865set_ref_in_py(const int copyID)
5866{
5867 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005868 list_T *ll;
5869 int i;
5870 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005871 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005872
5873 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005874 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005875 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005876 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
5877 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005878 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005879
5880 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005881 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005882 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005883 {
5884 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005885 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005886 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005887 }
5888
Bram Moolenaar8110a092016-04-14 15:56:09 +02005889 if (lastfunc != NULL)
5890 {
5891 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5892 {
5893 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005894 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02005895 if (func->argc)
5896 for (i = 0; !abort && i < func->argc; ++i)
5897 abort = abort
5898 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5899 }
5900 }
5901
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005902 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005903}
5904
5905 static int
5906set_string_copy(char_u *str, typval_T *tv)
5907{
5908 tv->vval.v_string = vim_strsave(str);
5909 if (tv->vval.v_string == NULL)
5910 {
5911 PyErr_NoMemory();
5912 return -1;
5913 }
5914 return 0;
5915}
5916
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005917 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005918pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005919{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005920 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005921 char_u *key;
5922 dictitem_T *di;
5923 PyObject *keyObject;
5924 PyObject *valObject;
5925 Py_ssize_t iter = 0;
5926
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005927 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005928 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005929
5930 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005931 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005932
5933 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5934 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005935 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936
Bram Moolenaara03e6312013-05-29 22:49:26 +02005937 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005938 {
5939 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005940 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005941 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005942
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005943 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005944 {
5945 dict_unref(dict);
5946 return -1;
5947 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005948
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005949 if (*key == NUL)
5950 {
5951 dict_unref(dict);
5952 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005953 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005954 return -1;
5955 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956
5957 di = dictitem_alloc(key);
5958
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005959 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005960
5961 if (di == NULL)
5962 {
5963 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965 return -1;
5966 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005967
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005968 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969 {
5970 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005971 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005972 return -1;
5973 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005974
5975 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005977 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005978 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005979 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005980 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 return -1;
5982 }
5983 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005984
5985 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005986 return 0;
5987}
5988
5989 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005990pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005991{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005992 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005993 char_u *key;
5994 dictitem_T *di;
5995 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005996 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005997 PyObject *keyObject;
5998 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005999
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006000 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006001 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006002
6003 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006004 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006005
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006006 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006007 {
6008 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006009 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006010 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006011
6012 if (!(iterator = PyObject_GetIter(list)))
6013 {
6014 dict_unref(dict);
6015 Py_DECREF(list);
6016 return -1;
6017 }
6018 Py_DECREF(list);
6019
6020 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006021 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006022 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006023
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006024 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006025 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006026 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006027 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006028 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006029 return -1;
6030 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006031
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006032 if (*key == NUL)
6033 {
6034 Py_DECREF(keyObject);
6035 Py_DECREF(iterator);
6036 Py_XDECREF(todecref);
6037 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006038 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006039 return -1;
6040 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006041
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006042 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006043 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006044 Py_DECREF(keyObject);
6045 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006046 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006047 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006048 return -1;
6049 }
6050
6051 di = dictitem_alloc(key);
6052
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006053 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006054 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006055
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006056 if (di == NULL)
6057 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006058 Py_DECREF(iterator);
6059 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006061 PyErr_NoMemory();
6062 return -1;
6063 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006065 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006066 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006067 Py_DECREF(iterator);
6068 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006069 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006071 return -1;
6072 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006073
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006074 Py_DECREF(valObject);
6075
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006076 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006077 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006078 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006079 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006080 dictitem_free(di);
6081 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006082 return -1;
6083 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006084 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006085 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006086 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006087 return 0;
6088}
6089
6090 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006091pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006092{
6093 list_T *l;
6094
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006095 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006096 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006097
6098 tv->v_type = VAR_LIST;
6099 tv->vval.v_list = l;
6100
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006101 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006102 {
6103 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006104 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006105 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006106
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006107 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006108 return 0;
6109}
6110
Bram Moolenaardb913952012-06-29 12:54:53 +02006111typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6112
6113 static int
6114convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006115 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006116{
6117 PyObject *capsule;
6118 char hexBuf[sizeof(void *) * 2 + 3];
6119
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006120 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006121
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006122#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006123 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006124#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006125 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006126#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006127 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006128 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006129#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006130 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006131#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006132 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006133#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006134 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6135 {
6136 Py_DECREF(capsule);
6137 tv->v_type = VAR_UNKNOWN;
6138 return -1;
6139 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006140
6141 Py_DECREF(capsule);
6142
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006143 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006144 {
6145 tv->v_type = VAR_UNKNOWN;
6146 return -1;
6147 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006148 // As we are not using copy_tv which increments reference count we must
6149 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006150 if (tv->v_type == VAR_DICT)
6151 ++tv->vval.v_dict->dv_refcount;
6152 else if (tv->v_type == VAR_LIST)
6153 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006154 }
6155 else
6156 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006157 typval_T *v;
6158
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006159#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006160 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006161#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006162 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006163#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006164 copy_tv(v, tv);
6165 }
6166 return 0;
6167}
6168
6169 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006170ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6171{
6172 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006173 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006174
6175 if (!(lookup_dict = PyDict_New()))
6176 return -1;
6177
6178 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6179 {
6180 tv->v_type = VAR_DICT;
6181 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6182 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006183 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006184 }
6185 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006186 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006187 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006188 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006189 else
6190 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006191 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006192 N_("unable to convert %s to a Vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006193 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006194 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006195 }
6196 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006197 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006198}
6199
6200 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006201ConvertFromPySequence(PyObject *obj, typval_T *tv)
6202{
6203 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006204 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006205
6206 if (!(lookup_dict = PyDict_New()))
6207 return -1;
6208
6209 if (PyType_IsSubtype(obj->ob_type, &ListType))
6210 {
6211 tv->v_type = VAR_LIST;
6212 tv->vval.v_list = (((ListObject *)(obj))->list);
6213 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006214 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006215 }
6216 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006217 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006218 else
6219 {
6220 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006221 N_("unable to convert %s to a Vim list"),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006222 Py_TYPE_NAME(obj));
6223 ret = -1;
6224 }
6225 Py_DECREF(lookup_dict);
6226 return ret;
6227}
6228
6229 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006230ConvertFromPyObject(PyObject *obj, typval_T *tv)
6231{
6232 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006233 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006234
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006235 if (!(lookup_dict = PyDict_New()))
6236 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006237 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006239 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006240}
6241
6242 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006243_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006244{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006245 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006246 {
6247 tv->v_type = VAR_DICT;
6248 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6249 ++tv->vval.v_dict->dv_refcount;
6250 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006251 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 {
6253 tv->v_type = VAR_LIST;
6254 tv->vval.v_list = (((ListObject *)(obj))->list);
6255 ++tv->vval.v_list->lv_refcount;
6256 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006257 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006258 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006259 FunctionObject *func = (FunctionObject *) obj;
6260 if (func->self != NULL || func->argv != NULL)
6261 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006262 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6263
Bram Moolenaar8110a092016-04-14 15:56:09 +02006264 set_partial(func, pt, TRUE);
6265 tv->vval.v_partial = pt;
6266 tv->v_type = VAR_PARTIAL;
6267 }
6268 else
6269 {
6270 if (set_string_copy(func->name, tv) == -1)
6271 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006272
Bram Moolenaar8110a092016-04-14 15:56:09 +02006273 tv->v_type = VAR_FUNC;
6274 }
6275 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006277 else if (PyBytes_Check(obj))
6278 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006279 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006281 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006282 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006283 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006284 return -1;
6285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006286 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006287 return -1;
6288
6289 tv->v_type = VAR_STRING;
6290 }
6291 else if (PyUnicode_Check(obj))
6292 {
6293 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006294 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006295
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006296 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006297 if (bytes == NULL)
6298 return -1;
6299
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006300 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006301 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006302 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006303 return -1;
6304
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006305 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006306 {
6307 Py_XDECREF(bytes);
6308 return -1;
6309 }
6310 Py_XDECREF(bytes);
6311
6312 tv->v_type = VAR_STRING;
6313 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006314#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006315 else if (PyInt_Check(obj))
6316 {
6317 tv->v_type = VAR_NUMBER;
6318 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006319 if (PyErr_Occurred())
6320 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006321 }
6322#endif
6323 else if (PyLong_Check(obj))
6324 {
6325 tv->v_type = VAR_NUMBER;
6326 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006327 if (PyErr_Occurred())
6328 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006329 }
6330 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006331 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006332#ifdef FEAT_FLOAT
6333 else if (PyFloat_Check(obj))
6334 {
6335 tv->v_type = VAR_FLOAT;
6336 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6337 }
6338#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006339 else if (PyObject_HasAttrString(obj, "keys"))
6340 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006341 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006342 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006343 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006344 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006345 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006346 else if (PyNumber_Check(obj))
6347 {
6348 PyObject *num;
6349
6350 if (!(num = PyNumber_Long(obj)))
6351 return -1;
6352
6353 tv->v_type = VAR_NUMBER;
6354 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6355
6356 Py_DECREF(num);
6357 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006358 else if (obj == Py_None)
6359 {
6360 tv->v_type = VAR_SPECIAL;
6361 tv->vval.v_number = VVAL_NONE;
6362 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006363 else
6364 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006365 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar86181df2020-05-11 23:14:04 +02006366 N_("unable to convert %s to a Vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006367 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006368 return -1;
6369 }
6370 return 0;
6371}
6372
6373 static PyObject *
6374ConvertToPyObject(typval_T *tv)
6375{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006376 typval_T *argv;
6377 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006378 if (tv == NULL)
6379 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006380 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006381 return NULL;
6382 }
6383 switch (tv->v_type)
6384 {
6385 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006386 return PyBytes_FromString(tv->vval.v_string == NULL
6387 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 case VAR_NUMBER:
6389 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006390 case VAR_FLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01006391#ifdef FEAT_FLOAT
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 return PyFloat_FromDouble((double) tv->vval.v_float);
6393#endif
6394 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006395 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006397 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006399 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006400 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006401 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006402 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006403 if (tv->vval.v_partial->pt_argc)
6404 {
6405 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6406 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6407 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6408 }
6409 else
6410 argv = NULL;
6411 if (tv->vval.v_partial->pt_dict != NULL)
6412 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006413 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006414 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006415 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006416 tv->vval.v_partial->pt_dict,
6417 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006418 case VAR_BLOB:
6419 return PyBytes_FromStringAndSize(
6420 (char*) tv->vval.v_blob->bv_ga.ga_data,
6421 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006422 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006423 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006425 case VAR_CHANNEL:
6426 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006427 case VAR_INSTR:
Bram Moolenaardb913952012-06-29 12:54:53 +02006428 Py_INCREF(Py_None);
6429 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006430 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006431 case VAR_SPECIAL:
6432 switch (tv->vval.v_number)
6433 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006434 case VVAL_FALSE: return ALWAYS_FALSE;
6435 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006436 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006437 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006438 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006439 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006440 return NULL;
6441 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006442 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006443}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006444
6445typedef struct
6446{
6447 PyObject_HEAD
6448} CurrentObject;
6449static PyTypeObject CurrentType;
6450
6451 static void
6452init_structs(void)
6453{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006454 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006455 OutputType.tp_name = "vim.message";
6456 OutputType.tp_basicsize = sizeof(OutputObject);
6457 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6458 OutputType.tp_doc = "vim message object";
6459 OutputType.tp_methods = OutputMethods;
6460#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006461 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6462 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006463 OutputType.tp_alloc = call_PyType_GenericAlloc;
6464 OutputType.tp_new = call_PyType_GenericNew;
6465 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006466 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006468 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6469 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006470 // Disabled, because this causes a crash in test86
6471 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006472#endif
6473
Bram Moolenaara80faa82020-04-12 19:37:17 +02006474 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006475 IterType.tp_name = "vim.iter";
6476 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006477 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006478 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006479 IterType.tp_iter = (getiterfunc)IterIter;
6480 IterType.tp_iternext = (iternextfunc)IterNext;
6481 IterType.tp_dealloc = (destructor)IterDestructor;
6482 IterType.tp_traverse = (traverseproc)IterTraverse;
6483 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006484
Bram Moolenaara80faa82020-04-12 19:37:17 +02006485 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006486 BufferType.tp_name = "vim.buffer";
6487 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006488 BufferType.tp_dealloc = (destructor)BufferDestructor;
6489 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006490 BufferType.tp_as_sequence = &BufferAsSeq;
6491 BufferType.tp_as_mapping = &BufferAsMapping;
6492 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6493 BufferType.tp_doc = "vim buffer object";
6494 BufferType.tp_methods = BufferMethods;
6495#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006496 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006497 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006498 BufferType.tp_alloc = call_PyType_GenericAlloc;
6499 BufferType.tp_new = call_PyType_GenericNew;
6500 BufferType.tp_free = call_PyObject_Free;
6501#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006503 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504#endif
6505
Bram Moolenaara80faa82020-04-12 19:37:17 +02006506 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006507 WindowType.tp_name = "vim.window";
6508 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006509 WindowType.tp_dealloc = (destructor)WindowDestructor;
6510 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006511 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006512 WindowType.tp_doc = "vim Window object";
6513 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006514 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6515 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006516#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006517 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6518 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006519 WindowType.tp_alloc = call_PyType_GenericAlloc;
6520 WindowType.tp_new = call_PyType_GenericNew;
6521 WindowType.tp_free = call_PyObject_Free;
6522#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006523 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6524 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006525#endif
6526
Bram Moolenaara80faa82020-04-12 19:37:17 +02006527 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006528 TabPageType.tp_name = "vim.tabpage";
6529 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006530 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6531 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006532 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6533 TabPageType.tp_doc = "vim tab page object";
6534 TabPageType.tp_methods = TabPageMethods;
6535#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006536 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006537 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6538 TabPageType.tp_new = call_PyType_GenericNew;
6539 TabPageType.tp_free = call_PyObject_Free;
6540#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006541 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006542#endif
6543
Bram Moolenaara80faa82020-04-12 19:37:17 +02006544 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006545 BufMapType.tp_name = "vim.bufferlist";
6546 BufMapType.tp_basicsize = sizeof(BufMapObject);
6547 BufMapType.tp_as_mapping = &BufMapAsMapping;
6548 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006549 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006550 BufferType.tp_doc = "vim buffer list";
6551
Bram Moolenaara80faa82020-04-12 19:37:17 +02006552 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006553 WinListType.tp_name = "vim.windowlist";
6554 WinListType.tp_basicsize = sizeof(WinListType);
6555 WinListType.tp_as_sequence = &WinListAsSeq;
6556 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6557 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006558 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006559
Bram Moolenaara80faa82020-04-12 19:37:17 +02006560 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006561 TabListType.tp_name = "vim.tabpagelist";
6562 TabListType.tp_basicsize = sizeof(TabListType);
6563 TabListType.tp_as_sequence = &TabListAsSeq;
6564 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6565 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006566
Bram Moolenaara80faa82020-04-12 19:37:17 +02006567 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568 RangeType.tp_name = "vim.range";
6569 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006570 RangeType.tp_dealloc = (destructor)RangeDestructor;
6571 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006572 RangeType.tp_as_sequence = &RangeAsSeq;
6573 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006574 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006575 RangeType.tp_doc = "vim Range object";
6576 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006577 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6578 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006579#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006580 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006581 RangeType.tp_alloc = call_PyType_GenericAlloc;
6582 RangeType.tp_new = call_PyType_GenericNew;
6583 RangeType.tp_free = call_PyObject_Free;
6584#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006585 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006586#endif
6587
Bram Moolenaara80faa82020-04-12 19:37:17 +02006588 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006589 CurrentType.tp_name = "vim.currentdata";
6590 CurrentType.tp_basicsize = sizeof(CurrentObject);
6591 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6592 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006593 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006594#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006595 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6596 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006597#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006598 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6599 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600#endif
6601
Bram Moolenaara80faa82020-04-12 19:37:17 +02006602 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006603 DictionaryType.tp_name = "vim.dictionary";
6604 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006605 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006606 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006607 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006608 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006609 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006610 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006611 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6612 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6613 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006614#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006615 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6616 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006617#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006618 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6619 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006620#endif
6621
Bram Moolenaara80faa82020-04-12 19:37:17 +02006622 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006623 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006624 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006625 ListType.tp_basicsize = sizeof(ListObject);
6626 ListType.tp_as_sequence = &ListAsSeq;
6627 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006628 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006629 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006630 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006631 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006632 ListType.tp_new = (newfunc)ListConstructor;
6633 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006634#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006635 ListType.tp_getattro = (getattrofunc)ListGetattro;
6636 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006637#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006638 ListType.tp_getattr = (getattrfunc)ListGetattr;
6639 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006640#endif
6641
Bram Moolenaara80faa82020-04-12 19:37:17 +02006642 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006643 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006644 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006645 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6646 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006647 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006648 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006649 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006650 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006651 FunctionType.tp_new = (newfunc)FunctionConstructor;
6652 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006653#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006654 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006655#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006656 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006657#endif
6658
Bram Moolenaara80faa82020-04-12 19:37:17 +02006659 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006660 OptionsType.tp_name = "vim.options";
6661 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006662 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006663 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006664 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006665 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006666 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006667 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6668 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6669 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006670
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006671#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006672 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006673 LoaderType.tp_name = "vim.Loader";
6674 LoaderType.tp_basicsize = sizeof(LoaderObject);
6675 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6676 LoaderType.tp_doc = "vim message object";
6677 LoaderType.tp_methods = LoaderMethods;
6678 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006679#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006680
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006681#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006682 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006683 vimmodule.m_name = "vim";
6684 vimmodule.m_doc = "Vim Python interface\n";
6685 vimmodule.m_size = -1;
6686 vimmodule.m_methods = VimMethods;
6687#endif
6688}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006689
6690#define PYTYPE_READY(type) \
kylo2529dac9b12022-03-27 20:05:17 +01006691 if (PyType_Ready(&(type))) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006692 return -1;
6693
6694 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006695init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006696{
6697 PYTYPE_READY(IterType);
6698 PYTYPE_READY(BufferType);
6699 PYTYPE_READY(RangeType);
6700 PYTYPE_READY(WindowType);
6701 PYTYPE_READY(TabPageType);
6702 PYTYPE_READY(BufMapType);
6703 PYTYPE_READY(WinListType);
6704 PYTYPE_READY(TabListType);
6705 PYTYPE_READY(CurrentType);
6706 PYTYPE_READY(DictionaryType);
6707 PYTYPE_READY(ListType);
6708 PYTYPE_READY(FunctionType);
6709 PYTYPE_READY(OptionsType);
6710 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006711#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006712 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006713#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006714 return 0;
6715}
6716
6717 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006718init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006719{
6720 PyObject *path;
6721 PyObject *path_hook;
6722 PyObject *path_hooks;
6723
6724 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6725 return -1;
6726
6727 if (!(path_hooks = PySys_GetObject("path_hooks")))
6728 {
6729 PyErr_Clear();
6730 path_hooks = PyList_New(1);
6731 PyList_SET_ITEM(path_hooks, 0, path_hook);
6732 if (PySys_SetObject("path_hooks", path_hooks))
6733 {
6734 Py_DECREF(path_hooks);
6735 return -1;
6736 }
6737 Py_DECREF(path_hooks);
6738 }
6739 else if (PyList_Check(path_hooks))
6740 {
6741 if (PyList_Append(path_hooks, path_hook))
6742 {
6743 Py_DECREF(path_hook);
6744 return -1;
6745 }
6746 Py_DECREF(path_hook);
6747 }
6748 else
6749 {
6750 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006751 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006752 "You should now do the following:\n"
6753 "- append vim.path_hook to sys.path_hooks\n"
6754 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006755 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006756 Py_DECREF(path_hook);
6757 return 0;
6758 }
6759
6760 if (!(path = PySys_GetObject("path")))
6761 {
6762 PyErr_Clear();
6763 path = PyList_New(1);
6764 Py_INCREF(vim_special_path_object);
6765 PyList_SET_ITEM(path, 0, vim_special_path_object);
6766 if (PySys_SetObject("path", path))
6767 {
6768 Py_DECREF(path);
6769 return -1;
6770 }
6771 Py_DECREF(path);
6772 }
6773 else if (PyList_Check(path))
6774 {
6775 if (PyList_Append(path, vim_special_path_object))
6776 return -1;
6777 }
6778 else
6779 {
6780 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006781 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006782 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006783 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006784 }
6785
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006786 return 0;
6787}
6788
6789static BufMapObject TheBufferMap =
6790{
6791 PyObject_HEAD_INIT(&BufMapType)
6792};
6793
6794static WinListObject TheWindowList =
6795{
6796 PyObject_HEAD_INIT(&WinListType)
6797 NULL
6798};
6799
6800static CurrentObject TheCurrent =
6801{
6802 PyObject_HEAD_INIT(&CurrentType)
6803};
6804
6805static TabListObject TheTabPageList =
6806{
6807 PyObject_HEAD_INIT(&TabListType)
6808};
6809
6810static struct numeric_constant {
6811 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006812 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006813} numeric_constants[] = {
6814 {"VAR_LOCKED", VAR_LOCKED},
6815 {"VAR_FIXED", VAR_FIXED},
6816 {"VAR_SCOPE", VAR_SCOPE},
6817 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6818};
6819
6820static struct object_constant {
6821 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006822 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006823} object_constants[] = {
6824 {"buffers", (PyObject *)(void *)&TheBufferMap},
6825 {"windows", (PyObject *)(void *)&TheWindowList},
6826 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6827 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006828
6829 {"Buffer", (PyObject *)&BufferType},
6830 {"Range", (PyObject *)&RangeType},
6831 {"Window", (PyObject *)&WindowType},
6832 {"TabPage", (PyObject *)&TabPageType},
6833 {"Dictionary", (PyObject *)&DictionaryType},
6834 {"List", (PyObject *)&ListType},
6835 {"Function", (PyObject *)&FunctionType},
6836 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006837#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006838 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006839#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840};
6841
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006842#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006843 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006844 return -1;
6845
6846#define ADD_CHECKED_OBJECT(m, name, obj) \
6847 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006848 PyObject *valObject = obj; \
6849 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006850 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006851 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006852 }
6853
6854 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006855populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006856{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006857 int i;
6858 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006859 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006860 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006861#if PY_VERSION_HEX >= 0x030700f0
6862 PyObject *dict;
6863 PyObject *cls;
6864#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006865
6866 for (i = 0; i < (int)(sizeof(numeric_constants)
6867 / sizeof(struct numeric_constant));
6868 ++i)
6869 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006870 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006871
6872 for (i = 0; i < (int)(sizeof(object_constants)
6873 / sizeof(struct object_constant));
6874 ++i)
6875 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006876 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006877
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006878 valObject = object_constants[i].valObject;
6879 Py_INCREF(valObject);
6880 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006881 }
6882
6883 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6884 return -1;
6885 ADD_OBJECT(m, "error", VimError);
6886
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006887 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6888 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006889 ADD_CHECKED_OBJECT(m, "options",
6890 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006891
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006892 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006893 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006894 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006895
Bram Moolenaar22081f42016-06-01 20:38:34 +02006896#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006897 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006898 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006899#else
6900 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6901 return -1;
6902#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006903 ADD_OBJECT(m, "_getcwd", py_getcwd)
6904
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006905 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006906 return -1;
6907 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006908 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006909 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006910 if (PyObject_SetAttrString(other_module, "chdir", attr))
6911 {
6912 Py_DECREF(attr);
6913 return -1;
6914 }
6915 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006916
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006917 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006918 {
6919 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006920 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006921 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006922 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6923 {
6924 Py_DECREF(attr);
6925 return -1;
6926 }
6927 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006928 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006929 else
6930 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006931
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006932 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6933 return -1;
6934
6935 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6936
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006937#if PY_VERSION_HEX >= 0x030700f0
6938 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6939 return -1;
6940
6941 dict = PyModule_GetDict(imp);
6942
6943 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6944 {
6945 Py_DECREF(imp);
6946 return -1;
6947 }
6948
6949 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6950 {
6951 Py_DECREF(imp);
6952 return -1;
6953 }
6954
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006955 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
6956 {
6957 // find_module() is deprecated, this may stop working in some later
6958 // version.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01006959 ADD_OBJECT(m, "_find_module", py_find_module);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006960 }
6961
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006962 Py_DECREF(imp);
6963
6964 ADD_OBJECT(m, "_find_spec", py_find_spec);
6965#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006966 if (!(imp = PyImport_ImportModule("imp")))
6967 return -1;
6968
6969 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6970 {
6971 Py_DECREF(imp);
6972 return -1;
6973 }
6974
6975 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6976 {
6977 Py_DECREF(py_find_module);
6978 Py_DECREF(imp);
6979 return -1;
6980 }
6981
6982 Py_DECREF(imp);
6983
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006984 ADD_OBJECT(m, "_find_module", py_find_module);
6985 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006986#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006987
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006988 return 0;
6989}