blob: 4a32124a1ee801322f27c1abf55890db8c881ac5 [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
Yee Cheng Chin02c51b12023-09-21 16:40:12 +0200605OutputSetattr(PyObject *self_obj, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200606{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +0200607 OutputObject *self = (OutputObject*)self_obj;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200608 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200609 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200610 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200611 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200612 return -1;
613 }
614
615 if (strcmp(name, "softspace") == 0)
616 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200617 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200618 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200619 return 0;
620 }
621
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200622 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200623 return -1;
624}
625
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100626// Buffer IO, we write one whole line at a time.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200627static garray_T io_ga = {0, 0, 1, 80, NULL};
628static writefn old_fn = NULL;
629
630 static void
631PythonIO_Flush(void)
632{
633 if (old_fn != NULL && io_ga.ga_len > 0)
634 {
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200635 ((char *)io_ga.ga_data)[io_ga.ga_len] = NUL;
636 old_fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200637 }
638 io_ga.ga_len = 0;
639}
640
641 static void
642writer(writefn fn, char_u *str, PyInt n)
643{
644 char_u *ptr;
645
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100646 // Flush when switching output function.
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200647 if (fn != old_fn)
648 PythonIO_Flush();
649 old_fn = fn;
650
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200651 // Write each NL separated line. Text after the last NL is kept for
652 // writing later.
653 // For normal messages: Do not output when "got_int" was set. This avoids
654 // a loop gone crazy flooding the terminal with messages. Also for when
655 // "q" is pressed at the more-prompt.
656 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL
657 && (fn == (writefn)emsg || !got_int))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200658 {
659 PyInt len = ptr - str;
660
661 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
662 break;
663
664 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
665 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
Bram Moolenaarefc0d942020-10-11 18:05:02 +0200666 fn((char *)io_ga.ga_data);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200667 str = ptr + 1;
668 n -= len + 1;
669 io_ga.ga_len = 0;
670 }
671
Bram Moolenaarb98678a2019-10-19 15:18:44 +0200672 // Put the remaining text into io_ga for later printing.
673 if (n > 0 && (fn == (writefn)emsg || !got_int)
674 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200675 {
676 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
677 io_ga.ga_len += (int)n;
678 }
679}
680
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200681 static int
682write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200683{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200684 Py_ssize_t len = 0;
685 char *str = NULL;
686 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200687
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200688 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
689 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200690
691 Py_BEGIN_ALLOW_THREADS
692 Python_Lock_Vim();
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200693 if (error)
694 emsg_severe = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100695 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200696 Python_Release_Vim();
697 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200698 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200699
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200700 return 0;
701}
702
703 static PyObject *
704OutputWrite(OutputObject *self, PyObject *string)
705{
706 if (write_output(self, string))
707 return NULL;
708
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200709 Py_INCREF(Py_None);
710 return Py_None;
711}
712
713 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200714OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200715{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200716 PyObject *iterator;
717 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200718
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200719 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200720 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200721
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200722 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200723 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200724 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200725 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200726 Py_DECREF(iterator);
727 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200728 return NULL;
729 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200730 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200731 }
732
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200733 Py_DECREF(iterator);
734
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100735 // Iterator may have finished due to an exception
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200736 if (PyErr_Occurred())
737 return NULL;
738
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200739 Py_INCREF(Py_None);
740 return Py_None;
741}
742
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100743 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200744AlwaysNone(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100745{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100746 // do nothing
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100747 Py_INCREF(Py_None);
748 return Py_None;
749}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200750#define ALWAYS_NONE AlwaysNone(NULL, NULL)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100751
Bram Moolenaard4247472015-11-02 13:28:59 +0100752 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200753AlwaysFalse(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100754{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100755 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100756 PyObject *ret = Py_False;
757 Py_INCREF(ret);
758 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100759}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200760#define ALWAYS_FALSE AlwaysFalse(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100761
762 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200763AlwaysTrue(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaard4247472015-11-02 13:28:59 +0100764{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100765 // do nothing
Bram Moolenaare7427f42015-11-10 13:24:20 +0100766 PyObject *ret = Py_True;
767 Py_INCREF(ret);
768 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100769}
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +0200770#define ALWAYS_TRUE AlwaysTrue(NULL, NULL)
Bram Moolenaard4247472015-11-02 13:28:59 +0100771
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200772/***************/
773
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200774static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100775 // name, function, calling, doc
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200776 {"write", (PyCFunction)OutputWrite, METH_O, ""},
777 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100778 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
779 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
780 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
781 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
782 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
783 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200784 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200785 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200786 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200787};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200788
789static OutputObject Output =
790{
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200791 PyObject_HEAD_INIT_TYPE(OutputType)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200792 0,
793 0
794};
795
796static OutputObject Error =
797{
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200798 PyObject_HEAD_INIT_TYPE(OutputType)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200799 0,
800 1
801};
802
803 static int
804PythonIO_Init_io(void)
805{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200806 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
807 return -1;
808 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
809 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200810
811 if (PyErr_Occurred())
812 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000813 emsg(_(e_python_error_initialising_io_object));
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200814 return -1;
815 }
816
817 return 0;
818}
819
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200820#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200821static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
822
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200823typedef struct
824{
825 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200826 char *fullname;
827 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200828} LoaderObject;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200829DEFINE_PY_TYPE_OBJECT(LoaderType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200830
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200831 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +0200832LoaderDestructor(PyObject *self_obj)
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200833{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +0200834 LoaderObject *self = (LoaderObject*)self_obj;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200835 vim_free(self->fullname);
836 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200837 DESTRUCTOR_FINISH(self);
838}
839
840 static PyObject *
841LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
842{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200843 char *fullname = self->fullname;
844 PyObject *result = self->result;
845 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200846
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200847 if (!fullname)
848 {
849 module = result ? result : Py_None;
850 Py_INCREF(module);
851 return module;
852 }
853
854 module = call_load_module(fullname, (int)STRLEN(fullname), result);
855
856 self->fullname = NULL;
857 self->result = module;
858
859 vim_free(fullname);
860 Py_DECREF(result);
861
862 if (!module)
863 {
864 if (PyErr_Occurred())
865 return NULL;
866
867 Py_INCREF(Py_None);
868 return Py_None;
869 }
870
871 Py_INCREF(module);
872 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200873}
874
875static struct PyMethodDef LoaderMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100876 // name, function, calling, doc
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200877 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
878 { NULL, NULL, 0, NULL}
879};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200880#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200881
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100882/*
883 * Check to see whether a Vim error has been reported, or a keyboard
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200884 * interrupt has been detected.
885 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200886 static void
887VimTryStart(void)
888{
889 ++trylevel;
890}
891
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200892 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200893VimTryEnd(void)
894{
895 --trylevel;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100896 // Without this it stops processing all subsequent Vim script commands and
897 // generates strange error messages if I e.g. try calling Test() in a cycle
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200898 did_emsg = FALSE;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100899 // Keyboard interrupt should be preferred over anything else
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200900 if (got_int)
901 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100902 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100903 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100904 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200905 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200906 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200907 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100908 else if (msg_list != NULL && *msg_list != NULL)
909 {
910 int should_free;
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100911 char *msg;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100912
913 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
914
915 if (msg == NULL)
916 {
917 PyErr_NoMemory();
918 return -1;
919 }
920
Bram Moolenaarb1443b42019-01-13 23:51:14 +0100921 PyErr_SetVim(msg);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100922
923 free_global_msglist();
924
925 if (should_free)
926 vim_free(msg);
927
928 return -1;
929 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200930 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200931 return (PyErr_Occurred() ? -1 : 0);
Bram Moolenaar86181df2020-05-11 23:14:04 +0200932 // Python exception is preferred over Vim one; unlikely to occur though
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200933 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200934 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100935 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200936 return -1;
937 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100938 // Finally transform Vim script exception to python one
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200939 else
940 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200941 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200942 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200943 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200944 }
945}
946
947 static int
948VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200949{
950 if (got_int)
951 {
952 PyErr_SetNone(PyExc_KeyboardInterrupt);
953 return 1;
954 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200955 return 0;
956}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200957
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100958// Vim module - Implementation
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200959
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200960 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200961VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200962{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200963 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200965 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200966
Bram Moolenaar389a1792013-06-23 13:00:44 +0200967 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200968 return NULL;
969
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200970 Py_BEGIN_ALLOW_THREADS
971 Python_Lock_Vim();
972
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200973 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200974 do_cmdline_cmd(cmd);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100975 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200976
977 Python_Release_Vim();
978 Py_END_ALLOW_THREADS
979
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200980 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200981 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200982 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200983 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200984
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200985 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200986 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200987 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200988}
989
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200990/*
991 * Function to translate a typval_T into a PyObject; this will recursively
992 * translate lists/dictionaries into their Python equivalents.
993 *
994 * The depth parameter is to avoid infinite recursion, set it to 1 when
995 * you call VimToPython.
996 */
997 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200998VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200999{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001000 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001001 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001002 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001003
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001004 // Avoid infinite recursion
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001005 if (depth > 100)
1006 {
1007 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001008 ret = Py_None;
1009 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001010 }
1011
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001012 // Check if we run into a recursive loop. The item must be in lookup_dict
1013 // then and we can use it again.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001014 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
1015 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
1016 {
Bram Moolenaardb913952012-06-29 12:54:53 +02001017 sprintf(ptrBuf, "%p",
1018 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
1019 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001020
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001021 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001022 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001023 Py_INCREF(ret);
1024 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001025 }
1026 }
1027
1028 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001029 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02001030 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001031 else if (our_tv->v_type == VAR_NUMBER)
1032 {
1033 char buf[NUMBUFLEN];
1034
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001035 // For backwards compatibility numbers are stored as strings.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001036 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +02001037 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001038 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001039 else if (our_tv->v_type == VAR_FLOAT)
1040 {
1041 char buf[NUMBUFLEN];
1042
1043 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +02001044 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001045 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001046 else if (our_tv->v_type == VAR_LIST)
1047 {
1048 list_T *list = our_tv->vval.v_list;
1049 listitem_T *curr;
1050
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001051 if (list == NULL)
1052 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001053
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001054 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001055 return NULL;
1056
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001057 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001058 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001059 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001060 return NULL;
1061 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001062
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001063 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001064 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001065 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001066 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001067 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001068 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001069 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001070 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001071 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001072 {
1073 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001074 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001075 return NULL;
1076 }
1077 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001078 }
1079 }
1080 else if (our_tv->v_type == VAR_DICT)
1081 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001082
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001083 hashtab_T *ht;
1084 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001085 hashitem_T *hi;
1086 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001087
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001088 if (our_tv->vval.v_dict == NULL)
1089 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001090 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001091
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001092 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001093 return NULL;
1094
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001095 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001096 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001097 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001098 return NULL;
1099 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001100
Bram Moolenaar24a6ff82015-02-10 18:41:58 +01001101 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001102 for (hi = ht->ht_array; todo > 0; ++hi)
1103 {
1104 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001105 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001106 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001107
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001108 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001109 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001110 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001111 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001112 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001113 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001114 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001115 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001116 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +02001117 Py_DECREF(newObj);
1118 return NULL;
1119 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001120 }
1121 }
1122 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001123 else if (our_tv->v_type == VAR_BOOL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001124 {
1125 if (our_tv->vval.v_number == VVAL_FALSE)
1126 {
1127 ret = Py_False;
1128 Py_INCREF(ret);
1129 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001130 else
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001131 {
1132 ret = Py_True;
1133 Py_INCREF(ret);
1134 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001135 return ret;
1136 }
1137 else if (our_tv->v_type == VAR_SPECIAL)
1138 {
1139 Py_INCREF(Py_None);
1140 ret = Py_None;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001141 return ret;
1142 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001143 else if (our_tv->v_type == VAR_BLOB)
1144 ret = PyBytes_FromStringAndSize(
1145 (char*) our_tv->vval.v_blob->bv_ga.ga_data,
1146 (Py_ssize_t) our_tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001147 else
1148 {
1149 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001150 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001151 }
1152
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001153 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001154}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001155
1156 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +02001157VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001158{
Bram Moolenaar389a1792013-06-23 13:00:44 +02001159 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001160 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +02001161 PyObject *string;
1162 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001163 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +02001164 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001165
Bram Moolenaar389a1792013-06-23 13:00:44 +02001166 if (!PyArg_ParseTuple(args, "O", &string))
1167 return NULL;
1168
1169 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001170 return NULL;
1171
1172 Py_BEGIN_ALLOW_THREADS
1173 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001174 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +02001175 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001176 Python_Release_Vim();
1177 Py_END_ALLOW_THREADS
1178
Bram Moolenaar389a1792013-06-23 13:00:44 +02001179 Py_XDECREF(todecref);
1180
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001181 if (VimTryEnd())
1182 return NULL;
1183
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001184 if (our_tv == NULL)
1185 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001186 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001187 return NULL;
1188 }
1189
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001190 // Convert the Vim type into a Python type. Create a dictionary that's
1191 // used to check for recursive loops.
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001192 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001193 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001194 else
1195 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001196 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02001197 Py_DECREF(lookup_dict);
1198 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001199
1200
1201 Py_BEGIN_ALLOW_THREADS
1202 Python_Lock_Vim();
1203 free_tv(our_tv);
1204 Python_Release_Vim();
1205 Py_END_ALLOW_THREADS
1206
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001207 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001208}
1209
Bram Moolenaardb913952012-06-29 12:54:53 +02001210static PyObject *ConvertToPyObject(typval_T *);
1211
1212 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001213VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +02001214{
Bram Moolenaardb913952012-06-29 12:54:53 +02001215 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001216 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +02001217 char_u *expr;
1218 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001219
Bram Moolenaar389a1792013-06-23 13:00:44 +02001220 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001221 return NULL;
1222
1223 Py_BEGIN_ALLOW_THREADS
1224 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001225 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +02001226 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001227 Python_Release_Vim();
1228 Py_END_ALLOW_THREADS
1229
Bram Moolenaar389a1792013-06-23 13:00:44 +02001230 Py_XDECREF(todecref);
1231
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001232 if (VimTryEnd())
1233 return NULL;
1234
Bram Moolenaardb913952012-06-29 12:54:53 +02001235 if (our_tv == NULL)
1236 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001237 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001238 return NULL;
1239 }
1240
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001241 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001242 Py_BEGIN_ALLOW_THREADS
1243 Python_Lock_Vim();
1244 free_tv(our_tv);
1245 Python_Release_Vim();
1246 Py_END_ALLOW_THREADS
1247
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001248 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001249}
1250
1251 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001252VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +02001253{
Bram Moolenaar389a1792013-06-23 13:00:44 +02001254 char_u *str;
1255 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001256 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +02001257
Bram Moolenaar389a1792013-06-23 13:00:44 +02001258 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001259 return NULL;
1260
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001261 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaar389a1792013-06-23 13:00:44 +02001262
1263 Py_XDECREF(todecref);
1264
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001265 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +02001266}
1267
Bram Moolenaarf4258302013-06-02 18:20:17 +02001268 static PyObject *
1269_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
1270{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001271 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001272 PyObject *newwd;
1273 PyObject *todecref;
1274 char_u *new_dir;
1275
Bram Moolenaard4209d22013-06-05 20:34:15 +02001276 if (_chdir == NULL)
1277 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001278 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001279 return NULL;
1280
1281 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1282 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001283 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001284 return NULL;
1285 }
1286
1287 if (!(new_dir = StringToChars(newwd, &todecref)))
1288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001289 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001290 Py_DECREF(newwd);
1291 return NULL;
1292 }
1293
1294 VimTryStart();
1295
1296 if (vim_chdir(new_dir))
1297 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001298 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001299 Py_DECREF(newwd);
1300 Py_XDECREF(todecref);
1301
1302 if (VimTryEnd())
1303 return NULL;
1304
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001305 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001306 return NULL;
1307 }
1308
1309 Py_DECREF(newwd);
1310 Py_XDECREF(todecref);
1311
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02001312 post_chdir(CDSCOPE_GLOBAL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001313
1314 if (VimTryEnd())
1315 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001316 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001317 return NULL;
1318 }
1319
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001320 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001321}
1322
1323 static PyObject *
1324VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1325{
1326 return _VimChdir(py_chdir, args, kwargs);
1327}
1328
1329 static PyObject *
1330VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1331{
1332 return _VimChdir(py_fchdir, args, kwargs);
1333}
1334
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001335typedef struct {
1336 PyObject *callable;
1337 PyObject *result;
1338} map_rtp_data;
1339
1340 static void
1341map_rtp_callback(char_u *path, void *_data)
1342{
1343 void **data = (void **) _data;
1344 PyObject *pathObject;
1345 map_rtp_data *mr_data = *((map_rtp_data **) data);
1346
Bram Moolenaar41009372013-07-01 22:03:04 +02001347 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001348 {
1349 *data = NULL;
1350 return;
1351 }
1352
1353 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1354 pathObject, NULL);
1355
1356 Py_DECREF(pathObject);
1357
1358 if (!mr_data->result || mr_data->result != Py_None)
1359 *data = NULL;
1360 else
1361 {
1362 Py_DECREF(mr_data->result);
1363 mr_data->result = NULL;
1364 }
1365}
1366
1367 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001368VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001369{
1370 map_rtp_data data;
1371
Bram Moolenaar389a1792013-06-23 13:00:44 +02001372 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001373 data.result = NULL;
1374
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001375 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001376
1377 if (data.result == NULL)
1378 {
1379 if (PyErr_Occurred())
1380 return NULL;
1381 else
1382 {
1383 Py_INCREF(Py_None);
1384 return Py_None;
1385 }
1386 }
1387 return data.result;
1388}
1389
1390/*
1391 * _vim_runtimepath_ special path implementation.
1392 */
1393
1394 static void
1395map_finder_callback(char_u *path, void *_data)
1396{
1397 void **data = (void **) _data;
1398 PyObject *list = *((PyObject **) data);
1399 PyObject *pathObject1, *pathObject2;
1400 char *pathbuf;
1401 size_t pathlen;
1402
1403 pathlen = STRLEN(path);
1404
1405#if PY_MAJOR_VERSION < 3
1406# define PY_MAIN_DIR_STRING "python2"
1407#else
1408# define PY_MAIN_DIR_STRING "python3"
1409#endif
1410#define PY_ALTERNATE_DIR_STRING "pythonx"
1411
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001412#define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001413 if (!(pathbuf = PyMem_New(char,
1414 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1415 {
1416 PyErr_NoMemory();
1417 *data = NULL;
1418 return;
1419 }
1420
1421 mch_memmove(pathbuf, path, pathlen + 1);
1422 add_pathsep((char_u *) pathbuf);
1423
1424 pathlen = STRLEN(pathbuf);
1425 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1426 PYTHONX_STRING_LENGTH + 1);
1427
1428 if (!(pathObject1 = PyString_FromString(pathbuf)))
1429 {
1430 *data = NULL;
1431 PyMem_Free(pathbuf);
1432 return;
1433 }
1434
1435 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1436 PYTHONX_STRING_LENGTH + 1);
1437
1438 if (!(pathObject2 = PyString_FromString(pathbuf)))
1439 {
1440 Py_DECREF(pathObject1);
1441 PyMem_Free(pathbuf);
1442 *data = NULL;
1443 return;
1444 }
1445
1446 PyMem_Free(pathbuf);
1447
1448 if (PyList_Append(list, pathObject1)
1449 || PyList_Append(list, pathObject2))
1450 *data = NULL;
1451
1452 Py_DECREF(pathObject1);
1453 Py_DECREF(pathObject2);
1454}
1455
1456 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001457Vim_GetPaths(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001458{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001459 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001461 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001462 return NULL;
1463
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001464 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001465
1466 if (PyErr_Occurred())
1467 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001468 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001469 return NULL;
1470 }
1471
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001472 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001473}
1474
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001475#if PY_VERSION_HEX >= 0x030700f0
1476 static PyObject *
1477FinderFindSpec(PyObject *self, PyObject *args)
1478{
1479 char *fullname;
1480 PyObject *paths;
1481 PyObject *target = Py_None;
1482 PyObject *spec;
1483
1484 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1485 return NULL;
1486
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001487 if (!(paths = Vim_GetPaths(self, NULL)))
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001488 return NULL;
1489
Bram Moolenaarde5b3802019-03-30 12:51:22 +01001490 spec = PyObject_CallFunction(py_find_spec, "sOO", fullname, paths, target);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001491
1492 Py_DECREF(paths);
1493
1494 if (!spec)
1495 {
1496 if (PyErr_Occurred())
1497 return NULL;
1498
1499 Py_INCREF(Py_None);
1500 return Py_None;
1501 }
1502
1503 return spec;
1504}
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001505
1506 static PyObject *
1507FinderFindModule(PyObject* self UNUSED, PyObject* args UNUSED)
1508{
1509 // Apparently returning None works.
1510 Py_INCREF(Py_None);
1511 return Py_None;
1512}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001513#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001514 static PyObject *
1515call_load_module(char *name, int len, PyObject *find_module_result)
1516{
1517 PyObject *fd, *pathname, *description;
1518
Bram Moolenaarc476e522013-06-23 13:46:40 +02001519 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001520 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001521 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001522 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001523 find_module_result);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001524 return NULL;
1525 }
1526 if (PyTuple_GET_SIZE(find_module_result) != 3)
1527 {
1528 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001529 N_("expected 3-tuple as imp.find_module() result, but got "
1530 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001531 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001532 return NULL;
1533 }
1534
1535 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1536 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1537 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1538 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001539 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001540 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001541 return NULL;
1542 }
1543
1544 return PyObject_CallFunction(py_load_module,
1545 "s#OOO", name, len, fd, pathname, description);
1546}
1547
1548 static PyObject *
1549find_module(char *fullname, char *tail, PyObject *new_path)
1550{
1551 PyObject *find_module_result;
1552 PyObject *module;
1553 char *dot;
1554
Bram Moolenaar41009372013-07-01 22:03:04 +02001555 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001556 {
1557 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001558 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001559 * first component
1560 */
1561 PyObject *newest_path;
1562 int partlen = (int) (dot - 1 - tail);
1563
1564 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1565 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001566 {
1567 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1568 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001569 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001570 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001571
1572 if (!(module = call_load_module(
1573 fullname,
1574 ((int) (tail - fullname)) + partlen,
1575 find_module_result)))
1576 {
1577 Py_DECREF(find_module_result);
1578 return NULL;
1579 }
1580
1581 Py_DECREF(find_module_result);
1582
1583 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1584 {
1585 Py_DECREF(module);
1586 return NULL;
1587 }
1588
1589 Py_DECREF(module);
1590
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001591 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001592
1593 Py_DECREF(newest_path);
1594
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001595 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001596 }
1597 else
1598 {
1599 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1600 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001601 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001602 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1603 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001604 return NULL;
1605 }
1606
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001607 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001608 }
1609}
1610
1611 static PyObject *
1612FinderFindModule(PyObject *self, PyObject *args)
1613{
1614 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001615 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001616 PyObject *new_path;
1617 LoaderObject *loader;
1618
1619 if (!PyArg_ParseTuple(args, "s", &fullname))
1620 return NULL;
1621
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001622 if (!(new_path = Vim_GetPaths(self, NULL)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001623 return NULL;
1624
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001625 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001626
1627 Py_DECREF(new_path);
1628
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001629 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001630 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001631 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001632 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001633
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001634 Py_INCREF(Py_None);
1635 return Py_None;
1636 }
1637
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001638 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001639 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001640 Py_DECREF(result);
1641 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001642 return NULL;
1643 }
1644
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001645 if (!(loader = PyObject_NEW(LoaderObject, LoaderTypePtr)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001646 {
1647 vim_free(fullname);
1648 Py_DECREF(result);
1649 return NULL;
1650 }
1651
1652 loader->fullname = fullname;
1653 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001654
1655 return (PyObject *) loader;
1656}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001657#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001658
1659 static PyObject *
1660VimPathHook(PyObject *self UNUSED, PyObject *args)
1661{
1662 char *path;
1663
1664 if (PyArg_ParseTuple(args, "s", &path)
1665 && STRCMP(path, vim_special_path) == 0)
1666 {
1667 Py_INCREF(vim_module);
1668 return vim_module;
1669 }
1670
1671 PyErr_Clear();
1672 PyErr_SetNone(PyExc_ImportError);
1673 return NULL;
1674}
1675
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001676/*
1677 * Vim module - Definitions
1678 */
1679
1680static struct PyMethodDef VimMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001681 // name, function, calling, documentation
Bram Moolenaar389a1792013-06-23 13:00:44 +02001682 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001683 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar86181df2020-05-11 23:14:04 +02001684 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to Vim ones"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001685 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001686 {"chdir", (PyCFunction)(void *)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1687 {"fchdir", (PyCFunction)(void *)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001688 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001689#if PY_VERSION_HEX >= 0x030700f0
1690 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001691#endif
Bram Moolenaar0b0ad352019-05-20 21:52:45 +02001692 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001693 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1694 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1695 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001696};
1697
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001698/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001699 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001700 */
1701
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001702DEFINE_PY_TYPE_OBJECT(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001703
1704typedef PyObject *(*nextfun)(void **);
1705typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001706typedef int (*traversefun)(void *, visitproc, void *);
1707typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001708
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001709// Main purpose of this object is removing the need for do python
1710// initialization (i.e. PyType_Ready and setting type attributes) for a big
1711// bunch of objects.
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001712typedef struct
1713{
1714 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001715 void *cur;
1716 nextfun next;
1717 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001718 traversefun traverse;
1719 clearfun clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001720 PyObject *iter_object;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001721} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001722
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001723 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001724IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001725 clearfun clear, PyObject *iter_object)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001726{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001727 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001728
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001729 self = PyObject_GC_New(IterObject, IterTypePtr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001730 self->cur = start;
1731 self->next = next;
1732 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001733 self->traverse = traverse;
1734 self->clear = clear;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001735 self->iter_object = iter_object;
1736
1737 if (iter_object)
1738 Py_INCREF(iter_object);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001739
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001740 return (PyObject *)(self);
1741}
1742
1743 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001744IterDestructor(PyObject *self_obj)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001745{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001746 IterObject *self = (IterObject*)self_obj;
Bram Moolenaar423a85a2020-08-29 12:57:16 +02001747 if (self->iter_object)
1748 Py_DECREF(self->iter_object);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001749 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001750 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001751 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001752}
1753
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001754 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001755IterTraverse(PyObject *self_obj, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001756{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001757 IterObject *self = (IterObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001758 if (self->traverse != NULL)
1759 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001760 else
1761 return 0;
1762}
1763
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001764// Mac OSX defines clear() somewhere.
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001765#ifdef clear
1766# undef clear
1767#endif
1768
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001769 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001770IterClear(PyObject *self_obj)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001771{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001772 IterObject *self = (IterObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001773 if (self->clear != NULL)
1774 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001775 else
1776 return 0;
1777}
1778
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001779 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001780IterNext(PyObject *self_obj)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001781{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001782 IterObject *self = (IterObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001783 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001784}
1785
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001786 static PyObject *
1787IterIter(PyObject *self)
1788{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001789 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001790 return self;
1791}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001792
Bram Moolenaardb913952012-06-29 12:54:53 +02001793typedef struct pylinkedlist_S {
1794 struct pylinkedlist_S *pll_next;
1795 struct pylinkedlist_S *pll_prev;
1796 PyObject *pll_obj;
1797} pylinkedlist_T;
1798
1799static pylinkedlist_T *lastdict = NULL;
1800static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001801static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001802
1803 static void
1804pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1805{
1806 if (ref->pll_prev == NULL)
1807 {
1808 if (ref->pll_next == NULL)
1809 {
1810 *last = NULL;
1811 return;
1812 }
1813 }
1814 else
1815 ref->pll_prev->pll_next = ref->pll_next;
1816
1817 if (ref->pll_next == NULL)
1818 *last = ref->pll_prev;
1819 else
1820 ref->pll_next->pll_prev = ref->pll_prev;
1821}
1822
1823 static void
1824pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1825{
1826 if (*last == NULL)
1827 ref->pll_prev = NULL;
1828 else
1829 {
1830 (*last)->pll_next = ref;
1831 ref->pll_prev = *last;
1832 }
1833 ref->pll_next = NULL;
1834 ref->pll_obj = self;
1835 *last = ref;
1836}
1837
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001838DEFINE_PY_TYPE_OBJECT(DictionaryType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001839
1840typedef struct
1841{
1842 PyObject_HEAD
1843 dict_T *dict;
1844 pylinkedlist_T ref;
1845} DictionaryObject;
1846
Bram Moolenaara9922d62013-05-30 13:01:18 +02001847static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1848
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001849#define NEW_DICTIONARY(dict) DictionaryNew(DictionaryTypePtr, dict)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001850
Bram Moolenaardb913952012-06-29 12:54:53 +02001851 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001852DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001853{
1854 DictionaryObject *self;
1855
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001856 self = (DictionaryObject *) Py_TYPE_GET_TP_ALLOC(subtype)(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001857 if (self == NULL)
1858 return NULL;
1859 self->dict = dict;
1860 ++dict->dv_refcount;
1861
1862 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1863
1864 return (PyObject *)(self);
1865}
1866
Bram Moolenaara9922d62013-05-30 13:01:18 +02001867 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001868py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001869{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001870 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001871
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001872 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001873 {
1874 PyErr_NoMemory();
1875 return NULL;
1876 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001877 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001878
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001879 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001880}
1881
1882 static PyObject *
1883DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1884{
1885 DictionaryObject *self;
1886 dict_T *dict;
1887
1888 if (!(dict = py_dict_alloc()))
1889 return NULL;
1890
1891 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1892
1893 --dict->dv_refcount;
1894
1895 if (kwargs || PyTuple_Size(args))
1896 {
1897 PyObject *tmp;
1898 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1899 {
1900 Py_DECREF(self);
1901 return NULL;
1902 }
1903
1904 Py_DECREF(tmp);
1905 }
1906
1907 return (PyObject *)(self);
1908}
1909
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001910 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001911DictionaryDestructor(PyObject *self_obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001912{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001913 DictionaryObject *self = (DictionaryObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001914 pyll_remove(&self->ref, &lastdict);
1915 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001916
1917 DESTRUCTOR_FINISH(self);
1918}
1919
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001920static char *DictionaryAttrs[] = {
1921 "locked", "scope",
1922 NULL
1923};
1924
1925 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001926DictionaryDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001927{
1928 return ObjectDir(self, DictionaryAttrs);
1929}
1930
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001932DictionarySetattr(PyObject *self_obj, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001933{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001934 DictionaryObject *self = (DictionaryObject*)self_obj;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001935 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001936 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001937 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001938 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001939 return -1;
1940 }
1941
1942 if (strcmp(name, "locked") == 0)
1943 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001944 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001945 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001946 PyErr_SET_STRING(PyExc_TypeError,
1947 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001948 return -1;
1949 }
1950 else
1951 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001952 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001953 if (istrue == -1)
1954 return -1;
1955 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001956 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001957 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001958 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001959 }
1960 return 0;
1961 }
1962 else
1963 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001964 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001965 return -1;
1966 }
1967}
1968
1969 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001970DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001971{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001972 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001973}
1974
Bram Moolenaara9922d62013-05-30 13:01:18 +02001975#define DICT_FLAG_HAS_DEFAULT 0x01
1976#define DICT_FLAG_POP 0x02
1977#define DICT_FLAG_NONE_DEFAULT 0x04
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01001978#define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
Bram Moolenaara9922d62013-05-30 13:01:18 +02001979#define DICT_FLAG_RETURN_PAIR 0x10
1980
Bram Moolenaardb913952012-06-29 12:54:53 +02001981 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001982_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001983{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001984 PyObject *keyObject;
1985 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001986 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001987 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001988 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001989 dict_T *dict = self->dict;
1990 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001991 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001992
Bram Moolenaara9922d62013-05-30 13:01:18 +02001993 if (flags & DICT_FLAG_HAS_DEFAULT)
1994 {
1995 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1996 return NULL;
1997 }
1998 else
1999 keyObject = args;
2000
2001 if (flags & DICT_FLAG_RETURN_BOOL)
2002 defObject = Py_False;
2003
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002004 if (!(key = StringToChars(keyObject, &todecref)))
2005 return NULL;
2006
2007 if (*key == NUL)
2008 {
2009 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002010 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002011 return NULL;
2012 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002013
Bram Moolenaara9922d62013-05-30 13:01:18 +02002014 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02002015
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002016 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02002017
Bram Moolenaara9922d62013-05-30 13:01:18 +02002018 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02002019 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002020 if (defObject)
2021 {
2022 Py_INCREF(defObject);
2023 return defObject;
2024 }
2025 else
2026 {
2027 PyErr_SetObject(PyExc_KeyError, keyObject);
2028 return NULL;
2029 }
2030 }
2031 else if (flags & DICT_FLAG_RETURN_BOOL)
2032 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01002033 ret = Py_True;
2034 Py_INCREF(ret);
2035 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002036 }
2037
2038 di = dict_lookup(hi);
2039
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002040 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002041 return NULL;
2042
2043 if (flags & DICT_FLAG_POP)
2044 {
2045 if (dict->dv_lock)
2046 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002047 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002048 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002049 return NULL;
2050 }
2051
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002052 hash_remove(&dict->dv_hashtab, hi, "Python remove variable");
Bram Moolenaara9922d62013-05-30 13:01:18 +02002053 dictitem_free(di);
2054 }
2055
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002056 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002057}
2058
2059 static PyObject *
2060DictionaryItem(DictionaryObject *self, PyObject *keyObject)
2061{
2062 return _DictionaryItem(self, keyObject, 0);
2063}
2064
2065 static int
2066DictionaryContains(DictionaryObject *self, PyObject *keyObject)
2067{
2068 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002069 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002070
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01002071 if (rObj == NULL)
2072 return -1;
2073
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002074 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002075
Bram Moolenaardee2e312013-06-23 16:35:47 +02002076 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002077
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002078 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002079}
2080
2081typedef struct
2082{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002083 int dii_changed;
2084 hashtab_T *dii_ht;
2085 hashitem_T *dii_hi;
2086 long_u dii_todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002087} dictiterinfo_T;
2088
2089 static PyObject *
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02002090DictionaryIterNext(void **arg)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002091{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002092 PyObject *ret;
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02002093 dictiterinfo_T **dii = (dictiterinfo_T**)arg;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002094
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002095 if (!(*dii)->dii_todo)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002096 return NULL;
2097
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002098 if ((*dii)->dii_ht->ht_changed != (*dii)->dii_changed)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002099 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002100 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002101 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02002102 return NULL;
2103 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002104
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002105 while (((*dii)->dii_todo) && HASHITEM_EMPTY((*dii)->dii_hi))
2106 ++((*dii)->dii_hi);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002107
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002108 --((*dii)->dii_todo);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002109
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002110 if (!(ret = PyBytes_FromString((char *)(*dii)->dii_hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002111 return NULL;
2112
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002113 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002114}
2115
2116 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002117DictionaryIter(PyObject *self_obj)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002118{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002119 DictionaryObject *self = (DictionaryObject*)self_obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002120 dictiterinfo_T *dii;
2121 hashtab_T *ht;
2122
2123 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
2124 {
2125 PyErr_NoMemory();
2126 return NULL;
2127 }
2128
2129 ht = &self->dict->dv_hashtab;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002130 dii->dii_changed = ht->ht_changed;
2131 dii->dii_ht = ht;
2132 dii->dii_hi = ht->ht_array;
2133 dii->dii_todo = ht->ht_used;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002134
2135 return IterNew(dii,
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02002136 PyMem_Free, DictionaryIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02002137 NULL, NULL, (PyObject *)self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002138}
2139
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002140 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002141DictionaryAssItem(
2142 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02002143{
2144 char_u *key;
2145 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002146 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02002147 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002148 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02002149
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002150 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02002151 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002152 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02002153 return -1;
2154 }
2155
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002156 if (!(key = StringToChars(keyObject, &todecref)))
2157 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002158
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002159 if (*key == NUL)
2160 {
2161 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02002162 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002163 return -1;
2164 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002165
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002166 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02002167
2168 if (valObject == NULL)
2169 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02002170 hashitem_T *hi;
2171
Bram Moolenaardb913952012-06-29 12:54:53 +02002172 if (di == NULL)
2173 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002174 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002175 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02002176 return -1;
2177 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002178 hi = hash_find(&dict->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002179 hash_remove(&dict->dv_hashtab, hi, "Python remove item");
Bram Moolenaardb913952012-06-29 12:54:53 +02002180 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02002181 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002182 return 0;
2183 }
2184
2185 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02002186 {
2187 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002188 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02002189 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002190
2191 if (di == NULL)
2192 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002193 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002194 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002195 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002196 PyErr_NoMemory();
2197 return -1;
2198 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002199 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02002200
Bram Moolenaarb38caae2013-05-29 22:39:52 +02002201 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02002202 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002203 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002204 RAISE_KEY_ADD_FAIL(key);
2205 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002206 return -1;
2207 }
2208 }
2209 else
2210 clear_tv(&di->di_tv);
2211
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002212 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02002213
2214 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002215 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002216 return 0;
2217}
2218
Bram Moolenaara9922d62013-05-30 13:01:18 +02002219typedef PyObject *(*hi_to_py)(hashitem_T *);
2220
Bram Moolenaardb913952012-06-29 12:54:53 +02002221 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02002222DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02002223{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002224 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02002225 long_u todo = dict->dv_hashtab.ht_used;
2226 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002227 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002228 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002229 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02002230
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002231 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02002232 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
2233 {
2234 if (!HASHITEM_EMPTY(hi))
2235 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002236 if (!(newObj = hiconvert(hi)))
2237 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002238 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002239 return NULL;
2240 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002241 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002242 --todo;
2243 ++i;
2244 }
2245 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002246 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002247}
2248
Bram Moolenaara9922d62013-05-30 13:01:18 +02002249 static PyObject *
2250dict_key(hashitem_T *hi)
2251{
2252 return PyBytes_FromString((char *)(hi->hi_key));
2253}
2254
2255 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002256DictionaryListKeys(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002257{
2258 return DictionaryListObjects(self, dict_key);
2259}
2260
2261 static PyObject *
2262dict_val(hashitem_T *hi)
2263{
2264 dictitem_T *di;
2265
2266 di = dict_lookup(hi);
2267 return ConvertToPyObject(&di->di_tv);
2268}
2269
2270 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002271DictionaryListValues(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002272{
2273 return DictionaryListObjects(self, dict_val);
2274}
2275
2276 static PyObject *
2277dict_item(hashitem_T *hi)
2278{
2279 PyObject *keyObject;
2280 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002281 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002282
2283 if (!(keyObject = dict_key(hi)))
2284 return NULL;
2285
2286 if (!(valObject = dict_val(hi)))
2287 {
2288 Py_DECREF(keyObject);
2289 return NULL;
2290 }
2291
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002292 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002293
2294 Py_DECREF(keyObject);
2295 Py_DECREF(valObject);
2296
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002297 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002298}
2299
2300 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002301DictionaryListItems(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002302{
2303 return DictionaryListObjects(self, dict_item);
2304}
2305
2306 static PyObject *
2307DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2308{
2309 dict_T *dict = self->dict;
2310
2311 if (dict->dv_lock)
2312 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002313 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002314 return NULL;
2315 }
2316
2317 if (kwargs)
2318 {
2319 typval_T tv;
2320
2321 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2322 return NULL;
2323
2324 VimTryStart();
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002325 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force", NULL);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002326 clear_tv(&tv);
2327 if (VimTryEnd())
2328 return NULL;
2329 }
2330 else
2331 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002332 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002333
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002334 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002335 return NULL;
2336
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002337 if (obj == NULL)
2338 {
2339 Py_INCREF(Py_None);
2340 return Py_None;
2341 }
2342
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002343 if (PyObject_HasAttrString(obj, "keys"))
2344 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002345 else
2346 {
2347 PyObject *iterator;
2348 PyObject *item;
2349
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002350 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002351 return NULL;
2352
2353 while ((item = PyIter_Next(iterator)))
2354 {
2355 PyObject *fast;
2356 PyObject *keyObject;
2357 PyObject *valObject;
2358 PyObject *todecref;
2359 char_u *key;
2360 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002361 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002362
2363 if (!(fast = PySequence_Fast(item, "")))
2364 {
2365 Py_DECREF(iterator);
2366 Py_DECREF(item);
2367 return NULL;
2368 }
2369
2370 Py_DECREF(item);
2371
2372 if (PySequence_Fast_GET_SIZE(fast) != 2)
2373 {
2374 Py_DECREF(iterator);
2375 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002376 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002377 N_("expected sequence element of size 2, "
2378 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002379 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002380 return NULL;
2381 }
2382
2383 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2384
2385 if (!(key = StringToChars(keyObject, &todecref)))
2386 {
2387 Py_DECREF(iterator);
2388 Py_DECREF(fast);
2389 return NULL;
2390 }
2391
2392 di = dictitem_alloc(key);
2393
2394 Py_XDECREF(todecref);
2395
2396 if (di == NULL)
2397 {
2398 Py_DECREF(fast);
2399 Py_DECREF(iterator);
2400 PyErr_NoMemory();
2401 return NULL;
2402 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002403 di->di_tv.v_type = VAR_UNKNOWN;
2404
2405 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2406
2407 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2408 {
2409 Py_DECREF(iterator);
2410 Py_DECREF(fast);
2411 dictitem_free(di);
2412 return NULL;
2413 }
2414
2415 Py_DECREF(fast);
2416
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002417 hi = hash_find(&dict->dv_hashtab, di->di_key);
2418 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002419 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002420 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002421 Py_DECREF(iterator);
2422 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002423 return NULL;
2424 }
2425 }
2426
2427 Py_DECREF(iterator);
2428
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002429 // Iterator may have finished due to an exception
Bram Moolenaara9922d62013-05-30 13:01:18 +02002430 if (PyErr_Occurred())
2431 return NULL;
2432 }
2433 }
2434 Py_INCREF(Py_None);
2435 return Py_None;
2436}
2437
2438 static PyObject *
2439DictionaryGet(DictionaryObject *self, PyObject *args)
2440{
2441 return _DictionaryItem(self, args,
2442 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2443}
2444
2445 static PyObject *
2446DictionaryPop(DictionaryObject *self, PyObject *args)
2447{
2448 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2449}
2450
2451 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002452DictionaryPopItem(DictionaryObject *self, PyObject *args UNUSED)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002453{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002454 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002455 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002456 PyObject *valObject;
2457 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002458
Bram Moolenaarde71b562013-06-02 17:41:54 +02002459 if (self->dict->dv_hashtab.ht_used == 0)
2460 {
2461 PyErr_SetNone(PyExc_KeyError);
2462 return NULL;
2463 }
2464
2465 hi = self->dict->dv_hashtab.ht_array;
2466 while (HASHITEM_EMPTY(hi))
2467 ++hi;
2468
2469 di = dict_lookup(hi);
2470
2471 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002472 return NULL;
2473
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002474 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002475 {
2476 Py_DECREF(valObject);
2477 return NULL;
2478 }
2479
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002480 hash_remove(&self->dict->dv_hashtab, hi, "Python pop item");
Bram Moolenaarde71b562013-06-02 17:41:54 +02002481 dictitem_free(di);
2482
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002483 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002484}
2485
2486 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002487DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002488{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002489 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2490}
2491
2492static PySequenceMethods DictionaryAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002493 0, // sq_length
2494 0, // sq_concat
2495 0, // sq_repeat
2496 0, // sq_item
2497 0, // sq_slice
2498 0, // sq_ass_item
2499 0, // sq_ass_slice
2500 (objobjproc) DictionaryContains, // sq_contains
2501 0, // sq_inplace_concat
2502 0, // sq_inplace_repeat
Bram Moolenaara9922d62013-05-30 13:01:18 +02002503};
2504
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002505static PyMappingMethods DictionaryAsMapping = {
2506 (lenfunc) DictionaryLength,
2507 (binaryfunc) DictionaryItem,
2508 (objobjargproc) DictionaryAssItem,
2509};
2510
Bram Moolenaardb913952012-06-29 12:54:53 +02002511static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002512 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002513 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2514 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02002515 {"update", (PyCFunction)(void *)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002516 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2517 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002518 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002519 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002520 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2521 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002522};
2523
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002524DEFINE_PY_TYPE_OBJECT(ListType);
Bram Moolenaardb913952012-06-29 12:54:53 +02002525
2526typedef struct
2527{
2528 PyObject_HEAD
2529 list_T *list;
2530 pylinkedlist_T ref;
2531} ListObject;
2532
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002533#define NEW_LIST(list) ListNew(ListTypePtr, list)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002534
Bram Moolenaardb913952012-06-29 12:54:53 +02002535 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002536ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002537{
2538 ListObject *self;
2539
Bram Moolenaarab589462020-07-06 21:03:06 +02002540 if (list == NULL)
2541 return NULL;
2542
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002543 self = (ListObject *) Py_TYPE_GET_TP_ALLOC(subtype)(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002544 if (self == NULL)
2545 return NULL;
2546 self->list = list;
2547 ++list->lv_refcount;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002548 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaardb913952012-06-29 12:54:53 +02002549
2550 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2551
2552 return (PyObject *)(self);
2553}
2554
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002555 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002556py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002557{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002558 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002559
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002560 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002561 {
2562 PyErr_NoMemory();
2563 return NULL;
2564 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002565 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002566
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002567 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002568}
2569
2570 static int
2571list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2572{
2573 PyObject *iterator;
2574 PyObject *item;
2575 listitem_T *li;
2576
2577 if (!(iterator = PyObject_GetIter(obj)))
2578 return -1;
2579
2580 while ((item = PyIter_Next(iterator)))
2581 {
2582 if (!(li = listitem_alloc()))
2583 {
2584 PyErr_NoMemory();
2585 Py_DECREF(item);
2586 Py_DECREF(iterator);
2587 return -1;
2588 }
2589 li->li_tv.v_lock = 0;
2590 li->li_tv.v_type = VAR_UNKNOWN;
2591
2592 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2593 {
2594 Py_DECREF(item);
2595 Py_DECREF(iterator);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 listitem_free(l, li);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002597 return -1;
2598 }
2599
2600 Py_DECREF(item);
2601
2602 list_append(l, li);
2603 }
2604
2605 Py_DECREF(iterator);
2606
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002607 // Iterator may have finished due to an exception
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002608 if (PyErr_Occurred())
2609 return -1;
2610
2611 return 0;
2612}
2613
2614 static PyObject *
2615ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2616{
2617 list_T *list;
2618 PyObject *obj = NULL;
2619
2620 if (kwargs)
2621 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002622 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002623 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002624 return NULL;
2625 }
2626
2627 if (!PyArg_ParseTuple(args, "|O", &obj))
2628 return NULL;
2629
2630 if (!(list = py_list_alloc()))
2631 return NULL;
2632
2633 if (obj)
2634 {
2635 PyObject *lookup_dict;
2636
2637 if (!(lookup_dict = PyDict_New()))
2638 {
2639 list_unref(list);
2640 return NULL;
2641 }
2642
2643 if (list_py_concat(list, obj, lookup_dict) == -1)
2644 {
2645 Py_DECREF(lookup_dict);
2646 list_unref(list);
2647 return NULL;
2648 }
2649
2650 Py_DECREF(lookup_dict);
2651 }
2652
2653 return ListNew(subtype, list);
2654}
2655
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002656 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002657ListDestructor(PyObject *self_obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002658{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002659 ListObject *self = (ListObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002660 pyll_remove(&self->ref, &lastlist);
2661 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002662
2663 DESTRUCTOR_FINISH(self);
2664}
2665
Bram Moolenaardb913952012-06-29 12:54:53 +02002666 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002667ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002668{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002669 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002670}
2671
2672 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002673ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002674{
2675 listitem_T *li;
2676
Bram Moolenaard6e39182013-05-21 18:30:34 +02002677 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002678 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002679 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002680 return NULL;
2681 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002682 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002683 if (li == NULL)
2684 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002685 // No more suitable format specifications in python-2.3
Bram Moolenaar86181df2020-05-11 23:14:04 +02002686 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002687 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002688 return NULL;
2689 }
2690 return ConvertToPyObject(&li->li_tv);
2691}
2692
Bram Moolenaardb913952012-06-29 12:54:53 +02002693 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002694ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2695 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002696{
2697 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002698 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002699
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002700 if (step == 0)
2701 {
2702 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2703 return NULL;
2704 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002705
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002706 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002707 if (list == NULL)
2708 return NULL;
2709
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002710 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002711 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002712 PyObject *item;
2713
2714 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002715 if (item == NULL)
2716 {
2717 Py_DECREF(list);
2718 return NULL;
2719 }
2720
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002721 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002722 }
2723
2724 return list;
2725}
2726
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002727 static PyObject *
2728ListItem(ListObject *self, PyObject* idx)
2729{
2730#if PY_MAJOR_VERSION < 3
2731 if (PyInt_Check(idx))
2732 {
2733 long _idx = PyInt_AsLong(idx);
2734 return ListIndex(self, _idx);
2735 }
2736 else
2737#endif
2738 if (PyLong_Check(idx))
2739 {
2740 long _idx = PyLong_AsLong(idx);
2741 return ListIndex(self, _idx);
2742 }
2743 else if (PySlice_Check(idx))
2744 {
2745 Py_ssize_t start, stop, step, slicelen;
2746
Bram Moolenaar922a4662014-03-30 16:11:43 +02002747 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002748 &start, &stop, &step, &slicelen) < 0)
2749 return NULL;
2750 return ListSlice(self, start, step, slicelen);
2751 }
2752 else
2753 {
2754 RAISE_INVALID_INDEX_TYPE(idx);
2755 return NULL;
2756 }
2757}
2758
2759 static void
2760list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2761 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2762{
2763 while (numreplaced--)
2764 {
2765 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2766 listitem_remove(l, lis[slicelen + numreplaced]);
2767 }
2768 while (numadded--)
2769 {
2770 listitem_T *next;
2771
2772 next = lastaddedli->li_prev;
2773 listitem_remove(l, lastaddedli);
2774 lastaddedli = next;
2775 }
2776}
2777
2778 static int
2779ListAssSlice(ListObject *self, Py_ssize_t first,
2780 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2781{
2782 PyObject *iterator;
2783 PyObject *item;
2784 listitem_T *li;
2785 listitem_T *lastaddedli = NULL;
2786 listitem_T *next;
2787 typval_T v;
2788 list_T *l = self->list;
2789 PyInt i;
2790 PyInt j;
2791 PyInt numreplaced = 0;
2792 PyInt numadded = 0;
2793 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002794 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002795
2796 size = ListLength(self);
2797
2798 if (l->lv_lock)
2799 {
2800 RAISE_LOCKED_LIST;
2801 return -1;
2802 }
2803
2804 if (step == 0)
2805 {
2806 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2807 return -1;
2808 }
2809
2810 if (step != 1 && slicelen == 0)
2811 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002812 // Nothing to do. Only error out if obj has some items.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002813 int ret = 0;
2814
2815 if (obj == NULL)
2816 return 0;
2817
2818 if (!(iterator = PyObject_GetIter(obj)))
2819 return -1;
2820
2821 if ((item = PyIter_Next(iterator)))
2822 {
2823 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002824 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002825 "to extended slice"), 0);
2826 Py_DECREF(item);
2827 ret = -1;
2828 }
2829 Py_DECREF(iterator);
2830 return ret;
2831 }
2832
2833 if (obj != NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01002834 // XXX May allocate zero bytes.
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002835 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2836 {
2837 PyErr_NoMemory();
2838 return -1;
2839 }
2840
2841 if (first == size)
2842 li = NULL;
2843 else
2844 {
2845 li = list_find(l, (long) first);
2846 if (li == NULL)
2847 {
Bram Moolenaar86181df2020-05-11 23:14:04 +02002848 PyErr_VIM_FORMAT(N_("internal error: no Vim list item %d"),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002849 (int)first);
2850 if (obj != NULL)
2851 PyMem_Free(lis);
2852 return -1;
2853 }
2854 i = slicelen;
2855 while (i-- && li != NULL)
2856 {
2857 j = step;
2858 next = li;
2859 if (step > 0)
2860 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2861 else
2862 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2863
2864 if (obj == NULL)
2865 listitem_remove(l, li);
2866 else
2867 lis[slicelen - i - 1] = li;
2868
2869 li = next;
2870 }
2871 if (li == NULL && i != -1)
2872 {
2873 PyErr_SET_VIM(N_("internal error: not enough list items"));
2874 if (obj != NULL)
2875 PyMem_Free(lis);
2876 return -1;
2877 }
2878 }
2879
2880 if (obj == NULL)
2881 return 0;
2882
2883 if (!(iterator = PyObject_GetIter(obj)))
2884 {
2885 PyMem_Free(lis);
2886 return -1;
2887 }
2888
2889 i = 0;
2890 while ((item = PyIter_Next(iterator)))
2891 {
2892 if (ConvertFromPyObject(item, &v) == -1)
2893 {
2894 Py_DECREF(iterator);
2895 Py_DECREF(item);
2896 PyMem_Free(lis);
2897 return -1;
2898 }
2899 Py_DECREF(item);
2900 if (list_insert_tv(l, &v, numreplaced < slicelen
2901 ? lis[numreplaced]
2902 : li) == FAIL)
2903 {
2904 clear_tv(&v);
2905 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2906 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2907 PyMem_Free(lis);
2908 return -1;
2909 }
2910 if (numreplaced < slicelen)
2911 {
2912 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002913 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002914 numreplaced++;
2915 }
2916 else
2917 {
2918 if (li)
2919 lastaddedli = li->li_prev;
2920 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002921 lastaddedli = l->lv_u.mat.lv_last;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002922 numadded++;
2923 }
2924 clear_tv(&v);
2925 if (step != 1 && i >= slicelen)
2926 {
2927 Py_DECREF(iterator);
2928 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002929 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002930 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002931 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2932 PyMem_Free(lis);
2933 return -1;
2934 }
2935 ++i;
2936 }
2937 Py_DECREF(iterator);
2938
2939 if (step != 1 && i != slicelen)
2940 {
2941 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002942 N_("attempt to assign sequence of size %d to extended slice "
2943 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002944 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2945 PyMem_Free(lis);
2946 return -1;
2947 }
2948
2949 if (PyErr_Occurred())
2950 {
2951 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2952 PyMem_Free(lis);
2953 return -1;
2954 }
2955
2956 for (i = 0; i < numreplaced; i++)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 listitem_free(l, lis[i]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002958 if (step == 1)
2959 for (i = numreplaced; i < slicelen; i++)
2960 listitem_remove(l, lis[i]);
2961
2962 PyMem_Free(lis);
2963
2964 return 0;
2965}
2966
2967 static int
2968ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2969{
2970 typval_T tv;
2971 list_T *l = self->list;
2972 listitem_T *li;
2973 Py_ssize_t length = ListLength(self);
2974
2975 if (l->lv_lock)
2976 {
2977 RAISE_LOCKED_LIST;
2978 return -1;
2979 }
2980 if (index > length || (index == length && obj == NULL))
2981 {
2982 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2983 return -1;
2984 }
2985
2986 if (obj == NULL)
2987 {
2988 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02002989 if (li == NULL)
2990 {
2991 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
2992 "list item %d"), (int) index);
2993 return -1;
2994 }
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002995 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002996 clear_tv(&li->li_tv);
2997 vim_free(li);
2998 return 0;
2999 }
3000
3001 if (ConvertFromPyObject(obj, &tv) == -1)
3002 return -1;
3003
3004 if (index == length)
3005 {
3006 if (list_append_tv(l, &tv) == FAIL)
3007 {
3008 clear_tv(&tv);
3009 PyErr_SET_VIM(N_("failed to add item to list"));
3010 return -1;
3011 }
3012 }
3013 else
3014 {
3015 li = list_find(l, (long) index);
Bram Moolenaarab589462020-07-06 21:03:06 +02003016 if (li == NULL)
3017 {
3018 PyErr_VIM_FORMAT(N_("internal error: failed to get Vim "
3019 "list item %d"), (int) index);
3020 return -1;
3021 }
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003022 clear_tv(&li->li_tv);
3023 copy_tv(&tv, &li->li_tv);
3024 clear_tv(&tv);
3025 }
3026 return 0;
3027}
3028
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003029 static int
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003030ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
3031{
3032#if PY_MAJOR_VERSION < 3
3033 if (PyInt_Check(idx))
3034 {
3035 long _idx = PyInt_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003036 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003037 }
3038 else
3039#endif
3040 if (PyLong_Check(idx))
3041 {
3042 long _idx = PyLong_AsLong(idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003043 return (int)ListAssIndex(self, _idx, obj);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003044 }
3045 else if (PySlice_Check(idx))
3046 {
3047 Py_ssize_t start, stop, step, slicelen;
3048
Bram Moolenaar922a4662014-03-30 16:11:43 +02003049 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003050 &start, &stop, &step, &slicelen) < 0)
3051 return -1;
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003052 return (int)ListAssSlice(self, start, step, slicelen,
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003053 obj);
3054 }
3055 else
3056 {
3057 RAISE_INVALID_INDEX_TYPE(idx);
3058 return -1;
3059 }
3060}
3061
3062 static PyObject *
3063ListConcatInPlace(ListObject *self, PyObject *obj)
3064{
3065 list_T *l = self->list;
3066 PyObject *lookup_dict;
3067
3068 if (l->lv_lock)
3069 {
3070 RAISE_LOCKED_LIST;
3071 return NULL;
3072 }
3073
3074 if (!(lookup_dict = PyDict_New()))
3075 return NULL;
3076
3077 if (list_py_concat(l, obj, lookup_dict) == -1)
3078 {
3079 Py_DECREF(lookup_dict);
3080 return NULL;
3081 }
3082 Py_DECREF(lookup_dict);
3083
3084 Py_INCREF(self);
3085 return (PyObject *)(self);
3086}
3087
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003088typedef struct
3089{
3090 listwatch_T lw;
3091 list_T *list;
3092} listiterinfo_T;
3093
3094 static void
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003095ListIterDestruct(void *arg)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003096{
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003097 listiterinfo_T *lii = (listiterinfo_T*)arg;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003098 list_rem_watch(lii->list, &lii->lw);
Bram Moolenaar21578272021-02-21 19:12:47 +01003099 list_unref(lii->list);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003100 PyMem_Free(lii);
3101}
3102
3103 static PyObject *
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003104ListIterNext(void **arg)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003105{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003106 PyObject *ret;
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003107 listiterinfo_T **lii = (listiterinfo_T**)arg;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003108
3109 if (!((*lii)->lw.lw_item))
3110 return NULL;
3111
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003112 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003113 return NULL;
3114
3115 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
3116
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003117 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003118}
3119
3120 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003121ListIter(PyObject *self_obj)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003122{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003123 ListObject *self = (ListObject*)self_obj;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003124 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003125 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003126
3127 if (!(lii = PyMem_New(listiterinfo_T, 1)))
3128 {
3129 PyErr_NoMemory();
3130 return NULL;
3131 }
3132
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003133 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003134 list_add_watch(l, &lii->lw);
3135 lii->lw.lw_item = l->lv_first;
3136 lii->list = l;
Bram Moolenaar21578272021-02-21 19:12:47 +01003137 ++l->lv_refcount;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003138
3139 return IterNew(lii,
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003140 ListIterDestruct, ListIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003141 NULL, NULL, (PyObject *)self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003142}
3143
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003144static char *ListAttrs[] = {
3145 "locked",
3146 NULL
3147};
3148
3149 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003150ListDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003151{
3152 return ObjectDir(self, ListAttrs);
3153}
3154
Bram Moolenaar66b79852012-09-21 14:00:35 +02003155 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003156ListSetattr(PyObject *self_obj, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02003157{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003158 ListObject *self = (ListObject*)self_obj;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003159 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02003160 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003161 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003162 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02003163 return -1;
3164 }
3165
3166 if (strcmp(name, "locked") == 0)
3167 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003168 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02003169 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003170 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02003171 return -1;
3172 }
3173 else
3174 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003175 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02003176 if (istrue == -1)
3177 return -1;
3178 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003179 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02003180 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003181 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02003182 }
3183 return 0;
3184 }
3185 else
3186 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003187 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02003188 return -1;
3189 }
3190}
3191
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003192static PySequenceMethods ListAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003193 (lenfunc) ListLength, // sq_length, len(x)
3194 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
3195 0, // RangeRepeat, sq_repeat, x*n
3196 (PyIntArgFunc) ListIndex, // sq_item, x[i]
3197 0, // was_sq_slice, x[i:j]
3198 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
3199 0, // was_sq_ass_slice, x[i:j]=v
3200 0, // sq_contains
3201 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
3202 0, // sq_inplace_repeat
Bram Moolenaar063a46b2014-01-14 16:36:51 +01003203};
3204
3205static PyMappingMethods ListAsMapping = {
3206 /* mp_length */ (lenfunc) ListLength,
3207 /* mp_subscript */ (binaryfunc) ListItem,
3208 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
3209};
3210
Bram Moolenaardb913952012-06-29 12:54:53 +02003211static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003212 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
3213 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
3214 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003215};
3216
3217typedef struct
3218{
3219 PyObject_HEAD
3220 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003221 int argc;
3222 typval_T *argv;
3223 dict_T *self;
3224 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003225 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02003226} FunctionObject;
3227
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003228DEFINE_PY_TYPE_OBJECT(FunctionType);
Bram Moolenaardb913952012-06-29 12:54:53 +02003229
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003230#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003231 FunctionNew(FunctionTypePtr, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003232
Bram Moolenaardb913952012-06-29 12:54:53 +02003233 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003234FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003235 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02003236{
3237 FunctionObject *self;
3238
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003239 self = (FunctionObject *) Py_TYPE_GET_TP_ALLOC(subtype)(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02003240 if (self == NULL)
3241 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003242
3243 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02003244 {
Bram Moolenaara14bb7e2020-04-28 00:02:41 +02003245 if (!translated_function_exists(name, FALSE))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003246 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003247 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003248 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003249 return NULL;
3250 }
3251 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003252 }
3253 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01003254 {
3255 char_u *p;
3256
3257 if ((p = get_expanded_name(name,
3258 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003259 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003260 PyErr_FORMAT(PyExc_ValueError,
3261 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02003262 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003263 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003264
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01003265 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
3266 {
3267 char_u *np;
3268 size_t len = STRLEN(p) + 1;
3269
Bram Moolenaar51e14382019-05-25 20:21:28 +02003270 if ((np = alloc(len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01003271 {
3272 vim_free(p);
3273 return NULL;
3274 }
3275 mch_memmove(np, "<SNR>", 5);
3276 mch_memmove(np + 5, p + 3, len - 3);
3277 vim_free(p);
3278 self->name = np;
3279 }
3280 else
3281 self->name = p;
3282 }
3283
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02003284 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003285 self->argc = argc;
3286 self->argv = argv;
3287 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003288 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003289
3290 if (self->argv || self->self)
3291 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
3292
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003293 return (PyObject *)(self);
3294}
3295
3296 static PyObject *
3297FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
3298{
3299 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003300 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003301 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003302 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003303 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003304 typval_T selfdicttv;
3305 typval_T argstv;
3306 list_T *argslist = NULL;
3307 dict_T *selfdict = NULL;
3308 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003309 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003310 typval_T *argv = NULL;
3311 typval_T *curtv;
3312 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003313
Bram Moolenaar8110a092016-04-14 15:56:09 +02003314 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003315 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003316 selfdictObject = PyDict_GetItemString(kwargs, "self");
3317 if (selfdictObject != NULL)
3318 {
3319 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3320 return NULL;
3321 selfdict = selfdicttv.vval.v_dict;
3322 }
3323 argsObject = PyDict_GetItemString(kwargs, "args");
3324 if (argsObject != NULL)
3325 {
3326 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3327 {
3328 dict_unref(selfdict);
3329 return NULL;
3330 }
3331 argslist = argstv.vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003332 CHECK_LIST_MATERIALIZE(argslist);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003333
3334 argc = argslist->lv_len;
3335 if (argc != 0)
3336 {
3337 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003338 if (argv == NULL)
3339 {
3340 PyErr_NoMemory();
3341 dict_unref(selfdict);
3342 list_unref(argslist);
3343 return NULL;
3344 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003345 curtv = argv;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003346 FOR_ALL_LIST_ITEMS(argslist, li)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003347 copy_tv(&li->li_tv, curtv++);
3348 }
3349 list_unref(argslist);
3350 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003351 if (selfdict != NULL)
3352 {
3353 auto_rebind = FALSE;
3354 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3355 if (autoRebindObject != NULL)
3356 {
3357 auto_rebind = PyObject_IsTrue(autoRebindObject);
3358 if (auto_rebind == -1)
3359 {
3360 dict_unref(selfdict);
3361 list_unref(argslist);
3362 return NULL;
3363 }
3364 }
3365 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003366 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003367
Bram Moolenaar389a1792013-06-23 13:00:44 +02003368 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003369 {
3370 dict_unref(selfdict);
3371 while (argc--)
3372 clear_tv(&argv[argc]);
3373 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003374 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003375 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003376
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003377 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003378
Bram Moolenaar389a1792013-06-23 13:00:44 +02003379 PyMem_Free(name);
3380
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003381 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003382}
3383
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003384 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003385FunctionDestructor(PyObject *self_obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003386{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003387 FunctionObject *self = (FunctionObject*)self_obj;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003388 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003389 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003390 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003391 for (i = 0; i < self->argc; ++i)
3392 clear_tv(&self->argv[i]);
3393 PyMem_Free(self->argv);
3394 dict_unref(self->self);
3395 if (self->argv || self->self)
3396 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003397
3398 DESTRUCTOR_FINISH(self);
3399}
3400
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003401static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003402 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003403 NULL
3404};
3405
3406 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003407FunctionDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003408{
3409 return ObjectDir(self, FunctionAttrs);
3410}
3411
Bram Moolenaardb913952012-06-29 12:54:53 +02003412 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003413FunctionAttr(FunctionObject *self, char *name)
3414{
3415 list_T *list;
3416 int i;
3417 if (strcmp(name, "name") == 0)
3418 return PyString_FromString((char *)(self->name));
3419 else if (strcmp(name, "args") == 0)
3420 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003421 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003422 return ALWAYS_NONE;
Bram Moolenaar9f289532016-08-26 16:39:03 +02003423
Bram Moolenaar8110a092016-04-14 15:56:09 +02003424 for (i = 0; i < self->argc; ++i)
3425 list_append_tv(list, &self->argv[i]);
3426 return NEW_LIST(list);
3427 }
3428 else if (strcmp(name, "self") == 0)
3429 return self->self == NULL
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003430 ? ALWAYS_NONE
Bram Moolenaar8110a092016-04-14 15:56:09 +02003431 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003432 else if (strcmp(name, "auto_rebind") == 0)
3433 return self->auto_rebind
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02003434 ? ALWAYS_TRUE
3435 : ALWAYS_FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003436 else if (strcmp(name, "__members__") == 0)
3437 return ObjectDir(NULL, FunctionAttrs);
3438 return NULL;
3439}
3440
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003441/*
3442 * Populate partial_T given function object.
Bram Moolenaar8110a092016-04-14 15:56:09 +02003443 *
3444 * "exported" should be set to true when it is needed to construct a partial
3445 * that may be stored in a variable (i.e. may be freed by Vim).
3446 */
3447 static void
3448set_partial(FunctionObject *self, partial_T *pt, int exported)
3449{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003450 int i;
3451
3452 pt->pt_name = self->name;
3453 if (self->argv)
3454 {
3455 pt->pt_argc = self->argc;
3456 if (exported)
3457 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003458 pt->pt_argv = ALLOC_CLEAR_MULT(typval_T, self->argc);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003459 for (i = 0; i < pt->pt_argc; ++i)
3460 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3461 }
3462 else
3463 pt->pt_argv = self->argv;
3464 }
3465 else
3466 {
3467 pt->pt_argc = 0;
3468 pt->pt_argv = NULL;
3469 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003470 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003471 pt->pt_dict = self->self;
3472 if (exported && self->self)
3473 ++pt->pt_dict->dv_refcount;
3474 if (exported)
3475 pt->pt_name = vim_strsave(pt->pt_name);
3476 pt->pt_refcount = 1;
3477}
3478
3479 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003480FunctionCall(PyObject *self_obj, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003481{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003482 FunctionObject *self = (FunctionObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003483 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003484 typval_T args;
3485 typval_T selfdicttv;
3486 typval_T rettv;
3487 dict_T *selfdict = NULL;
3488 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003489 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003490 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003491 partial_T pt;
3492 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003493
Bram Moolenaar8110a092016-04-14 15:56:09 +02003494 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003495 return NULL;
3496
3497 if (kwargs != NULL)
3498 {
3499 selfdictObject = PyDict_GetItemString(kwargs, "self");
3500 if (selfdictObject != NULL)
3501 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003502 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003503 {
3504 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003505 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003506 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003507 selfdict = selfdicttv.vval.v_dict;
3508 }
3509 }
3510
Bram Moolenaar8110a092016-04-14 15:56:09 +02003511 if (self->argv || self->self)
3512 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003513 CLEAR_FIELD(pt);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003514 set_partial(self, &pt, FALSE);
3515 pt_ptr = &pt;
3516 }
3517
Bram Moolenaar71700b82013-05-15 17:49:05 +02003518 Py_BEGIN_ALLOW_THREADS
3519 Python_Lock_Vim();
3520
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003521 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003522 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003523
3524 Python_Release_Vim();
3525 Py_END_ALLOW_THREADS
3526
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003527 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003528 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003529 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003530 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003531 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003532 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003533 }
3534 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003535 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003536
Bram Moolenaardb913952012-06-29 12:54:53 +02003537 clear_tv(&args);
3538 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003539 if (selfdict != NULL)
3540 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003541
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003542 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003543}
3544
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003545 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003546FunctionRepr(PyObject *self_obj)
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003547{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003548 FunctionObject *self = (FunctionObject*)self_obj;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003549 PyObject *ret;
3550 garray_T repr_ga;
3551 int i;
3552 char_u *tofree = NULL;
3553 typval_T tv;
3554 char_u numbuf[NUMBUFLEN];
3555
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003556 ga_init2(&repr_ga, sizeof(char), 70);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003557 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3558 if (self->name)
3559 ga_concat(&repr_ga, self->name);
3560 else
3561 ga_concat(&repr_ga, (char_u *)"<NULL>");
3562 ga_append(&repr_ga, '\'');
3563 if (self->argv)
3564 {
3565 ga_concat(&repr_ga, (char_u *)", args=[");
3566 ++emsg_silent;
3567 for (i = 0; i < self->argc; i++)
3568 {
3569 if (i != 0)
3570 ga_concat(&repr_ga, (char_u *)", ");
3571 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3572 get_copyID()));
3573 vim_free(tofree);
3574 }
3575 --emsg_silent;
3576 ga_append(&repr_ga, ']');
3577 }
3578 if (self->self)
3579 {
3580 ga_concat(&repr_ga, (char_u *)", self=");
3581 tv.v_type = VAR_DICT;
3582 tv.vval.v_dict = self->self;
3583 ++emsg_silent;
3584 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3585 --emsg_silent;
3586 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003587 if (self->auto_rebind)
3588 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003589 }
3590 ga_append(&repr_ga, '>');
3591 ret = PyString_FromString((char *)repr_ga.ga_data);
3592 ga_clear(&repr_ga);
3593 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003594}
3595
Bram Moolenaardb913952012-06-29 12:54:53 +02003596static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003597 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3598 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003599};
3600
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003601/*
3602 * Options object
3603 */
3604
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003605DEFINE_PY_TYPE_OBJECT(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003606
3607typedef int (*checkfun)(void *);
3608
3609typedef struct
3610{
3611 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003612 int opt_type;
3613 void *from;
3614 checkfun Check;
3615 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003616} OptionsObject;
3617
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003618 static int
3619dummy_check(void *arg UNUSED)
3620{
3621 return 0;
3622}
3623
3624 static PyObject *
3625OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3626{
3627 OptionsObject *self;
3628
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003629 self = PyObject_GC_New(OptionsObject, OptionsTypePtr);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003630 if (self == NULL)
3631 return NULL;
3632
3633 self->opt_type = opt_type;
3634 self->from = from;
3635 self->Check = Check;
3636 self->fromObj = fromObj;
3637 if (fromObj)
3638 Py_INCREF(fromObj);
3639
3640 return (PyObject *)(self);
3641}
3642
3643 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003644OptionsDestructor(PyObject *self_obj)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003645{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003646 OptionsObject *self = (OptionsObject*)self_obj;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003647 PyObject_GC_UnTrack((void *)(self));
3648 Py_XDECREF(self->fromObj);
3649 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003650}
3651
3652 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003653OptionsTraverse(PyObject *self_obj, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003654{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003655 OptionsObject *self = (OptionsObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003656 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003657 return 0;
3658}
3659
3660 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003661OptionsClear(PyObject *self_obj)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003662{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003663 OptionsObject *self = (OptionsObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003664 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003665 return 0;
3666}
3667
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003668 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003669OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003670{
3671 char_u *key;
3672 int flags;
3673 long numval;
3674 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003675 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003676
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003677 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003678 return NULL;
3679
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003680 if (!(key = StringToChars(keyObject, &todecref)))
3681 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003682
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003683 if (*key == NUL)
3684 {
3685 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003686 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003687 return NULL;
3688 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003689
3690 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003691 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003692
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003693 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003694
3695 if (flags == 0)
3696 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003697 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003698 return NULL;
3699 }
3700
3701 if (flags & SOPT_UNSET)
3702 {
3703 Py_INCREF(Py_None);
3704 return Py_None;
3705 }
3706 else if (flags & SOPT_BOOL)
3707 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003708 PyObject *ret;
3709 ret = numval ? Py_True : Py_False;
3710 Py_INCREF(ret);
3711 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003712 }
3713 else if (flags & SOPT_NUM)
3714 return PyInt_FromLong(numval);
3715 else if (flags & SOPT_STRING)
3716 {
3717 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003718 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003719 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003720 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003721 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003722 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003723 else
3724 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003725 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003726 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003727 return NULL;
3728 }
3729 }
3730 else
3731 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003732 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003733 return NULL;
3734 }
3735}
3736
3737 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003738OptionsContains(OptionsObject *self, PyObject *keyObject)
3739{
3740 char_u *key;
3741 PyObject *todecref;
3742
3743 if (!(key = StringToChars(keyObject, &todecref)))
3744 return -1;
3745
3746 if (*key == NUL)
3747 {
3748 Py_XDECREF(todecref);
3749 return 0;
3750 }
3751
3752 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3753 {
3754 Py_XDECREF(todecref);
3755 return 1;
3756 }
3757 else
3758 {
3759 Py_XDECREF(todecref);
3760 return 0;
3761 }
3762}
3763
3764typedef struct
3765{
3766 void *lastoption;
3767 int opt_type;
3768} optiterinfo_T;
3769
3770 static PyObject *
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003771OptionsIterNext(void **arg)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003772{
3773 char_u *name;
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003774 optiterinfo_T **oii = (optiterinfo_T**)arg;
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003775
3776 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3777 return PyString_FromString((char *)name);
3778
3779 return NULL;
3780}
3781
3782 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003783OptionsIter(PyObject *self_obj)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003784{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02003785 OptionsObject *self = (OptionsObject*)self_obj;
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003786 optiterinfo_T *oii;
3787
3788 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3789 {
3790 PyErr_NoMemory();
3791 return NULL;
3792 }
3793
3794 oii->opt_type = self->opt_type;
3795 oii->lastoption = NULL;
3796
3797 return IterNew(oii,
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02003798 PyMem_Free, OptionsIterNext,
Bram Moolenaar423a85a2020-08-29 12:57:16 +02003799 NULL, NULL, (PyObject *)self);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003800}
3801
3802 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003803set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003804{
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003805 char *errmsg;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003806
3807 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3808 {
3809 if (VimTryEnd())
3810 return FAIL;
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003811 PyErr_SetVim(errmsg);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003812 return FAIL;
3813 }
3814 return OK;
3815}
3816
3817 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003818set_option_value_for(
3819 char_u *key,
3820 int numval,
3821 char_u *stringval,
3822 int opt_flags,
3823 int opt_type,
3824 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003825{
Bram Moolenaar18f47402022-01-06 13:24:51 +00003826 switchwin_T switchwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003827 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003828 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003829
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003830 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003831 switch (opt_type)
3832 {
3833 case SREQ_WIN:
Bram Moolenaar18f47402022-01-06 13:24:51 +00003834 if (switch_win(&switchwin, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003835 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003836 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00003837 restore_win(&switchwin, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003838 if (VimTryEnd())
3839 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003840 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003841 return -1;
3842 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003843 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar18f47402022-01-06 13:24:51 +00003844 restore_win(&switchwin, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003845 break;
3846 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003847 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003848 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003849 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003850 break;
3851 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003852 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003853 break;
3854 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003855 if (set_ret == FAIL)
3856 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003857 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003858}
3859
3860 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003861OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003862{
3863 char_u *key;
3864 int flags;
3865 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003866 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003867 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003868
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02003869 if (self->Check(self->fromObj))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003870 return -1;
3871
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003872 if (!(key = StringToChars(keyObject, &todecref)))
3873 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003874
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003875 if (*key == NUL)
3876 {
3877 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003878 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003879 return -1;
3880 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003881
3882 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003883 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003884
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003885 if (flags == 0)
3886 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003887 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003888 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003889 return -1;
3890 }
3891
3892 if (valObject == NULL)
3893 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003894 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003895 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003896 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003897 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003898 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003899 return -1;
3900 }
3901 else if (!(flags & SOPT_GLOBAL))
3902 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003903 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003904 N_("unable to unset option %s "
3905 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003906 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003907 return -1;
3908 }
3909 else
3910 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003911 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003912 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003913 return 0;
3914 }
3915 }
3916
Bram Moolenaard6e39182013-05-21 18:30:34 +02003917 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003918
3919 if (flags & SOPT_BOOL)
3920 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003921 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003922
Bram Moolenaarb983f752013-05-15 16:11:50 +02003923 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003924 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003925 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003926 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003927 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003928 }
3929 else if (flags & SOPT_NUM)
3930 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003931 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003932
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003933 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003934 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003935 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003936 return -1;
3937 }
3938
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003939 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003940 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003941 }
3942 else
3943 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003944 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003945 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003946
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003947 if ((val = StringToChars(valObject, &todecref2)))
3948 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003949 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003950 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003951 Py_XDECREF(todecref2);
3952 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003953 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003954 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003955 }
3956
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003957 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003958
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003959 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003960}
3961
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003962static PySequenceMethods OptionsAsSeq = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003963 0, // sq_length
3964 0, // sq_concat
3965 0, // sq_repeat
3966 0, // sq_item
3967 0, // sq_slice
3968 0, // sq_ass_item
3969 0, // sq_ass_slice
3970 (objobjproc) OptionsContains, // sq_contains
3971 0, // sq_inplace_concat
3972 0, // sq_inplace_repeat
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003973};
3974
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003975static PyMappingMethods OptionsAsMapping = {
3976 (lenfunc) NULL,
3977 (binaryfunc) OptionsItem,
3978 (objobjargproc) OptionsAssItem,
3979};
3980
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01003981// Tabpage object
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003982
3983typedef struct
3984{
3985 PyObject_HEAD
3986 tabpage_T *tab;
3987} TabPageObject;
3988
3989static PyObject *WinListNew(TabPageObject *tabObject);
3990
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02003991DEFINE_PY_TYPE_OBJECT(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003992
3993 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003994CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003995{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003997 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003998 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003999 return -1;
4000 }
4001
4002 return 0;
4003}
4004
4005 static PyObject *
4006TabPageNew(tabpage_T *tab)
4007{
4008 TabPageObject *self;
4009
4010 if (TAB_PYTHON_REF(tab))
4011 {
4012 self = TAB_PYTHON_REF(tab);
4013 Py_INCREF(self);
4014 }
4015 else
4016 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004017 self = PyObject_NEW(TabPageObject, TabPageTypePtr);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004018 if (self == NULL)
4019 return NULL;
4020 self->tab = tab;
4021 TAB_PYTHON_REF(tab) = self;
4022 }
4023
4024 return (PyObject *)(self);
4025}
4026
4027 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004028TabPageDestructor(PyObject *self_obj)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004029{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004030 TabPageObject *self = (TabPageObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004031 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
4032 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004033
4034 DESTRUCTOR_FINISH(self);
4035}
4036
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004037static char *TabPageAttrs[] = {
4038 "windows", "number", "vars", "window", "valid",
4039 NULL
4040};
4041
4042 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02004043TabPageDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004044{
4045 return ObjectDir(self, TabPageAttrs);
4046}
4047
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004048 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004049TabPageAttrValid(TabPageObject *self, char *name)
4050{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004051 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004052
4053 if (strcmp(name, "valid") != 0)
4054 return NULL;
4055
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004056 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
4057 Py_INCREF(ret);
4058 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004059}
4060
4061 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004062TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004063{
4064 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004065 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004066 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004067 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004068 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004069 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004070 else if (strcmp(name, "window") == 0)
4071 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004072 // For current tab window.c does not bother to set or update tp_curwin
Bram Moolenaard6e39182013-05-21 18:30:34 +02004073 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004074 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004075 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02004076 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004077 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004078 else if (strcmp(name, "__members__") == 0)
4079 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004080 return NULL;
4081}
4082
4083 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004084TabPageRepr(PyObject *self_obj)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004085{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004086 TabPageObject *self = (TabPageObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004087 if (self->tab == INVALID_TABPAGE_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00004088 return PyString_FromFormat("<tabpage object (deleted) at %p>", (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004089 else
4090 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004091 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004092
4093 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004094 return PyString_FromFormat("<tabpage object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00004095 (void *)self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004096 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004097 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004098 }
4099}
4100
4101static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004102 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004103 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
4104 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004105};
4106
4107/*
4108 * Window list object
4109 */
4110
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004111DEFINE_PY_TYPE_OBJECT(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004112static PySequenceMethods TabListAsSeq;
4113
4114typedef struct
4115{
4116 PyObject_HEAD
4117} TabListObject;
4118
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004119static TabListObject TheTabPageList =
4120{
4121 PyObject_HEAD_INIT_TYPE(TabListType)
4122};
4123
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004124 static PyInt
4125TabListLength(PyObject *self UNUSED)
4126{
4127 tabpage_T *tp = first_tabpage;
4128 PyInt n = 0;
4129
4130 while (tp != NULL)
4131 {
4132 ++n;
4133 tp = tp->tp_next;
4134 }
4135
4136 return n;
4137}
4138
4139 static PyObject *
4140TabListItem(PyObject *self UNUSED, PyInt n)
4141{
4142 tabpage_T *tp;
4143
4144 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
4145 if (n == 0)
4146 return TabPageNew(tp);
4147
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004148 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004149 return NULL;
4150}
4151
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02004152/*
4153 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004154 */
4155
4156typedef struct
4157{
4158 PyObject_HEAD
4159 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004160 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004161} WindowObject;
4162
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004163DEFINE_PY_TYPE_OBJECT(WindowType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164
4165 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004166CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004168 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004169 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004170 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004171 return -1;
4172 }
4173
4174 return 0;
4175}
4176
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02004177 static int
4178CheckWindowCb(void *self)
4179{
4180 return CheckWindow((WindowObject*)self);
4181}
4182
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004183 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004184WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02004185{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004186 /*
4187 * We need to handle deletion of windows underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02004188 * If we add a "w_python*_ref" field to the win_T structure,
4189 * then we can get at it in win_free() in vim. We then
4190 * need to create only ONE Python object per window - if
4191 * we try to create a second, just INCREF the existing one
4192 * and return it. The (single) Python object referring to
4193 * the window is stored in "w_python*_ref".
4194 * On a win_free() we set the Python object's win_T* field
4195 * to an invalid value. We trap all uses of a window
4196 * object, and reject them if the win_T* field is invalid.
4197 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004198 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004199 * w_python_ref and w_python3_ref fields respectively.
4200 */
4201
4202 WindowObject *self;
4203
4204 if (WIN_PYTHON_REF(win))
4205 {
4206 self = WIN_PYTHON_REF(win);
4207 Py_INCREF(self);
4208 }
4209 else
4210 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004211 self = PyObject_GC_New(WindowObject, WindowTypePtr);
Bram Moolenaar971db462013-05-12 18:44:48 +02004212 if (self == NULL)
4213 return NULL;
4214 self->win = win;
4215 WIN_PYTHON_REF(win) = self;
4216 }
4217
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004218 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
4219
Bram Moolenaar971db462013-05-12 18:44:48 +02004220 return (PyObject *)(self);
4221}
4222
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004223 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004224WindowDestructor(PyObject *self_obj)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004225{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004226 WindowObject *self = (WindowObject*)self_obj;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004227 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02004228 if (self->win && self->win != INVALID_WINDOW_VALUE)
4229 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaarab589462020-07-06 21:03:06 +02004230 Py_XDECREF(((PyObject *)(self->tabObject)));
Bram Moolenaar774267b2013-05-21 20:51:59 +02004231 PyObject_GC_Del((void *)(self));
4232}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004233
Bram Moolenaar774267b2013-05-21 20:51:59 +02004234 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004235WindowTraverse(PyObject *self_obj, visitproc visit, void *arg)
Bram Moolenaar774267b2013-05-21 20:51:59 +02004236{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004237 WindowObject *self = (WindowObject*)self_obj;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004238 Py_VISIT(((PyObject *)(self->tabObject)));
4239 return 0;
4240}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004241
Bram Moolenaar774267b2013-05-21 20:51:59 +02004242 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004243WindowClear(PyObject *self_obj)
Bram Moolenaar774267b2013-05-21 20:51:59 +02004244{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004245 WindowObject *self = (WindowObject*)self_obj;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004246 Py_CLEAR(self->tabObject);
4247 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004248}
4249
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004250 static win_T *
4251get_firstwin(TabPageObject *tabObject)
4252{
4253 if (tabObject)
4254 {
4255 if (CheckTabPage(tabObject))
4256 return NULL;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004257 // For current tab window.c does not bother to set or update tp_firstwin
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004258 else if (tabObject->tab == curtab)
4259 return firstwin;
4260 else
4261 return tabObject->tab->tp_firstwin;
4262 }
4263 else
4264 return firstwin;
4265}
Bram Moolenaare950f992018-06-10 13:55:55 +02004266
4267// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004268static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02004269 "buffer",
4270 "cursor",
4271 "height",
4272 "row",
4273 "width",
4274 "col",
4275 "vars",
4276 "options",
4277 "number",
4278 "tabpage",
4279 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004280 NULL
4281};
4282
4283 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02004284WindowDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004285{
4286 return ObjectDir(self, WindowAttrs);
4287}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004288
Bram Moolenaar971db462013-05-12 18:44:48 +02004289 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004290WindowAttrValid(WindowObject *self, char *name)
4291{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004292 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004293
4294 if (strcmp(name, "valid") != 0)
4295 return NULL;
4296
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004297 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
4298 Py_INCREF(ret);
4299 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004300}
4301
4302 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004303WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004304{
4305 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004306 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004307 else if (strcmp(name, "cursor") == 0)
4308 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004309 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004310
4311 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
4312 }
4313 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004314 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004315 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004316 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004317 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02004318 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02004319 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004320 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02004321 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004322 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004323 else if (strcmp(name, "options") == 0)
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02004324 return OptionsNew(SREQ_WIN, self->win, CheckWindowCb,
Bram Moolenaard6e39182013-05-21 18:30:34 +02004325 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02004326 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004327 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004328 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004329 return NULL;
4330 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004331 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004332 }
4333 else if (strcmp(name, "tabpage") == 0)
4334 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004335 Py_INCREF(self->tabObject);
4336 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004337 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004338 else if (strcmp(name, "__members__") == 0)
4339 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004340 else
4341 return NULL;
4342}
4343
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004344 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004345WindowSetattr(PyObject *self_obj, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004346{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004347 WindowObject *self = (WindowObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004348 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004349 return -1;
4350
4351 if (strcmp(name, "buffer") == 0)
4352 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004353 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004354 return -1;
4355 }
4356 else if (strcmp(name, "cursor") == 0)
4357 {
4358 long lnum;
4359 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004360
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004361 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004362 return -1;
4363
Bram Moolenaard6e39182013-05-21 18:30:34 +02004364 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004365 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004366 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004367 return -1;
4368 }
4369
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004370 // Check for keyboard interrupts
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004371 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004372 return -1;
4373
Bram Moolenaard6e39182013-05-21 18:30:34 +02004374 self->win->w_cursor.lnum = lnum;
4375 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004376 self->win->w_set_curswant = TRUE;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004377 self->win->w_cursor.coladd = 0;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004378 // When column is out of range silently correct it.
Bram Moolenaard6e39182013-05-21 18:30:34 +02004379 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004380
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004381 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004382 return 0;
4383 }
4384 else if (strcmp(name, "height") == 0)
4385 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004386 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004387 win_T *savewin;
4388
Bram Moolenaardee2e312013-06-23 16:35:47 +02004389 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 return -1;
4391
4392#ifdef FEAT_GUI
4393 need_mouse_correct = TRUE;
4394#endif
4395 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004396 curwin = self->win;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004397 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004398
4399 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004400 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 curwin = savewin;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004402 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004403 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004404 return -1;
4405
4406 return 0;
4407 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004408 else if (strcmp(name, "width") == 0)
4409 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004410 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004411 win_T *savewin;
4412
Bram Moolenaardee2e312013-06-23 16:35:47 +02004413 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004414 return -1;
4415
4416#ifdef FEAT_GUI
4417 need_mouse_correct = TRUE;
4418#endif
4419 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004420 curwin = self->win;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004421 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004422
4423 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004424 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425 curwin = savewin;
Bram Moolenaar6c87bbb2022-12-10 11:17:11 +00004426 curbuf = curwin->w_buffer;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004427 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428 return -1;
4429
4430 return 0;
4431 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 else
4433 {
4434 PyErr_SetString(PyExc_AttributeError, name);
4435 return -1;
4436 }
4437}
4438
4439 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004440WindowRepr(PyObject *self_obj)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02004442 WindowObject *self = (WindowObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004443 if (self->win == INVALID_WINDOW_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00004444 return PyString_FromFormat("<window object (deleted) at %p>", (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 else
4446 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004447 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448
Bram Moolenaar6d216452013-05-12 19:00:41 +02004449 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004450 return PyString_FromFormat("<window object (unknown) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00004451 (void *)self);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004452 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004453 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004454 }
4455}
4456
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004457static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004458 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004459 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4460 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004461};
4462
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004463/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004464 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004466
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004467DEFINE_PY_TYPE_OBJECT(WinListType);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004468static PySequenceMethods WinListAsSeq;
4469
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004470typedef struct
4471{
4472 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004473 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004474} WinListObject;
4475
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004476static WinListObject TheWindowList =
4477{
4478 PyObject_HEAD_INIT_TYPE(WinListType)
4479 NULL
4480};
4481
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004482 static PyObject *
4483WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004484{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004485 WinListObject *self;
4486
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004487 self = PyObject_NEW(WinListObject, WinListTypePtr);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004488 self->tabObject = tabObject;
4489 Py_INCREF(tabObject);
4490
4491 return (PyObject *)(self);
4492}
4493
4494 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004495WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004496{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004497 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004498
4499 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004500 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004501 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004502 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004503
4504 DESTRUCTOR_FINISH(self);
4505}
4506
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004507 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004508WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004509{
4510 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004511 PyInt n = 0;
4512
Bram Moolenaard6e39182013-05-21 18:30:34 +02004513 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004514 return -1;
4515
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004516 while (w != NULL)
4517 {
4518 ++n;
4519 w = W_NEXT(w);
4520 }
4521
4522 return n;
4523}
4524
4525 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004526WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004527{
4528 win_T *w;
4529
Bram Moolenaard6e39182013-05-21 18:30:34 +02004530 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004531 return NULL;
4532
4533 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004534 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004535 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004536
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004537 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004538 return NULL;
4539}
4540
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004541/*
4542 * Convert a Python string into a Vim line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004543 *
4544 * The result is in allocated memory. All internal nulls are replaced by
4545 * newline characters. It is an error for the string to contain newline
4546 * characters.
4547 *
4548 * On errors, the Python exception data is set, and NULL is returned.
4549 */
4550 static char *
4551StringToLine(PyObject *obj)
4552{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004553 char *str;
4554 char *save;
4555 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004556 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004557 PyInt i;
4558 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004559
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004560 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004561 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004562 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4563 || str == NULL)
4564 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004565 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004566 else if (PyUnicode_Check(obj))
4567 {
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01004568 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT,
4569 ERRORS_ENCODE_ARG)))
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004570 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004571
Bram Moolenaardaa27022013-06-24 22:33:30 +02004572 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004573 || str == NULL)
4574 {
4575 Py_DECREF(bytes);
4576 return NULL;
4577 }
4578 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004579 else
4580 {
4581#if PY_MAJOR_VERSION < 3
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004582 PyErr_FORMAT_TYPE(
Bram Moolenaardaa27022013-06-24 22:33:30 +02004583 N_("expected str() or unicode() instance, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004584 obj);
Bram Moolenaardaa27022013-06-24 22:33:30 +02004585#else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004586 PyErr_FORMAT_TYPE(
Bram Moolenaardaa27022013-06-24 22:33:30 +02004587 N_("expected bytes() or str() instance, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02004588 obj);
Bram Moolenaardaa27022013-06-24 22:33:30 +02004589#endif
4590 return NULL;
4591 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004592
4593 /*
4594 * Error checking: String must not contain newlines, as we
4595 * are replacing a single line, and we must replace it with
4596 * a single line.
4597 * A trailing newline is removed, so that append(f.readlines()) works.
4598 */
4599 p = memchr(str, '\n', len);
4600 if (p != NULL)
4601 {
4602 if (p == str + len - 1)
4603 --len;
4604 else
4605 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004606 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004607 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004608 return NULL;
4609 }
4610 }
4611
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004612 /*
4613 * Create a copy of the string, with internal nulls replaced by
Bram Moolenaar86181df2020-05-11 23:14:04 +02004614 * newline characters, as is the Vim convention.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004615 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004616 save = alloc(len+1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004617 if (save == NULL)
4618 {
4619 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004620 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004621 return NULL;
4622 }
4623
4624 for (i = 0; i < len; ++i)
4625 {
4626 if (str[i] == '\0')
4627 save[i] = '\n';
4628 else
4629 save[i] = str[i];
4630 }
4631
4632 save[i] = '\0';
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004633 Py_XDECREF(bytes); // Python 2 does nothing here
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004634
4635 return save;
4636}
4637
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004638/*
4639 * Get a line from the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004640 * in Vim format (1-based). The line is returned as a Python
4641 * string object.
4642 */
4643 static PyObject *
4644GetBufferLine(buf_T *buf, PyInt n)
4645{
4646 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4647}
4648
4649
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004650/*
4651 * Get a list of lines from the specified buffer. The line numbers
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652 * are in Vim format (1-based). The range is from lo up to, but not
4653 * including, hi. The list is returned as a Python list of string objects.
4654 */
4655 static PyObject *
4656GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4657{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004658 PyInt i;
4659 PyInt n = hi - lo;
4660 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004661
4662 if (list == NULL)
4663 return NULL;
4664
4665 for (i = 0; i < n; ++i)
4666 {
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004667 linenr_T lnum = (linenr_T)(lo + i);
4668 char *text;
4669 PyObject *string;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004670
Bram Moolenaarbb790dc2020-07-07 20:12:54 +02004671 if (lnum > buf->b_ml.ml_line_count)
4672 text = "";
4673 else
4674 text = (char *)ml_get_buf(buf, lnum, FALSE);
4675 string = LineToString(text);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004676 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677 {
4678 Py_DECREF(list);
4679 return NULL;
4680 }
4681
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004682 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683 }
4684
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004685 // The ownership of the Python list is passed to the caller (ie,
4686 // the caller should Py_DECREF() the object when it is finished
4687 // with it).
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004688
4689 return list;
4690}
4691
4692/*
4693 * Check if deleting lines made the cursor position invalid.
4694 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4695 * deleted).
4696 */
4697 static void
4698py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4699{
4700 if (curwin->w_cursor.lnum >= lo)
4701 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004702 // Adjust the cursor position if it's in/after the changed
4703 // lines.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004704 if (curwin->w_cursor.lnum >= hi)
4705 {
4706 curwin->w_cursor.lnum += extra;
4707 check_cursor_col();
4708 }
4709 else if (extra < 0)
4710 {
4711 curwin->w_cursor.lnum = lo;
4712 check_cursor();
4713 }
4714 else
4715 check_cursor_col();
4716 changed_cline_bef_curs();
4717 }
4718 invalidate_botline();
4719}
4720
Bram Moolenaar19e60942011-06-19 00:27:51 +02004721/*
4722 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723 * in Vim format (1-based). The replacement line is given as
4724 * a Python string object. The object is checked for validity
4725 * and correct format. Errors are returned as a value of FAIL.
4726 * The return value is OK on success.
4727 * If OK is returned and len_change is not NULL, *len_change
4728 * is set to the change in the buffer length.
4729 */
4730 static int
4731SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4732{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004733 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004734 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004735
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004736 // First of all, we check the type of the supplied Python object.
4737 // There are three cases:
4738 // 1. NULL, or None - this is a deletion.
4739 // 2. A string - this is a replacement.
4740 // 3. Anything else - this is an error.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741 if (line == Py_None || line == NULL)
4742 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004743 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004744 switchwin.sw_curwin = NULL;
4745 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004746
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004747 VimTryStart();
4748
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004750 RAISE_UNDO_FAIL;
Bram Moolenaarca70c072020-05-30 20:30:46 +02004751 else if (ml_delete((linenr_T)n) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004752 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004753 else
4754 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00004755 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01004756 || save_curbuf.br_buf == NULL))
4757 // Using an existing window for the buffer, adjust the cursor
4758 // position.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004759 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004760 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004761 // Only adjust marks if we managed to switch to a window that
4762 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004763 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004764 }
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 Moolenaara7b64ce2013-05-21 20:40:40 +02004768 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004769 return FAIL;
4770
4771 if (len_change)
4772 *len_change = -1;
4773
4774 return OK;
4775 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004776 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004777 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004778 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779
4780 if (save == NULL)
4781 return FAIL;
4782
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004783 VimTryStart();
4784
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004785 // We do not need to free "save" if ml_replace() consumes it.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 PyErr_Clear();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004787 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004788
4789 if (u_savesub((linenr_T)n) == FAIL)
4790 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004791 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004792 vim_free(save);
4793 }
4794 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4795 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004796 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004797 vim_free(save);
4798 }
4799 else
4800 changed_bytes((linenr_T)n, 0);
4801
Bram Moolenaar18f47402022-01-06 13:24:51 +00004802 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004803
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004804 // Check that the cursor is not beyond the end of the line now.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004805 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004806 check_cursor_col();
4807
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004808 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004809 return FAIL;
4810
4811 if (len_change)
4812 *len_change = 0;
4813
4814 return OK;
4815 }
4816 else
4817 {
4818 PyErr_BadArgument();
4819 return FAIL;
4820 }
4821}
4822
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004823/*
4824 * Replace a range of lines in the specified buffer. The line numbers are in
Bram Moolenaar19e60942011-06-19 00:27:51 +02004825 * Vim format (1-based). The range is from lo up to, but not including, hi.
4826 * The replacement lines are given as a Python list of string objects. The
4827 * list is checked for validity and correct format. Errors are returned as a
4828 * value of FAIL. The return value is OK on success.
4829 * If OK is returned and len_change is not NULL, *len_change
4830 * is set to the change in the buffer length.
4831 */
4832 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004833SetBufferLineList(
4834 buf_T *buf,
4835 PyInt lo,
4836 PyInt hi,
4837 PyObject *list,
4838 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004839{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004840 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00004841 switchwin_T switchwin;
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004842
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004843 // First of all, we check the type of the supplied Python object.
4844 // There are three cases:
4845 // 1. NULL, or None - this is a deletion.
4846 // 2. A list - this is a replacement.
4847 // 3. Anything else - this is an error.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004848 if (list == Py_None || list == NULL)
4849 {
4850 PyInt i;
4851 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004852
4853 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004854 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00004855 switchwin.sw_curwin = NULL;
4856 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004857
4858 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004859 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004860 else
4861 {
4862 for (i = 0; i < n; ++i)
4863 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02004864 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004865 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004866 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004867 break;
4868 }
4869 }
Bram Moolenaar18f47402022-01-06 13:24:51 +00004870 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004871 || save_curbuf.br_buf == NULL))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004872 // Using an existing window for the buffer, adjust the cursor
4873 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004874 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004875 if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004876 // Only adjust marks if we managed to switch to a window that
4877 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004878 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004879 }
4880
Bram Moolenaar18f47402022-01-06 13:24:51 +00004881 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004882
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004883 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004884 return FAIL;
4885
4886 if (len_change)
4887 *len_change = -n;
4888
4889 return OK;
4890 }
4891 else if (PyList_Check(list))
4892 {
4893 PyInt i;
4894 PyInt new_len = PyList_Size(list);
4895 PyInt old_len = hi - lo;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004896 PyInt extra = 0; // lines added to text, can be negative
Bram Moolenaar19e60942011-06-19 00:27:51 +02004897 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004898
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004899 if (new_len == 0) // avoid allocating zero bytes
Bram Moolenaar19e60942011-06-19 00:27:51 +02004900 array = NULL;
4901 else
4902 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004903 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004904 if (array == NULL)
4905 {
4906 PyErr_NoMemory();
4907 return FAIL;
4908 }
4909 }
4910
4911 for (i = 0; i < new_len; ++i)
4912 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004913 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004914
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004915 if (!(line = PyList_GetItem(list, i)) ||
4916 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004917 {
4918 while (i)
4919 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004920 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004921 return FAIL;
4922 }
4923 }
4924
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004925 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004926 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004927
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004928 // START of region without "return". Must call restore_buffer()!
Bram Moolenaar18f47402022-01-06 13:24:51 +00004929 switchwin.sw_curwin = NULL;
4930 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004931
4932 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004933 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004934
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004935 // If the size of the range is reducing (ie, new_len < old_len) we
4936 // need to delete some old_len. We do this at the start, by
4937 // repeatedly deleting line "lo".
Bram Moolenaar19e60942011-06-19 00:27:51 +02004938 if (!PyErr_Occurred())
4939 {
4940 for (i = 0; i < old_len - new_len; ++i)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004941 if (ml_delete((linenr_T)lo) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004942 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004943 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004944 break;
4945 }
4946 extra -= i;
4947 }
4948
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004949 // For as long as possible, replace the existing old_len with the
4950 // new old_len. This is a more efficient operation, as it requires
4951 // less memory allocation and freeing.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004952 if (!PyErr_Occurred())
4953 {
4954 for (i = 0; i < old_len && i < new_len; ++i)
4955 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4956 == FAIL)
4957 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004958 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004959 break;
4960 }
4961 }
4962 else
4963 i = 0;
4964
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004965 // Now we may need to insert the remaining new old_len. If we do, we
4966 // must free the strings as we finish with them (we can't pass the
Bram Moolenaar86181df2020-05-11 23:14:04 +02004967 // responsibility to Vim in this case).
Bram Moolenaar19e60942011-06-19 00:27:51 +02004968 if (!PyErr_Occurred())
4969 {
4970 while (i < new_len)
4971 {
4972 if (ml_append((linenr_T)(lo + i - 1),
4973 (char_u *)array[i], 0, FALSE) == FAIL)
4974 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004975 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004976 break;
4977 }
4978 vim_free(array[i]);
4979 ++i;
4980 ++extra;
4981 }
4982 }
4983
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004984 // Free any left-over old_len, as a result of an error
Bram Moolenaar19e60942011-06-19 00:27:51 +02004985 while (i < new_len)
4986 {
4987 vim_free(array[i]);
4988 ++i;
4989 }
4990
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004991 // Free the array of old_len. All of its contents have now
4992 // been dealt with (either freed, or the responsibility passed
4993 // to vim.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004994 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004995
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01004996 // Adjust marks. Invalidate any which lie in the
4997 // changed range, and move any in the remainder of the buffer.
4998 // Only adjust marks if we managed to switch to a window that holds
4999 // the buffer, otherwise line numbers will be invalid.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005000 if (save_curbuf.br_buf == NULL)
Bram Moolenaar37233f62022-05-22 12:23:48 +01005001 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02005002 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02005003 (long)MAXLNUM, (long)extra);
Bram Moolenaar37233f62022-05-22 12:23:48 +01005004 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
5005 }
Bram Moolenaar19e60942011-06-19 00:27:51 +02005006
Bram Moolenaar18f47402022-01-06 13:24:51 +00005007 if (buf == curbuf && (switchwin.sw_curwin != NULL
Bram Moolenaar63dbfd32019-03-23 17:41:59 +01005008 || save_curbuf.br_buf == NULL))
5009 // Using an existing window for the buffer, adjust the cursor
5010 // position.
Bram Moolenaar19e60942011-06-19 00:27:51 +02005011 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
5012
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005013 // END of region without "return".
Bram Moolenaar18f47402022-01-06 13:24:51 +00005014 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02005015
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005016 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02005017 return FAIL;
5018
5019 if (len_change)
5020 *len_change = new_len - old_len;
5021
5022 return OK;
5023 }
5024 else
5025 {
5026 PyErr_BadArgument();
5027 return FAIL;
5028 }
5029}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005030
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005031/*
5032 * Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005033 * The line number is in Vim format (1-based). The lines to be inserted are
5034 * given as a Python list of string objects or as a single string. The lines
5035 * to be added are checked for validity and correct format. Errors are
5036 * returned as a value of FAIL. The return value is OK on success.
5037 * If OK is returned and len_change is not NULL, *len_change
5038 * is set to the change in the buffer length.
5039 */
5040 static int
5041InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
5042{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02005043 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar18f47402022-01-06 13:24:51 +00005044 switchwin_T switchwin;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02005045
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005046 // First of all, we check the type of the supplied Python object.
5047 // It must be a string or a list, or the call is in error.
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005048 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005049 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02005050 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005051
5052 if (str == NULL)
5053 return FAIL;
5054
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005055 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005056 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00005057 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005058
Bram Moolenaar95064ec2013-07-17 17:15:25 +02005059 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02005060 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005061 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005062 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005063 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005064 // Only adjust marks if we managed to switch to a window that
5065 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005066 appended_lines_mark((linenr_T)n, 1L);
5067
5068 vim_free(str);
Bram Moolenaar18f47402022-01-06 13:24:51 +00005069 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005070 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005071
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005072 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005073 return FAIL;
5074
5075 if (len_change)
5076 *len_change = 1;
5077
5078 return OK;
5079 }
5080 else if (PyList_Check(lines))
5081 {
5082 PyInt i;
5083 PyInt size = PyList_Size(lines);
5084 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005085
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005086 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005087 if (array == NULL)
5088 {
5089 PyErr_NoMemory();
5090 return FAIL;
5091 }
5092
5093 for (i = 0; i < size; ++i)
5094 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005095 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005096
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005097 if (!(line = PyList_GetItem(lines, i)) ||
5098 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005099 {
5100 while (i)
5101 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005102 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005103 return FAIL;
5104 }
5105 }
5106
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005107 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005108 VimTryStart();
Bram Moolenaar18f47402022-01-06 13:24:51 +00005109 switch_to_win_for_buf(buf, &switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005110
5111 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02005112 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005113 else
5114 {
5115 for (i = 0; i < size; ++i)
5116 {
5117 if (ml_append((linenr_T)(n + i),
5118 (char_u *)array[i], 0, FALSE) == FAIL)
5119 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005120 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005121
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005122 // Free the rest of the lines
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005123 while (i < size)
5124 vim_free(array[i++]);
5125
5126 break;
5127 }
5128 vim_free(array[i]);
5129 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005130 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005131 // Only adjust marks if we managed to switch to a window that
5132 // holds the buffer, otherwise line numbers will be invalid.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005133 appended_lines_mark((linenr_T)n, (long)i);
5134 }
5135
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005136 // Free the array of lines. All of its contents have now
5137 // been freed.
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005138 PyMem_Free(array);
Bram Moolenaar18f47402022-01-06 13:24:51 +00005139 restore_win_for_buf(&switchwin, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005140
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005141 update_screen(UPD_VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005142
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005143 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005144 return FAIL;
5145
5146 if (len_change)
5147 *len_change = size;
5148
5149 return OK;
5150 }
5151 else
5152 {
5153 PyErr_BadArgument();
5154 return FAIL;
5155 }
5156}
5157
5158/*
5159 * Common routines for buffers and line ranges
5160 * -------------------------------------------
5161 */
5162
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005163typedef struct
5164{
5165 PyObject_HEAD
5166 buf_T *buf;
5167} BufferObject;
5168
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005169 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02005170CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005171{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005173 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005174 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005175 return -1;
5176 }
5177
5178 return 0;
5179}
5180
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005181 static int
5182CheckBufferCb(void *self)
5183{
5184 return CheckBuffer((BufferObject*)self);
5185}
5186
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005187 static PyObject *
5188RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
5189{
5190 if (CheckBuffer(self))
5191 return NULL;
5192
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005193 if (end == -1)
5194 end = self->buf->b_ml.ml_line_count;
5195
Bram Moolenaarbd80f352013-05-12 21:16:23 +02005196 if (n < 0)
5197 n += end - start + 1;
5198
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005199 if (n < 0 || n > end - start)
5200 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005201 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005202 return NULL;
5203 }
5204
5205 return GetBufferLine(self->buf, n+start);
5206}
5207
5208 static PyObject *
5209RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
5210{
5211 PyInt size;
5212
5213 if (CheckBuffer(self))
5214 return NULL;
5215
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005216 if (end == -1)
5217 end = self->buf->b_ml.ml_line_count;
5218
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005219 size = end - start + 1;
5220
5221 if (lo < 0)
5222 lo = 0;
5223 else if (lo > size)
5224 lo = size;
5225 if (hi < 0)
5226 hi = 0;
5227 if (hi < lo)
5228 hi = lo;
5229 else if (hi > size)
5230 hi = size;
5231
5232 return GetBufferLineList(self->buf, lo+start, hi+start);
5233}
5234
5235 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005236RBAsItem(
5237 BufferObject *self,
5238 PyInt n,
5239 PyObject *valObject,
5240 PyInt start,
5241 PyInt end,
5242 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005243{
5244 PyInt len_change;
5245
5246 if (CheckBuffer(self))
5247 return -1;
5248
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005249 if (end == -1)
5250 end = self->buf->b_ml.ml_line_count;
5251
Bram Moolenaarbd80f352013-05-12 21:16:23 +02005252 if (n < 0)
5253 n += end - start + 1;
5254
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005255 if (n < 0 || n > end - start)
5256 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005257 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005258 return -1;
5259 }
5260
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005261 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005262 return -1;
5263
5264 if (new_end)
5265 *new_end = end + len_change;
5266
5267 return 0;
5268}
5269
Bram Moolenaar19e60942011-06-19 00:27:51 +02005270 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005271RBAsSlice(
5272 BufferObject *self,
5273 PyInt lo,
5274 PyInt hi,
5275 PyObject *valObject,
5276 PyInt start,
5277 PyInt end,
5278 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02005279{
5280 PyInt size;
5281 PyInt len_change;
5282
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005283 // Self must be a valid buffer
Bram Moolenaar19e60942011-06-19 00:27:51 +02005284 if (CheckBuffer(self))
5285 return -1;
5286
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005287 if (end == -1)
5288 end = self->buf->b_ml.ml_line_count;
5289
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005290 // Sort out the slice range
Bram Moolenaar19e60942011-06-19 00:27:51 +02005291 size = end - start + 1;
5292
5293 if (lo < 0)
5294 lo = 0;
5295 else if (lo > size)
5296 lo = size;
5297 if (hi < 0)
5298 hi = 0;
5299 if (hi < lo)
5300 hi = lo;
5301 else if (hi > size)
5302 hi = size;
5303
5304 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005305 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02005306 return -1;
5307
5308 if (new_end)
5309 *new_end = end + len_change;
5310
5311 return 0;
5312}
5313
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005314
5315 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005316RBAppend(
5317 BufferObject *self,
5318 PyObject *args,
5319 PyInt start,
5320 PyInt end,
5321 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005322{
5323 PyObject *lines;
5324 PyInt len_change;
5325 PyInt max;
5326 PyInt n;
5327
5328 if (CheckBuffer(self))
5329 return NULL;
5330
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02005331 if (end == -1)
5332 end = self->buf->b_ml.ml_line_count;
5333
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005334 max = n = end - start + 1;
5335
5336 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
5337 return NULL;
5338
5339 if (n < 0 || n > max)
5340 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005341 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005342 return NULL;
5343 }
5344
5345 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
5346 return NULL;
5347
5348 if (new_end)
5349 *new_end = end + len_change;
5350
5351 Py_INCREF(Py_None);
5352 return Py_None;
5353}
5354
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005355// Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005356
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005357DEFINE_PY_TYPE_OBJECT(RangeType);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005358static PySequenceMethods RangeAsSeq;
5359static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005360
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005361typedef struct
5362{
5363 PyObject_HEAD
5364 BufferObject *buf;
5365 PyInt start;
5366 PyInt end;
5367} RangeObject;
5368
5369 static PyObject *
5370RangeNew(buf_T *buf, PyInt start, PyInt end)
5371{
5372 BufferObject *bufr;
5373 RangeObject *self;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005374 self = PyObject_GC_New(RangeObject, RangeTypePtr);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005375 if (self == NULL)
5376 return NULL;
5377
5378 bufr = (BufferObject *)BufferNew(buf);
5379 if (bufr == NULL)
5380 {
5381 Py_DECREF(self);
5382 return NULL;
5383 }
5384 Py_INCREF(bufr);
5385
5386 self->buf = bufr;
5387 self->start = start;
5388 self->end = end;
5389
5390 return (PyObject *)(self);
5391}
5392
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005393 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005394RangeDestructor(PyObject *self_obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005395{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005396 RangeObject *self = (RangeObject*)self_obj;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005397 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005398 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005399 PyObject_GC_Del((void *)(self));
5400}
5401
5402 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005403RangeTraverse(PyObject *self_obj, visitproc visit, void *arg)
Bram Moolenaar774267b2013-05-21 20:51:59 +02005404{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005405 RangeObject *self = (RangeObject*)self_obj;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005406 Py_VISIT(((PyObject *)(self->buf)));
5407 return 0;
5408}
5409
5410 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005411RangeClear(PyObject *self_obj)
Bram Moolenaar774267b2013-05-21 20:51:59 +02005412{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005413 RangeObject *self = (RangeObject*)self_obj;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005414 Py_CLEAR(self->buf);
5415 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005416}
5417
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005418 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005419RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005420{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005421 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005422 if (CheckBuffer(self->buf))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005423 return -1; // ???
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005424
Bram Moolenaard6e39182013-05-21 18:30:34 +02005425 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005426}
5427
5428 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005429RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005430{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005431 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005432}
5433
5434 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005435RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005436{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005437 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005438}
5439
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005440static char *RangeAttrs[] = {
5441 "start", "end",
5442 NULL
5443};
5444
5445 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005446RangeDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005447{
5448 return ObjectDir(self, RangeAttrs);
5449}
5450
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005451 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005452RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005453{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005454 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005455}
5456
5457 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005458RangeRepr(PyObject *self_obj)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005459{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005460 RangeObject *self = (RangeObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005461 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005462 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
Dominique Pellec14f6672022-01-09 12:57:48 +00005463 (void *)self);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005464 else
5465 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005466 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005467
5468 if (name == NULL)
5469 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005470
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005471 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005472 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005473 }
5474}
5475
5476static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005477 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005478 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005479 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5480 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005481};
5482
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005483DEFINE_PY_TYPE_OBJECT(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005484static PySequenceMethods BufferAsSeq;
5485static PyMappingMethods BufferAsMapping;
5486
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005487 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005488BufferNew(buf_T *buf)
5489{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005490 /*
5491 * We need to handle deletion of buffers underneath us.
Bram Moolenaar971db462013-05-12 18:44:48 +02005492 * If we add a "b_python*_ref" field to the buf_T structure,
5493 * then we can get at it in buf_freeall() in vim. We then
5494 * need to create only ONE Python object per buffer - if
5495 * we try to create a second, just INCREF the existing one
5496 * and return it. The (single) Python object referring to
5497 * the buffer is stored in "b_python*_ref".
5498 * Question: what to do on a buf_freeall(). We'll probably
5499 * have to either delete the Python object (DECREF it to
5500 * zero - a bad idea, as it leaves dangling refs!) or
5501 * set the buf_T * value to an invalid value (-1?), which
5502 * means we need checks in all access functions... Bah.
5503 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005504 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005505 * b_python_ref and b_python3_ref fields respectively.
5506 */
5507
5508 BufferObject *self;
5509
5510 if (BUF_PYTHON_REF(buf) != NULL)
5511 {
5512 self = BUF_PYTHON_REF(buf);
5513 Py_INCREF(self);
5514 }
5515 else
5516 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005517 self = PyObject_NEW(BufferObject, BufferTypePtr);
Bram Moolenaar971db462013-05-12 18:44:48 +02005518 if (self == NULL)
5519 return NULL;
5520 self->buf = buf;
5521 BUF_PYTHON_REF(buf) = self;
5522 }
5523
5524 return (PyObject *)(self);
5525}
5526
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005527 static void
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005528BufferDestructor(PyObject *self_obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005529{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005530 BufferObject *self = (BufferObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005531 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5532 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005533
5534 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005535}
5536
Bram Moolenaar971db462013-05-12 18:44:48 +02005537 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005538BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005539{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005540 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
Bram Moolenaard6e39182013-05-21 18:30:34 +02005541 if (CheckBuffer(self))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005542 return -1; // ???
Bram Moolenaar971db462013-05-12 18:44:48 +02005543
Bram Moolenaard6e39182013-05-21 18:30:34 +02005544 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005545}
5546
5547 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005548BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005549{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005550 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005551}
5552
5553 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005554BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005555{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005556 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005557}
5558
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005559static char *BufferAttrs[] = {
5560 "name", "number", "vars", "options", "valid",
5561 NULL
5562};
5563
5564 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005565BufferDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005566{
5567 return ObjectDir(self, BufferAttrs);
5568}
5569
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005570 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005571BufferAttrValid(BufferObject *self, char *name)
5572{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005574
5575 if (strcmp(name, "valid") != 0)
5576 return NULL;
5577
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005578 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5579 Py_INCREF(ret);
5580 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005581}
5582
5583 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005584BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005585{
5586 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005587 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005588 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005589 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005590 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005591 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005592 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005593 else if (strcmp(name, "options") == 0)
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005594 return OptionsNew(SREQ_BUF, self->buf, CheckBufferCb,
Bram Moolenaard6e39182013-05-21 18:30:34 +02005595 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005596 else if (strcmp(name, "__members__") == 0)
5597 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005598 else
5599 return NULL;
5600}
5601
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005602 static int
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005603BufferSetattr(PyObject *self_obj, char *name, PyObject *valObject)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005604{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005605 BufferObject *self = (BufferObject*)self_obj;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005606 if (CheckBuffer(self))
5607 return -1;
5608
5609 if (strcmp(name, "name") == 0)
5610 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005611 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005612 aco_save_T aco;
Bram Moolenaar37199892022-11-29 13:46:48 +00005613 int ren_ret = OK;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005614 PyObject *todecref;
5615
5616 if (!(val = StringToChars(valObject, &todecref)))
5617 return -1;
5618
5619 VimTryStart();
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005620 // Using aucmd_*: autocommands will be executed by rename_buffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005621 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00005622 if (curbuf == self->buf)
5623 {
5624 ren_ret = rename_buffer(val);
5625 aucmd_restbuf(&aco);
5626 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005627 Py_XDECREF(todecref);
5628 if (VimTryEnd())
5629 return -1;
5630
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005631 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005632 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005633 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005634 return -1;
5635 }
5636 return 0;
5637 }
5638 else
5639 {
5640 PyErr_SetString(PyExc_AttributeError, name);
5641 return -1;
5642 }
5643}
5644
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005645 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005646BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005647{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005648 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005649}
5650
5651 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005652BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005653{
5654 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005655 char_u *pmark;
5656 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005657 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005658 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005659
Bram Moolenaard6e39182013-05-21 18:30:34 +02005660 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005661 return NULL;
5662
Bram Moolenaar389a1792013-06-23 13:00:44 +02005663 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005664 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005665
Bram Moolenaar389a1792013-06-23 13:00:44 +02005666 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005667 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005668 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005669 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005670 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005671 return NULL;
5672 }
5673
5674 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005675
5676 Py_XDECREF(todecref);
5677
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005678 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005679 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005680 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005681 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005682 if (VimTryEnd())
5683 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005684
5685 if (posp == NULL)
5686 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005687 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005688 return NULL;
5689 }
5690
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005691 if (posp->lnum <= 0)
5692 {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005693 // Or raise an error?
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005694 Py_INCREF(Py_None);
5695 return Py_None;
5696 }
5697
5698 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5699}
5700
5701 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005702BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005703{
5704 PyInt start;
5705 PyInt end;
5706
Bram Moolenaard6e39182013-05-21 18:30:34 +02005707 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005708 return NULL;
5709
5710 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5711 return NULL;
5712
Bram Moolenaard6e39182013-05-21 18:30:34 +02005713 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005714}
5715
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005716 static PyObject *
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005717BufferRepr(PyObject *self_obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005718{
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005719 BufferObject *self = (BufferObject*)self_obj;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005720 if (self->buf == INVALID_BUFFER_VALUE)
Dominique Pellec14f6672022-01-09 12:57:48 +00005721 return PyString_FromFormat("<buffer object (deleted) at %p>", (void *)self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005722 else
5723 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005724 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005725
5726 if (name == NULL)
5727 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005728
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005729 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005730 }
5731}
5732
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005733static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005734 // name, function, calling, documentation
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005735 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005736 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005737 {"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 +02005738 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5739 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005740};
5741
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005742/*
5743 * Buffer list object - Implementation
5744 */
5745
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005746DEFINE_PY_TYPE_OBJECT(BufMapType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005747
5748typedef struct
5749{
5750 PyObject_HEAD
5751} BufMapObject;
5752
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005753static BufMapObject TheBufferMap =
5754{
5755 PyObject_HEAD_INIT_TYPE(BufMapType)
5756};
5757
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005758 static PyInt
5759BufMapLength(PyObject *self UNUSED)
5760{
5761 buf_T *b = firstbuf;
5762 PyInt n = 0;
5763
5764 while (b)
5765 {
5766 ++n;
5767 b = b->b_next;
5768 }
5769
5770 return n;
5771}
5772
5773 static PyObject *
5774BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5775{
5776 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005777 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005778
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005779 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005780 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005781
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005782 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005783
5784 if (b)
5785 return BufferNew(b);
5786 else
5787 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005788 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005789 return NULL;
5790 }
5791}
5792
5793 static void
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005794BufMapIterDestruct(void* arg)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005795{
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005796 PyObject *buffer = (PyObject*)arg;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005797 // Iteration was stopped before all buffers were processed
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005798 if (buffer)
5799 {
5800 Py_DECREF(buffer);
5801 }
5802}
5803
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005804 static int
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005805BufMapIterTraverse(void *iter, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005806{
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005807 PyObject *buffer = (PyObject*)iter;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005808 if (buffer)
5809 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005810 return 0;
5811}
5812
5813 static int
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005814BufMapIterClear(void **iter)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005815{
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005816 PyObject **buffer = (PyObject**)iter;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005817 if (*buffer)
5818 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005819 return 0;
5820}
5821
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005822 static PyObject *
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005823BufMapIterNext(void **arg)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005824{
5825 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005826 PyObject *ret;
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005827 PyObject **buffer = (PyObject**)arg;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005828
5829 if (!*buffer)
5830 return NULL;
5831
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005832 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005833
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005834 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005835 {
5836 *buffer = NULL;
5837 return NULL;
5838 }
5839
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005840 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005841 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005842 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005843 return NULL;
5844 *buffer = next;
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005845 // Do not increment reference: we no longer hold it (decref), but whoever
5846 // on other side will hold (incref). Decref+incref = nothing.
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005847 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005848}
5849
5850 static PyObject *
Bram Moolenaar423a85a2020-08-29 12:57:16 +02005851BufMapIter(PyObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005852{
5853 PyObject *buffer;
5854
5855 buffer = BufferNew(firstbuf);
5856 return IterNew(buffer,
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02005857 BufMapIterDestruct, BufMapIterNext,
5858 BufMapIterTraverse, BufMapIterClear,
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02005859 self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005860}
5861
5862static PyMappingMethods BufMapAsMapping = {
5863 (lenfunc) BufMapLength,
5864 (binaryfunc) BufMapItem,
5865 (objobjargproc) 0,
5866};
5867
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01005868// Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005869
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005870static char *CurrentAttrs[] = {
5871 "buffer", "window", "line", "range", "tabpage",
5872 NULL
5873};
5874
5875 static PyObject *
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02005876CurrentDir(PyObject *self, PyObject *args UNUSED)
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005877{
5878 return ObjectDir(self, CurrentAttrs);
5879}
5880
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005881 static PyObject *
5882CurrentGetattr(PyObject *self UNUSED, char *name)
5883{
5884 if (strcmp(name, "buffer") == 0)
5885 return (PyObject *)BufferNew(curbuf);
5886 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005887 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005888 else if (strcmp(name, "tabpage") == 0)
5889 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005890 else if (strcmp(name, "line") == 0)
5891 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5892 else if (strcmp(name, "range") == 0)
5893 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005894 else if (strcmp(name, "__members__") == 0)
5895 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005896 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005897#if PY_MAJOR_VERSION < 3
5898 return Py_FindMethod(WindowMethods, self, name);
5899#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005900 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005901#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005902}
5903
5904 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005905CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005906{
5907 if (strcmp(name, "line") == 0)
5908 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005909 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5910 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005911 return -1;
5912
5913 return 0;
5914 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005915 else if (strcmp(name, "buffer") == 0)
5916 {
5917 int count;
5918
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005919 if (valObject->ob_type != BufferTypePtr)
Bram Moolenaare7614592013-05-15 15:51:08 +02005920 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005921 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005922 N_("expected vim.Buffer object, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005923 valObject);
Bram Moolenaare7614592013-05-15 15:51:08 +02005924 return -1;
5925 }
5926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005927 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005928 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005929 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005930
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005931 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005932 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5933 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005934 if (VimTryEnd())
5935 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005936 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005937 return -1;
5938 }
5939
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005940 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005941 }
5942 else if (strcmp(name, "window") == 0)
5943 {
5944 int count;
5945
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005946 if (valObject->ob_type != WindowTypePtr)
Bram Moolenaare7614592013-05-15 15:51:08 +02005947 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005948 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005949 N_("expected vim.Window object, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005950 valObject);
Bram Moolenaare7614592013-05-15 15:51:08 +02005951 return -1;
5952 }
5953
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005954 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005955 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005956 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005957
5958 if (!count)
5959 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005960 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005961 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005962 return -1;
5963 }
5964
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005965 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005966 win_goto(((WindowObject *)(valObject))->win);
5967 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005968 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005969 if (VimTryEnd())
5970 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005971 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005972 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005973 return -1;
5974 }
5975
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005976 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005977 }
5978 else if (strcmp(name, "tabpage") == 0)
5979 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005980 if (valObject->ob_type != TabPageTypePtr)
Bram Moolenaare7614592013-05-15 15:51:08 +02005981 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005982 PyErr_FORMAT_TYPE(
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005983 N_("expected vim.TabPage object, but got %s"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02005984 valObject);
Bram Moolenaare7614592013-05-15 15:51:08 +02005985 return -1;
5986 }
5987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005988 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005989 return -1;
5990
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005991 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005992 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5993 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005994 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005995 if (VimTryEnd())
5996 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005997 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005998 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005999 return -1;
6000 }
6001
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02006002 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02006003 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006004 else
6005 {
6006 PyErr_SetString(PyExc_AttributeError, name);
6007 return -1;
6008 }
6009}
6010
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006011static struct PyMethodDef CurrentMethods[] = {
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006012 // name, function, calling, documentation
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006013 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
6014 { NULL, NULL, 0, NULL}
6015};
6016
Bram Moolenaardb913952012-06-29 12:54:53 +02006017 static void
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02006018init_range_cmd(void *arg)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006019{
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02006020 exarg_T *eap = (exarg_T*)arg;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006021 RangeStart = eap->line1;
6022 RangeEnd = eap->line2;
6023}
6024
6025 static void
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02006026init_range_eval(void *rettv UNUSED)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006027{
6028 RangeStart = (PyInt) curwin->w_cursor.lnum;
6029 RangeEnd = RangeStart;
6030}
6031
6032 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006033run_cmd(const char *cmd, void *arg UNUSED
6034#ifdef PY_CAN_RECURSE
6035 , PyGILState_STATE *pygilstate UNUSED
6036#endif
6037 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006038{
Bram Moolenaar41009372013-07-01 22:03:04 +02006039 PyObject *run_ret;
6040 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
6041 if (run_ret != NULL)
6042 {
6043 Py_DECREF(run_ret);
6044 }
6045 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
6046 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006047 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02006048 PyErr_Clear();
6049 }
6050 else
6051 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006052}
6053
6054static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
6055static int code_hdr_len = 30;
6056
6057 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006058run_do(const char *cmd, void *arg UNUSED
6059#ifdef PY_CAN_RECURSE
6060 , PyGILState_STATE *pygilstate
6061#endif
6062 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006063{
6064 PyInt lnum;
6065 size_t len;
6066 char *code;
6067 int status;
6068 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02006069 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01006070 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006071
Bram Moolenaar4ac66762013-05-28 22:31:46 +02006072 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006074 emsg(_("cannot save undo information"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006075 return;
6076 }
6077
6078 len = code_hdr_len + STRLEN(cmd);
6079 code = PyMem_New(char, len + 1);
6080 memcpy(code, code_hdr, code_hdr_len);
6081 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02006082 run_ret = PyRun_String(code, Py_file_input, globals, globals);
6083 status = -1;
6084 if (run_ret != NULL)
6085 {
6086 status = 0;
6087 Py_DECREF(run_ret);
6088 }
6089 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
6090 {
6091 PyMem_Free(code);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006092 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02006093 PyErr_Clear();
6094 return;
6095 }
6096 else
6097 PyErr_PrintEx(1);
6098
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006099 PyMem_Free(code);
6100
6101 if (status)
6102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006103 emsg(_("failed to run the code"));
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006104 return;
6105 }
6106
6107 status = 0;
6108 pymain = PyImport_AddModule("__main__");
6109 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006110#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006111 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006112#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006113
6114 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
6115 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006116 PyObject *line;
6117 PyObject *linenr;
6118 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006119
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006120#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006121 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006122#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006123 // Check the line number, the command my have deleted lines.
Bram Moolenaara58883b2017-01-29 21:31:09 +01006124 if (lnum > curbuf->b_ml.ml_line_count
6125 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006126 goto err;
6127 if (!(linenr = PyInt_FromLong((long) lnum)))
6128 {
6129 Py_DECREF(line);
6130 goto err;
6131 }
6132 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
6133 Py_DECREF(line);
6134 Py_DECREF(linenr);
6135 if (!ret)
6136 goto err;
6137
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006138 // Check that the command didn't switch to another buffer.
Bram Moolenaara58883b2017-01-29 21:31:09 +01006139 if (curbuf != was_curbuf)
6140 {
6141 Py_XDECREF(ret);
6142 goto err;
6143 }
6144
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006145 if (ret != Py_None)
6146 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01006147 {
6148 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006149 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01006150 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006151
6152 Py_XDECREF(ret);
6153 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006154#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006155 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006156#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006157 }
6158 goto out;
6159err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006160#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006161 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006162#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006163 PyErr_PrintEx(0);
6164 PythonIO_Flush();
6165 status = 1;
6166out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006167#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006168 if (!status)
6169 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006170#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006171 Py_DECREF(pyfunc);
6172 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
6173 if (status)
6174 return;
6175 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006176 update_curbuf(UPD_NOT_VALID);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006177}
6178
6179 static void
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02006180run_eval(const char *cmd, void *arg
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02006181#ifdef PY_CAN_RECURSE
6182 , PyGILState_STATE *pygilstate UNUSED
6183#endif
6184 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006185{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006186 PyObject *run_ret;
Yee Cheng Chind606fcc2023-09-20 19:59:47 +02006187 typval_T *rettv = (typval_T*)arg;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006188
Bram Moolenaar41009372013-07-01 22:03:04 +02006189 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006190 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006191 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02006192 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02006193 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006194 emsg(_(e_cant_handle_systemexit_of_python_exception_in_vim));
Bram Moolenaar41009372013-07-01 22:03:04 +02006195 PyErr_Clear();
6196 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02006197 else
6198 {
6199 if (PyErr_Occurred() && !msg_silent)
6200 PyErr_PrintEx(0);
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00006201 emsg(_(e_eval_did_not_return_valid_python_object));
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02006202 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006203 }
6204 else
6205 {
Bram Moolenaarde323092017-11-09 19:56:08 +01006206 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00006207 emsg(_(e_failed_to_convert_returned_python_object_to_vim_value));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006208 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02006209 }
6210 PyErr_Clear();
6211}
6212
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006213 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006214set_ref_in_py(const int copyID)
6215{
6216 pylinkedlist_T *cur;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006217 list_T *ll;
6218 int i;
6219 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006220 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02006221
6222 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006223 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006224 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006225 abort = set_ref_in_dict(((DictionaryObject *)(cur->pll_obj))->dict,
6226 copyID);
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006227 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006228
6229 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006230 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006231 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02006232 {
6233 ll = ((ListObject *) (cur->pll_obj))->list;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006234 abort = set_ref_in_list(ll, copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006235 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006236 }
6237
Bram Moolenaar8110a092016-04-14 15:56:09 +02006238 if (lastfunc != NULL)
6239 {
6240 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
6241 {
6242 func = (FunctionObject *) cur->pll_obj;
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006243 abort = set_ref_in_dict(func->self, copyID);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006244 if (func->argc)
6245 for (i = 0; !abort && i < func->argc; ++i)
6246 abort = abort
6247 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
6248 }
6249 }
6250
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006251 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02006252}
6253
6254 static int
6255set_string_copy(char_u *str, typval_T *tv)
6256{
6257 tv->vval.v_string = vim_strsave(str);
6258 if (tv->vval.v_string == NULL)
6259 {
6260 PyErr_NoMemory();
6261 return -1;
6262 }
6263 return 0;
6264}
6265
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006266 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006267pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006268{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006269 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006270 char_u *key;
6271 dictitem_T *di;
6272 PyObject *keyObject;
6273 PyObject *valObject;
6274 Py_ssize_t iter = 0;
6275
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006276 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006277 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006278
6279 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006280 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006281
6282 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
6283 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006284 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006285
Bram Moolenaara03e6312013-05-29 22:49:26 +02006286 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006287 {
6288 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006289 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006290 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006291
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006292 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006293 {
6294 dict_unref(dict);
6295 return -1;
6296 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006297
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006298 if (*key == NUL)
6299 {
6300 dict_unref(dict);
6301 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006302 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006303 return -1;
6304 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006305
6306 di = dictitem_alloc(key);
6307
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006308 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006309
6310 if (di == NULL)
6311 {
6312 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006313 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006314 return -1;
6315 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006316
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006317 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006318 {
6319 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006320 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006321 return -1;
6322 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006323
6324 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006325 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006326 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02006327 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006328 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006329 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006330 return -1;
6331 }
6332 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006333
6334 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006335 return 0;
6336}
6337
6338 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006339pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006340{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006341 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006342 char_u *key;
6343 dictitem_T *di;
6344 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006345 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006346 PyObject *keyObject;
6347 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006348
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006349 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006350 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006351
6352 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006353 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006354
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006355 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006356 {
6357 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006358 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006359 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006360
6361 if (!(iterator = PyObject_GetIter(list)))
6362 {
6363 dict_unref(dict);
6364 Py_DECREF(list);
6365 return -1;
6366 }
6367 Py_DECREF(list);
6368
6369 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006370 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006371 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006372
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006373 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006374 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006375 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006376 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006377 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006378 return -1;
6379 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006380
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006381 if (*key == NUL)
6382 {
6383 Py_DECREF(keyObject);
6384 Py_DECREF(iterator);
6385 Py_XDECREF(todecref);
6386 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006387 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006388 return -1;
6389 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006390
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006391 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006392 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006393 Py_DECREF(keyObject);
6394 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006395 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006396 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006397 return -1;
6398 }
6399
6400 di = dictitem_alloc(key);
6401
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006402 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006403 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006404
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006405 if (di == NULL)
6406 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006407 Py_DECREF(iterator);
6408 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006409 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006410 PyErr_NoMemory();
6411 return -1;
6412 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006413
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006414 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006415 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006416 Py_DECREF(iterator);
6417 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006418 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006419 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006420 return -1;
6421 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006422
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006423 Py_DECREF(valObject);
6424
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006425 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006426 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006427 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006428 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006429 dictitem_free(di);
6430 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006431 return -1;
6432 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006433 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006434 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006435 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006436 return 0;
6437}
6438
6439 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006440pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006441{
6442 list_T *l;
6443
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006444 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006445 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006446
6447 tv->v_type = VAR_LIST;
6448 tv->vval.v_list = l;
6449
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006450 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006451 {
6452 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006453 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006454 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006455
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006456 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006457 return 0;
6458}
6459
Bram Moolenaardb913952012-06-29 12:54:53 +02006460typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6461
6462 static int
6463convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006464 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006465{
6466 PyObject *capsule;
6467 char hexBuf[sizeof(void *) * 2 + 3];
6468
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006469 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006470
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006471#ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006472 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006473#else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006474 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006475#endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006476 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006477 {
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006478#ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006479 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006480#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006481 capsule = PyCObject_FromVoidPtr(tv, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006482#endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006483 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6484 {
6485 Py_DECREF(capsule);
6486 tv->v_type = VAR_UNKNOWN;
6487 return -1;
6488 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006489
6490 Py_DECREF(capsule);
6491
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006492 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006493 {
6494 tv->v_type = VAR_UNKNOWN;
6495 return -1;
6496 }
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006497 // As we are not using copy_tv which increments reference count we must
6498 // do it ourself.
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006499 if (tv->v_type == VAR_DICT)
6500 ++tv->vval.v_dict->dv_refcount;
6501 else if (tv->v_type == VAR_LIST)
6502 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006503 }
6504 else
6505 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006506 typval_T *v;
6507
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006508#ifdef PY_USE_CAPSULE
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006509 v = PyCapsule_GetPointer(capsule, NULL);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006510#else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006511 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01006512#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006513 copy_tv(v, tv);
6514 }
6515 return 0;
6516}
6517
6518 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006519ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6520{
6521 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006522 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006523
6524 if (!(lookup_dict = PyDict_New()))
6525 return -1;
6526
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006527 if (PyType_IsSubtype(obj->ob_type, DictionaryTypePtr))
Bram Moolenaara9922d62013-05-30 13:01:18 +02006528 {
6529 tv->v_type = VAR_DICT;
6530 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6531 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006532 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006533 }
6534 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006535 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006536 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006537 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006538 else
6539 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006540 PyErr_FORMAT_TYPE(
Bram Moolenaar86181df2020-05-11 23:14:04 +02006541 N_("unable to convert %s to a Vim dictionary"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006542 obj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006543 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006544 }
6545 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006546 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006547}
6548
6549 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006550ConvertFromPySequence(PyObject *obj, typval_T *tv)
6551{
6552 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006553 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006554
6555 if (!(lookup_dict = PyDict_New()))
6556 return -1;
6557
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006558 if (PyType_IsSubtype(obj->ob_type, ListTypePtr))
Bram Moolenaar8110a092016-04-14 15:56:09 +02006559 {
6560 tv->v_type = VAR_LIST;
6561 tv->vval.v_list = (((ListObject *)(obj))->list);
6562 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006563 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006564 }
6565 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006566 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006567 else
6568 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006569 PyErr_FORMAT_TYPE(
Bram Moolenaar86181df2020-05-11 23:14:04 +02006570 N_("unable to convert %s to a Vim list"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006571 obj);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006572 ret = -1;
6573 }
6574 Py_DECREF(lookup_dict);
6575 return ret;
6576}
6577
6578 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006579ConvertFromPyObject(PyObject *obj, typval_T *tv)
6580{
6581 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006582 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006583
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006584 if (!(lookup_dict = PyDict_New()))
6585 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006586 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006587 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006588 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006589}
6590
6591 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006592_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006593{
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006594 if (PyType_IsSubtype(obj->ob_type, DictionaryTypePtr))
Bram Moolenaardb913952012-06-29 12:54:53 +02006595 {
6596 tv->v_type = VAR_DICT;
6597 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6598 ++tv->vval.v_dict->dv_refcount;
6599 }
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006600 else if (PyType_IsSubtype(obj->ob_type, ListTypePtr))
Bram Moolenaardb913952012-06-29 12:54:53 +02006601 {
6602 tv->v_type = VAR_LIST;
6603 tv->vval.v_list = (((ListObject *)(obj))->list);
6604 ++tv->vval.v_list->lv_refcount;
6605 }
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006606 else if (PyType_IsSubtype(obj->ob_type, FunctionTypePtr))
Bram Moolenaardb913952012-06-29 12:54:53 +02006607 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006608 FunctionObject *func = (FunctionObject *) obj;
6609 if (func->self != NULL || func->argv != NULL)
6610 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006611 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
6612
Bram Moolenaar8110a092016-04-14 15:56:09 +02006613 set_partial(func, pt, TRUE);
6614 tv->vval.v_partial = pt;
6615 tv->v_type = VAR_PARTIAL;
6616 }
6617 else
6618 {
6619 if (set_string_copy(func->name, tv) == -1)
6620 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006621
Bram Moolenaar8110a092016-04-14 15:56:09 +02006622 tv->v_type = VAR_FUNC;
6623 }
6624 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006625 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006626 else if (PyBytes_Check(obj))
6627 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006628 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006629
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006630 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006631 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006632 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006633 return -1;
6634
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006635 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006636 return -1;
6637
6638 tv->v_type = VAR_STRING;
6639 }
6640 else if (PyUnicode_Check(obj))
6641 {
6642 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006643 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006644
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006645 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, ERRORS_ENCODE_ARG);
Bram Moolenaardb913952012-06-29 12:54:53 +02006646 if (bytes == NULL)
6647 return -1;
6648
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01006649 if (PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006650 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006651 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006652 return -1;
6653
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006654 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006655 {
6656 Py_XDECREF(bytes);
6657 return -1;
6658 }
6659 Py_XDECREF(bytes);
6660
6661 tv->v_type = VAR_STRING;
6662 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006663#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006664 else if (PyInt_Check(obj))
6665 {
6666 tv->v_type = VAR_NUMBER;
6667 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006668 if (PyErr_Occurred())
6669 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006670 }
6671#endif
6672 else if (PyLong_Check(obj))
6673 {
6674 tv->v_type = VAR_NUMBER;
6675 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006676 if (PyErr_Occurred())
6677 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006678 }
6679 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006680 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006681 else if (PyFloat_Check(obj))
6682 {
6683 tv->v_type = VAR_FLOAT;
6684 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6685 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006686 else if (PyObject_HasAttrString(obj, "keys"))
6687 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01006688 // PyObject_GetIter can create built-in iterator for any sequence object
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006689 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006690 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006691 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006692 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006693 else if (PyNumber_Check(obj))
6694 {
6695 PyObject *num;
6696
6697 if (!(num = PyNumber_Long(obj)))
6698 return -1;
6699
6700 tv->v_type = VAR_NUMBER;
6701 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6702
6703 Py_DECREF(num);
6704 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006705 else if (obj == Py_None)
6706 {
6707 tv->v_type = VAR_SPECIAL;
6708 tv->vval.v_number = VVAL_NONE;
6709 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006710 else
6711 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006712 PyErr_FORMAT_TYPE(
Bram Moolenaar86181df2020-05-11 23:14:04 +02006713 N_("unable to convert %s to a Vim structure"),
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006714 obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006715 return -1;
6716 }
6717 return 0;
6718}
6719
6720 static PyObject *
6721ConvertToPyObject(typval_T *tv)
6722{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006723 typval_T *argv;
6724 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006725 if (tv == NULL)
6726 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006727 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006728 return NULL;
6729 }
6730 switch (tv->v_type)
6731 {
6732 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006733 return PyBytes_FromString(tv->vval.v_string == NULL
6734 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006735 case VAR_NUMBER:
6736 return PyLong_FromLong((long) tv->vval.v_number);
Bram Moolenaardb913952012-06-29 12:54:53 +02006737 case VAR_FLOAT:
6738 return PyFloat_FromDouble((double) tv->vval.v_float);
Bram Moolenaardb913952012-06-29 12:54:53 +02006739 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006740 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006741 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006742 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006743 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006744 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006745 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006746 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006747 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006748 if (tv->vval.v_partial->pt_argc)
6749 {
6750 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6751 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6752 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6753 }
6754 else
6755 argv = NULL;
6756 if (tv->vval.v_partial->pt_dict != NULL)
6757 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006758 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006759 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006760 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006761 tv->vval.v_partial->pt_dict,
6762 tv->vval.v_partial->pt_auto);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006763 case VAR_BLOB:
6764 return PyBytes_FromStringAndSize(
6765 (char*) tv->vval.v_blob->bv_ga.ga_data,
6766 (Py_ssize_t) tv->vval.v_blob->bv_ga.ga_len);
Bram Moolenaardb913952012-06-29 12:54:53 +02006767 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006768 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 case VAR_VOID:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006770 case VAR_CHANNEL:
6771 case VAR_JOB:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006772 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006773 case VAR_CLASS:
6774 case VAR_OBJECT:
Bram Moolenaardb913952012-06-29 12:54:53 +02006775 Py_INCREF(Py_None);
6776 return Py_None;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006777 case VAR_BOOL:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006778 case VAR_SPECIAL:
6779 switch (tv->vval.v_number)
6780 {
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006781 case VVAL_FALSE: return ALWAYS_FALSE;
6782 case VVAL_TRUE: return ALWAYS_TRUE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006783 case VVAL_NONE:
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02006784 case VVAL_NULL: return ALWAYS_NONE;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006785 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006786 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006787 return NULL;
6788 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006789 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006790}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006791
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006792DEFINE_PY_TYPE_OBJECT(CurrentType);
6793
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006794typedef struct
6795{
6796 PyObject_HEAD
6797} CurrentObject;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006798
6799static CurrentObject TheCurrent =
6800{
6801 PyObject_HEAD_INIT_TYPE(CurrentType)
6802};
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006803
6804 static void
6805init_structs(void)
6806{
Bram Moolenaara80faa82020-04-12 19:37:17 +02006807 CLEAR_FIELD(OutputType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006808 OutputType.tp_name = "vim.message";
6809 OutputType.tp_basicsize = sizeof(OutputObject);
6810 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6811 OutputType.tp_doc = "vim message object";
6812 OutputType.tp_methods = OutputMethods;
6813#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006814 OutputType.tp_getattro = OutputGetattro;
6815 OutputType.tp_setattro = OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006816 OutputType.tp_alloc = call_PyType_GenericAlloc;
6817 OutputType.tp_new = call_PyType_GenericNew;
6818 OutputType.tp_free = call_PyObject_Free;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006819# ifndef USE_LIMITED_API
6820 // The std printer type is only exposed in full API. It is not essential
6821 // anyway and so in limited API we don't set it.
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006822 OutputType.tp_base = &PyStdPrinter_Type;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006823# endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006824#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006825 OutputType.tp_getattr = OutputGetattr;
6826 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006827 // Disabled, because this causes a crash in test86
6828 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006829#endif
6830
Bram Moolenaara80faa82020-04-12 19:37:17 +02006831 CLEAR_FIELD(IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006832 IterType.tp_name = "vim.iter";
6833 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006834 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006835 IterType.tp_doc = "generic iterator object";
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006836 IterType.tp_iter = IterIter;
6837 IterType.tp_iternext = IterNext;
6838 IterType.tp_dealloc = IterDestructor;
6839 IterType.tp_traverse = IterTraverse;
6840 IterType.tp_clear = IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006841
Bram Moolenaara80faa82020-04-12 19:37:17 +02006842 CLEAR_FIELD(BufferType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006843 BufferType.tp_name = "vim.buffer";
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006844 BufferType.tp_basicsize = sizeof(BufferObject);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006845 BufferType.tp_dealloc = BufferDestructor;
6846 BufferType.tp_repr = BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006847 BufferType.tp_as_sequence = &BufferAsSeq;
6848 BufferType.tp_as_mapping = &BufferAsMapping;
6849 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6850 BufferType.tp_doc = "vim buffer object";
6851 BufferType.tp_methods = BufferMethods;
6852#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006853 BufferType.tp_getattro = BufferGetattro;
6854 BufferType.tp_setattro = BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006855 BufferType.tp_alloc = call_PyType_GenericAlloc;
6856 BufferType.tp_new = call_PyType_GenericNew;
6857 BufferType.tp_free = call_PyObject_Free;
6858#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006859 BufferType.tp_getattr = BufferGetattr;
6860 BufferType.tp_setattr = BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006861#endif
6862
Bram Moolenaara80faa82020-04-12 19:37:17 +02006863 CLEAR_FIELD(WindowType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006864 WindowType.tp_name = "vim.window";
6865 WindowType.tp_basicsize = sizeof(WindowObject);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006866 WindowType.tp_dealloc = WindowDestructor;
6867 WindowType.tp_repr = WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006868 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006869 WindowType.tp_doc = "vim Window object";
6870 WindowType.tp_methods = WindowMethods;
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006871 WindowType.tp_traverse = WindowTraverse;
6872 WindowType.tp_clear = WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006873#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006874 WindowType.tp_getattro = WindowGetattro;
6875 WindowType.tp_setattro = WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006876 WindowType.tp_alloc = call_PyType_GenericAlloc;
6877 WindowType.tp_new = call_PyType_GenericNew;
6878 WindowType.tp_free = call_PyObject_Free;
6879#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006880 WindowType.tp_getattr = WindowGetattr;
6881 WindowType.tp_setattr = WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006882#endif
6883
Bram Moolenaara80faa82020-04-12 19:37:17 +02006884 CLEAR_FIELD(TabPageType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006885 TabPageType.tp_name = "vim.tabpage";
6886 TabPageType.tp_basicsize = sizeof(TabPageObject);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006887 TabPageType.tp_dealloc = TabPageDestructor;
6888 TabPageType.tp_repr = TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006889 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6890 TabPageType.tp_doc = "vim tab page object";
6891 TabPageType.tp_methods = TabPageMethods;
6892#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006893 TabPageType.tp_getattro = TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006894 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6895 TabPageType.tp_new = call_PyType_GenericNew;
6896 TabPageType.tp_free = call_PyObject_Free;
6897#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006898 TabPageType.tp_getattr = TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006899#endif
6900
Bram Moolenaara80faa82020-04-12 19:37:17 +02006901 CLEAR_FIELD(BufMapType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006902 BufMapType.tp_name = "vim.bufferlist";
6903 BufMapType.tp_basicsize = sizeof(BufMapObject);
6904 BufMapType.tp_as_mapping = &BufMapAsMapping;
6905 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006906 BufMapType.tp_iter = BufMapIter;
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006907 BufMapType.tp_doc = "vim buffer list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006908
Bram Moolenaara80faa82020-04-12 19:37:17 +02006909 CLEAR_FIELD(WinListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006910 WinListType.tp_name = "vim.windowlist";
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006911 WinListType.tp_basicsize = sizeof(WinListObject);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006912 WinListType.tp_as_sequence = &WinListAsSeq;
6913 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6914 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006915 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006916
Bram Moolenaara80faa82020-04-12 19:37:17 +02006917 CLEAR_FIELD(TabListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006918 TabListType.tp_name = "vim.tabpagelist";
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02006919 TabListType.tp_basicsize = sizeof(TabListObject);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006920 TabListType.tp_as_sequence = &TabListAsSeq;
6921 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6922 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006923
Bram Moolenaara80faa82020-04-12 19:37:17 +02006924 CLEAR_FIELD(RangeType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006925 RangeType.tp_name = "vim.range";
6926 RangeType.tp_basicsize = sizeof(RangeObject);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006927 RangeType.tp_dealloc = RangeDestructor;
6928 RangeType.tp_repr = RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006929 RangeType.tp_as_sequence = &RangeAsSeq;
6930 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006931 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006932 RangeType.tp_doc = "vim Range object";
6933 RangeType.tp_methods = RangeMethods;
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006934 RangeType.tp_traverse = RangeTraverse;
6935 RangeType.tp_clear = RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006936#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006937 RangeType.tp_getattro = RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006938 RangeType.tp_alloc = call_PyType_GenericAlloc;
6939 RangeType.tp_new = call_PyType_GenericNew;
6940 RangeType.tp_free = call_PyObject_Free;
6941#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006942 RangeType.tp_getattr = RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006943#endif
6944
Bram Moolenaara80faa82020-04-12 19:37:17 +02006945 CLEAR_FIELD(CurrentType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006946 CurrentType.tp_name = "vim.currentdata";
6947 CurrentType.tp_basicsize = sizeof(CurrentObject);
6948 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6949 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006950 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006951#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006952 CurrentType.tp_getattro = CurrentGetattro;
6953 CurrentType.tp_setattro = CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006954#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006955 CurrentType.tp_getattr = CurrentGetattr;
6956 CurrentType.tp_setattr = CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006957#endif
6958
Bram Moolenaara80faa82020-04-12 19:37:17 +02006959 CLEAR_FIELD(DictionaryType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006960 DictionaryType.tp_name = "vim.dictionary";
6961 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006962 DictionaryType.tp_dealloc = DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006963 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006964 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006965 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006966 DictionaryType.tp_doc = "dictionary pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006967 DictionaryType.tp_methods = DictionaryMethods;
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006968 DictionaryType.tp_iter = DictionaryIter;
6969 DictionaryType.tp_new = DictionaryConstructor;
6970 DictionaryType.tp_alloc = PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006971#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006972 DictionaryType.tp_getattro = DictionaryGetattro;
6973 DictionaryType.tp_setattro = DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006974#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006975 DictionaryType.tp_getattr = DictionaryGetattr;
6976 DictionaryType.tp_setattr = DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006977#endif
6978
Bram Moolenaara80faa82020-04-12 19:37:17 +02006979 CLEAR_FIELD(ListType);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006980 ListType.tp_name = "vim.list";
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006981 ListType.tp_dealloc = ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006982 ListType.tp_basicsize = sizeof(ListObject);
6983 ListType.tp_as_sequence = &ListAsSeq;
6984 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006985 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02006986 ListType.tp_doc = "list pushing modifications to Vim structure";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006987 ListType.tp_methods = ListMethods;
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006988 ListType.tp_iter = ListIter;
6989 ListType.tp_new = ListConstructor;
6990 ListType.tp_alloc = PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006991#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006992 ListType.tp_getattro = ListGetattro;
6993 ListType.tp_setattro = ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006994#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02006995 ListType.tp_getattr = ListGetattr;
6996 ListType.tp_setattr = ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006997#endif
6998
Bram Moolenaara80faa82020-04-12 19:37:17 +02006999 CLEAR_FIELD(FunctionType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02007000 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02007001 FunctionType.tp_basicsize = sizeof(FunctionObject);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02007002 FunctionType.tp_dealloc = FunctionDestructor;
7003 FunctionType.tp_call = FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02007004 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar86181df2020-05-11 23:14:04 +02007005 FunctionType.tp_doc = "object that calls Vim function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02007006 FunctionType.tp_methods = FunctionMethods;
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02007007 FunctionType.tp_repr = FunctionRepr;
7008 FunctionType.tp_new = FunctionConstructor;
7009 FunctionType.tp_alloc = PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02007010#if PY_MAJOR_VERSION >= 3
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02007011 FunctionType.tp_getattro = FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02007012#else
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02007013 FunctionType.tp_getattr = FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02007014#endif
7015
Bram Moolenaara80faa82020-04-12 19:37:17 +02007016 CLEAR_FIELD(OptionsType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007017 OptionsType.tp_name = "vim.options";
7018 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01007019 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02007020 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007021 OptionsType.tp_doc = "object for manipulating options";
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02007022 OptionsType.tp_iter = OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007023 OptionsType.tp_as_mapping = &OptionsAsMapping;
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02007024 OptionsType.tp_dealloc = OptionsDestructor;
7025 OptionsType.tp_traverse = OptionsTraverse;
7026 OptionsType.tp_clear = OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007027
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007028#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaara80faa82020-04-12 19:37:17 +02007029 CLEAR_FIELD(LoaderType);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02007030 LoaderType.tp_name = "vim.Loader";
7031 LoaderType.tp_basicsize = sizeof(LoaderObject);
7032 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
7033 LoaderType.tp_doc = "vim message object";
7034 LoaderType.tp_methods = LoaderMethods;
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02007035 LoaderType.tp_dealloc = LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007036#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02007037
Bram Moolenaar4d1da492013-04-24 13:39:15 +02007038#if PY_MAJOR_VERSION >= 3
Bram Moolenaara80faa82020-04-12 19:37:17 +02007039 CLEAR_FIELD(vimmodule);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02007040 vimmodule.m_name = "vim";
7041 vimmodule.m_doc = "Vim Python interface\n";
7042 vimmodule.m_size = -1;
7043 vimmodule.m_methods = VimMethods;
7044#endif
7045}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007046
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007047 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02007048init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007049{
7050 PYTYPE_READY(IterType);
7051 PYTYPE_READY(BufferType);
7052 PYTYPE_READY(RangeType);
7053 PYTYPE_READY(WindowType);
7054 PYTYPE_READY(TabPageType);
7055 PYTYPE_READY(BufMapType);
7056 PYTYPE_READY(WinListType);
7057 PYTYPE_READY(TabListType);
7058 PYTYPE_READY(CurrentType);
7059 PYTYPE_READY(DictionaryType);
7060 PYTYPE_READY(ListType);
7061 PYTYPE_READY(FunctionType);
7062 PYTYPE_READY(OptionsType);
7063 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007064#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02007065 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007066#endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007067
7068#ifdef USE_LIMITED_API
7069 // We need to finish initializing all the static objects because the types
7070 // are only just allocated on the heap now.
7071 // Each PyObject_HEAD_INIT_TYPE should correspond to a
7072 // PyObject_FINISH_INIT_TYPE below.
7073 PyObject_FINISH_INIT_TYPE(Output, OutputType);
7074 PyObject_FINISH_INIT_TYPE(Error, OutputType);
7075 PyObject_FINISH_INIT_TYPE(TheBufferMap, BufMapType);
7076 PyObject_FINISH_INIT_TYPE(TheWindowList, WinListType);
7077 PyObject_FINISH_INIT_TYPE(TheCurrent, CurrentType);
7078 PyObject_FINISH_INIT_TYPE(TheTabPageList, TabListType);
7079#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007080 return 0;
7081}
7082
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007083#ifdef USE_LIMITED_API
7084 static void
7085shutdown_types(void)
7086{
7087 PYTYPE_CLEANUP(IterType);
7088 PYTYPE_CLEANUP(BufferType);
7089 PYTYPE_CLEANUP(RangeType);
7090 PYTYPE_CLEANUP(WindowType);
7091 PYTYPE_CLEANUP(TabPageType);
7092 PYTYPE_CLEANUP(BufMapType);
7093 PYTYPE_CLEANUP(WinListType);
7094 PYTYPE_CLEANUP(TabListType);
7095 PYTYPE_CLEANUP(CurrentType);
7096 PYTYPE_CLEANUP(DictionaryType);
7097 PYTYPE_CLEANUP(ListType);
7098 PYTYPE_CLEANUP(FunctionType);
7099 PYTYPE_CLEANUP(OptionsType);
7100 PYTYPE_CLEANUP(OutputType);
7101# if PY_VERSION_HEX < 0x030700f0
7102 PYTYPE_CLEANUP(LoaderType);
7103# endif
7104}
7105#endif
7106
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007107 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02007108init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007109{
7110 PyObject *path;
7111 PyObject *path_hook;
7112 PyObject *path_hooks;
7113
7114 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
7115 return -1;
7116
7117 if (!(path_hooks = PySys_GetObject("path_hooks")))
7118 {
7119 PyErr_Clear();
7120 path_hooks = PyList_New(1);
7121 PyList_SET_ITEM(path_hooks, 0, path_hook);
7122 if (PySys_SetObject("path_hooks", path_hooks))
7123 {
7124 Py_DECREF(path_hooks);
7125 return -1;
7126 }
7127 Py_DECREF(path_hooks);
7128 }
7129 else if (PyList_Check(path_hooks))
7130 {
7131 if (PyList_Append(path_hooks, path_hook))
7132 {
7133 Py_DECREF(path_hook);
7134 return -1;
7135 }
7136 Py_DECREF(path_hook);
7137 }
7138 else
7139 {
7140 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007141 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007142 "You should now do the following:\n"
7143 "- append vim.path_hook to sys.path_hooks\n"
7144 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01007145 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007146 Py_DECREF(path_hook);
7147 return 0;
7148 }
7149
7150 if (!(path = PySys_GetObject("path")))
7151 {
7152 PyErr_Clear();
7153 path = PyList_New(1);
7154 Py_INCREF(vim_special_path_object);
7155 PyList_SET_ITEM(path, 0, vim_special_path_object);
7156 if (PySys_SetObject("path", path))
7157 {
7158 Py_DECREF(path);
7159 return -1;
7160 }
7161 Py_DECREF(path);
7162 }
7163 else if (PyList_Check(path))
7164 {
7165 if (PyList_Append(path, vim_special_path_object))
7166 return -1;
7167 }
7168 else
7169 {
7170 VimTryStart();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007171 emsg(_("Failed to set path: sys.path is not a list\n"
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007172 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
Bram Moolenaar9bf703d2019-11-30 19:44:38 +01007173 VimTryEnd(); // Discard the error
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007174 }
7175
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007176 return 0;
7177}
7178
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007179static struct numeric_constant {
7180 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007181 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007182} numeric_constants[] = {
7183 {"VAR_LOCKED", VAR_LOCKED},
7184 {"VAR_FIXED", VAR_FIXED},
7185 {"VAR_SCOPE", VAR_SCOPE},
7186 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
7187};
7188
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007189struct object_constant {
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007190 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007191 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007192};
7193
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007194#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02007195 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007196 return -1;
7197
7198#define ADD_CHECKED_OBJECT(m, name, obj) \
7199 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007200 PyObject *valObject = obj; \
7201 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007202 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007203 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007204 }
7205
7206 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02007207populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007208{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007209 int i;
7210 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02007211 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02007212 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007213#if PY_VERSION_HEX >= 0x030700f0
7214 PyObject *dict;
7215 PyObject *cls;
7216#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007217
7218 for (i = 0; i < (int)(sizeof(numeric_constants)
7219 / sizeof(struct numeric_constant));
7220 ++i)
7221 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007222 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007223
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02007224 struct object_constant object_constants[] = {
7225 {"buffers", (PyObject *)(void *)&TheBufferMap},
7226 {"windows", (PyObject *)(void *)&TheWindowList},
7227 {"tabpages", (PyObject *)(void *)&TheTabPageList},
7228 {"current", (PyObject *)(void *)&TheCurrent},
7229
7230 {"Buffer", (PyObject *)BufferTypePtr},
7231 {"Range", (PyObject *)RangeTypePtr},
7232 {"Window", (PyObject *)WindowTypePtr},
7233 {"TabPage", (PyObject *)TabPageTypePtr},
7234 {"Dictionary", (PyObject *)DictionaryTypePtr},
7235 {"List", (PyObject *)ListTypePtr},
7236 {"Function", (PyObject *)FunctionTypePtr},
7237 {"Options", (PyObject *)OptionsTypePtr},
7238#if PY_VERSION_HEX < 0x030700f0
7239 {"_Loader", (PyObject *)LoaderTypePtr},
7240#endif
7241 };
7242
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007243 for (i = 0; i < (int)(sizeof(object_constants)
7244 / sizeof(struct object_constant));
7245 ++i)
7246 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007247 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007248
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02007249 valObject = object_constants[i].valObject;
7250 Py_INCREF(valObject);
7251 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007252 }
7253
7254 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
7255 return -1;
7256 ADD_OBJECT(m, "error", VimError);
7257
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007258 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
7259 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007260 ADD_CHECKED_OBJECT(m, "options",
7261 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02007262
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007263 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007264 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007265 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007266
Bram Moolenaar22081f42016-06-01 20:38:34 +02007267#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007268 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007269 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02007270#else
7271 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
7272 return -1;
7273#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02007274 ADD_OBJECT(m, "_getcwd", py_getcwd)
7275
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007276 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007277 return -1;
7278 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02007279 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007280 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02007281 if (PyObject_SetAttrString(other_module, "chdir", attr))
7282 {
7283 Py_DECREF(attr);
7284 return -1;
7285 }
7286 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007287
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02007288 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007289 {
7290 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02007291 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02007292 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02007293 if (PyObject_SetAttrString(other_module, "fchdir", attr))
7294 {
7295 Py_DECREF(attr);
7296 return -1;
7297 }
7298 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007299 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02007300 else
7301 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02007302
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007303 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
7304 return -1;
7305
7306 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
7307
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007308#if PY_VERSION_HEX >= 0x030700f0
7309 if (!(imp = PyImport_ImportModule("importlib.machinery")))
7310 return -1;
7311
7312 dict = PyModule_GetDict(imp);
7313
7314 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
7315 {
7316 Py_DECREF(imp);
7317 return -1;
7318 }
7319
7320 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
7321 {
7322 Py_DECREF(imp);
7323 return -1;
7324 }
7325
Bram Moolenaarb999ba22019-02-14 13:28:45 +01007326 if ((py_find_module = PyObject_GetAttrString(cls, "find_module")))
7327 {
7328 // find_module() is deprecated, this may stop working in some later
7329 // version.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01007330 ADD_OBJECT(m, "_find_module", py_find_module);
Bram Moolenaarb999ba22019-02-14 13:28:45 +01007331 }
7332
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007333 Py_DECREF(imp);
7334
7335 ADD_OBJECT(m, "_find_spec", py_find_spec);
7336#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02007337 if (!(imp = PyImport_ImportModule("imp")))
7338 return -1;
7339
7340 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
7341 {
7342 Py_DECREF(imp);
7343 return -1;
7344 }
7345
7346 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
7347 {
7348 Py_DECREF(py_find_module);
7349 Py_DECREF(imp);
7350 return -1;
7351 }
7352
7353 Py_DECREF(imp);
7354
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02007355 ADD_OBJECT(m, "_find_module", py_find_module);
7356 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02007357#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02007358
Bram Moolenaar1dc28782013-05-21 19:11:01 +02007359 return 0;
7360}