blob: 6e2ef26e7dee9c4be94b71d3db76c78c48b41085 [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
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +020033#ifdef USE_LIMITED_API
34// Limited Python API. Need to call only exposed functions and remap macros.
35// PyTypeObject is an opaque struct.
36
37typedef struct {
38 lenfunc sq_length;
39 binaryfunc sq_concat;
40 ssizeargfunc sq_repeat;
41 ssizeargfunc sq_item;
42 void *was_sq_slice;
43 ssizeobjargproc sq_ass_item;
44 void *was_sq_ass_slice;
45 objobjproc sq_contains;
46
47 binaryfunc sq_inplace_concat;
48 ssizeargfunc sq_inplace_repeat;
49} PySequenceMethods;
50
51typedef struct {
52 lenfunc mp_length;
53 binaryfunc mp_subscript;
54 objobjargproc mp_ass_subscript;
55} PyMappingMethods;
56
57// This struct emulates the concrete _typeobject struct to allow the code to
58// work the same way in both limited and full Python APIs.
59struct typeobject_wrapper {
60 const char *tp_name;
61 Py_ssize_t tp_basicsize;
62 unsigned long tp_flags;
63
64 // When adding new slots below, also need to make sure we add ADD_TP_SLOT
65 // call in AddHeapType for it.
66
67 destructor tp_dealloc;
68 reprfunc tp_repr;
69
70 PySequenceMethods *tp_as_sequence;
71 PyMappingMethods *tp_as_mapping;
72
73 ternaryfunc tp_call;
74 getattrofunc tp_getattro;
75 setattrofunc tp_setattro;
76
77 const char *tp_doc;
78
79 traverseproc tp_traverse;
80
81 inquiry tp_clear;
82
83 getiterfunc tp_iter;
84 iternextfunc tp_iternext;
85
86 struct PyMethodDef *tp_methods;
87 struct _typeobject *tp_base;
88 allocfunc tp_alloc;
89 newfunc tp_new;
90 freefunc tp_free;
91};
92
93# define DEFINE_PY_TYPE_OBJECT(type) \
94 static struct typeobject_wrapper type; \
95 static PyTypeObject* type##Ptr = NULL
96
97// PyObject_HEAD_INIT_TYPE and PyObject_FINISH_INIT_TYPE need to come in pairs
98// We first initialize with NULL because the type is not allocated until
99// init_types() is called later. It's in FINISH_INIT_TYPE where we fill the
100// type in with the newly allocated type.
101# define PyObject_HEAD_INIT_TYPE(type) PyObject_HEAD_INIT(NULL)
102# define PyObject_FINISH_INIT_TYPE(obj, type) obj.ob_base.ob_type = type##Ptr
103
104# define Py_TYPE_GET_TP_ALLOC(type) ((allocfunc)PyType_GetSlot(type, Py_tp_alloc))
105# define Py_TYPE_GET_TP_METHODS(type) ((PyMethodDef *)PyType_GetSlot(type, Py_tp_methods))
106
107// PyObject_NEW is not part of stable ABI, but PyObject_Malloc/Init are.
108PyObject* Vim_PyObject_New(PyTypeObject *type, size_t objsize)
109{
110 PyObject *obj = (PyObject *)PyObject_Malloc(objsize);
111 if (obj == NULL)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +0200112 return PyErr_NoMemory();
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200113 return PyObject_Init(obj, type);
114}
115# undef PyObject_NEW
116# define PyObject_NEW(type, typeobj) ((type *)Vim_PyObject_New(typeobj, sizeof(type)))
117
118// This is a somewhat convoluted because limited API doesn't expose an easy way
119// to get the tp_name field, and so we have to manually reconstruct it as
120// "__module__.__name__" (with __module__ omitted for builtins to emulate
121// Python behavior). Also, some of the more convenient functions like
122// PyUnicode_AsUTF8AndSize and PyType_GetQualName() are not available until
123// late Python 3 versions, and won't be available if you set Py_LIMITED_API too
124// low.
125# define PyErr_FORMAT_TYPE(msg, obj) \
126 do { \
127 PyObject* qualname = PyObject_GetAttrString((PyObject*)(obj)->ob_type, "__qualname__"); \
128 if (qualname == NULL) \
129 { \
130 PyErr_FORMAT(PyExc_TypeError, msg, "(NULL)"); \
131 break; \
132 } \
133 PyObject* module = PyObject_GetAttrString((PyObject*)(obj)->ob_type, "__module__"); \
134 PyObject* full; \
135 if (module == NULL || PyUnicode_CompareWithASCIIString(module, "builtins") == 0 \
136 || PyUnicode_CompareWithASCIIString(module, "__main__") == 0) \
137 { \
138 full = qualname; \
139 Py_INCREF(full); \
140 } \
141 else \
142 full = PyUnicode_FromFormat("%U.%U", module, qualname); \
143 PyObject* full_bytes = PyUnicode_AsUTF8String(full); \
144 const char* full_str = PyBytes_AsString(full_bytes); \
145 full_str = full_str == NULL ? "(NULL)" : full_str; \
146 PyErr_FORMAT(PyExc_TypeError, msg, full_str); \
147 Py_DECREF(qualname); \
148 Py_XDECREF(module); \
149 Py_XDECREF(full); \
150 Py_XDECREF(full_bytes); \
Christian Brabandt75dc1ed2023-08-20 23:19:24 +0200151 } while (0)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200152
153# define PyList_GET_ITEM(list, i) PyList_GetItem(list, i)
154# define PyList_GET_SIZE(o) PyList_Size(o)
155# define PyTuple_GET_ITEM(o, pos) PyTuple_GetItem(o, pos)
156# define PyTuple_GET_SIZE(o) PyTuple_Size(o)
157
158// PyList_SET_ITEM and PyList_SetItem have slightly different behaviors. The
159// former will leave the old item dangling, and the latter will decref on it.
160// Since we only use this on new lists, this difference doesn't matter.
161# define PyList_SET_ITEM(list, i, item) PyList_SetItem(list, i, item)
162
163# if Py_LIMITED_API < 0x03080000
164// PyIter_check only became part of stable ABI in 3.8, and there is no easy way
165// to check for it in the API. We simply return false as a compromise. This
166// does mean we should avoid compiling with stable ABI < 3.8.
167# undef PyIter_Check
168# define PyIter_Check(obj) (FALSE)
169# endif
170
171PyTypeObject* AddHeapType(struct typeobject_wrapper* type_object)
172{
173 PyType_Spec type_spec;
174 type_spec.name = type_object->tp_name;
175 type_spec.basicsize = type_object->tp_basicsize;
176 type_spec.itemsize = 0;
177 type_spec.flags = type_object->tp_flags;
178
179 // We just need to statically allocate a large enough buffer that can hold
180 // all slots. We need to leave a null-terminated slot at the end.
181 PyType_Slot slots[40] = { {0, NULL} };
182 size_t slot_i = 0;
183
184# define ADD_TP_SLOT(slot_name) \
185 if (slot_i >= 40) return NULL; /* this should never happen */ \
186 if (type_object->slot_name != NULL) \
187 { \
188 slots[slot_i].slot = Py_##slot_name; \
189 slots[slot_i].pfunc = (void*)type_object->slot_name; \
190 ++slot_i; \
191 }
192# define ADD_TP_SUB_SLOT(sub_slot, slot_name) \
193 if (slot_i >= 40) return NULL; /* this should never happen */ \
194 if (type_object->sub_slot != NULL && type_object->sub_slot->slot_name != NULL) \
195 { \
196 slots[slot_i].slot = Py_##slot_name; \
197 slots[slot_i].pfunc = (void*)type_object->sub_slot->slot_name; \
198 ++slot_i; \
199 }
200
201 ADD_TP_SLOT(tp_dealloc)
202 ADD_TP_SLOT(tp_repr)
203 ADD_TP_SLOT(tp_call)
204 ADD_TP_SLOT(tp_getattro)
205 ADD_TP_SLOT(tp_setattro)
206 ADD_TP_SLOT(tp_doc)
207 ADD_TP_SLOT(tp_traverse)
208 ADD_TP_SLOT(tp_clear)
209 ADD_TP_SLOT(tp_iter)
210 ADD_TP_SLOT(tp_iternext)
211 ADD_TP_SLOT(tp_methods)
212 ADD_TP_SLOT(tp_base)
213 ADD_TP_SLOT(tp_alloc)
214 ADD_TP_SLOT(tp_new)
215 ADD_TP_SLOT(tp_free)
216
217 ADD_TP_SUB_SLOT(tp_as_sequence, sq_length)
218 ADD_TP_SUB_SLOT(tp_as_sequence, sq_concat)
219 ADD_TP_SUB_SLOT(tp_as_sequence, sq_repeat)
220 ADD_TP_SUB_SLOT(tp_as_sequence, sq_item)
221 ADD_TP_SUB_SLOT(tp_as_sequence, sq_ass_item)
222 ADD_TP_SUB_SLOT(tp_as_sequence, sq_contains)
223 ADD_TP_SUB_SLOT(tp_as_sequence, sq_inplace_concat)
224 ADD_TP_SUB_SLOT(tp_as_sequence, sq_inplace_repeat)
225
226 ADD_TP_SUB_SLOT(tp_as_mapping, mp_length)
227 ADD_TP_SUB_SLOT(tp_as_mapping, mp_subscript)
228 ADD_TP_SUB_SLOT(tp_as_mapping, mp_ass_subscript)
229# undef ADD_TP_SLOT
230# undef ADD_TP_SUB_SLOT
231
232 type_spec.slots = slots;
233
234 PyObject* newtype = PyType_FromSpec(&type_spec);
235 return (PyTypeObject*)newtype;
236}
237
238// Add a heap type, since static types do not work in limited API
239// Each PYTYPE_READY is paired with PYTYPE_CLEANUP.
240//
241// Note that we don't call Py_DECREF(type##Ptr) in clean up. The reason for
242// that in 3.7, it's possible to de-allocate a heap type before all instances
243// are cleared, leading to a crash, whereas in 3.8 the semantics were changed
244// and instances hold strong references to types. Since these types are
245// designed to be static, just keep them around to avoid having to write
246// version-specific handling. Vim does not re-start the Python runtime so there
247// will be no long-term leak.
248# define PYTYPE_READY(type) \
249 type##Ptr = AddHeapType(&(type)); \
250 if (type##Ptr == NULL) \
251 return -1;
252# define PYTYPE_CLEANUP(type) \
253 type##Ptr = NULL;
254
255// Limited API does not provide PyRun_* functions. Need to implement manually
256// using PyCompile and PyEval.
257PyObject* Vim_PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
258{
259 // Just pass "" for filename for now.
260 PyObject* compiled = Py_CompileString(str, "", start);
261 if (compiled == NULL)
262 return NULL;
263
264 PyObject* eval_result = PyEval_EvalCode(compiled, globals, locals);
265 Py_DECREF(compiled);
266 return eval_result;
267}
268int Vim_PyRun_SimpleString(const char *str)
269{
270 // This function emulates CPython's implementation.
271 PyObject* m = PyImport_AddModule("__main__");
272 if (m == NULL)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +0200273 return -1;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200274 PyObject* d = PyModule_GetDict(m);
275 PyObject* output = Vim_PyRun_String(str, Py_file_input, d, d);
276 if (output == NULL)
277 {
278 PyErr_PrintEx(TRUE);
279 return -1;
280 }
281 Py_DECREF(output);
282 return 0;
283}
284#define PyRun_String Vim_PyRun_String
285#define PyRun_SimpleString Vim_PyRun_SimpleString
286
287#else // !defined(USE_LIMITED_API)
288
289// Full Python API. Can make use of structs and macros directly.
290# define DEFINE_PY_TYPE_OBJECT(type) \
291 static PyTypeObject type; \
292 static PyTypeObject* type##Ptr = &type
293# define PyObject_HEAD_INIT_TYPE(type) PyObject_HEAD_INIT(&type)
294
295# define Py_TYPE_GET_TP_ALLOC(type) type->tp_alloc
296# define Py_TYPE_GET_TP_METHODS(type) type->tp_methods
297
298# define Py_TYPE_NAME(obj) ((obj)->ob_type->tp_name == NULL \
Bram Moolenaarc476e522013-06-23 13:46:40 +0200299 ? "(NULL)" \
kylo2529dac9b12022-03-27 20:05:17 +0100300 : (obj)->ob_type->tp_name)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200301# define PyErr_FORMAT_TYPE(msg, obj) \
302 PyErr_FORMAT(PyExc_TypeError, msg, \
303 Py_TYPE_NAME(obj))
304
305// Add a static type
306# define PYTYPE_READY(type) \
307 if (PyType_Ready(type##Ptr)) \
308 return -1;
309
310#endif
311
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200312
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200313#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200314 N_("empty keys are not allowed"))
315#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
316#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
317#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
318#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
319#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
320#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +0200321#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200322 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +0200323#define RAISE_INVALID_INDEX_TYPE(idx) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200324 PyErr_FORMAT_TYPE(N_("index must be int or slice, not %s"), idx);
Bram Moolenaar35eacd72013-05-30 22:06:33 +0200325
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200326#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
327#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200328#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200329
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200330typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200331typedef void (*runner)(const char *, void *
332#ifdef PY_CAN_RECURSE
333 , PyGILState_STATE *
334#endif
335 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200336
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200337static int ConvertFromPyObject(PyObject *, typval_T *);
338static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200339static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +0200340static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +0200341static PyObject *WindowNew(win_T *, tabpage_T *);
342static PyObject *BufferNew (buf_T *);
343static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200344
345static PyInt RangeStart;
346static PyInt RangeEnd;
347
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200348static PyObject *globals;
349
Bram Moolenaarf4258302013-06-02 18:20:17 +0200350static PyObject *py_chdir;
351static PyObject *py_fchdir;
352static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200353static PyObject *vim_module;
354static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200355
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200356#if PY_VERSION_HEX >= 0x030700f0
357static PyObject *py_find_spec;
358#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200359static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200360#endif
Bram Moolenaarb999ba22019-02-14 13:28:45 +0100361static PyObject *py_find_module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200362
363static PyObject *VimError;
364
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200365/*
366 * obtain a lock on the Vim data structures
367 */
368 static void
369Python_Lock_Vim(void)
370{
371}
372
373/*
374 * release a lock on the Vim data structures
375 */
376 static void
377Python_Release_Vim(void)
378{
379}
380
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200381/*
382 * The "todecref" argument holds a pointer to PyObject * that must be
383 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
384 * was needed to generate returned value is object.
385 *
386 * Use Py_XDECREF to decrement reference count.
387 */
388 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200389StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200390{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200391 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200392
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200393 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200394 {
395
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200396 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
397 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200398 return NULL;
399
400 *todecref = NULL;
401 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200402 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200403 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200404 PyObject *bytes;
405
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100406 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
407 ERRORS_ENCODE_ARG)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200408 return NULL;
409
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100410 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200411 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200412 {
413 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200414 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200415 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200416
417 *todecref = bytes;
418 }
419 else
420 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200421#if PY_MAJOR_VERSION < 3
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200422 PyErr_FORMAT_TYPE(N_("expected str() or unicode() instance, but got %s"),
423 obj);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200424#else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200425 PyErr_FORMAT_TYPE(N_("expected bytes() or str() instance, but got %s"),
426 obj);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200427#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200428 return NULL;
429 }
430
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200431 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200432}
433
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200434#define NUMBER_LONG 1
435#define NUMBER_INT 2
436#define NUMBER_NATURAL 4
437#define NUMBER_UNSIGNED 8
438
439 static int
440NumberToLong(PyObject *obj, long *result, int flags)
441{
442#if PY_MAJOR_VERSION < 3
443 if (PyInt_Check(obj))
444 {
445 *result = PyInt_AsLong(obj);
446 if (PyErr_Occurred())
447 return -1;
448 }
449 else
450#endif
451 if (PyLong_Check(obj))
452 {
453 *result = PyLong_AsLong(obj);
454 if (PyErr_Occurred())
455 return -1;
456 }
457 else if (PyNumber_Check(obj))
458 {
459 PyObject *num;
460
461 if (!(num = PyNumber_Long(obj)))
462 return -1;
463
464 *result = PyLong_AsLong(num);
465
466 Py_DECREF(num);
467
468 if (PyErr_Occurred())
469 return -1;
470 }
471 else
472 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200473#if PY_MAJOR_VERSION < 3
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200474 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200475 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200476 "coercing to long(), but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200477 obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200478#else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200479 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200480 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200481 "but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200482 obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200483#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200484 return -1;
485 }
486
487 if (flags & NUMBER_INT)
488 {
489 if (*result > INT_MAX)
490 {
491 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200492 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200493 return -1;
494 }
495 else if (*result < INT_MIN)
496 {
497 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200498 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200499 return -1;
500 }
501 }
502
503 if (flags & NUMBER_NATURAL)
504 {
505 if (*result <= 0)
506 {
507 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100508 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200509 return -1;
510 }
511 }
512 else if (flags & NUMBER_UNSIGNED)
513 {
514 if (*result < 0)
515 {
516 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200517 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200518 return -1;
519 }
520 }
521
522 return 0;
523}
524
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200525 static int
526add_string(PyObject *list, char *s)
527{
528 PyObject *string;
529
530 if (!(string = PyString_FromString(s)))
531 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200532
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200533 if (PyList_Append(list, string))
534 {
535 Py_DECREF(string);
536 return -1;
537 }
538
539 Py_DECREF(string);
540 return 0;
541}
542
543 static PyObject *
544ObjectDir(PyObject *self, char **attributes)
545{
546 PyMethodDef *method;
547 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200548 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200549
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200550 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200551 return NULL;
552
553 if (self)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200554 for (method = Py_TYPE_GET_TP_METHODS(self->ob_type) ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200555 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200556 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200557 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200558 return NULL;
559 }
560
561 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200562 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200563 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200564 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200565 return NULL;
566 }
567
568#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200569 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200570 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200571 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200572 return NULL;
573 }
574#endif
575
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200576 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200577}
578
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100579// Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200580
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100581// Function to write a line, points to either msg() or emsg().
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200582typedef int (*writefn)(char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200583
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200584DEFINE_PY_TYPE_OBJECT(OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200585
586typedef struct
587{
588 PyObject_HEAD
589 long softspace;
590 long error;
591} OutputObject;
592
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200593static char *OutputAttrs[] = {
594 "softspace",
595 NULL
596};
597
598 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200599OutputDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200600{
601 return ObjectDir(self, OutputAttrs);
602}
603
Bram Moolenaar77045652012-09-21 13:46:06 +0200604 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200605OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200606{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200607 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200608 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200609 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200610 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200611 return -1;
612 }
613
614 if (strcmp(name, "softspace") == 0)
615 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200616 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200617 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200618 return 0;
619 }
620
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200621 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200622 return -1;
623}
624
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100625// Buffer IO, we write one whole line at a time.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200626static garray_T io_ga = {0, 0, 1, 80, NULL};
627static writefn old_fn = NULL;
628
629 static void
630PythonIO_Flush(void)
631{
632 if (old_fn != NULL && io_ga.ga_len > 0)
633 {
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200634 ((char *)io_ga.ga_data)[io_ga.ga_len] = NUL;
635 old_fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200636 }
637 io_ga.ga_len = 0;
638}
639
640 static void
641writer(writefn fn, char_u *str, PyInt n)
642{
643 char_u *ptr;
644
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100645 // Flush when switching output function.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200646 if (fn != old_fn)
647 PythonIO_Flush();
648 old_fn = fn;
649
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200650 // Write each NL separated line. Text after the last NL is kept for
651 // writing later.
652 // For normal messages: Do not output when "got_int" was set. This avoids
653 // a loop gone crazy flooding the terminal with messages. Also for when
654 // "q" is pressed at the more-prompt.
655 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
656 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200657 {
658 PyInt len = ptr - str;
659
660 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
661 break;
662
663 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
664 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200665 fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200666 str = ptr + 1;
667 n -= len + 1;
668 io_ga.ga_len = 0;
669 }
670
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200671 // Put the remaining text into io_ga for later printing.
672 if (n > 0 && (fn == (writefn)emsg || !got_int)
673 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200674 {
675 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
676 io_ga.ga_len += (int)n;
677 }
678}
679
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200680 static int
681write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200682{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200683 Py_ssize_t len = 0;
684 char *str = NULL;
685 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200686
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200687 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
688 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200689
690 Py_BEGIN_ALLOW_THREADS
691 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200692 if (error)
693 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100694 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200695 Python_Release_Vim();
696 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200697 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200698
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200699 return 0;
700}
701
702 static PyObject *
703OutputWrite(OutputObject *self, PyObject *string)
704{
705 if (write_output(self, string))
706 return NULL;
707
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200708 Py_INCREF(Py_None);
709 return Py_None;
710}
711
712 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200713OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200714{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200715 PyObject *iterator;
716 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200717
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200718 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200719 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200720
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200721 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200722 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200723 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200724 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200725 Py_DECREF(iterator);
726 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200727 return NULL;
728 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200729 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200730 }
731
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200732 Py_DECREF(iterator);
733
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100734 // Iterator may have finished due to an exception
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200735 if (PyErr_Occurred())
736 return NULL;
737
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200738 Py_INCREF(Py_None);
739 return Py_None;
740}
741
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100742 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200743AlwaysNone(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100744{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100745 // do nothing
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100746 Py_INCREF(Py_None);
747 return Py_None;
748}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200749#define ALWAYS_NONE AlwaysNone(NULL, NULL)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100750
Bram Moolenaard4247472015-11-02 13:28:59 +0100751 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200752AlwaysFalse(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100753{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100754 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100755 PyObject *ret = Py_False;
756 Py_INCREF(ret);
757 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100758}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200759#define ALWAYS_FALSE AlwaysFalse(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100760
761 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200762AlwaysTrue(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100763{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100764 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100765 PyObject *ret = Py_True;
766 Py_INCREF(ret);
767 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100768}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200769#define ALWAYS_TRUE AlwaysTrue(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100770
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200771/***************/
772
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200773static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100774 // name, function, calling, doc
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200775 {"write", (PyCFunction)OutputWrite, METH_O, ""},
776 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100777 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
778 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
779 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
780 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
781 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
782 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200783 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200784 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200785 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200786};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200787
788static OutputObject Output =
789{
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200790 PyObject_HEAD_INIT_TYPE(OutputType)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200791 0,
792 0
793};
794
795static OutputObject Error =
796{
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200797 PyObject_HEAD_INIT_TYPE(OutputType)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200798 0,
799 1
800};
801
802 static int
803PythonIO_Init_io(void)
804{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200805 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
806 return -1;
807 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
808 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200809
810 if (PyErr_Occurred())
811 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000812 emsg(_(e_python_error_initialising_io_object));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200813 return -1;
814 }
815
816 return 0;
817}
818
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200819#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200820static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
821
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200822typedef struct
823{
824 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200825 char *fullname;
826 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200827} LoaderObject;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200828DEFINE_PY_TYPE_OBJECT(LoaderType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200829
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200830 static void
831LoaderDestructor(LoaderObject *self)
832{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200833 vim_free(self->fullname);
834 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200835 DESTRUCTOR_FINISH(self);
836}
837
838 static PyObject *
839LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
840{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200841 char *fullname = self->fullname;
842 PyObject *result = self->result;
843 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200844
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200845 if (!fullname)
846 {
847 module = result ? result : Py_None;
848 Py_INCREF(module);
849 return module;
850 }
851
852 module = call_load_module(fullname, (int)STRLEN(fullname), result);
853
854 self->fullname = NULL;
855 self->result = module;
856
857 vim_free(fullname);
858 Py_DECREF(result);
859
860 if (!module)
861 {
862 if (PyErr_Occurred())
863 return NULL;
864
865 Py_INCREF(Py_None);
866 return Py_None;
867 }
868
869 Py_INCREF(module);
870 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200871}
872
873static struct PyMethodDef LoaderMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100874 // name, function, calling, doc
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200875 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
876 { NULL, NULL, 0, NULL}
877};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200878#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200879
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100880/*
881 * Check to see whether a Vim error has been reported, or a keyboard
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200882 * interrupt has been detected.
883 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200884 static void
885VimTryStart(void)
886{
887 ++trylevel;
888}
889
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200890 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200891VimTryEnd(void)
892{
893 --trylevel;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100894 // Without this it stops processing all subsequent Vim script commands and
895 // generates strange error messages if I e.g. try calling Test() in a cycle
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200896 did_emsg = FALSE;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100897 // Keyboard interrupt should be preferred over anything else
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200898 if (got_int)
899 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100900 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100901 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100902 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200903 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200904 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200905 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100906 else if (msg_list != NULL && *msg_list != NULL)
907 {
908 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100909 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100910
911 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
912
913 if (msg == NULL)
914 {
915 PyErr_NoMemory();
916 return -1;
917 }
918
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100919 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100920
921 free_global_msglist();
922
923 if (should_free)
924 vim_free(msg);
925
926 return -1;
927 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200928 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200929 return (PyErr_Occurred() ? -1 : 0);
Bram Moolenaar86181df2020-05-11 23:14:04 +0200930 // Python exception is preferred over Vim one; unlikely to occur though
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200931 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200932 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100933 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200934 return -1;
935 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100936 // Finally transform Vim script exception to python one
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200937 else
938 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200939 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200940 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200941 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200942 }
943}
944
945 static int
946VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200947{
948 if (got_int)
949 {
950 PyErr_SetNone(PyExc_KeyboardInterrupt);
951 return 1;
952 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200953 return 0;
954}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200955
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100956// Vim module - Implementation
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200957
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200958 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200959VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200960{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200961 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200962 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200963 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200964
Bram Moolenaar389a1792013-06-23 13:00:44 +0200965 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200966 return NULL;
967
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200968 Py_BEGIN_ALLOW_THREADS
969 Python_Lock_Vim();
970
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200971 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200972 do_cmdline_cmd(cmd);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100973 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200974
975 Python_Release_Vim();
976 Py_END_ALLOW_THREADS
977
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200978 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200979 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200980 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200981 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200982
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200983 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200984 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200985 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200986}
987
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200988/*
989 * Function to translate a typval_T into a PyObject; this will recursively
990 * translate lists/dictionaries into their Python equivalents.
991 *
992 * The depth parameter is to avoid infinite recursion, set it to 1 when
993 * you call VimToPython.
994 */
995 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200996VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200997{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200998 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200999 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001000 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001001
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001002 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001003 if (depth > 100)
1004 {
1005 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001006 ret = Py_None;
1007 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001008 }
1009
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001010 // Check if we run into a recursive loop. The item must be in lookup_dict
1011 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001012 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
1013 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
1014 {
Bram Moolenaardb913952012-06-29 12:54:53 +02001015 sprintf(ptrBuf, "%p",
1016 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
1017 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001018
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001019 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001020 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001021 Py_INCREF(ret);
1022 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001023 }
1024 }
1025
1026 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001027 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02001028 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001029 else if (our_tv->v_type == VAR_NUMBER)
1030 {
1031 char buf[NUMBUFLEN];
1032
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001033 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001034 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +02001035 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001036 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001037 else if (our_tv->v_type == VAR_FLOAT)
1038 {
1039 char buf[NUMBUFLEN];
1040
1041 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +02001042 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001043 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001044 else if (our_tv->v_type == VAR_LIST)
1045 {
1046 list_T *list = our_tv->vval.v_list;
1047 listitem_T *curr;
1048
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001049 if (list == NULL)
1050 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001051
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001052 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001053 return NULL;
1054
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001055 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001056 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001057 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001058 return NULL;
1059 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001060
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001061 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001062 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001063 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001064 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001065 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001066 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001067 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001068 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001069 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001070 {
1071 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001072 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001073 return NULL;
1074 }
1075 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001076 }
1077 }
1078 else if (our_tv->v_type == VAR_DICT)
1079 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001080
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001081 hashtab_T *ht;
1082 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001083 hashitem_T *hi;
1084 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001085
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001086 if (our_tv->vval.v_dict == NULL)
1087 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001088 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001089
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001090 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001091 return NULL;
1092
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001093 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001094 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001095 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001096 return NULL;
1097 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001098
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001099 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001100 for (hi = ht->ht_array; todo > 0; ++hi)
1101 {
1102 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001103 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001104 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001105
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001106 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001107 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001108 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001109 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001110 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001111 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001112 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001113 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001114 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001115 Py_DECREF(newObj);
1116 return NULL;
1117 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001118 }
1119 }
1120 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001121 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001122 {
1123 if (our_tv->vval.v_number == VVAL_FALSE)
1124 {
1125 ret = Py_False;
1126 Py_INCREF(ret);
1127 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001128 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001129 {
1130 ret = Py_True;
1131 Py_INCREF(ret);
1132 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001133 return ret;
1134 }
1135 else if (our_tv->v_type == VAR_SPECIAL)
1136 {
1137 Py_INCREF(Py_None);
1138 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001139 return ret;
1140 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001141 else if (our_tv->v_type == VAR_BLOB)
1142 ret = PyBytes_FromStringAndSize(
1143 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
1144 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001145 else
1146 {
1147 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001148 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001149 }
1150
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001151 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001152}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001153
1154 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +02001155VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001156{
Bram Moolenaar389a1792013-06-23 13:00:44 +02001157 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001158 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +02001159 PyObject *string;
1160 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001161 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +02001162 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001163
Bram Moolenaar389a1792013-06-23 13:00:44 +02001164 if (!PyArg_ParseTuple(args, "O", &string))
1165 return NULL;
1166
1167 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001168 return NULL;
1169
1170 Py_BEGIN_ALLOW_THREADS
1171 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001172 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +02001173 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001174 Python_Release_Vim();
1175 Py_END_ALLOW_THREADS
1176
Bram Moolenaar389a1792013-06-23 13:00:44 +02001177 Py_XDECREF(todecref);
1178
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001179 if (VimTryEnd())
1180 return NULL;
1181
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001182 if (our_tv == NULL)
1183 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001184 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001185 return NULL;
1186 }
1187
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001188 // Convert the Vim type into a Python type. Create a dictionary that's
1189 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001190 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001191 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001192 else
1193 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001194 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001195 Py_DECREF(lookup_dict);
1196 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001197
1198
1199 Py_BEGIN_ALLOW_THREADS
1200 Python_Lock_Vim();
1201 free_tv(our_tv);
1202 Python_Release_Vim();
1203 Py_END_ALLOW_THREADS
1204
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001205 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001206}
1207
Bram Moolenaardb913952012-06-29 12:54:53 +02001208static PyObject *ConvertToPyObject(typval_T *);
1209
1210 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001211VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +02001212{
Bram Moolenaardb913952012-06-29 12:54:53 +02001213 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001214 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +02001215 char_u *expr;
1216 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001217
Bram Moolenaar389a1792013-06-23 13:00:44 +02001218 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001219 return NULL;
1220
1221 Py_BEGIN_ALLOW_THREADS
1222 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001223 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +02001224 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001225 Python_Release_Vim();
1226 Py_END_ALLOW_THREADS
1227
Bram Moolenaar389a1792013-06-23 13:00:44 +02001228 Py_XDECREF(todecref);
1229
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001230 if (VimTryEnd())
1231 return NULL;
1232
Bram Moolenaardb913952012-06-29 12:54:53 +02001233 if (our_tv == NULL)
1234 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001235 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001236 return NULL;
1237 }
1238
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001239 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001240 Py_BEGIN_ALLOW_THREADS
1241 Python_Lock_Vim();
1242 free_tv(our_tv);
1243 Python_Release_Vim();
1244 Py_END_ALLOW_THREADS
1245
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001246 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001247}
1248
1249 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001250VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +02001251{
Bram Moolenaar389a1792013-06-23 13:00:44 +02001252 char_u *str;
1253 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001254 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +02001255
Bram Moolenaar389a1792013-06-23 13:00:44 +02001256 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001257 return NULL;
1258
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001259 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +02001260
1261 Py_XDECREF(todecref);
1262
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001263 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +02001264}
1265
Bram Moolenaarf4258302013-06-02 18:20:17 +02001266 static PyObject *
1267_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
1268{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001269 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001270 PyObject *newwd;
1271 PyObject *todecref;
1272 char_u *new_dir;
1273
Bram Moolenaard4209d22013-06-05 20:34:15 +02001274 if (_chdir == NULL)
1275 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001276 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001277 return NULL;
1278
1279 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1280 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001281 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001282 return NULL;
1283 }
1284
1285 if (!(new_dir = StringToChars(newwd, &todecref)))
1286 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001287 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001288 Py_DECREF(newwd);
1289 return NULL;
1290 }
1291
1292 VimTryStart();
1293
1294 if (vim_chdir(new_dir))
1295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001296 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001297 Py_DECREF(newwd);
1298 Py_XDECREF(todecref);
1299
1300 if (VimTryEnd())
1301 return NULL;
1302
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001303 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001304 return NULL;
1305 }
1306
1307 Py_DECREF(newwd);
1308 Py_XDECREF(todecref);
1309
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001310 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001311
1312 if (VimTryEnd())
1313 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001314 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001315 return NULL;
1316 }
1317
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001318 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001319}
1320
1321 static PyObject *
1322VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1323{
1324 return _VimChdir(py_chdir, args, kwargs);
1325}
1326
1327 static PyObject *
1328VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1329{
1330 return _VimChdir(py_fchdir, args, kwargs);
1331}
1332
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001333typedef struct {
1334 PyObject *callable;
1335 PyObject *result;
1336} map_rtp_data;
1337
1338 static void
1339map_rtp_callback(char_u *path, void *_data)
1340{
1341 void **data = (void **) _data;
1342 PyObject *pathObject;
1343 map_rtp_data *mr_data = *((map_rtp_data **) data);
1344
Bram Moolenaar41009372013-07-01 22:03:04 +02001345 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001346 {
1347 *data = NULL;
1348 return;
1349 }
1350
1351 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1352 pathObject, NULL);
1353
1354 Py_DECREF(pathObject);
1355
1356 if (!mr_data->result || mr_data->result != Py_None)
1357 *data = NULL;
1358 else
1359 {
1360 Py_DECREF(mr_data->result);
1361 mr_data->result = NULL;
1362 }
1363}
1364
1365 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001366VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001367{
1368 map_rtp_data data;
1369
Bram Moolenaar389a1792013-06-23 13:00:44 +02001370 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001371 data.result = NULL;
1372
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001373 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001374
1375 if (data.result == NULL)
1376 {
1377 if (PyErr_Occurred())
1378 return NULL;
1379 else
1380 {
1381 Py_INCREF(Py_None);
1382 return Py_None;
1383 }
1384 }
1385 return data.result;
1386}
1387
1388/*
1389 * _vim_runtimepath_ special path implementation.
1390 */
1391
1392 static void
1393map_finder_callback(char_u *path, void *_data)
1394{
1395 void **data = (void **) _data;
1396 PyObject *list = *((PyObject **) data);
1397 PyObject *pathObject1, *pathObject2;
1398 char *pathbuf;
1399 size_t pathlen;
1400
1401 pathlen = STRLEN(path);
1402
1403#if PY_MAJOR_VERSION < 3
1404# define PY_MAIN_DIR_STRING "python2"
1405#else
1406# define PY_MAIN_DIR_STRING "python3"
1407#endif
1408#define PY_ALTERNATE_DIR_STRING "pythonx"
1409
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001410#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001411 if (!(pathbuf = PyMem_New(char,
1412 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1413 {
1414 PyErr_NoMemory();
1415 *data = NULL;
1416 return;
1417 }
1418
1419 mch_memmove(pathbuf, path, pathlen + 1);
1420 add_pathsep((char_u *) pathbuf);
1421
1422 pathlen = STRLEN(pathbuf);
1423 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1424 PYTHONX_STRING_LENGTH + 1);
1425
1426 if (!(pathObject1 = PyString_FromString(pathbuf)))
1427 {
1428 *data = NULL;
1429 PyMem_Free(pathbuf);
1430 return;
1431 }
1432
1433 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1434 PYTHONX_STRING_LENGTH + 1);
1435
1436 if (!(pathObject2 = PyString_FromString(pathbuf)))
1437 {
1438 Py_DECREF(pathObject1);
1439 PyMem_Free(pathbuf);
1440 *data = NULL;
1441 return;
1442 }
1443
1444 PyMem_Free(pathbuf);
1445
1446 if (PyList_Append(list, pathObject1)
1447 || PyList_Append(list, pathObject2))
1448 *data = NULL;
1449
1450 Py_DECREF(pathObject1);
1451 Py_DECREF(pathObject2);
1452}
1453
1454 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001455Vim_GetPaths(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001456{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001457 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001458
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001459 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001460 return NULL;
1461
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001462 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001463
1464 if (PyErr_Occurred())
1465 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001467 return NULL;
1468 }
1469
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001470 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001471}
1472
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001473#if PY_VERSION_HEX >= 0x030700f0
1474 static PyObject *
1475FinderFindSpec(PyObject *self, PyObject *args)
1476{
1477 char *fullname;
1478 PyObject *paths;
1479 PyObject *target = Py_None;
1480 PyObject *spec;
1481
1482 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1483 return NULL;
1484
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001485 if (!(paths = Vim_GetPaths(self, NULL)))
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001486 return NULL;
1487
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001488 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001489
1490 Py_DECREF(paths);
1491
1492 if (!spec)
1493 {
1494 if (PyErr_Occurred())
1495 return NULL;
1496
1497 Py_INCREF(Py_None);
1498 return Py_None;
1499 }
1500
1501 return spec;
1502}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001503
1504 static PyObject *
1505FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1506{
1507 // Apparently returning None works.
1508 Py_INCREF(Py_None);
1509 return Py_None;
1510}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001511#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001512 static PyObject *
1513call_load_module(char *name, int len, PyObject *find_module_result)
1514{
1515 PyObject *fd, *pathname, *description;
1516
Bram Moolenaarc476e522013-06-23 13:46:40 +02001517 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001518 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001519 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001520 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001521 find_module_result);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001522 return NULL;
1523 }
1524 if (PyTuple_GET_SIZE(find_module_result) != 3)
1525 {
1526 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001527 N_("expected 3-tuple as imp.find_module() result, but got "
1528 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001529 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001530 return NULL;
1531 }
1532
1533 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1534 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1535 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1536 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001537 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001538 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001539 return NULL;
1540 }
1541
1542 return PyObject_CallFunction(py_load_module,
1543 "s#OOO", name, len, fd, pathname, description);
1544}
1545
1546 static PyObject *
1547find_module(char *fullname, char *tail, PyObject *new_path)
1548{
1549 PyObject *find_module_result;
1550 PyObject *module;
1551 char *dot;
1552
Bram Moolenaar41009372013-07-01 22:03:04 +02001553 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001554 {
1555 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001556 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001557 * first component
1558 */
1559 PyObject *newest_path;
1560 int partlen = (int) (dot - 1 - tail);
1561
1562 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1563 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001564 {
1565 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1566 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001567 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001568 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001569
1570 if (!(module = call_load_module(
1571 fullname,
1572 ((int) (tail - fullname)) + partlen,
1573 find_module_result)))
1574 {
1575 Py_DECREF(find_module_result);
1576 return NULL;
1577 }
1578
1579 Py_DECREF(find_module_result);
1580
1581 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1582 {
1583 Py_DECREF(module);
1584 return NULL;
1585 }
1586
1587 Py_DECREF(module);
1588
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001589 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001590
1591 Py_DECREF(newest_path);
1592
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001593 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001594 }
1595 else
1596 {
1597 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1598 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001599 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001600 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1601 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001602 return NULL;
1603 }
1604
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001605 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001606 }
1607}
1608
1609 static PyObject *
1610FinderFindModule(PyObject *self, PyObject *args)
1611{
1612 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001613 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001614 PyObject *new_path;
1615 LoaderObject *loader;
1616
1617 if (!PyArg_ParseTuple(args, "s", &fullname))
1618 return NULL;
1619
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001620 if (!(new_path = Vim_GetPaths(self, NULL)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001621 return NULL;
1622
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001623 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001624
1625 Py_DECREF(new_path);
1626
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001627 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001628 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001629 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001630 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001631
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001632 Py_INCREF(Py_None);
1633 return Py_None;
1634 }
1635
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001636 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001637 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001638 Py_DECREF(result);
1639 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001640 return NULL;
1641 }
1642
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001643 if (!(loader = PyObject_NEW(LoaderObject, LoaderTypePtr)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001644 {
1645 vim_free(fullname);
1646 Py_DECREF(result);
1647 return NULL;
1648 }
1649
1650 loader->fullname = fullname;
1651 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001652
1653 return (PyObject *) loader;
1654}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001655#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001656
1657 static PyObject *
1658VimPathHook(PyObject *self UNUSED, PyObject *args)
1659{
1660 char *path;
1661
1662 if (PyArg_ParseTuple(args, "s", &path)
1663 && STRCMP(path, vim_special_path) == 0)
1664 {
1665 Py_INCREF(vim_module);
1666 return vim_module;
1667 }
1668
1669 PyErr_Clear();
1670 PyErr_SetNone(PyExc_ImportError);
1671 return NULL;
1672}
1673
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001674/*
1675 * Vim module - Definitions
1676 */
1677
1678static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001679 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001680 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001681 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001682 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001683 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001684 {"chdir", (PyCFunction)(void *)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1685 {"fchdir", (PyCFunction)(void *)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001686 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001687#if PY_VERSION_HEX >= 0x030700f0
1688 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001689#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001690 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001691 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1692 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1693 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001694};
1695
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001696/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001697 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001698 */
1699
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001700DEFINE_PY_TYPE_OBJECT(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001701
1702typedef PyObject *(*nextfun)(void **);
1703typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001704typedef int (*traversefun)(void *, visitproc, void *);
1705typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001706
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001707// Main purpose of this object is removing the need for do python
1708// initialization (i.e. PyType_Ready and setting type attributes) for a big
1709// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001710typedef struct
1711{
1712 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001713 void *cur;
1714 nextfun next;
1715 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001716 traversefun traverse;
1717 clearfun clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001718 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001719} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001720
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001721 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001722IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001723 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001724{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001725 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001726
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001727 self = PyObject_GC_New(IterObject, IterTypePtr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001728 self->cur = start;
1729 self->next = next;
1730 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001731 self->traverse = traverse;
1732 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001733 self->iter_object = iter_object;
1734
1735 if (iter_object)
1736 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001737
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001738 return (PyObject *)(self);
1739}
1740
1741 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001742IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001743{
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001744 if (self->iter_object)
1745 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001746 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001747 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001748 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001749}
1750
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001751 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001752IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001753{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001754 if (self->traverse != NULL)
1755 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001756 else
1757 return 0;
1758}
1759
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001760// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001761#ifdef clear
1762# undef clear
1763#endif
1764
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001765 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001766IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001767{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001768 if (self->clear != NULL)
1769 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001770 else
1771 return 0;
1772}
1773
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001774 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001775IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001776{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001777 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001778}
1779
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001780 static PyObject *
1781IterIter(PyObject *self)
1782{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001783 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001784 return self;
1785}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001786
Bram Moolenaardb913952012-06-29 12:54:53 +02001787typedef struct pylinkedlist_S {
1788 struct pylinkedlist_S *pll_next;
1789 struct pylinkedlist_S *pll_prev;
1790 PyObject *pll_obj;
1791} pylinkedlist_T;
1792
1793static pylinkedlist_T *lastdict = NULL;
1794static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001795static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001796
1797 static void
1798pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1799{
1800 if (ref->pll_prev == NULL)
1801 {
1802 if (ref->pll_next == NULL)
1803 {
1804 *last = NULL;
1805 return;
1806 }
1807 }
1808 else
1809 ref->pll_prev->pll_next = ref->pll_next;
1810
1811 if (ref->pll_next == NULL)
1812 *last = ref->pll_prev;
1813 else
1814 ref->pll_next->pll_prev = ref->pll_prev;
1815}
1816
1817 static void
1818pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1819{
1820 if (*last == NULL)
1821 ref->pll_prev = NULL;
1822 else
1823 {
1824 (*last)->pll_next = ref;
1825 ref->pll_prev = *last;
1826 }
1827 ref->pll_next = NULL;
1828 ref->pll_obj = self;
1829 *last = ref;
1830}
1831
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001832DEFINE_PY_TYPE_OBJECT(DictionaryType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001833
1834typedef struct
1835{
1836 PyObject_HEAD
1837 dict_T *dict;
1838 pylinkedlist_T ref;
1839} DictionaryObject;
1840
Bram Moolenaara9922d62013-05-30 13:01:18 +02001841static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1842
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001843#define NEW_DICTIONARY(dict) DictionaryNew(DictionaryTypePtr, dict)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001844
Bram Moolenaardb913952012-06-29 12:54:53 +02001845 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001846DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001847{
1848 DictionaryObject *self;
1849
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001850 self = (DictionaryObject *) Py_TYPE_GET_TP_ALLOC(subtype)(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001851 if (self == NULL)
1852 return NULL;
1853 self->dict = dict;
1854 ++dict->dv_refcount;
1855
1856 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1857
1858 return (PyObject *)(self);
1859}
1860
Bram Moolenaara9922d62013-05-30 13:01:18 +02001861 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001862py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001863{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001864 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001865
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001866 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001867 {
1868 PyErr_NoMemory();
1869 return NULL;
1870 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001871 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001872
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001873 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001874}
1875
1876 static PyObject *
1877DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1878{
1879 DictionaryObject *self;
1880 dict_T *dict;
1881
1882 if (!(dict = py_dict_alloc()))
1883 return NULL;
1884
1885 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1886
1887 --dict->dv_refcount;
1888
1889 if (kwargs || PyTuple_Size(args))
1890 {
1891 PyObject *tmp;
1892 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1893 {
1894 Py_DECREF(self);
1895 return NULL;
1896 }
1897
1898 Py_DECREF(tmp);
1899 }
1900
1901 return (PyObject *)(self);
1902}
1903
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001904 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001905DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001906{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001907 pyll_remove(&self->ref, &lastdict);
1908 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001909
1910 DESTRUCTOR_FINISH(self);
1911}
1912
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001913static char *DictionaryAttrs[] = {
1914 "locked", "scope",
1915 NULL
1916};
1917
1918 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001919DictionaryDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001920{
1921 return ObjectDir(self, DictionaryAttrs);
1922}
1923
Bram Moolenaardb913952012-06-29 12:54:53 +02001924 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001925DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001926{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001927 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001928 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001929 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001930 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001931 return -1;
1932 }
1933
1934 if (strcmp(name, "locked") == 0)
1935 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001936 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001937 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001938 PyErr_SET_STRING(PyExc_TypeError,
1939 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001940 return -1;
1941 }
1942 else
1943 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001944 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001945 if (istrue == -1)
1946 return -1;
1947 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001948 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001949 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001950 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001951 }
1952 return 0;
1953 }
1954 else
1955 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001956 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001957 return -1;
1958 }
1959}
1960
1961 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001962DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001963{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001964 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001965}
1966
Bram Moolenaara9922d62013-05-30 13:01:18 +02001967#define DICT_FLAG_HAS_DEFAULT 0x01
1968#define DICT_FLAG_POP 0x02
1969#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001970#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001971#define DICT_FLAG_RETURN_PAIR 0x10
1972
Bram Moolenaardb913952012-06-29 12:54:53 +02001973 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001974_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001975{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001976 PyObject *keyObject;
1977 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001978 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001979 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001980 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001981 dict_T *dict = self->dict;
1982 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001983 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001984
Bram Moolenaara9922d62013-05-30 13:01:18 +02001985 if (flags & DICT_FLAG_HAS_DEFAULT)
1986 {
1987 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1988 return NULL;
1989 }
1990 else
1991 keyObject = args;
1992
1993 if (flags & DICT_FLAG_RETURN_BOOL)
1994 defObject = Py_False;
1995
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001996 if (!(key = StringToChars(keyObject, &todecref)))
1997 return NULL;
1998
1999 if (*key == NUL)
2000 {
2001 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002002 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002003 return NULL;
2004 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002005
Bram Moolenaara9922d62013-05-30 13:01:18 +02002006 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02002007
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002008 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02002009
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02002011 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012 if (defObject)
2013 {
2014 Py_INCREF(defObject);
2015 return defObject;
2016 }
2017 else
2018 {
2019 PyErr_SetObject(PyExc_KeyError, keyObject);
2020 return NULL;
2021 }
2022 }
2023 else if (flags & DICT_FLAG_RETURN_BOOL)
2024 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01002025 ret = Py_True;
2026 Py_INCREF(ret);
2027 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002028 }
2029
2030 di = dict_lookup(hi);
2031
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002032 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002033 return NULL;
2034
2035 if (flags & DICT_FLAG_POP)
2036 {
2037 if (dict->dv_lock)
2038 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002039 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002040 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002041 return NULL;
2042 }
2043
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002044 hash_remove(&dict->dv_hashtab, hi, "Python remove variable");
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045 dictitem_free(di);
2046 }
2047
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002048 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002049}
2050
2051 static PyObject *
2052DictionaryItem(DictionaryObject *self, PyObject *keyObject)
2053{
2054 return _DictionaryItem(self, keyObject, 0);
2055}
2056
2057 static int
2058DictionaryContains(DictionaryObject *self, PyObject *keyObject)
2059{
2060 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002061 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002062
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01002063 if (rObj == NULL)
2064 return -1;
2065
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002066 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002067
Bram Moolenaardee2e312013-06-23 16:35:47 +02002068 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002069
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002070 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002071}
2072
2073typedef struct
2074{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002075 int dii_changed;
2076 hashtab_T *dii_ht;
2077 hashitem_T *dii_hi;
2078 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002079} dictiterinfo_T;
2080
2081 static PyObject *
2082DictionaryIterNext(dictiterinfo_T **dii)
2083{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002084 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002085
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002086 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002087 return NULL;
2088
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002089 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002090 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002091 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002092 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02002093 return NULL;
2094 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002095
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002096 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
2097 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002098
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002099 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002100
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002101 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002102 return NULL;
2103
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002104 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002105}
2106
2107 static PyObject *
2108DictionaryIter(DictionaryObject *self)
2109{
2110 dictiterinfo_T *dii;
2111 hashtab_T *ht;
2112
2113 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
2114 {
2115 PyErr_NoMemory();
2116 return NULL;
2117 }
2118
2119 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002120 dii->dii_changed = ht->ht_changed;
2121 dii->dii_ht = ht;
2122 dii->dii_hi = ht->ht_array;
2123 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002124
2125 return IterNew(dii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002126 (destructorfun)(void *) PyMem_Free, (nextfun) DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002127 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002128}
2129
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002130 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002131DictionaryAssItem(
2132 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02002133{
2134 char_u *key;
2135 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002136 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02002137 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002138 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02002139
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002140 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02002141 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002142 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02002143 return -1;
2144 }
2145
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002146 if (!(key = StringToChars(keyObject, &todecref)))
2147 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002148
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002149 if (*key == NUL)
2150 {
2151 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02002152 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002153 return -1;
2154 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002155
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002156 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02002157
2158 if (valObject == NULL)
2159 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02002160 hashitem_T *hi;
2161
Bram Moolenaardb913952012-06-29 12:54:53 +02002162 if (di == NULL)
2163 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002164 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002165 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02002166 return -1;
2167 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002168 hi = hash_find(&dict->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002169 hash_remove(&dict->dv_hashtab, hi, "Python remove item");
Bram Moolenaardb913952012-06-29 12:54:53 +02002170 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02002171 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002172 return 0;
2173 }
2174
2175 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02002176 {
2177 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002178 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02002179 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002180
2181 if (di == NULL)
2182 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002183 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002184 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002185 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002186 PyErr_NoMemory();
2187 return -1;
2188 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002189 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02002190
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002191 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02002192 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002193 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002194 RAISE_KEY_ADD_FAIL(key);
2195 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002196 return -1;
2197 }
2198 }
2199 else
2200 clear_tv(&di->di_tv);
2201
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002202 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002203
2204 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002205 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002206 return 0;
2207}
2208
Bram Moolenaara9922d62013-05-30 13:01:18 +02002209typedef PyObject *(*hi_to_py)(hashitem_T *);
2210
Bram Moolenaardb913952012-06-29 12:54:53 +02002211 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02002212DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02002213{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002214 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02002215 long_u todo = dict->dv_hashtab.ht_used;
2216 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002217 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002218 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002219 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02002220
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002221 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02002222 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
2223 {
2224 if (!HASHITEM_EMPTY(hi))
2225 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002226 if (!(newObj = hiconvert(hi)))
2227 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002228 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002229 return NULL;
2230 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002231 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002232 --todo;
2233 ++i;
2234 }
2235 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002236 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002237}
2238
Bram Moolenaara9922d62013-05-30 13:01:18 +02002239 static PyObject *
2240dict_key(hashitem_T *hi)
2241{
2242 return PyBytes_FromString((char *)(hi->hi_key));
2243}
2244
2245 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002246DictionaryListKeys(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002247{
2248 return DictionaryListObjects(self, dict_key);
2249}
2250
2251 static PyObject *
2252dict_val(hashitem_T *hi)
2253{
2254 dictitem_T *di;
2255
2256 di = dict_lookup(hi);
2257 return ConvertToPyObject(&di->di_tv);
2258}
2259
2260 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002261DictionaryListValues(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002262{
2263 return DictionaryListObjects(self, dict_val);
2264}
2265
2266 static PyObject *
2267dict_item(hashitem_T *hi)
2268{
2269 PyObject *keyObject;
2270 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002271 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002272
2273 if (!(keyObject = dict_key(hi)))
2274 return NULL;
2275
2276 if (!(valObject = dict_val(hi)))
2277 {
2278 Py_DECREF(keyObject);
2279 return NULL;
2280 }
2281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002282 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002283
2284 Py_DECREF(keyObject);
2285 Py_DECREF(valObject);
2286
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002287 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002288}
2289
2290 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002291DictionaryListItems(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002292{
2293 return DictionaryListObjects(self, dict_item);
2294}
2295
2296 static PyObject *
2297DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2298{
2299 dict_T *dict = self->dict;
2300
2301 if (dict->dv_lock)
2302 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002303 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002304 return NULL;
2305 }
2306
2307 if (kwargs)
2308 {
2309 typval_T tv;
2310
2311 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2312 return NULL;
2313
2314 VimTryStart();
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002315 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force", NULL);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002316 clear_tv(&tv);
2317 if (VimTryEnd())
2318 return NULL;
2319 }
2320 else
2321 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002322 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002323
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002324 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002325 return NULL;
2326
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002327 if (obj == NULL)
2328 {
2329 Py_INCREF(Py_None);
2330 return Py_None;
2331 }
2332
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002333 if (PyObject_HasAttrString(obj, "keys"))
2334 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002335 else
2336 {
2337 PyObject *iterator;
2338 PyObject *item;
2339
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002340 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002341 return NULL;
2342
2343 while ((item = PyIter_Next(iterator)))
2344 {
2345 PyObject *fast;
2346 PyObject *keyObject;
2347 PyObject *valObject;
2348 PyObject *todecref;
2349 char_u *key;
2350 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002351 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002352
2353 if (!(fast = PySequence_Fast(item, "")))
2354 {
2355 Py_DECREF(iterator);
2356 Py_DECREF(item);
2357 return NULL;
2358 }
2359
2360 Py_DECREF(item);
2361
2362 if (PySequence_Fast_GET_SIZE(fast) != 2)
2363 {
2364 Py_DECREF(iterator);
2365 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002366 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002367 N_("expected sequence element of size 2, "
2368 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002369 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002370 return NULL;
2371 }
2372
2373 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2374
2375 if (!(key = StringToChars(keyObject, &todecref)))
2376 {
2377 Py_DECREF(iterator);
2378 Py_DECREF(fast);
2379 return NULL;
2380 }
2381
2382 di = dictitem_alloc(key);
2383
2384 Py_XDECREF(todecref);
2385
2386 if (di == NULL)
2387 {
2388 Py_DECREF(fast);
2389 Py_DECREF(iterator);
2390 PyErr_NoMemory();
2391 return NULL;
2392 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002393 di->di_tv.v_type = VAR_UNKNOWN;
2394
2395 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2396
2397 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2398 {
2399 Py_DECREF(iterator);
2400 Py_DECREF(fast);
2401 dictitem_free(di);
2402 return NULL;
2403 }
2404
2405 Py_DECREF(fast);
2406
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002407 hi = hash_find(&dict->dv_hashtab, di->di_key);
2408 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002409 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002410 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002411 Py_DECREF(iterator);
2412 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002413 return NULL;
2414 }
2415 }
2416
2417 Py_DECREF(iterator);
2418
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002419 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002420 if (PyErr_Occurred())
2421 return NULL;
2422 }
2423 }
2424 Py_INCREF(Py_None);
2425 return Py_None;
2426}
2427
2428 static PyObject *
2429DictionaryGet(DictionaryObject *self, PyObject *args)
2430{
2431 return _DictionaryItem(self, args,
2432 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2433}
2434
2435 static PyObject *
2436DictionaryPop(DictionaryObject *self, PyObject *args)
2437{
2438 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2439}
2440
2441 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002442DictionaryPopItem(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002443{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002444 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002445 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002446 PyObject *valObject;
2447 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002448
Bram Moolenaarde71b562013-06-02 17:41:54 +02002449 if (self->dict->dv_hashtab.ht_used == 0)
2450 {
2451 PyErr_SetNone(PyExc_KeyError);
2452 return NULL;
2453 }
2454
2455 hi = self->dict->dv_hashtab.ht_array;
2456 while (HASHITEM_EMPTY(hi))
2457 ++hi;
2458
2459 di = dict_lookup(hi);
2460
2461 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002462 return NULL;
2463
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002464 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002465 {
2466 Py_DECREF(valObject);
2467 return NULL;
2468 }
2469
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002470 hash_remove(&self->dict->dv_hashtab, hi, "Python pop item");
Bram Moolenaarde71b562013-06-02 17:41:54 +02002471 dictitem_free(di);
2472
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002473 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002474}
2475
2476 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002477DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002478{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002479 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2480}
2481
2482static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002483 0, // sq_length
2484 0, // sq_concat
2485 0, // sq_repeat
2486 0, // sq_item
2487 0, // sq_slice
2488 0, // sq_ass_item
2489 0, // sq_ass_slice
2490 (objobjproc) DictionaryContains, // sq_contains
2491 0, // sq_inplace_concat
2492 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002493};
2494
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002495static PyMappingMethods DictionaryAsMapping = {
2496 (lenfunc) DictionaryLength,
2497 (binaryfunc) DictionaryItem,
2498 (objobjargproc) DictionaryAssItem,
2499};
2500
Bram Moolenaardb913952012-06-29 12:54:53 +02002501static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002502 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002503 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2504 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002505 {"update", (PyCFunction)(void *)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002506 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2507 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002508 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002509 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002510 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2511 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002512};
2513
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002514DEFINE_PY_TYPE_OBJECT(ListType);
Bram Moolenaardb913952012-06-29 12:54:53 +02002515
2516typedef struct
2517{
2518 PyObject_HEAD
2519 list_T *list;
2520 pylinkedlist_T ref;
2521} ListObject;
2522
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002523#define NEW_LIST(list) ListNew(ListTypePtr, list)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002524
Bram Moolenaardb913952012-06-29 12:54:53 +02002525 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002526ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002527{
2528 ListObject *self;
2529
Bram Moolenaarab589462020-07-06 21:03:06 +02002530 if (list == NULL)
2531 return NULL;
2532
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002533 self = (ListObject *) Py_TYPE_GET_TP_ALLOC(subtype)(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002534 if (self == NULL)
2535 return NULL;
2536 self->list = list;
2537 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002538 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002539
2540 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2541
2542 return (PyObject *)(self);
2543}
2544
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002545 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002546py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002547{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002548 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002549
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002550 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002551 {
2552 PyErr_NoMemory();
2553 return NULL;
2554 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002555 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002556
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002557 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002558}
2559
2560 static int
2561list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2562{
2563 PyObject *iterator;
2564 PyObject *item;
2565 listitem_T *li;
2566
2567 if (!(iterator = PyObject_GetIter(obj)))
2568 return -1;
2569
2570 while ((item = PyIter_Next(iterator)))
2571 {
2572 if (!(li = listitem_alloc()))
2573 {
2574 PyErr_NoMemory();
2575 Py_DECREF(item);
2576 Py_DECREF(iterator);
2577 return -1;
2578 }
2579 li->li_tv.v_lock = 0;
2580 li->li_tv.v_type = VAR_UNKNOWN;
2581
2582 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2583 {
2584 Py_DECREF(item);
2585 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002587 return -1;
2588 }
2589
2590 Py_DECREF(item);
2591
2592 list_append(l, li);
2593 }
2594
2595 Py_DECREF(iterator);
2596
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002597 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002598 if (PyErr_Occurred())
2599 return -1;
2600
2601 return 0;
2602}
2603
2604 static PyObject *
2605ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2606{
2607 list_T *list;
2608 PyObject *obj = NULL;
2609
2610 if (kwargs)
2611 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002612 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002613 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002614 return NULL;
2615 }
2616
2617 if (!PyArg_ParseTuple(args, "|O", &obj))
2618 return NULL;
2619
2620 if (!(list = py_list_alloc()))
2621 return NULL;
2622
2623 if (obj)
2624 {
2625 PyObject *lookup_dict;
2626
2627 if (!(lookup_dict = PyDict_New()))
2628 {
2629 list_unref(list);
2630 return NULL;
2631 }
2632
2633 if (list_py_concat(list, obj, lookup_dict) == -1)
2634 {
2635 Py_DECREF(lookup_dict);
2636 list_unref(list);
2637 return NULL;
2638 }
2639
2640 Py_DECREF(lookup_dict);
2641 }
2642
2643 return ListNew(subtype, list);
2644}
2645
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002646 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002647ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002648{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002649 pyll_remove(&self->ref, &lastlist);
2650 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002651
2652 DESTRUCTOR_FINISH(self);
2653}
2654
Bram Moolenaardb913952012-06-29 12:54:53 +02002655 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002656ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002657{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002658 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002659}
2660
2661 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002662ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002663{
2664 listitem_T *li;
2665
Bram Moolenaard6e39182013-05-21 18:30:34 +02002666 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002667 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002668 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002669 return NULL;
2670 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002671 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002672 if (li == NULL)
2673 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002674 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002675 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002676 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002677 return NULL;
2678 }
2679 return ConvertToPyObject(&li->li_tv);
2680}
2681
Bram Moolenaardb913952012-06-29 12:54:53 +02002682 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002683ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2684 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002685{
2686 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002687 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002688
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002689 if (step == 0)
2690 {
2691 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2692 return NULL;
2693 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002694
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002695 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002696 if (list == NULL)
2697 return NULL;
2698
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002699 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002700 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002701 PyObject *item;
2702
2703 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002704 if (item == NULL)
2705 {
2706 Py_DECREF(list);
2707 return NULL;
2708 }
2709
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002710 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002711 }
2712
2713 return list;
2714}
2715
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002716 static PyObject *
2717ListItem(ListObject *self, PyObject* idx)
2718{
2719#if PY_MAJOR_VERSION < 3
2720 if (PyInt_Check(idx))
2721 {
2722 long _idx = PyInt_AsLong(idx);
2723 return ListIndex(self, _idx);
2724 }
2725 else
2726#endif
2727 if (PyLong_Check(idx))
2728 {
2729 long _idx = PyLong_AsLong(idx);
2730 return ListIndex(self, _idx);
2731 }
2732 else if (PySlice_Check(idx))
2733 {
2734 Py_ssize_t start, stop, step, slicelen;
2735
Bram Moolenaar922a4662014-03-30 16:11:43 +02002736 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002737 &start, &stop, &step, &slicelen) < 0)
2738 return NULL;
2739 return ListSlice(self, start, step, slicelen);
2740 }
2741 else
2742 {
2743 RAISE_INVALID_INDEX_TYPE(idx);
2744 return NULL;
2745 }
2746}
2747
2748 static void
2749list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2750 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2751{
2752 while (numreplaced--)
2753 {
2754 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2755 listitem_remove(l, lis[slicelen + numreplaced]);
2756 }
2757 while (numadded--)
2758 {
2759 listitem_T *next;
2760
2761 next = lastaddedli->li_prev;
2762 listitem_remove(l, lastaddedli);
2763 lastaddedli = next;
2764 }
2765}
2766
2767 static int
2768ListAssSlice(ListObject *self, Py_ssize_t first,
2769 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2770{
2771 PyObject *iterator;
2772 PyObject *item;
2773 listitem_T *li;
2774 listitem_T *lastaddedli = NULL;
2775 listitem_T *next;
2776 typval_T v;
2777 list_T *l = self->list;
2778 PyInt i;
2779 PyInt j;
2780 PyInt numreplaced = 0;
2781 PyInt numadded = 0;
2782 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002783 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002784
2785 size = ListLength(self);
2786
2787 if (l->lv_lock)
2788 {
2789 RAISE_LOCKED_LIST;
2790 return -1;
2791 }
2792
2793 if (step == 0)
2794 {
2795 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2796 return -1;
2797 }
2798
2799 if (step != 1 && slicelen == 0)
2800 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002801 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002802 int ret = 0;
2803
2804 if (obj == NULL)
2805 return 0;
2806
2807 if (!(iterator = PyObject_GetIter(obj)))
2808 return -1;
2809
2810 if ((item = PyIter_Next(iterator)))
2811 {
2812 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002813 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002814 "to extended slice"), 0);
2815 Py_DECREF(item);
2816 ret = -1;
2817 }
2818 Py_DECREF(iterator);
2819 return ret;
2820 }
2821
2822 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002823 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002824 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2825 {
2826 PyErr_NoMemory();
2827 return -1;
2828 }
2829
2830 if (first == size)
2831 li = NULL;
2832 else
2833 {
2834 li = list_find(l, (long) first);
2835 if (li == NULL)
2836 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002837 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002838 (int)first);
2839 if (obj != NULL)
2840 PyMem_Free(lis);
2841 return -1;
2842 }
2843 i = slicelen;
2844 while (i-- && li != NULL)
2845 {
2846 j = step;
2847 next = li;
2848 if (step > 0)
2849 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2850 else
2851 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2852
2853 if (obj == NULL)
2854 listitem_remove(l, li);
2855 else
2856 lis[slicelen - i - 1] = li;
2857
2858 li = next;
2859 }
2860 if (li == NULL && i != -1)
2861 {
2862 PyErr_SET_VIM(N_("internal error: not enough list items"));
2863 if (obj != NULL)
2864 PyMem_Free(lis);
2865 return -1;
2866 }
2867 }
2868
2869 if (obj == NULL)
2870 return 0;
2871
2872 if (!(iterator = PyObject_GetIter(obj)))
2873 {
2874 PyMem_Free(lis);
2875 return -1;
2876 }
2877
2878 i = 0;
2879 while ((item = PyIter_Next(iterator)))
2880 {
2881 if (ConvertFromPyObject(item, &v) == -1)
2882 {
2883 Py_DECREF(iterator);
2884 Py_DECREF(item);
2885 PyMem_Free(lis);
2886 return -1;
2887 }
2888 Py_DECREF(item);
2889 if (list_insert_tv(l, &v, numreplaced < slicelen
2890 ? lis[numreplaced]
2891 : li) == FAIL)
2892 {
2893 clear_tv(&v);
2894 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2895 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2896 PyMem_Free(lis);
2897 return -1;
2898 }
2899 if (numreplaced < slicelen)
2900 {
2901 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002902 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002903 numreplaced++;
2904 }
2905 else
2906 {
2907 if (li)
2908 lastaddedli = li->li_prev;
2909 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002910 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002911 numadded++;
2912 }
2913 clear_tv(&v);
2914 if (step != 1 && i >= slicelen)
2915 {
2916 Py_DECREF(iterator);
2917 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002918 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002919 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002920 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2921 PyMem_Free(lis);
2922 return -1;
2923 }
2924 ++i;
2925 }
2926 Py_DECREF(iterator);
2927
2928 if (step != 1 && i != slicelen)
2929 {
2930 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002931 N_("attempt to assign sequence of size %d to extended slice "
2932 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002933 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2934 PyMem_Free(lis);
2935 return -1;
2936 }
2937
2938 if (PyErr_Occurred())
2939 {
2940 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2941 PyMem_Free(lis);
2942 return -1;
2943 }
2944
2945 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002947 if (step == 1)
2948 for (i = numreplaced; i < slicelen; i++)
2949 listitem_remove(l, lis[i]);
2950
2951 PyMem_Free(lis);
2952
2953 return 0;
2954}
2955
2956 static int
2957ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2958{
2959 typval_T tv;
2960 list_T *l = self->list;
2961 listitem_T *li;
2962 Py_ssize_t length = ListLength(self);
2963
2964 if (l->lv_lock)
2965 {
2966 RAISE_LOCKED_LIST;
2967 return -1;
2968 }
2969 if (index > length || (index == length && obj == NULL))
2970 {
2971 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2972 return -1;
2973 }
2974
2975 if (obj == NULL)
2976 {
2977 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002978 if (li == NULL)
2979 {
2980 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2981 "list item %d"), (int) index);
2982 return -1;
2983 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002984 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002985 clear_tv(&li->li_tv);
2986 vim_free(li);
2987 return 0;
2988 }
2989
2990 if (ConvertFromPyObject(obj, &tv) == -1)
2991 return -1;
2992
2993 if (index == length)
2994 {
2995 if (list_append_tv(l, &tv) == FAIL)
2996 {
2997 clear_tv(&tv);
2998 PyErr_SET_VIM(N_("failed to add item to list"));
2999 return -1;
3000 }
3001 }
3002 else
3003 {
3004 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02003005 if (li == NULL)
3006 {
3007 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
3008 "list item %d"), (int) index);
3009 return -1;
3010 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003011 clear_tv(&li->li_tv);
3012 copy_tv(&tv, &li->li_tv);
3013 clear_tv(&tv);
3014 }
3015 return 0;
3016}
3017
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003018 static int
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003019ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
3020{
3021#if PY_MAJOR_VERSION < 3
3022 if (PyInt_Check(idx))
3023 {
3024 long _idx = PyInt_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003025 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003026 }
3027 else
3028#endif
3029 if (PyLong_Check(idx))
3030 {
3031 long _idx = PyLong_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003032 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003033 }
3034 else if (PySlice_Check(idx))
3035 {
3036 Py_ssize_t start, stop, step, slicelen;
3037
Bram Moolenaar922a4662014-03-30 16:11:43 +02003038 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003039 &start, &stop, &step, &slicelen) < 0)
3040 return -1;
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003041 return (int)ListAssSlice(self, start, step, slicelen,
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003042 obj);
3043 }
3044 else
3045 {
3046 RAISE_INVALID_INDEX_TYPE(idx);
3047 return -1;
3048 }
3049}
3050
3051 static PyObject *
3052ListConcatInPlace(ListObject *self, PyObject *obj)
3053{
3054 list_T *l = self->list;
3055 PyObject *lookup_dict;
3056
3057 if (l->lv_lock)
3058 {
3059 RAISE_LOCKED_LIST;
3060 return NULL;
3061 }
3062
3063 if (!(lookup_dict = PyDict_New()))
3064 return NULL;
3065
3066 if (list_py_concat(l, obj, lookup_dict) == -1)
3067 {
3068 Py_DECREF(lookup_dict);
3069 return NULL;
3070 }
3071 Py_DECREF(lookup_dict);
3072
3073 Py_INCREF(self);
3074 return (PyObject *)(self);
3075}
3076
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003077typedef struct
3078{
3079 listwatch_T lw;
3080 list_T *list;
3081} listiterinfo_T;
3082
3083 static void
3084ListIterDestruct(listiterinfo_T *lii)
3085{
3086 list_rem_watch(lii->list, &lii->lw);
Bram Moolenaar21578272021-02-21 19:12:47 +01003087 list_unref(lii->list);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003088 PyMem_Free(lii);
3089}
3090
3091 static PyObject *
3092ListIterNext(listiterinfo_T **lii)
3093{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003094 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003095
3096 if (!((*lii)->lw.lw_item))
3097 return NULL;
3098
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003099 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003100 return NULL;
3101
3102 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
3103
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003104 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003105}
3106
3107 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003108ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003109{
3110 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003111 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003112
3113 if (!(lii = PyMem_New(listiterinfo_T, 1)))
3114 {
3115 PyErr_NoMemory();
3116 return NULL;
3117 }
3118
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003119 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003120 list_add_watch(l, &lii->lw);
3121 lii->lw.lw_item = l->lv_first;
3122 lii->list = l;
Bram Moolenaar21578272021-02-21 19:12:47 +01003123 ++l->lv_refcount;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003124
3125 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003126 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003127 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003128}
3129
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003130static char *ListAttrs[] = {
3131 "locked",
3132 NULL
3133};
3134
3135 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003136ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003137{
3138 return ObjectDir(self, ListAttrs);
3139}
3140
Bram Moolenaar66b79852012-09-21 14:00:35 +02003141 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003142ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02003143{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003144 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02003145 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003146 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003147 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02003148 return -1;
3149 }
3150
3151 if (strcmp(name, "locked") == 0)
3152 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003153 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02003154 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003155 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02003156 return -1;
3157 }
3158 else
3159 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003160 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02003161 if (istrue == -1)
3162 return -1;
3163 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003164 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02003165 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003166 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02003167 }
3168 return 0;
3169 }
3170 else
3171 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003172 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02003173 return -1;
3174 }
3175}
3176
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003177static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003178 (lenfunc) ListLength, // sq_length, len(x)
3179 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
3180 0, // RangeRepeat, sq_repeat, x*n
3181 (PyIntArgFunc) ListIndex, // sq_item, x[i]
3182 0, // was_sq_slice, x[i:j]
3183 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
3184 0, // was_sq_ass_slice, x[i:j]=v
3185 0, // sq_contains
3186 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
3187 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003188};
3189
3190static PyMappingMethods ListAsMapping = {
3191 /* mp_length */ (lenfunc) ListLength,
3192 /* mp_subscript */ (binaryfunc) ListItem,
3193 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
3194};
3195
Bram Moolenaardb913952012-06-29 12:54:53 +02003196static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003197 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
3198 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
3199 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003200};
3201
3202typedef struct
3203{
3204 PyObject_HEAD
3205 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003206 int argc;
3207 typval_T *argv;
3208 dict_T *self;
3209 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003210 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02003211} FunctionObject;
3212
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003213DEFINE_PY_TYPE_OBJECT(FunctionType);
Bram Moolenaardb913952012-06-29 12:54:53 +02003214
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003215#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003216 FunctionNew(FunctionTypePtr, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003217
Bram Moolenaardb913952012-06-29 12:54:53 +02003218 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003219FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003220 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02003221{
3222 FunctionObject *self;
3223
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003224 self = (FunctionObject *) Py_TYPE_GET_TP_ALLOC(subtype)(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02003225 if (self == NULL)
3226 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003227
3228 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02003229 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02003230 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003231 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003232 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003233 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003234 return NULL;
3235 }
3236 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003237 }
3238 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01003239 {
3240 char_u *p;
3241
3242 if ((p = get_expanded_name(name,
3243 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003244 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003245 PyErr_FORMAT(PyExc_ValueError,
3246 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02003247 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003248 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003249
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01003250 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
3251 {
3252 char_u *np;
3253 size_t len = STRLEN(p) + 1;
3254
Bram Moolenaar51e14382019-05-25 20:21:28 +02003255 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01003256 {
3257 vim_free(p);
3258 return NULL;
3259 }
3260 mch_memmove(np, "<SNR>", 5);
3261 mch_memmove(np + 5, p + 3, len - 3);
3262 vim_free(p);
3263 self->name = np;
3264 }
3265 else
3266 self->name = p;
3267 }
3268
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02003269 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003270 self->argc = argc;
3271 self->argv = argv;
3272 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003273 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003274
3275 if (self->argv || self->self)
3276 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3277
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003278 return (PyObject *)(self);
3279}
3280
3281 static PyObject *
3282FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3283{
3284 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003285 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003286 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003287 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003288 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003289 typval_T selfdicttv;
3290 typval_T argstv;
3291 list_T *argslist = NULL;
3292 dict_T *selfdict = NULL;
3293 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003294 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003295 typval_T *argv = NULL;
3296 typval_T *curtv;
3297 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003298
Bram Moolenaar8110a092016-04-14 15:56:09 +02003299 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003300 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003301 selfdictObject = PyDict_GetItemString(kwargs, "self");
3302 if (selfdictObject != NULL)
3303 {
3304 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3305 return NULL;
3306 selfdict = selfdicttv.vval.v_dict;
3307 }
3308 argsObject = PyDict_GetItemString(kwargs, "args");
3309 if (argsObject != NULL)
3310 {
3311 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3312 {
3313 dict_unref(selfdict);
3314 return NULL;
3315 }
3316 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003317 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003318
3319 argc = argslist->lv_len;
3320 if (argc != 0)
3321 {
3322 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003323 if (argv == NULL)
3324 {
3325 PyErr_NoMemory();
3326 dict_unref(selfdict);
3327 list_unref(argslist);
3328 return NULL;
3329 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003330 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003331 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003332 copy_tv(&li->li_tv, curtv++);
3333 }
3334 list_unref(argslist);
3335 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003336 if (selfdict != NULL)
3337 {
3338 auto_rebind = FALSE;
3339 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3340 if (autoRebindObject != NULL)
3341 {
3342 auto_rebind = PyObject_IsTrue(autoRebindObject);
3343 if (auto_rebind == -1)
3344 {
3345 dict_unref(selfdict);
3346 list_unref(argslist);
3347 return NULL;
3348 }
3349 }
3350 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003351 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003352
Bram Moolenaar389a1792013-06-23 13:00:44 +02003353 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003354 {
3355 dict_unref(selfdict);
3356 while (argc--)
3357 clear_tv(&argv[argc]);
3358 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003359 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003360 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003361
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003362 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003363
Bram Moolenaar389a1792013-06-23 13:00:44 +02003364 PyMem_Free(name);
3365
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003366 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003367}
3368
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003369 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003371{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003372 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003374 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003375 for (i = 0; i < self->argc; ++i)
3376 clear_tv(&self->argv[i]);
3377 PyMem_Free(self->argv);
3378 dict_unref(self->self);
3379 if (self->argv || self->self)
3380 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003381
3382 DESTRUCTOR_FINISH(self);
3383}
3384
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003385static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003386 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003387 NULL
3388};
3389
3390 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003391FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003392{
3393 return ObjectDir(self, FunctionAttrs);
3394}
3395
Bram Moolenaardb913952012-06-29 12:54:53 +02003396 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003397FunctionAttr(FunctionObject *self, char *name)
3398{
3399 list_T *list;
3400 int i;
3401 if (strcmp(name, "name") == 0)
3402 return PyString_FromString((char *)(self->name));
3403 else if (strcmp(name, "args") == 0)
3404 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003405 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003406 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003407
Bram Moolenaar8110a092016-04-14 15:56:09 +02003408 for (i = 0; i < self->argc; ++i)
3409 list_append_tv(list, &self->argv[i]);
3410 return NEW_LIST(list);
3411 }
3412 else if (strcmp(name, "self") == 0)
3413 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003414 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003415 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003416 else if (strcmp(name, "auto_rebind") == 0)
3417 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003418 ? ALWAYS_TRUE
3419 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003420 else if (strcmp(name, "__members__") == 0)
3421 return ObjectDir(NULL, FunctionAttrs);
3422 return NULL;
3423}
3424
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003425/*
3426 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003427 *
3428 * "exported" should be set to true when it is needed to construct a partial
3429 * that may be stored in a variable (i.e. may be freed by Vim).
3430 */
3431 static void
3432set_partial(FunctionObject *self, partial_T *pt, int exported)
3433{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003434 int i;
3435
3436 pt->pt_name = self->name;
3437 if (self->argv)
3438 {
3439 pt->pt_argc = self->argc;
3440 if (exported)
3441 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003442 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003443 for (i = 0; i < pt->pt_argc; ++i)
3444 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3445 }
3446 else
3447 pt->pt_argv = self->argv;
3448 }
3449 else
3450 {
3451 pt->pt_argc = 0;
3452 pt->pt_argv = NULL;
3453 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003454 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003455 pt->pt_dict = self->self;
3456 if (exported && self->self)
3457 ++pt->pt_dict->dv_refcount;
3458 if (exported)
3459 pt->pt_name = vim_strsave(pt->pt_name);
3460 pt->pt_refcount = 1;
3461}
3462
3463 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003464FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003465{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003466 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003467 typval_T args;
3468 typval_T selfdicttv;
3469 typval_T rettv;
3470 dict_T *selfdict = NULL;
3471 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003472 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003473 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003474 partial_T pt;
3475 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003476
Bram Moolenaar8110a092016-04-14 15:56:09 +02003477 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003478 return NULL;
3479
3480 if (kwargs != NULL)
3481 {
3482 selfdictObject = PyDict_GetItemString(kwargs, "self");
3483 if (selfdictObject != NULL)
3484 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003485 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003486 {
3487 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003488 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003489 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003490 selfdict = selfdicttv.vval.v_dict;
3491 }
3492 }
3493
Bram Moolenaar8110a092016-04-14 15:56:09 +02003494 if (self->argv || self->self)
3495 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003496 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003497 set_partial(self, &pt, FALSE);
3498 pt_ptr = &pt;
3499 }
3500
Bram Moolenaar71700b82013-05-15 17:49:05 +02003501 Py_BEGIN_ALLOW_THREADS
3502 Python_Lock_Vim();
3503
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003504 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003505 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003506
3507 Python_Release_Vim();
3508 Py_END_ALLOW_THREADS
3509
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003510 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003511 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003512 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003513 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003514 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003515 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003516 }
3517 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003518 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003519
Bram Moolenaardb913952012-06-29 12:54:53 +02003520 clear_tv(&args);
3521 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003522 if (selfdict != NULL)
3523 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003524
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003525 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003526}
3527
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003528 static PyObject *
3529FunctionRepr(FunctionObject *self)
3530{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003531 PyObject *ret;
3532 garray_T repr_ga;
3533 int i;
3534 char_u *tofree = NULL;
3535 typval_T tv;
3536 char_u numbuf[NUMBUFLEN];
3537
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003538 ga_init2(&repr_ga, sizeof(char), 70);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003539 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3540 if (self->name)
3541 ga_concat(&repr_ga, self->name);
3542 else
3543 ga_concat(&repr_ga, (char_u *)"<NULL>");
3544 ga_append(&repr_ga, '\'');
3545 if (self->argv)
3546 {
3547 ga_concat(&repr_ga, (char_u *)", args=[");
3548 ++emsg_silent;
3549 for (i = 0; i < self->argc; i++)
3550 {
3551 if (i != 0)
3552 ga_concat(&repr_ga, (char_u *)", ");
3553 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3554 get_copyID()));
3555 vim_free(tofree);
3556 }
3557 --emsg_silent;
3558 ga_append(&repr_ga, ']');
3559 }
3560 if (self->self)
3561 {
3562 ga_concat(&repr_ga, (char_u *)", self=");
3563 tv.v_type = VAR_DICT;
3564 tv.vval.v_dict = self->self;
3565 ++emsg_silent;
3566 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3567 --emsg_silent;
3568 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003569 if (self->auto_rebind)
3570 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003571 }
3572 ga_append(&repr_ga, '>');
3573 ret = PyString_FromString((char *)repr_ga.ga_data);
3574 ga_clear(&repr_ga);
3575 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003576}
3577
Bram Moolenaardb913952012-06-29 12:54:53 +02003578static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003579 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3580 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003581};
3582
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003583/*
3584 * Options object
3585 */
3586
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003587DEFINE_PY_TYPE_OBJECT(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003588
3589typedef int (*checkfun)(void *);
3590
3591typedef struct
3592{
3593 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003594 int opt_type;
3595 void *from;
3596 checkfun Check;
3597 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003598} OptionsObject;
3599
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003600 static int
3601dummy_check(void *arg UNUSED)
3602{
3603 return 0;
3604}
3605
3606 static PyObject *
3607OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3608{
3609 OptionsObject *self;
3610
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003611 self = PyObject_GC_New(OptionsObject, OptionsTypePtr);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003612 if (self == NULL)
3613 return NULL;
3614
3615 self->opt_type = opt_type;
3616 self->from = from;
3617 self->Check = Check;
3618 self->fromObj = fromObj;
3619 if (fromObj)
3620 Py_INCREF(fromObj);
3621
3622 return (PyObject *)(self);
3623}
3624
3625 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003626OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003627{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003628 PyObject_GC_UnTrack((void *)(self));
3629 Py_XDECREF(self->fromObj);
3630 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003631}
3632
3633 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003634OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003635{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003636 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003637 return 0;
3638}
3639
3640 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003641OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003642{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003643 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003644 return 0;
3645}
3646
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003647 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003648OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003649{
3650 char_u *key;
3651 int flags;
3652 long numval;
3653 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003654 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003655
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003656 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003657 return NULL;
3658
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003659 if (!(key = StringToChars(keyObject, &todecref)))
3660 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003661
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003662 if (*key == NUL)
3663 {
3664 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003665 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003666 return NULL;
3667 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003668
3669 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003670 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003671
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003672 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003673
3674 if (flags == 0)
3675 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003676 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003677 return NULL;
3678 }
3679
3680 if (flags & SOPT_UNSET)
3681 {
3682 Py_INCREF(Py_None);
3683 return Py_None;
3684 }
3685 else if (flags & SOPT_BOOL)
3686 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003687 PyObject *ret;
3688 ret = numval ? Py_True : Py_False;
3689 Py_INCREF(ret);
3690 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003691 }
3692 else if (flags & SOPT_NUM)
3693 return PyInt_FromLong(numval);
3694 else if (flags & SOPT_STRING)
3695 {
3696 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003697 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003698 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003699 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003700 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003701 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003702 else
3703 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003704 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003705 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003706 return NULL;
3707 }
3708 }
3709 else
3710 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003711 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003712 return NULL;
3713 }
3714}
3715
3716 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003717OptionsContains(OptionsObject *self, PyObject *keyObject)
3718{
3719 char_u *key;
3720 PyObject *todecref;
3721
3722 if (!(key = StringToChars(keyObject, &todecref)))
3723 return -1;
3724
3725 if (*key == NUL)
3726 {
3727 Py_XDECREF(todecref);
3728 return 0;
3729 }
3730
3731 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3732 {
3733 Py_XDECREF(todecref);
3734 return 1;
3735 }
3736 else
3737 {
3738 Py_XDECREF(todecref);
3739 return 0;
3740 }
3741}
3742
3743typedef struct
3744{
3745 void *lastoption;
3746 int opt_type;
3747} optiterinfo_T;
3748
3749 static PyObject *
3750OptionsIterNext(optiterinfo_T **oii)
3751{
3752 char_u *name;
3753
3754 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3755 return PyString_FromString((char *)name);
3756
3757 return NULL;
3758}
3759
3760 static PyObject *
3761OptionsIter(OptionsObject *self)
3762{
3763 optiterinfo_T *oii;
3764
3765 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3766 {
3767 PyErr_NoMemory();
3768 return NULL;
3769 }
3770
3771 oii->opt_type = self->opt_type;
3772 oii->lastoption = NULL;
3773
3774 return IterNew(oii,
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003775 (destructorfun)(void *) PyMem_Free, (nextfun) OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003776 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003777}
3778
3779 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003780set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003781{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003782 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003783
3784 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3785 {
3786 if (VimTryEnd())
3787 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003788 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003789 return FAIL;
3790 }
3791 return OK;
3792}
3793
3794 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003795set_option_value_for(
3796 char_u *key,
3797 int numval,
3798 char_u *stringval,
3799 int opt_flags,
3800 int opt_type,
3801 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003802{
Bram Moolenaar18f47402022-01-06 13:24:51 +00003803 switchwin_T switchwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003804 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003805 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003806
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003807 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003808 switch (opt_type)
3809 {
3810 case SREQ_WIN:
Bram Moolenaar18f47402022-01-06 13:24:51 +00003811 if (switch_win(&switchwin, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003812 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003813 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00003814 restore_win(&switchwin, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003815 if (VimTryEnd())
3816 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003817 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003818 return -1;
3819 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003820 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar18f47402022-01-06 13:24:51 +00003821 restore_win(&switchwin, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003822 break;
3823 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003824 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003825 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003826 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003827 break;
3828 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003829 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003830 break;
3831 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003832 if (set_ret == FAIL)
3833 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003834 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003835}
3836
3837 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003838OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003839{
3840 char_u *key;
3841 int flags;
3842 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003843 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003844 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003845
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003846 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003847 return -1;
3848
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003849 if (!(key = StringToChars(keyObject, &todecref)))
3850 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003851
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003852 if (*key == NUL)
3853 {
3854 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003855 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003856 return -1;
3857 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003858
3859 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003860 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003861
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003862 if (flags == 0)
3863 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003864 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003865 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003866 return -1;
3867 }
3868
3869 if (valObject == NULL)
3870 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003871 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003872 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003873 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003874 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003875 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003876 return -1;
3877 }
3878 else if (!(flags & SOPT_GLOBAL))
3879 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003880 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003881 N_("unable to unset option %s "
3882 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003883 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003884 return -1;
3885 }
3886 else
3887 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003888 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003889 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003890 return 0;
3891 }
3892 }
3893
Bram Moolenaard6e39182013-05-21 18:30:34 +02003894 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003895
3896 if (flags & SOPT_BOOL)
3897 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003898 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003899
Bram Moolenaarb983f752013-05-15 16:11:50 +02003900 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003901 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003902 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003903 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003904 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003905 }
3906 else if (flags & SOPT_NUM)
3907 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003908 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003909
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003910 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003911 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003912 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003913 return -1;
3914 }
3915
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003916 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003917 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003918 }
3919 else
3920 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003921 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003922 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003923
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003924 if ((val = StringToChars(valObject, &todecref2)))
3925 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003926 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003927 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003928 Py_XDECREF(todecref2);
3929 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003930 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003931 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003932 }
3933
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003934 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003935
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003936 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003937}
3938
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003939static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003940 0, // sq_length
3941 0, // sq_concat
3942 0, // sq_repeat
3943 0, // sq_item
3944 0, // sq_slice
3945 0, // sq_ass_item
3946 0, // sq_ass_slice
3947 (objobjproc) OptionsContains, // sq_contains
3948 0, // sq_inplace_concat
3949 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003950};
3951
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003952static PyMappingMethods OptionsAsMapping = {
3953 (lenfunc) NULL,
3954 (binaryfunc) OptionsItem,
3955 (objobjargproc) OptionsAssItem,
3956};
3957
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003958// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003959
3960typedef struct
3961{
3962 PyObject_HEAD
3963 tabpage_T *tab;
3964} TabPageObject;
3965
3966static PyObject *WinListNew(TabPageObject *tabObject);
3967
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003968DEFINE_PY_TYPE_OBJECT(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003969
3970 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003971CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003972{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003973 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003974 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003975 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003976 return -1;
3977 }
3978
3979 return 0;
3980}
3981
3982 static PyObject *
3983TabPageNew(tabpage_T *tab)
3984{
3985 TabPageObject *self;
3986
3987 if (TAB_PYTHON_REF(tab))
3988 {
3989 self = TAB_PYTHON_REF(tab);
3990 Py_INCREF(self);
3991 }
3992 else
3993 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003994 self = PyObject_NEW(TabPageObject, TabPageTypePtr);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003995 if (self == NULL)
3996 return NULL;
3997 self->tab = tab;
3998 TAB_PYTHON_REF(tab) = self;
3999 }
4000
4001 return (PyObject *)(self);
4002}
4003
4004 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004005TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004006{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004007 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
4008 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004009
4010 DESTRUCTOR_FINISH(self);
4011}
4012
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004013static char *TabPageAttrs[] = {
4014 "windows", "number", "vars", "window", "valid",
4015 NULL
4016};
4017
4018 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02004019TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004020{
4021 return ObjectDir(self, TabPageAttrs);
4022}
4023
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004024 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004025TabPageAttrValid(TabPageObject *self, char *name)
4026{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004027 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004028
4029 if (strcmp(name, "valid") != 0)
4030 return NULL;
4031
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004032 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
4033 Py_INCREF(ret);
4034 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004035}
4036
4037 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004038TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004039{
4040 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004042 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004043 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004044 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004045 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004046 else if (strcmp(name, "window") == 0)
4047 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004048 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02004049 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004050 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004051 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004052 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004053 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004054 else if (strcmp(name, "__members__") == 0)
4055 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004056 return NULL;
4057}
4058
4059 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004060TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004061{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004062 if (self->tab == INVALID_TABPAGE_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00004063 return PyString_FromFormat("<tabpage object (deleted) at %p>", (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004064 else
4065 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004066 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004067
4068 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004069 return PyString_FromFormat("<tabpage object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00004070 (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004071 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004072 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004073 }
4074}
4075
4076static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004077 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004078 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
4079 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004080};
4081
4082/*
4083 * Window list object
4084 */
4085
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004086DEFINE_PY_TYPE_OBJECT(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004087static PySequenceMethods TabListAsSeq;
4088
4089typedef struct
4090{
4091 PyObject_HEAD
4092} TabListObject;
4093
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004094static TabListObject TheTabPageList =
4095{
4096 PyObject_HEAD_INIT_TYPE(TabListType)
4097};
4098
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004099 static PyInt
4100TabListLength(PyObject *self UNUSED)
4101{
4102 tabpage_T *tp = first_tabpage;
4103 PyInt n = 0;
4104
4105 while (tp != NULL)
4106 {
4107 ++n;
4108 tp = tp->tp_next;
4109 }
4110
4111 return n;
4112}
4113
4114 static PyObject *
4115TabListItem(PyObject *self UNUSED, PyInt n)
4116{
4117 tabpage_T *tp;
4118
4119 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
4120 if (n == 0)
4121 return TabPageNew(tp);
4122
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004123 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004124 return NULL;
4125}
4126
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02004127/*
4128 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004129 */
4130
4131typedef struct
4132{
4133 PyObject_HEAD
4134 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004135 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004136} WindowObject;
4137
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004138DEFINE_PY_TYPE_OBJECT(WindowType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139
4140 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004141CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004142{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004143 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004144 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004145 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146 return -1;
4147 }
4148
4149 return 0;
4150}
4151
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004152 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004153WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02004154{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004155 /*
4156 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02004157 * If we add a "w_python*_ref" field to the win_T structure,
4158 * then we can get at it in win_free() in vim. We then
4159 * need to create only ONE Python object per window - if
4160 * we try to create a second, just INCREF the existing one
4161 * and return it. The (single) Python object referring to
4162 * the window is stored in "w_python*_ref".
4163 * On a win_free() we set the Python object's win_T* field
4164 * to an invalid value. We trap all uses of a window
4165 * object, and reject them if the win_T* field is invalid.
4166 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004167 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004168 * w_python_ref and w_python3_ref fields respectively.
4169 */
4170
4171 WindowObject *self;
4172
4173 if (WIN_PYTHON_REF(win))
4174 {
4175 self = WIN_PYTHON_REF(win);
4176 Py_INCREF(self);
4177 }
4178 else
4179 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004180 self = PyObject_GC_New(WindowObject, WindowTypePtr);
Bram Moolenaar971db462013-05-12 18:44:48 +02004181 if (self == NULL)
4182 return NULL;
4183 self->win = win;
4184 WIN_PYTHON_REF(win) = self;
4185 }
4186
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004187 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
4188
Bram Moolenaar971db462013-05-12 18:44:48 +02004189 return (PyObject *)(self);
4190}
4191
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004192 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004193WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004194{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004195 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02004196 if (self->win && self->win != INVALID_WINDOW_VALUE)
4197 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02004198 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02004199 PyObject_GC_Del((void *)(self));
4200}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004201
Bram Moolenaar774267b2013-05-21 20:51:59 +02004202 static int
4203WindowTraverse(WindowObject *self, visitproc visit, void *arg)
4204{
4205 Py_VISIT(((PyObject *)(self->tabObject)));
4206 return 0;
4207}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004208
Bram Moolenaar774267b2013-05-21 20:51:59 +02004209 static int
4210WindowClear(WindowObject *self)
4211{
4212 Py_CLEAR(self->tabObject);
4213 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004214}
4215
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004216 static win_T *
4217get_firstwin(TabPageObject *tabObject)
4218{
4219 if (tabObject)
4220 {
4221 if (CheckTabPage(tabObject))
4222 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004223 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004224 else if (tabObject->tab == curtab)
4225 return firstwin;
4226 else
4227 return tabObject->tab->tp_firstwin;
4228 }
4229 else
4230 return firstwin;
4231}
Bram Moolenaare950f992018-06-10 13:55:55 +02004232
4233// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004234static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02004235 "buffer",
4236 "cursor",
4237 "height",
4238 "row",
4239 "width",
4240 "col",
4241 "vars",
4242 "options",
4243 "number",
4244 "tabpage",
4245 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004246 NULL
4247};
4248
4249 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02004250WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004251{
4252 return ObjectDir(self, WindowAttrs);
4253}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004254
Bram Moolenaar971db462013-05-12 18:44:48 +02004255 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004256WindowAttrValid(WindowObject *self, char *name)
4257{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004258 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004259
4260 if (strcmp(name, "valid") != 0)
4261 return NULL;
4262
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004263 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
4264 Py_INCREF(ret);
4265 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004266}
4267
4268 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004269WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004270{
4271 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004272 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004273 else if (strcmp(name, "cursor") == 0)
4274 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004275 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004276
4277 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4278 }
4279 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004280 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004281 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004282 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004283 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004284 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004285 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004286 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004287 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004288 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004289 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004290 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
4291 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004292 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004293 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004294 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004295 return NULL;
4296 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004297 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004298 }
4299 else if (strcmp(name, "tabpage") == 0)
4300 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004301 Py_INCREF(self->tabObject);
4302 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004303 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004304 else if (strcmp(name, "__members__") == 0)
4305 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004306 else
4307 return NULL;
4308}
4309
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004310 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004311WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004312{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004313 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004314 return -1;
4315
4316 if (strcmp(name, "buffer") == 0)
4317 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004318 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004319 return -1;
4320 }
4321 else if (strcmp(name, "cursor") == 0)
4322 {
4323 long lnum;
4324 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004325
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004326 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004327 return -1;
4328
Bram Moolenaard6e39182013-05-21 18:30:34 +02004329 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004330 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004331 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004332 return -1;
4333 }
4334
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004335 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004336 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004337 return -1;
4338
Bram Moolenaard6e39182013-05-21 18:30:34 +02004339 self->win->w_cursor.lnum = lnum;
4340 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004341 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004342 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004343 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004344 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004345
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004346 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004347 return 0;
4348 }
4349 else if (strcmp(name, "height") == 0)
4350 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004351 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004352 win_T *savewin;
4353
Bram Moolenaardee2e312013-06-23 16:35:47 +02004354 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004355 return -1;
4356
4357#ifdef FEAT_GUI
4358 need_mouse_correct = TRUE;
4359#endif
4360 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004361 curwin = self->win;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004362 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004363
4364 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004365 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004366 curwin = savewin;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004367 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004368 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004369 return -1;
4370
4371 return 0;
4372 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004373 else if (strcmp(name, "width") == 0)
4374 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004375 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004376 win_T *savewin;
4377
Bram Moolenaardee2e312013-06-23 16:35:47 +02004378 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004379 return -1;
4380
4381#ifdef FEAT_GUI
4382 need_mouse_correct = TRUE;
4383#endif
4384 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004385 curwin = self->win;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004386 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004387
4388 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004389 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 curwin = savewin;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004391 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004392 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 return -1;
4394
4395 return 0;
4396 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 else
4398 {
4399 PyErr_SetString(PyExc_AttributeError, name);
4400 return -1;
4401 }
4402}
4403
4404 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004405WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004407 if (self->win == INVALID_WINDOW_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00004408 return PyString_FromFormat("<window object (deleted) at %p>", (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004409 else
4410 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004411 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412
Bram Moolenaar6d216452013-05-12 19:00:41 +02004413 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004414 return PyString_FromFormat("<window object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00004415 (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004417 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004418 }
4419}
4420
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004421static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004422 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004423 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4424 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004425};
4426
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004427/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004428 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004430
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004431DEFINE_PY_TYPE_OBJECT(WinListType);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004432static PySequenceMethods WinListAsSeq;
4433
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004434typedef struct
4435{
4436 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004437 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004438} WinListObject;
4439
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004440static WinListObject TheWindowList =
4441{
4442 PyObject_HEAD_INIT_TYPE(WinListType)
4443 NULL
4444};
4445
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004446 static PyObject *
4447WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004449 WinListObject *self;
4450
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004451 self = PyObject_NEW(WinListObject, WinListTypePtr);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004452 self->tabObject = tabObject;
4453 Py_INCREF(tabObject);
4454
4455 return (PyObject *)(self);
4456}
4457
4458 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004459WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004460{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004461 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004462
4463 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004464 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004465 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004466 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004467
4468 DESTRUCTOR_FINISH(self);
4469}
4470
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004471 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004472WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004473{
4474 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004475 PyInt n = 0;
4476
Bram Moolenaard6e39182013-05-21 18:30:34 +02004477 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004478 return -1;
4479
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004480 while (w != NULL)
4481 {
4482 ++n;
4483 w = W_NEXT(w);
4484 }
4485
4486 return n;
4487}
4488
4489 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004490WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004491{
4492 win_T *w;
4493
Bram Moolenaard6e39182013-05-21 18:30:34 +02004494 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004495 return NULL;
4496
4497 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004498 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004499 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004500
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004501 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004502 return NULL;
4503}
4504
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004505/*
4506 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004507 *
4508 * The result is in allocated memory. All internal nulls are replaced by
4509 * newline characters. It is an error for the string to contain newline
4510 * characters.
4511 *
4512 * On errors, the Python exception data is set, and NULL is returned.
4513 */
4514 static char *
4515StringToLine(PyObject *obj)
4516{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004517 char *str;
4518 char *save;
4519 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004520 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004521 PyInt i;
4522 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004523
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004524 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004525 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004526 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4527 || str == NULL)
4528 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004529 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004530 else if (PyUnicode_Check(obj))
4531 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004532 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4533 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004534 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004535
Bram Moolenaardaa27022013-06-24 22:33:30 +02004536 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004537 || str == NULL)
4538 {
4539 Py_DECREF(bytes);
4540 return NULL;
4541 }
4542 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004543 else
4544 {
4545#if PY_MAJOR_VERSION < 3
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004546 PyErr_FORMAT_TYPE(
Bram Moolenaardaa27022013-06-24 22:33:30 +02004547 N_("expected str() or unicode() instance, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004548 obj);
Bram Moolenaardaa27022013-06-24 22:33:30 +02004549#else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004550 PyErr_FORMAT_TYPE(
Bram Moolenaardaa27022013-06-24 22:33:30 +02004551 N_("expected bytes() or str() instance, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004552 obj);
Bram Moolenaardaa27022013-06-24 22:33:30 +02004553#endif
4554 return NULL;
4555 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004556
4557 /*
4558 * Error checking: String must not contain newlines, as we
4559 * are replacing a single line, and we must replace it with
4560 * a single line.
4561 * A trailing newline is removed, so that append(f.readlines()) works.
4562 */
4563 p = memchr(str, '\n', len);
4564 if (p != NULL)
4565 {
4566 if (p == str + len - 1)
4567 --len;
4568 else
4569 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004570 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004571 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572 return NULL;
4573 }
4574 }
4575
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004576 /*
4577 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004578 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004579 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004580 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004581 if (save == NULL)
4582 {
4583 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004584 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004585 return NULL;
4586 }
4587
4588 for (i = 0; i < len; ++i)
4589 {
4590 if (str[i] == '\0')
4591 save[i] = '\n';
4592 else
4593 save[i] = str[i];
4594 }
4595
4596 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004597 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004598
4599 return save;
4600}
4601
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004602/*
4603 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004604 * in Vim format (1-based). The line is returned as a Python
4605 * string object.
4606 */
4607 static PyObject *
4608GetBufferLine(buf_T *buf, PyInt n)
4609{
4610 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4611}
4612
4613
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004614/*
4615 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004616 * are in Vim format (1-based). The range is from lo up to, but not
4617 * including, hi. The list is returned as a Python list of string objects.
4618 */
4619 static PyObject *
4620GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4621{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004622 PyInt i;
4623 PyInt n = hi - lo;
4624 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004625
4626 if (list == NULL)
4627 return NULL;
4628
4629 for (i = 0; i < n; ++i)
4630 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004631 linenr_T lnum = (linenr_T)(lo + i);
4632 char *text;
4633 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004634
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004635 if (lnum > buf->b_ml.ml_line_count)
4636 text = "";
4637 else
4638 text = (char *)ml_get_buf(buf, lnum, FALSE);
4639 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004640 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004641 {
4642 Py_DECREF(list);
4643 return NULL;
4644 }
4645
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004646 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004647 }
4648
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004649 // The ownership of the Python list is passed to the caller (ie,
4650 // the caller should Py_DECREF() the object when it is finished
4651 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652
4653 return list;
4654}
4655
4656/*
4657 * Check if deleting lines made the cursor position invalid.
4658 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4659 * deleted).
4660 */
4661 static void
4662py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4663{
4664 if (curwin->w_cursor.lnum >= lo)
4665 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004666 // Adjust the cursor position if it's in/after the changed
4667 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004668 if (curwin->w_cursor.lnum >= hi)
4669 {
4670 curwin->w_cursor.lnum += extra;
4671 check_cursor_col();
4672 }
4673 else if (extra < 0)
4674 {
4675 curwin->w_cursor.lnum = lo;
4676 check_cursor();
4677 }
4678 else
4679 check_cursor_col();
4680 changed_cline_bef_curs();
4681 }
4682 invalidate_botline();
4683}
4684
Bram Moolenaar19e60942011-06-19 00:27:51 +02004685/*
4686 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004687 * in Vim format (1-based). The replacement line is given as
4688 * a Python string object. The object is checked for validity
4689 * and correct format. Errors are returned as a value of FAIL.
4690 * The return value is OK on success.
4691 * If OK is returned and len_change is not NULL, *len_change
4692 * is set to the change in the buffer length.
4693 */
4694 static int
4695SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4696{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004697 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004698 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004699
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004700 // First of all, we check the type of the supplied Python object.
4701 // There are three cases:
4702 // 1. NULL, or None - this is a deletion.
4703 // 2. A string - this is a replacement.
4704 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004705 if (line == Py_None || line == NULL)
4706 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004707 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004708 switchwin.sw_curwin = NULL;
4709 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004710
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004711 VimTryStart();
4712
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004713 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004714 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004715 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004716 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004717 else
4718 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00004719 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004720 || save_curbuf.br_buf == NULL))
4721 // Using an existing window for the buffer, adjust the cursor
4722 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004724 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004725 // Only adjust marks if we managed to switch to a window that
4726 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004727 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004728 }
4729
Bram Moolenaar18f47402022-01-06 13:24:51 +00004730 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004732 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733 return FAIL;
4734
4735 if (len_change)
4736 *len_change = -1;
4737
4738 return OK;
4739 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004740 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004742 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004743
4744 if (save == NULL)
4745 return FAIL;
4746
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004747 VimTryStart();
4748
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004749 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004750 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004751 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752
4753 if (u_savesub((linenr_T)n) == FAIL)
4754 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004755 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004756 vim_free(save);
4757 }
4758 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4759 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004760 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004761 vim_free(save);
4762 }
4763 else
4764 changed_bytes((linenr_T)n, 0);
4765
Bram Moolenaar18f47402022-01-06 13:24:51 +00004766 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004767
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004768 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004769 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004770 check_cursor_col();
4771
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004772 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004773 return FAIL;
4774
4775 if (len_change)
4776 *len_change = 0;
4777
4778 return OK;
4779 }
4780 else
4781 {
4782 PyErr_BadArgument();
4783 return FAIL;
4784 }
4785}
4786
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004787/*
4788 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004789 * Vim format (1-based). The range is from lo up to, but not including, hi.
4790 * The replacement lines are given as a Python list of string objects. The
4791 * list is checked for validity and correct format. Errors are returned as a
4792 * value of FAIL. The return value is OK on success.
4793 * If OK is returned and len_change is not NULL, *len_change
4794 * is set to the change in the buffer length.
4795 */
4796 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004797SetBufferLineList(
4798 buf_T *buf,
4799 PyInt lo,
4800 PyInt hi,
4801 PyObject *list,
4802 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004803{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004804 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004805 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004806
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004807 // First of all, we check the type of the supplied Python object.
4808 // There are three cases:
4809 // 1. NULL, or None - this is a deletion.
4810 // 2. A list - this is a replacement.
4811 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004812 if (list == Py_None || list == NULL)
4813 {
4814 PyInt i;
4815 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004816
4817 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004818 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004819 switchwin.sw_curwin = NULL;
4820 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004821
4822 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004823 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004824 else
4825 {
4826 for (i = 0; i < n; ++i)
4827 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004828 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004829 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004830 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004831 break;
4832 }
4833 }
Bram Moolenaar18f47402022-01-06 13:24:51 +00004834 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004835 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004836 // Using an existing window for the buffer, adjust the cursor
4837 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004838 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004839 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004840 // Only adjust marks if we managed to switch to a window that
4841 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004842 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004843 }
4844
Bram Moolenaar18f47402022-01-06 13:24:51 +00004845 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004846
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004847 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004848 return FAIL;
4849
4850 if (len_change)
4851 *len_change = -n;
4852
4853 return OK;
4854 }
4855 else if (PyList_Check(list))
4856 {
4857 PyInt i;
4858 PyInt new_len = PyList_Size(list);
4859 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004860 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004861 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004862
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004863 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004864 array = NULL;
4865 else
4866 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004867 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004868 if (array == NULL)
4869 {
4870 PyErr_NoMemory();
4871 return FAIL;
4872 }
4873 }
4874
4875 for (i = 0; i < new_len; ++i)
4876 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004877 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004878
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004879 if (!(line = PyList_GetItem(list, i)) ||
4880 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004881 {
4882 while (i)
4883 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004884 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004885 return FAIL;
4886 }
4887 }
4888
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004889 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004890 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004891
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004892 // START of region without "return". Must call restore_buffer()!
Bram Moolenaar18f47402022-01-06 13:24:51 +00004893 switchwin.sw_curwin = NULL;
4894 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004895
4896 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004897 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004898
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004899 // If the size of the range is reducing (ie, new_len < old_len) we
4900 // need to delete some old_len. We do this at the start, by
4901 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004902 if (!PyErr_Occurred())
4903 {
4904 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004905 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004906 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004907 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004908 break;
4909 }
4910 extra -= i;
4911 }
4912
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004913 // For as long as possible, replace the existing old_len with the
4914 // new old_len. This is a more efficient operation, as it requires
4915 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004916 if (!PyErr_Occurred())
4917 {
4918 for (i = 0; i < old_len && i < new_len; ++i)
4919 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4920 == FAIL)
4921 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004922 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004923 break;
4924 }
4925 }
4926 else
4927 i = 0;
4928
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004929 // Now we may need to insert the remaining new old_len. If we do, we
4930 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004931 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004932 if (!PyErr_Occurred())
4933 {
4934 while (i < new_len)
4935 {
4936 if (ml_append((linenr_T)(lo + i - 1),
4937 (char_u *)array[i], 0, FALSE) == FAIL)
4938 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004939 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004940 break;
4941 }
4942 vim_free(array[i]);
4943 ++i;
4944 ++extra;
4945 }
4946 }
4947
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004948 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004949 while (i < new_len)
4950 {
4951 vim_free(array[i]);
4952 ++i;
4953 }
4954
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004955 // Free the array of old_len. All of its contents have now
4956 // been dealt with (either freed, or the responsibility passed
4957 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004958 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004959
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004960 // Adjust marks. Invalidate any which lie in the
4961 // changed range, and move any in the remainder of the buffer.
4962 // Only adjust marks if we managed to switch to a window that holds
4963 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004964 if (save_curbuf.br_buf == NULL)
Bram Moolenaar37233f62022-05-22 12:23:48 +01004965 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004966 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004967 (long)MAXLNUM, (long)extra);
Bram Moolenaar37233f62022-05-22 12:23:48 +01004968 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4969 }
Bram Moolenaar19e60942011-06-19 00:27:51 +02004970
Bram Moolenaar18f47402022-01-06 13:24:51 +00004971 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004972 || save_curbuf.br_buf == NULL))
4973 // Using an existing window for the buffer, adjust the cursor
4974 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004975 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4976
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004977 // END of region without "return".
Bram Moolenaar18f47402022-01-06 13:24:51 +00004978 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004979
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004980 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004981 return FAIL;
4982
4983 if (len_change)
4984 *len_change = new_len - old_len;
4985
4986 return OK;
4987 }
4988 else
4989 {
4990 PyErr_BadArgument();
4991 return FAIL;
4992 }
4993}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004994
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004995/*
4996 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004997 * The line number is in Vim format (1-based). The lines to be inserted are
4998 * given as a Python list of string objects or as a single string. The lines
4999 * to be added are checked for validity and correct format. Errors are
5000 * returned as a value of FAIL. The return value is OK on success.
5001 * If OK is returned and len_change is not NULL, *len_change
5002 * is set to the change in the buffer length.
5003 */
5004 static int
5005InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
5006{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02005007 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00005008 switchwin_T switchwin;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02005009
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005010 // First of all, we check the type of the supplied Python object.
5011 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005012 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005013 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02005014 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005015
5016 if (str == NULL)
5017 return FAIL;
5018
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005019 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005020 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00005021 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005022
Bram Moolenaar95064ec2013-07-17 17:15:25 +02005023 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02005024 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005025 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005026 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005027 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005028 // Only adjust marks if we managed to switch to a window that
5029 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005030 appended_lines_mark((linenr_T)n, 1L);
5031
5032 vim_free(str);
Bram Moolenaar18f47402022-01-06 13:24:51 +00005033 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005034 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005035
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005036 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005037 return FAIL;
5038
5039 if (len_change)
5040 *len_change = 1;
5041
5042 return OK;
5043 }
5044 else if (PyList_Check(lines))
5045 {
5046 PyInt i;
5047 PyInt size = PyList_Size(lines);
5048 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005049
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005050 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005051 if (array == NULL)
5052 {
5053 PyErr_NoMemory();
5054 return FAIL;
5055 }
5056
5057 for (i = 0; i < size; ++i)
5058 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005059 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005060
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005061 if (!(line = PyList_GetItem(lines, i)) ||
5062 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005063 {
5064 while (i)
5065 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005066 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005067 return FAIL;
5068 }
5069 }
5070
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005071 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005072 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00005073 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005074
5075 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02005076 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005077 else
5078 {
5079 for (i = 0; i < size; ++i)
5080 {
5081 if (ml_append((linenr_T)(n + i),
5082 (char_u *)array[i], 0, FALSE) == FAIL)
5083 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005084 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005085
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005086 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005087 while (i < size)
5088 vim_free(array[i++]);
5089
5090 break;
5091 }
5092 vim_free(array[i]);
5093 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005094 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005095 // Only adjust marks if we managed to switch to a window that
5096 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005097 appended_lines_mark((linenr_T)n, (long)i);
5098 }
5099
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005100 // Free the array of lines. All of its contents have now
5101 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005102 PyMem_Free(array);
Bram Moolenaar18f47402022-01-06 13:24:51 +00005103 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005104
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005105 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005106
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005107 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005108 return FAIL;
5109
5110 if (len_change)
5111 *len_change = size;
5112
5113 return OK;
5114 }
5115 else
5116 {
5117 PyErr_BadArgument();
5118 return FAIL;
5119 }
5120}
5121
5122/*
5123 * Common routines for buffers and line ranges
5124 * -------------------------------------------
5125 */
5126
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005127typedef struct
5128{
5129 PyObject_HEAD
5130 buf_T *buf;
5131} BufferObject;
5132
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005133 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02005134CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005135{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005136 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005137 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005138 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005139 return -1;
5140 }
5141
5142 return 0;
5143}
5144
5145 static PyObject *
5146RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
5147{
5148 if (CheckBuffer(self))
5149 return NULL;
5150
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005151 if (end == -1)
5152 end = self->buf->b_ml.ml_line_count;
5153
Bram Moolenaarbd80f352013-05-12 21:16:23 +02005154 if (n < 0)
5155 n += end - start + 1;
5156
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005157 if (n < 0 || n > end - start)
5158 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005159 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005160 return NULL;
5161 }
5162
5163 return GetBufferLine(self->buf, n+start);
5164}
5165
5166 static PyObject *
5167RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
5168{
5169 PyInt size;
5170
5171 if (CheckBuffer(self))
5172 return NULL;
5173
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005174 if (end == -1)
5175 end = self->buf->b_ml.ml_line_count;
5176
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005177 size = end - start + 1;
5178
5179 if (lo < 0)
5180 lo = 0;
5181 else if (lo > size)
5182 lo = size;
5183 if (hi < 0)
5184 hi = 0;
5185 if (hi < lo)
5186 hi = lo;
5187 else if (hi > size)
5188 hi = size;
5189
5190 return GetBufferLineList(self->buf, lo+start, hi+start);
5191}
5192
5193 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005194RBAsItem(
5195 BufferObject *self,
5196 PyInt n,
5197 PyObject *valObject,
5198 PyInt start,
5199 PyInt end,
5200 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005201{
5202 PyInt len_change;
5203
5204 if (CheckBuffer(self))
5205 return -1;
5206
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005207 if (end == -1)
5208 end = self->buf->b_ml.ml_line_count;
5209
Bram Moolenaarbd80f352013-05-12 21:16:23 +02005210 if (n < 0)
5211 n += end - start + 1;
5212
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005213 if (n < 0 || n > end - start)
5214 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005215 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005216 return -1;
5217 }
5218
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005219 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005220 return -1;
5221
5222 if (new_end)
5223 *new_end = end + len_change;
5224
5225 return 0;
5226}
5227
Bram Moolenaar19e60942011-06-19 00:27:51 +02005228 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005229RBAsSlice(
5230 BufferObject *self,
5231 PyInt lo,
5232 PyInt hi,
5233 PyObject *valObject,
5234 PyInt start,
5235 PyInt end,
5236 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02005237{
5238 PyInt size;
5239 PyInt len_change;
5240
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005241 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02005242 if (CheckBuffer(self))
5243 return -1;
5244
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005245 if (end == -1)
5246 end = self->buf->b_ml.ml_line_count;
5247
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005248 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02005249 size = end - start + 1;
5250
5251 if (lo < 0)
5252 lo = 0;
5253 else if (lo > size)
5254 lo = size;
5255 if (hi < 0)
5256 hi = 0;
5257 if (hi < lo)
5258 hi = lo;
5259 else if (hi > size)
5260 hi = size;
5261
5262 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005263 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02005264 return -1;
5265
5266 if (new_end)
5267 *new_end = end + len_change;
5268
5269 return 0;
5270}
5271
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005272
5273 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005274RBAppend(
5275 BufferObject *self,
5276 PyObject *args,
5277 PyInt start,
5278 PyInt end,
5279 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005280{
5281 PyObject *lines;
5282 PyInt len_change;
5283 PyInt max;
5284 PyInt n;
5285
5286 if (CheckBuffer(self))
5287 return NULL;
5288
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005289 if (end == -1)
5290 end = self->buf->b_ml.ml_line_count;
5291
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005292 max = n = end - start + 1;
5293
5294 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5295 return NULL;
5296
5297 if (n < 0 || n > max)
5298 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005299 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005300 return NULL;
5301 }
5302
5303 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5304 return NULL;
5305
5306 if (new_end)
5307 *new_end = end + len_change;
5308
5309 Py_INCREF(Py_None);
5310 return Py_None;
5311}
5312
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005313// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005314
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005315DEFINE_PY_TYPE_OBJECT(RangeType);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005316static PySequenceMethods RangeAsSeq;
5317static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005318
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005319typedef struct
5320{
5321 PyObject_HEAD
5322 BufferObject *buf;
5323 PyInt start;
5324 PyInt end;
5325} RangeObject;
5326
5327 static PyObject *
5328RangeNew(buf_T *buf, PyInt start, PyInt end)
5329{
5330 BufferObject *bufr;
5331 RangeObject *self;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005332 self = PyObject_GC_New(RangeObject, RangeTypePtr);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005333 if (self == NULL)
5334 return NULL;
5335
5336 bufr = (BufferObject *)BufferNew(buf);
5337 if (bufr == NULL)
5338 {
5339 Py_DECREF(self);
5340 return NULL;
5341 }
5342 Py_INCREF(bufr);
5343
5344 self->buf = bufr;
5345 self->start = start;
5346 self->end = end;
5347
5348 return (PyObject *)(self);
5349}
5350
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005351 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005352RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005353{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005354 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005355 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005356 PyObject_GC_Del((void *)(self));
5357}
5358
5359 static int
5360RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5361{
5362 Py_VISIT(((PyObject *)(self->buf)));
5363 return 0;
5364}
5365
5366 static int
5367RangeClear(RangeObject *self)
5368{
5369 Py_CLEAR(self->buf);
5370 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005371}
5372
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005373 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005374RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005375{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005376 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005377 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005378 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005379
Bram Moolenaard6e39182013-05-21 18:30:34 +02005380 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005381}
5382
5383 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005384RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005385{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005386 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005387}
5388
5389 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005390RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005391{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005392 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005393}
5394
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005395static char *RangeAttrs[] = {
5396 "start", "end",
5397 NULL
5398};
5399
5400 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005401RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005402{
5403 return ObjectDir(self, RangeAttrs);
5404}
5405
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005406 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005407RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005408{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005409 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005410}
5411
5412 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005413RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005414{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005415 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005416 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00005417 (void *)self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005418 else
5419 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005420 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005421
5422 if (name == NULL)
5423 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005424
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005425 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005426 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005427 }
5428}
5429
5430static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005431 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005432 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005433 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5434 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005435};
5436
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005437DEFINE_PY_TYPE_OBJECT(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005438static PySequenceMethods BufferAsSeq;
5439static PyMappingMethods BufferAsMapping;
5440
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005441 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005442BufferNew(buf_T *buf)
5443{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005444 /*
5445 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005446 * If we add a "b_python*_ref" field to the buf_T structure,
5447 * then we can get at it in buf_freeall() in vim. We then
5448 * need to create only ONE Python object per buffer - if
5449 * we try to create a second, just INCREF the existing one
5450 * and return it. The (single) Python object referring to
5451 * the buffer is stored in "b_python*_ref".
5452 * Question: what to do on a buf_freeall(). We'll probably
5453 * have to either delete the Python object (DECREF it to
5454 * zero - a bad idea, as it leaves dangling refs!) or
5455 * set the buf_T * value to an invalid value (-1?), which
5456 * means we need checks in all access functions... Bah.
5457 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005458 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005459 * b_python_ref and b_python3_ref fields respectively.
5460 */
5461
5462 BufferObject *self;
5463
5464 if (BUF_PYTHON_REF(buf) != NULL)
5465 {
5466 self = BUF_PYTHON_REF(buf);
5467 Py_INCREF(self);
5468 }
5469 else
5470 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005471 self = PyObject_NEW(BufferObject, BufferTypePtr);
Bram Moolenaar971db462013-05-12 18:44:48 +02005472 if (self == NULL)
5473 return NULL;
5474 self->buf = buf;
5475 BUF_PYTHON_REF(buf) = self;
5476 }
5477
5478 return (PyObject *)(self);
5479}
5480
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005481 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005482BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005483{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005484 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5485 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005486
5487 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005488}
5489
Bram Moolenaar971db462013-05-12 18:44:48 +02005490 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005491BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005492{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005493 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005494 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005495 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005496
Bram Moolenaard6e39182013-05-21 18:30:34 +02005497 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005498}
5499
5500 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005501BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005502{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005503 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005504}
5505
5506 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005507BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005508{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005509 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005510}
5511
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005512static char *BufferAttrs[] = {
5513 "name", "number", "vars", "options", "valid",
5514 NULL
5515};
5516
5517 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005518BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005519{
5520 return ObjectDir(self, BufferAttrs);
5521}
5522
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005523 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005524BufferAttrValid(BufferObject *self, char *name)
5525{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005526 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005527
5528 if (strcmp(name, "valid") != 0)
5529 return NULL;
5530
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005531 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5532 Py_INCREF(ret);
5533 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005534}
5535
5536 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005537BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005538{
5539 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005540 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005541 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005542 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005543 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005544 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005545 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005546 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005547 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5548 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005549 else if (strcmp(name, "__members__") == 0)
5550 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005551 else
5552 return NULL;
5553}
5554
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005555 static int
5556BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5557{
5558 if (CheckBuffer(self))
5559 return -1;
5560
5561 if (strcmp(name, "name") == 0)
5562 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005564 aco_save_T aco;
Bram Moolenaar37199892022-11-29 13:46:48 +00005565 int ren_ret = OK;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005566 PyObject *todecref;
5567
5568 if (!(val = StringToChars(valObject, &todecref)))
5569 return -1;
5570
5571 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005572 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005573 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00005574 if (curbuf == self->buf)
5575 {
5576 ren_ret = rename_buffer(val);
5577 aucmd_restbuf(&aco);
5578 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005579 Py_XDECREF(todecref);
5580 if (VimTryEnd())
5581 return -1;
5582
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005583 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005584 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005585 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005586 return -1;
5587 }
5588 return 0;
5589 }
5590 else
5591 {
5592 PyErr_SetString(PyExc_AttributeError, name);
5593 return -1;
5594 }
5595}
5596
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005597 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005598BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005599{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005600 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005601}
5602
5603 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005604BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005605{
5606 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005607 char_u *pmark;
5608 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005609 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005610 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005611
Bram Moolenaard6e39182013-05-21 18:30:34 +02005612 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005613 return NULL;
5614
Bram Moolenaar389a1792013-06-23 13:00:44 +02005615 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005616 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005617
Bram Moolenaar389a1792013-06-23 13:00:44 +02005618 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005619 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005620 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005621 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005622 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005623 return NULL;
5624 }
5625
5626 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005627
5628 Py_XDECREF(todecref);
5629
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005630 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005631 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005632 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005633 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005634 if (VimTryEnd())
5635 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005636
5637 if (posp == NULL)
5638 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005639 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005640 return NULL;
5641 }
5642
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005643 if (posp->lnum <= 0)
5644 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005645 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005646 Py_INCREF(Py_None);
5647 return Py_None;
5648 }
5649
5650 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5651}
5652
5653 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005654BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005655{
5656 PyInt start;
5657 PyInt end;
5658
Bram Moolenaard6e39182013-05-21 18:30:34 +02005659 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005660 return NULL;
5661
5662 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5663 return NULL;
5664
Bram Moolenaard6e39182013-05-21 18:30:34 +02005665 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005666}
5667
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005668 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005669BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005670{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005671 if (self->buf == INVALID_BUFFER_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00005672 return PyString_FromFormat("<buffer object (deleted) at %p>", (void *)self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005673 else
5674 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005675 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005676
5677 if (name == NULL)
5678 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005679
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005680 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005681 }
5682}
5683
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005684static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005685 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005686 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005687 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005688 {"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 +02005689 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5690 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005691};
5692
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005693/*
5694 * Buffer list object - Implementation
5695 */
5696
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005697DEFINE_PY_TYPE_OBJECT(BufMapType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005698
5699typedef struct
5700{
5701 PyObject_HEAD
5702} BufMapObject;
5703
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005704static BufMapObject TheBufferMap =
5705{
5706 PyObject_HEAD_INIT_TYPE(BufMapType)
5707};
5708
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005709 static PyInt
5710BufMapLength(PyObject *self UNUSED)
5711{
5712 buf_T *b = firstbuf;
5713 PyInt n = 0;
5714
5715 while (b)
5716 {
5717 ++n;
5718 b = b->b_next;
5719 }
5720
5721 return n;
5722}
5723
5724 static PyObject *
5725BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5726{
5727 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005728 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005729
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005730 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005731 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005732
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005733 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005734
5735 if (b)
5736 return BufferNew(b);
5737 else
5738 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005739 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005740 return NULL;
5741 }
5742}
5743
5744 static void
5745BufMapIterDestruct(PyObject *buffer)
5746{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005747 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005748 if (buffer)
5749 {
5750 Py_DECREF(buffer);
5751 }
5752}
5753
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005754 static int
5755BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5756{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005757 if (buffer)
5758 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005759 return 0;
5760}
5761
5762 static int
5763BufMapIterClear(PyObject **buffer)
5764{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005765 if (*buffer)
5766 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005767 return 0;
5768}
5769
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005770 static PyObject *
5771BufMapIterNext(PyObject **buffer)
5772{
5773 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005774 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005775
5776 if (!*buffer)
5777 return NULL;
5778
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005779 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005780
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005781 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005782 {
5783 *buffer = NULL;
5784 return NULL;
5785 }
5786
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005787 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005788 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005789 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005790 return NULL;
5791 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005792 // Do not increment reference: we no longer hold it (decref), but whoever
5793 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005794 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005795}
5796
5797 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005798BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005799{
5800 PyObject *buffer;
5801
5802 buffer = BufferNew(firstbuf);
5803 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005804 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005805 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear,
5806 (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005807}
5808
5809static PyMappingMethods BufMapAsMapping = {
5810 (lenfunc) BufMapLength,
5811 (binaryfunc) BufMapItem,
5812 (objobjargproc) 0,
5813};
5814
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005815// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005816
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005817static char *CurrentAttrs[] = {
5818 "buffer", "window", "line", "range", "tabpage",
5819 NULL
5820};
5821
5822 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005823CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005824{
5825 return ObjectDir(self, CurrentAttrs);
5826}
5827
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005828 static PyObject *
5829CurrentGetattr(PyObject *self UNUSED, char *name)
5830{
5831 if (strcmp(name, "buffer") == 0)
5832 return (PyObject *)BufferNew(curbuf);
5833 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005834 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005835 else if (strcmp(name, "tabpage") == 0)
5836 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005837 else if (strcmp(name, "line") == 0)
5838 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5839 else if (strcmp(name, "range") == 0)
5840 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005841 else if (strcmp(name, "__members__") == 0)
5842 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005843 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005844#if PY_MAJOR_VERSION < 3
5845 return Py_FindMethod(WindowMethods, self, name);
5846#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005847 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005848#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005849}
5850
5851 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005852CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005853{
5854 if (strcmp(name, "line") == 0)
5855 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005856 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5857 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005858 return -1;
5859
5860 return 0;
5861 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005862 else if (strcmp(name, "buffer") == 0)
5863 {
5864 int count;
5865
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005866 if (valObject->ob_type != BufferTypePtr)
Bram Moolenaare7614592013-05-15 15:51:08 +02005867 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005868 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005869 N_("expected vim.Buffer object, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005870 valObject);
Bram Moolenaare7614592013-05-15 15:51:08 +02005871 return -1;
5872 }
5873
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005874 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005875 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005876 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005877
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005878 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005879 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5880 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005881 if (VimTryEnd())
5882 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005883 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005884 return -1;
5885 }
5886
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005887 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005888 }
5889 else if (strcmp(name, "window") == 0)
5890 {
5891 int count;
5892
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005893 if (valObject->ob_type != WindowTypePtr)
Bram Moolenaare7614592013-05-15 15:51:08 +02005894 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005895 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005896 N_("expected vim.Window object, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005897 valObject);
Bram Moolenaare7614592013-05-15 15:51:08 +02005898 return -1;
5899 }
5900
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005901 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005902 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005903 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005904
5905 if (!count)
5906 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005907 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005908 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005909 return -1;
5910 }
5911
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005912 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005913 win_goto(((WindowObject *)(valObject))->win);
5914 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005915 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005916 if (VimTryEnd())
5917 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005918 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005919 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005920 return -1;
5921 }
5922
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005923 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005924 }
5925 else if (strcmp(name, "tabpage") == 0)
5926 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005927 if (valObject->ob_type != TabPageTypePtr)
Bram Moolenaare7614592013-05-15 15:51:08 +02005928 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005929 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005930 N_("expected vim.TabPage object, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005931 valObject);
Bram Moolenaare7614592013-05-15 15:51:08 +02005932 return -1;
5933 }
5934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005935 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005936 return -1;
5937
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005938 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005939 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5940 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005941 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005942 if (VimTryEnd())
5943 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005944 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005945 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005946 return -1;
5947 }
5948
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005949 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005950 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005951 else
5952 {
5953 PyErr_SetString(PyExc_AttributeError, name);
5954 return -1;
5955 }
5956}
5957
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005958static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005959 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005960 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5961 { NULL, NULL, 0, NULL}
5962};
5963
Bram Moolenaardb913952012-06-29 12:54:53 +02005964 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005965init_range_cmd(exarg_T *eap)
5966{
5967 RangeStart = eap->line1;
5968 RangeEnd = eap->line2;
5969}
5970
5971 static void
5972init_range_eval(typval_T *rettv UNUSED)
5973{
5974 RangeStart = (PyInt) curwin->w_cursor.lnum;
5975 RangeEnd = RangeStart;
5976}
5977
5978 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005979run_cmd(const char *cmd, void *arg UNUSED
5980#ifdef PY_CAN_RECURSE
5981 , PyGILState_STATE *pygilstate UNUSED
5982#endif
5983 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005984{
Bram Moolenaar41009372013-07-01 22:03:04 +02005985 PyObject *run_ret;
5986 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5987 if (run_ret != NULL)
5988 {
5989 Py_DECREF(run_ret);
5990 }
5991 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5992 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005993 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02005994 PyErr_Clear();
5995 }
5996 else
5997 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005998}
5999
6000static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
6001static int code_hdr_len = 30;
6002
6003 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006004run_do(const char *cmd, void *arg UNUSED
6005#ifdef PY_CAN_RECURSE
6006 , PyGILState_STATE *pygilstate
6007#endif
6008 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006009{
6010 PyInt lnum;
6011 size_t len;
6012 char *code;
6013 int status;
6014 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02006015 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01006016 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006017
Bram Moolenaar4ac66762013-05-28 22:31:46 +02006018 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006020 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006021 return;
6022 }
6023
6024 len = code_hdr_len + STRLEN(cmd);
6025 code = PyMem_New(char, len + 1);
6026 memcpy(code, code_hdr, code_hdr_len);
6027 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02006028 run_ret = PyRun_String(code, Py_file_input, globals, globals);
6029 status = -1;
6030 if (run_ret != NULL)
6031 {
6032 status = 0;
6033 Py_DECREF(run_ret);
6034 }
6035 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
6036 {
6037 PyMem_Free(code);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006038 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02006039 PyErr_Clear();
6040 return;
6041 }
6042 else
6043 PyErr_PrintEx(1);
6044
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006045 PyMem_Free(code);
6046
6047 if (status)
6048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006049 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006050 return;
6051 }
6052
6053 status = 0;
6054 pymain = PyImport_AddModule("__main__");
6055 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006056#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006057 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006058#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006059
6060 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
6061 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006062 PyObject *line;
6063 PyObject *linenr;
6064 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006065
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006066#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006067 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006068#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006069 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01006070 if (lnum > curbuf->b_ml.ml_line_count
6071 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006072 goto err;
6073 if (!(linenr = PyInt_FromLong((long) lnum)))
6074 {
6075 Py_DECREF(line);
6076 goto err;
6077 }
6078 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
6079 Py_DECREF(line);
6080 Py_DECREF(linenr);
6081 if (!ret)
6082 goto err;
6083
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006084 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01006085 if (curbuf != was_curbuf)
6086 {
6087 Py_XDECREF(ret);
6088 goto err;
6089 }
6090
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006091 if (ret != Py_None)
6092 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01006093 {
6094 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006095 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01006096 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006097
6098 Py_XDECREF(ret);
6099 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006100#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006101 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006102#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006103 }
6104 goto out;
6105err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006106#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006107 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006108#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006109 PyErr_PrintEx(0);
6110 PythonIO_Flush();
6111 status = 1;
6112out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006113#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006114 if (!status)
6115 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006116#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006117 Py_DECREF(pyfunc);
6118 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
6119 if (status)
6120 return;
6121 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006122 update_curbuf(UPD_NOT_VALID);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006123}
6124
6125 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006126run_eval(const char *cmd, typval_T *rettv
6127#ifdef PY_CAN_RECURSE
6128 , PyGILState_STATE *pygilstate UNUSED
6129#endif
6130 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006131{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006132 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006133
Bram Moolenaar41009372013-07-01 22:03:04 +02006134 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006135 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006136 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02006137 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02006138 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006139 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02006140 PyErr_Clear();
6141 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02006142 else
6143 {
6144 if (PyErr_Occurred() && !msg_silent)
6145 PyErr_PrintEx(0);
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00006146 emsg(_(e_eval_did_not_return_valid_python_object));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02006147 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006148 }
6149 else
6150 {
Bram Moolenaarde323092017-11-09 19:56:08 +01006151 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00006152 emsg(_(e_failed_to_convert_returned_python_object_to_vim_value));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006153 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006154 }
6155 PyErr_Clear();
6156}
6157
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006158 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006159set_ref_in_py(const int copyID)
6160{
6161 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006162 list_T *ll;
6163 int i;
6164 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006165 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02006166
6167 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006168 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006169 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006170 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
6171 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006172 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006173
6174 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006175 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006176 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02006177 {
6178 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006179 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006180 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006181 }
6182
Bram Moolenaar8110a092016-04-14 15:56:09 +02006183 if (lastfunc != NULL)
6184 {
6185 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
6186 {
6187 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006188 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006189 if (func->argc)
6190 for (i = 0; !abort && i < func->argc; ++i)
6191 abort = abort
6192 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
6193 }
6194 }
6195
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006196 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02006197}
6198
6199 static int
6200set_string_copy(char_u *str, typval_T *tv)
6201{
6202 tv->vval.v_string = vim_strsave(str);
6203 if (tv->vval.v_string == NULL)
6204 {
6205 PyErr_NoMemory();
6206 return -1;
6207 }
6208 return 0;
6209}
6210
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006211 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006212pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006213{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006214 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006215 char_u *key;
6216 dictitem_T *di;
6217 PyObject *keyObject;
6218 PyObject *valObject;
6219 Py_ssize_t iter = 0;
6220
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006221 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006222 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006223
6224 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006225 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006226
6227 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
6228 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006229 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006230
Bram Moolenaara03e6312013-05-29 22:49:26 +02006231 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006232 {
6233 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006234 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006235 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006236
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006237 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006238 {
6239 dict_unref(dict);
6240 return -1;
6241 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006242
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006243 if (*key == NUL)
6244 {
6245 dict_unref(dict);
6246 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006247 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006248 return -1;
6249 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006250
6251 di = dictitem_alloc(key);
6252
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006253 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006254
6255 if (di == NULL)
6256 {
6257 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006258 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006259 return -1;
6260 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006261
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006262 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006263 {
6264 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006265 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006266 return -1;
6267 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006268
6269 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006270 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006271 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02006272 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006273 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006274 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006275 return -1;
6276 }
6277 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006278
6279 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006280 return 0;
6281}
6282
6283 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006284pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006285{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006286 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006287 char_u *key;
6288 dictitem_T *di;
6289 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006290 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006291 PyObject *keyObject;
6292 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006293
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006294 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006295 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006296
6297 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006298 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006299
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006300 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006301 {
6302 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006303 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006304 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006305
6306 if (!(iterator = PyObject_GetIter(list)))
6307 {
6308 dict_unref(dict);
6309 Py_DECREF(list);
6310 return -1;
6311 }
6312 Py_DECREF(list);
6313
6314 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006315 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006316 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006317
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006318 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006319 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006320 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006321 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006322 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006323 return -1;
6324 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006325
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006326 if (*key == NUL)
6327 {
6328 Py_DECREF(keyObject);
6329 Py_DECREF(iterator);
6330 Py_XDECREF(todecref);
6331 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006332 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006333 return -1;
6334 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006335
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006336 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006337 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006338 Py_DECREF(keyObject);
6339 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006340 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006341 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006342 return -1;
6343 }
6344
6345 di = dictitem_alloc(key);
6346
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006347 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006348 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006349
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006350 if (di == NULL)
6351 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006352 Py_DECREF(iterator);
6353 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006354 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006355 PyErr_NoMemory();
6356 return -1;
6357 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006358
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006359 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006360 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006361 Py_DECREF(iterator);
6362 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006363 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006364 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006365 return -1;
6366 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006367
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006368 Py_DECREF(valObject);
6369
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006370 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006371 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006372 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006373 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006374 dictitem_free(di);
6375 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006376 return -1;
6377 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006378 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006379 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006380 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006381 return 0;
6382}
6383
6384 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006385pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006386{
6387 list_T *l;
6388
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006389 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006390 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006391
6392 tv->v_type = VAR_LIST;
6393 tv->vval.v_list = l;
6394
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006395 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006396 {
6397 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006398 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006399 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006400
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006401 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006402 return 0;
6403}
6404
Bram Moolenaardb913952012-06-29 12:54:53 +02006405typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6406
6407 static int
6408convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006409 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006410{
6411 PyObject *capsule;
6412 char hexBuf[sizeof(void *) * 2 + 3];
6413
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006414 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006415
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006416#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006417 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006418#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006419 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006420#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006421 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006422 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006423#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006424 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006425#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006426 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006427#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006428 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6429 {
6430 Py_DECREF(capsule);
6431 tv->v_type = VAR_UNKNOWN;
6432 return -1;
6433 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006434
6435 Py_DECREF(capsule);
6436
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006437 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006438 {
6439 tv->v_type = VAR_UNKNOWN;
6440 return -1;
6441 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006442 // As we are not using copy_tv which increments reference count we must
6443 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006444 if (tv->v_type == VAR_DICT)
6445 ++tv->vval.v_dict->dv_refcount;
6446 else if (tv->v_type == VAR_LIST)
6447 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006448 }
6449 else
6450 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006451 typval_T *v;
6452
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006453#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006454 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006455#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006456 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006457#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006458 copy_tv(v, tv);
6459 }
6460 return 0;
6461}
6462
6463 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006464ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6465{
6466 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006467 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006468
6469 if (!(lookup_dict = PyDict_New()))
6470 return -1;
6471
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006472 if (PyType_IsSubtype(obj->ob_type, DictionaryTypePtr))
Bram Moolenaara9922d62013-05-30 13:01:18 +02006473 {
6474 tv->v_type = VAR_DICT;
6475 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6476 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006477 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006478 }
6479 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006480 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006481 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006482 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006483 else
6484 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006485 PyErr_FORMAT_TYPE(
Bram Moolenaar86181df2020-05-11 23:14:04 +02006486 N_("unable to convert %s to a Vim dictionary"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006487 obj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006488 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006489 }
6490 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006491 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006492}
6493
6494 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006495ConvertFromPySequence(PyObject *obj, typval_T *tv)
6496{
6497 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006498 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006499
6500 if (!(lookup_dict = PyDict_New()))
6501 return -1;
6502
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006503 if (PyType_IsSubtype(obj->ob_type, ListTypePtr))
Bram Moolenaar8110a092016-04-14 15:56:09 +02006504 {
6505 tv->v_type = VAR_LIST;
6506 tv->vval.v_list = (((ListObject *)(obj))->list);
6507 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006508 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006509 }
6510 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006511 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006512 else
6513 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006514 PyErr_FORMAT_TYPE(
Bram Moolenaar86181df2020-05-11 23:14:04 +02006515 N_("unable to convert %s to a Vim list"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006516 obj);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006517 ret = -1;
6518 }
6519 Py_DECREF(lookup_dict);
6520 return ret;
6521}
6522
6523 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006524ConvertFromPyObject(PyObject *obj, typval_T *tv)
6525{
6526 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006527 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006528
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006529 if (!(lookup_dict = PyDict_New()))
6530 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006531 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006532 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006533 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006534}
6535
6536 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006537_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006538{
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006539 if (PyType_IsSubtype(obj->ob_type, DictionaryTypePtr))
Bram Moolenaardb913952012-06-29 12:54:53 +02006540 {
6541 tv->v_type = VAR_DICT;
6542 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6543 ++tv->vval.v_dict->dv_refcount;
6544 }
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006545 else if (PyType_IsSubtype(obj->ob_type, ListTypePtr))
Bram Moolenaardb913952012-06-29 12:54:53 +02006546 {
6547 tv->v_type = VAR_LIST;
6548 tv->vval.v_list = (((ListObject *)(obj))->list);
6549 ++tv->vval.v_list->lv_refcount;
6550 }
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006551 else if (PyType_IsSubtype(obj->ob_type, FunctionTypePtr))
Bram Moolenaardb913952012-06-29 12:54:53 +02006552 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006553 FunctionObject *func = (FunctionObject *) obj;
6554 if (func->self != NULL || func->argv != NULL)
6555 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006556 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6557
Bram Moolenaar8110a092016-04-14 15:56:09 +02006558 set_partial(func, pt, TRUE);
6559 tv->vval.v_partial = pt;
6560 tv->v_type = VAR_PARTIAL;
6561 }
6562 else
6563 {
6564 if (set_string_copy(func->name, tv) == -1)
6565 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006566
Bram Moolenaar8110a092016-04-14 15:56:09 +02006567 tv->v_type = VAR_FUNC;
6568 }
6569 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006570 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 else if (PyBytes_Check(obj))
6572 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006573 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006574
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006575 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006576 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006577 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006578 return -1;
6579
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006580 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006581 return -1;
6582
6583 tv->v_type = VAR_STRING;
6584 }
6585 else if (PyUnicode_Check(obj))
6586 {
6587 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006588 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006589
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006590 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006591 if (bytes == NULL)
6592 return -1;
6593
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006594 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006595 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006596 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006597 return -1;
6598
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006599 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006600 {
6601 Py_XDECREF(bytes);
6602 return -1;
6603 }
6604 Py_XDECREF(bytes);
6605
6606 tv->v_type = VAR_STRING;
6607 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006608#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006609 else if (PyInt_Check(obj))
6610 {
6611 tv->v_type = VAR_NUMBER;
6612 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006613 if (PyErr_Occurred())
6614 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006615 }
6616#endif
6617 else if (PyLong_Check(obj))
6618 {
6619 tv->v_type = VAR_NUMBER;
6620 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006621 if (PyErr_Occurred())
6622 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006623 }
6624 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006625 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006626 else if (PyFloat_Check(obj))
6627 {
6628 tv->v_type = VAR_FLOAT;
6629 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6630 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006631 else if (PyObject_HasAttrString(obj, "keys"))
6632 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006633 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006634 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006635 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006636 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006637 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006638 else if (PyNumber_Check(obj))
6639 {
6640 PyObject *num;
6641
6642 if (!(num = PyNumber_Long(obj)))
6643 return -1;
6644
6645 tv->v_type = VAR_NUMBER;
6646 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6647
6648 Py_DECREF(num);
6649 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006650 else if (obj == Py_None)
6651 {
6652 tv->v_type = VAR_SPECIAL;
6653 tv->vval.v_number = VVAL_NONE;
6654 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006655 else
6656 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006657 PyErr_FORMAT_TYPE(
Bram Moolenaar86181df2020-05-11 23:14:04 +02006658 N_("unable to convert %s to a Vim structure"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006659 obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006660 return -1;
6661 }
6662 return 0;
6663}
6664
6665 static PyObject *
6666ConvertToPyObject(typval_T *tv)
6667{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006668 typval_T *argv;
6669 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006670 if (tv == NULL)
6671 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006672 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006673 return NULL;
6674 }
6675 switch (tv->v_type)
6676 {
6677 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006678 return PyBytes_FromString(tv->vval.v_string == NULL
6679 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006680 case VAR_NUMBER:
6681 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006682 case VAR_FLOAT:
6683 return PyFloat_FromDouble((double) tv->vval.v_float);
Bram Moolenaardb913952012-06-29 12:54:53 +02006684 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006685 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006686 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006687 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006688 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006689 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006690 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006691 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006692 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006693 if (tv->vval.v_partial->pt_argc)
6694 {
6695 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6696 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6697 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6698 }
6699 else
6700 argv = NULL;
6701 if (tv->vval.v_partial->pt_dict != NULL)
6702 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006703 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006704 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006705 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006706 tv->vval.v_partial->pt_dict,
6707 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006708 case VAR_BLOB:
6709 return PyBytes_FromStringAndSize(
6710 (char*) tv->vval.v_blob->bv_ga.ga_data,
6711 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006712 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006713 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006715 case VAR_CHANNEL:
6716 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006717 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006718 case VAR_CLASS:
6719 case VAR_OBJECT:
Bram Moolenaardb913952012-06-29 12:54:53 +02006720 Py_INCREF(Py_None);
6721 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006722 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006723 case VAR_SPECIAL:
6724 switch (tv->vval.v_number)
6725 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006726 case VVAL_FALSE: return ALWAYS_FALSE;
6727 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006728 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006729 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006730 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006731 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006732 return NULL;
6733 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006734 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006735}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006736
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006737DEFINE_PY_TYPE_OBJECT(CurrentType);
6738
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006739typedef struct
6740{
6741 PyObject_HEAD
6742} CurrentObject;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006743
6744static CurrentObject TheCurrent =
6745{
6746 PyObject_HEAD_INIT_TYPE(CurrentType)
6747};
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006748
6749 static void
6750init_structs(void)
6751{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006752 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006753 OutputType.tp_name = "vim.message";
6754 OutputType.tp_basicsize = sizeof(OutputObject);
6755 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6756 OutputType.tp_doc = "vim message object";
6757 OutputType.tp_methods = OutputMethods;
6758#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006759 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6760 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006761 OutputType.tp_alloc = call_PyType_GenericAlloc;
6762 OutputType.tp_new = call_PyType_GenericNew;
6763 OutputType.tp_free = call_PyObject_Free;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006764# ifndef USE_LIMITED_API
6765 // The std printer type is only exposed in full API. It is not essential
6766 // anyway and so in limited API we don't set it.
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006767 OutputType.tp_base = &PyStdPrinter_Type;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006768# endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006769#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006770 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6771 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006772 // Disabled, because this causes a crash in test86
6773 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006774#endif
6775
Bram Moolenaara80faa82020-04-12 19:37:17 +02006776 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006777 IterType.tp_name = "vim.iter";
6778 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006779 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006780 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006781 IterType.tp_iter = (getiterfunc)IterIter;
6782 IterType.tp_iternext = (iternextfunc)IterNext;
6783 IterType.tp_dealloc = (destructor)IterDestructor;
6784 IterType.tp_traverse = (traverseproc)IterTraverse;
6785 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006786
Bram Moolenaara80faa82020-04-12 19:37:17 +02006787 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006788 BufferType.tp_name = "vim.buffer";
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006789 BufferType.tp_basicsize = sizeof(BufferObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006790 BufferType.tp_dealloc = (destructor)BufferDestructor;
6791 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006792 BufferType.tp_as_sequence = &BufferAsSeq;
6793 BufferType.tp_as_mapping = &BufferAsMapping;
6794 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6795 BufferType.tp_doc = "vim buffer object";
6796 BufferType.tp_methods = BufferMethods;
6797#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006798 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006799 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006800 BufferType.tp_alloc = call_PyType_GenericAlloc;
6801 BufferType.tp_new = call_PyType_GenericNew;
6802 BufferType.tp_free = call_PyObject_Free;
6803#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006804 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006805 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006806#endif
6807
Bram Moolenaara80faa82020-04-12 19:37:17 +02006808 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006809 WindowType.tp_name = "vim.window";
6810 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006811 WindowType.tp_dealloc = (destructor)WindowDestructor;
6812 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006813 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006814 WindowType.tp_doc = "vim Window object";
6815 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006816 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6817 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006818#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006819 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6820 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006821 WindowType.tp_alloc = call_PyType_GenericAlloc;
6822 WindowType.tp_new = call_PyType_GenericNew;
6823 WindowType.tp_free = call_PyObject_Free;
6824#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006825 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6826 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006827#endif
6828
Bram Moolenaara80faa82020-04-12 19:37:17 +02006829 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006830 TabPageType.tp_name = "vim.tabpage";
6831 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006832 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6833 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006834 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6835 TabPageType.tp_doc = "vim tab page object";
6836 TabPageType.tp_methods = TabPageMethods;
6837#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006838 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006839 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6840 TabPageType.tp_new = call_PyType_GenericNew;
6841 TabPageType.tp_free = call_PyObject_Free;
6842#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006843 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006844#endif
6845
Bram Moolenaara80faa82020-04-12 19:37:17 +02006846 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006847 BufMapType.tp_name = "vim.bufferlist";
6848 BufMapType.tp_basicsize = sizeof(BufMapObject);
6849 BufMapType.tp_as_mapping = &BufMapAsMapping;
6850 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006851 BufMapType.tp_iter = BufMapIter;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006852 BufMapType.tp_doc = "vim buffer list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006853
Bram Moolenaara80faa82020-04-12 19:37:17 +02006854 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006855 WinListType.tp_name = "vim.windowlist";
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006856 WinListType.tp_basicsize = sizeof(WinListObject);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006857 WinListType.tp_as_sequence = &WinListAsSeq;
6858 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6859 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006860 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006861
Bram Moolenaara80faa82020-04-12 19:37:17 +02006862 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006863 TabListType.tp_name = "vim.tabpagelist";
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006864 TabListType.tp_basicsize = sizeof(TabListObject);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006865 TabListType.tp_as_sequence = &TabListAsSeq;
6866 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6867 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006868
Bram Moolenaara80faa82020-04-12 19:37:17 +02006869 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006870 RangeType.tp_name = "vim.range";
6871 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006872 RangeType.tp_dealloc = (destructor)RangeDestructor;
6873 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006874 RangeType.tp_as_sequence = &RangeAsSeq;
6875 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006876 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006877 RangeType.tp_doc = "vim Range object";
6878 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006879 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6880 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006881#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006882 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006883 RangeType.tp_alloc = call_PyType_GenericAlloc;
6884 RangeType.tp_new = call_PyType_GenericNew;
6885 RangeType.tp_free = call_PyObject_Free;
6886#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006887 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006888#endif
6889
Bram Moolenaara80faa82020-04-12 19:37:17 +02006890 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006891 CurrentType.tp_name = "vim.currentdata";
6892 CurrentType.tp_basicsize = sizeof(CurrentObject);
6893 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6894 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006895 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006896#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006897 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6898 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006899#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006900 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6901 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006902#endif
6903
Bram Moolenaara80faa82020-04-12 19:37:17 +02006904 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006905 DictionaryType.tp_name = "vim.dictionary";
6906 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006907 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006908 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006909 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006910 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006911 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006912 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006913 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6914 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6915 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006916#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006917 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6918 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006919#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006920 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6921 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006922#endif
6923
Bram Moolenaara80faa82020-04-12 19:37:17 +02006924 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006925 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006926 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006927 ListType.tp_basicsize = sizeof(ListObject);
6928 ListType.tp_as_sequence = &ListAsSeq;
6929 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006930 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006931 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006932 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006933 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006934 ListType.tp_new = (newfunc)ListConstructor;
6935 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006936#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006937 ListType.tp_getattro = (getattrofunc)ListGetattro;
6938 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006939#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006940 ListType.tp_getattr = (getattrfunc)ListGetattr;
6941 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006942#endif
6943
Bram Moolenaara80faa82020-04-12 19:37:17 +02006944 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006945 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006946 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006947 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6948 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006949 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006950 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006951 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006952 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006953 FunctionType.tp_new = (newfunc)FunctionConstructor;
6954 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006955#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006956 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006957#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006958 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006959#endif
6960
Bram Moolenaara80faa82020-04-12 19:37:17 +02006961 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006962 OptionsType.tp_name = "vim.options";
6963 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006964 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006965 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006966 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006967 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006968 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006969 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6970 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6971 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006972
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006973#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02006974 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006975 LoaderType.tp_name = "vim.Loader";
6976 LoaderType.tp_basicsize = sizeof(LoaderObject);
6977 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6978 LoaderType.tp_doc = "vim message object";
6979 LoaderType.tp_methods = LoaderMethods;
6980 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006981#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006982
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006983#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02006984 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006985 vimmodule.m_name = "vim";
6986 vimmodule.m_doc = "Vim Python interface\n";
6987 vimmodule.m_size = -1;
6988 vimmodule.m_methods = VimMethods;
6989#endif
6990}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006991
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006992 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006993init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006994{
6995 PYTYPE_READY(IterType);
6996 PYTYPE_READY(BufferType);
6997 PYTYPE_READY(RangeType);
6998 PYTYPE_READY(WindowType);
6999 PYTYPE_READY(TabPageType);
7000 PYTYPE_READY(BufMapType);
7001 PYTYPE_READY(WinListType);
7002 PYTYPE_READY(TabListType);
7003 PYTYPE_READY(CurrentType);
7004 PYTYPE_READY(DictionaryType);
7005 PYTYPE_READY(ListType);
7006 PYTYPE_READY(FunctionType);
7007 PYTYPE_READY(OptionsType);
7008 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007009#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02007010 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007011#endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007012
7013#ifdef USE_LIMITED_API
7014 // We need to finish initializing all the static objects because the types
7015 // are only just allocated on the heap now.
7016 // Each PyObject_HEAD_INIT_TYPE should correspond to a
7017 // PyObject_FINISH_INIT_TYPE below.
7018 PyObject_FINISH_INIT_TYPE(Output, OutputType);
7019 PyObject_FINISH_INIT_TYPE(Error, OutputType);
7020 PyObject_FINISH_INIT_TYPE(TheBufferMap, BufMapType);
7021 PyObject_FINISH_INIT_TYPE(TheWindowList, WinListType);
7022 PyObject_FINISH_INIT_TYPE(TheCurrent, CurrentType);
7023 PyObject_FINISH_INIT_TYPE(TheTabPageList, TabListType);
7024#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007025 return 0;
7026}
7027
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007028#ifdef USE_LIMITED_API
7029 static void
7030shutdown_types(void)
7031{
7032 PYTYPE_CLEANUP(IterType);
7033 PYTYPE_CLEANUP(BufferType);
7034 PYTYPE_CLEANUP(RangeType);
7035 PYTYPE_CLEANUP(WindowType);
7036 PYTYPE_CLEANUP(TabPageType);
7037 PYTYPE_CLEANUP(BufMapType);
7038 PYTYPE_CLEANUP(WinListType);
7039 PYTYPE_CLEANUP(TabListType);
7040 PYTYPE_CLEANUP(CurrentType);
7041 PYTYPE_CLEANUP(DictionaryType);
7042 PYTYPE_CLEANUP(ListType);
7043 PYTYPE_CLEANUP(FunctionType);
7044 PYTYPE_CLEANUP(OptionsType);
7045 PYTYPE_CLEANUP(OutputType);
7046# if PY_VERSION_HEX < 0x030700f0
7047 PYTYPE_CLEANUP(LoaderType);
7048# endif
7049}
7050#endif
7051
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007052 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02007053init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007054{
7055 PyObject *path;
7056 PyObject *path_hook;
7057 PyObject *path_hooks;
7058
7059 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
7060 return -1;
7061
7062 if (!(path_hooks = PySys_GetObject("path_hooks")))
7063 {
7064 PyErr_Clear();
7065 path_hooks = PyList_New(1);
7066 PyList_SET_ITEM(path_hooks, 0, path_hook);
7067 if (PySys_SetObject("path_hooks", path_hooks))
7068 {
7069 Py_DECREF(path_hooks);
7070 return -1;
7071 }
7072 Py_DECREF(path_hooks);
7073 }
7074 else if (PyList_Check(path_hooks))
7075 {
7076 if (PyList_Append(path_hooks, path_hook))
7077 {
7078 Py_DECREF(path_hook);
7079 return -1;
7080 }
7081 Py_DECREF(path_hook);
7082 }
7083 else
7084 {
7085 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007086 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007087 "You should now do the following:\n"
7088 "- append vim.path_hook to sys.path_hooks\n"
7089 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01007090 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007091 Py_DECREF(path_hook);
7092 return 0;
7093 }
7094
7095 if (!(path = PySys_GetObject("path")))
7096 {
7097 PyErr_Clear();
7098 path = PyList_New(1);
7099 Py_INCREF(vim_special_path_object);
7100 PyList_SET_ITEM(path, 0, vim_special_path_object);
7101 if (PySys_SetObject("path", path))
7102 {
7103 Py_DECREF(path);
7104 return -1;
7105 }
7106 Py_DECREF(path);
7107 }
7108 else if (PyList_Check(path))
7109 {
7110 if (PyList_Append(path, vim_special_path_object))
7111 return -1;
7112 }
7113 else
7114 {
7115 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007116 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007117 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01007118 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007119 }
7120
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007121 return 0;
7122}
7123
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007124static struct numeric_constant {
7125 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007126 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007127} numeric_constants[] = {
7128 {"VAR_LOCKED", VAR_LOCKED},
7129 {"VAR_FIXED", VAR_FIXED},
7130 {"VAR_SCOPE", VAR_SCOPE},
7131 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
7132};
7133
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007134struct object_constant {
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007135 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007136 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007137};
7138
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007139#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02007140 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007141 return -1;
7142
7143#define ADD_CHECKED_OBJECT(m, name, obj) \
7144 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007145 PyObject *valObject = obj; \
7146 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007147 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007148 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007149 }
7150
7151 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02007152populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007153{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007154 int i;
7155 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02007156 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02007157 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007158#if PY_VERSION_HEX >= 0x030700f0
7159 PyObject *dict;
7160 PyObject *cls;
7161#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007162
7163 for (i = 0; i < (int)(sizeof(numeric_constants)
7164 / sizeof(struct numeric_constant));
7165 ++i)
7166 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007167 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007168
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007169 struct object_constant object_constants[] = {
7170 {"buffers", (PyObject *)(void *)&TheBufferMap},
7171 {"windows", (PyObject *)(void *)&TheWindowList},
7172 {"tabpages", (PyObject *)(void *)&TheTabPageList},
7173 {"current", (PyObject *)(void *)&TheCurrent},
7174
7175 {"Buffer", (PyObject *)BufferTypePtr},
7176 {"Range", (PyObject *)RangeTypePtr},
7177 {"Window", (PyObject *)WindowTypePtr},
7178 {"TabPage", (PyObject *)TabPageTypePtr},
7179 {"Dictionary", (PyObject *)DictionaryTypePtr},
7180 {"List", (PyObject *)ListTypePtr},
7181 {"Function", (PyObject *)FunctionTypePtr},
7182 {"Options", (PyObject *)OptionsTypePtr},
7183#if PY_VERSION_HEX < 0x030700f0
7184 {"_Loader", (PyObject *)LoaderTypePtr},
7185#endif
7186 };
7187
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007188 for (i = 0; i < (int)(sizeof(object_constants)
7189 / sizeof(struct object_constant));
7190 ++i)
7191 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007192 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007193
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007194 valObject = object_constants[i].valObject;
7195 Py_INCREF(valObject);
7196 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007197 }
7198
7199 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
7200 return -1;
7201 ADD_OBJECT(m, "error", VimError);
7202
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007203 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
7204 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007205 ADD_CHECKED_OBJECT(m, "options",
7206 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02007207
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007208 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007209 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007210 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007211
Bram Moolenaar22081f42016-06-01 20:38:34 +02007212#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007213 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007214 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02007215#else
7216 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
7217 return -1;
7218#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02007219 ADD_OBJECT(m, "_getcwd", py_getcwd)
7220
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007221 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007222 return -1;
7223 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02007224 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007225 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02007226 if (PyObject_SetAttrString(other_module, "chdir", attr))
7227 {
7228 Py_DECREF(attr);
7229 return -1;
7230 }
7231 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007232
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007233 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007234 {
7235 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02007236 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007237 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02007238 if (PyObject_SetAttrString(other_module, "fchdir", attr))
7239 {
7240 Py_DECREF(attr);
7241 return -1;
7242 }
7243 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007244 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02007245 else
7246 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02007247
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007248 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
7249 return -1;
7250
7251 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
7252
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007253#if PY_VERSION_HEX >= 0x030700f0
7254 if (!(imp = PyImport_ImportModule("importlib.machinery")))
7255 return -1;
7256
7257 dict = PyModule_GetDict(imp);
7258
7259 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
7260 {
7261 Py_DECREF(imp);
7262 return -1;
7263 }
7264
7265 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
7266 {
7267 Py_DECREF(imp);
7268 return -1;
7269 }
7270
Bram Moolenaarb999ba22019-02-14 13:28:45 +01007271 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
7272 {
7273 // find_module() is deprecated, this may stop working in some later
7274 // version.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01007275 ADD_OBJECT(m, "_find_module", py_find_module);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01007276 }
7277
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007278 Py_DECREF(imp);
7279
7280 ADD_OBJECT(m, "_find_spec", py_find_spec);
7281#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02007282 if (!(imp = PyImport_ImportModule("imp")))
7283 return -1;
7284
7285 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
7286 {
7287 Py_DECREF(imp);
7288 return -1;
7289 }
7290
7291 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
7292 {
7293 Py_DECREF(py_find_module);
7294 Py_DECREF(imp);
7295 return -1;
7296 }
7297
7298 Py_DECREF(imp);
7299
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02007300 ADD_OBJECT(m, "_find_module", py_find_module);
7301 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007302#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007303
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007304 return 0;
7305}