blob: 48137f1e37b2123facb70524b142ad894d988b13 [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 Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020075static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020076static PyObject *WindowNew(win_T *, tabpage_T *);
77static PyObject *BufferNew (buf_T *);
78static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020079
80static PyInt RangeStart;
81static PyInt RangeEnd;
82
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020083static PyObject *globals;
84
Bram Moolenaarf4258302013-06-02 18:20:17 +020085static PyObject *py_chdir;
86static PyObject *py_fchdir;
87static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020088static PyObject *vim_module;
89static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020090
Bram Moolenaar81c40c52013-06-12 14:41:04 +020091static PyObject *py_find_module;
92static PyObject *py_load_module;
93
94static PyObject *VimError;
95
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020096/*
97 * obtain a lock on the Vim data structures
98 */
99 static void
100Python_Lock_Vim(void)
101{
102}
103
104/*
105 * release a lock on the Vim data structures
106 */
107 static void
108Python_Release_Vim(void)
109{
110}
111
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200112/*
113 * The "todecref" argument holds a pointer to PyObject * that must be
114 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
115 * was needed to generate returned value is object.
116 *
117 * Use Py_XDECREF to decrement reference count.
118 */
119 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200122 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200123
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 {
126
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200127 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
128 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 return NULL;
130
131 *todecref = NULL;
132 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 PyObject *bytes;
136
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200137 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200138 return NULL;
139
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200140 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
141 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200142 {
143 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200144 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200145 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200146
147 *todecref = bytes;
148 }
149 else
150 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected str() or unicode() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200156 PyErr_FORMAT(PyExc_TypeError,
157 N_("expected bytes() or str() instance, but got %s"),
158 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200159#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160 return NULL;
161 }
162
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200163 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200164}
165
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200166#define NUMBER_LONG 1
167#define NUMBER_INT 2
168#define NUMBER_NATURAL 4
169#define NUMBER_UNSIGNED 8
170
171 static int
172NumberToLong(PyObject *obj, long *result, int flags)
173{
174#if PY_MAJOR_VERSION < 3
175 if (PyInt_Check(obj))
176 {
177 *result = PyInt_AsLong(obj);
178 if (PyErr_Occurred())
179 return -1;
180 }
181 else
182#endif
183 if (PyLong_Check(obj))
184 {
185 *result = PyLong_AsLong(obj);
186 if (PyErr_Occurred())
187 return -1;
188 }
189 else if (PyNumber_Check(obj))
190 {
191 PyObject *num;
192
193 if (!(num = PyNumber_Long(obj)))
194 return -1;
195
196 *result = PyLong_AsLong(num);
197
198 Py_DECREF(num);
199
200 if (PyErr_Occurred())
201 return -1;
202 }
203 else
204 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200205#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200207 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 "coercing to long(), but got %s"),
209 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200210#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200211 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200212 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200213 "but got %s"),
214 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200216 return -1;
217 }
218
219 if (flags & NUMBER_INT)
220 {
221 if (*result > INT_MAX)
222 {
223 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200224 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200225 return -1;
226 }
227 else if (*result < INT_MIN)
228 {
229 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200230 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200231 return -1;
232 }
233 }
234
235 if (flags & NUMBER_NATURAL)
236 {
237 if (*result <= 0)
238 {
239 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100240 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200241 return -1;
242 }
243 }
244 else if (flags & NUMBER_UNSIGNED)
245 {
246 if (*result < 0)
247 {
248 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200249 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200250 return -1;
251 }
252 }
253
254 return 0;
255}
256
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200257 static int
258add_string(PyObject *list, char *s)
259{
260 PyObject *string;
261
262 if (!(string = PyString_FromString(s)))
263 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200264
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200265 if (PyList_Append(list, string))
266 {
267 Py_DECREF(string);
268 return -1;
269 }
270
271 Py_DECREF(string);
272 return 0;
273}
274
275 static PyObject *
276ObjectDir(PyObject *self, char **attributes)
277{
278 PyMethodDef *method;
279 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200282 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 return NULL;
284
285 if (self)
286 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200287 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 return NULL;
291 }
292
293 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 return NULL;
298 }
299
300#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200301 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200302 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200303 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200304 return NULL;
305 }
306#endif
307
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200308 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200309}
310
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312 */
313
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200314/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200316
317static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318
319typedef struct
320{
321 PyObject_HEAD
322 long softspace;
323 long error;
324} OutputObject;
325
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200326static char *OutputAttrs[] = {
327 "softspace",
328 NULL
329};
330
331 static PyObject *
332OutputDir(PyObject *self)
333{
334 return ObjectDir(self, OutputAttrs);
335}
336
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200338OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200340 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200342 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200343 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200344 return -1;
345 }
346
347 if (strcmp(name, "softspace") == 0)
348 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200349 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200351 return 0;
352 }
353
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200354 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200355 return -1;
356}
357
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200358/* Buffer IO, we write one whole line at a time. */
359static garray_T io_ga = {0, 0, 1, 80, NULL};
360static writefn old_fn = NULL;
361
362 static void
363PythonIO_Flush(void)
364{
365 if (old_fn != NULL && io_ga.ga_len > 0)
366 {
367 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
368 old_fn((char_u *)io_ga.ga_data);
369 }
370 io_ga.ga_len = 0;
371}
372
373 static void
374writer(writefn fn, char_u *str, PyInt n)
375{
376 char_u *ptr;
377
378 /* Flush when switching output function. */
379 if (fn != old_fn)
380 PythonIO_Flush();
381 old_fn = fn;
382
383 /* Write each NL separated line. Text after the last NL is kept for
384 * writing later. */
385 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
386 {
387 PyInt len = ptr - str;
388
389 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
390 break;
391
392 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
393 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
394 fn((char_u *)io_ga.ga_data);
395 str = ptr + 1;
396 n -= len + 1;
397 io_ga.ga_len = 0;
398 }
399
400 /* Put the remaining text into io_ga for later printing. */
401 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
420 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
421 Python_Release_Vim();
422 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425 return 0;
426}
427
428 static PyObject *
429OutputWrite(OutputObject *self, PyObject *string)
430{
431 if (write_output(self, string))
432 return NULL;
433
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 Py_INCREF(Py_None);
435 return Py_None;
436}
437
438 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200439OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 PyObject *iterator;
442 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200447 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200449 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200450 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 Py_DECREF(iterator);
452 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200453 return NULL;
454 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456 }
457
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200458 Py_DECREF(iterator);
459
460 /* Iterator may have finished due to an exception */
461 if (PyErr_Occurred())
462 return NULL;
463
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200464 Py_INCREF(Py_None);
465 return Py_None;
466}
467
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100468 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100469AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470{
471 /* do nothing */
472 Py_INCREF(Py_None);
473 return Py_None;
474}
475
Bram Moolenaard4247472015-11-02 13:28:59 +0100476 static PyObject *
477AlwaysFalse(PyObject *self UNUSED)
478{
479 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100480 PyObject *ret = Py_False;
481 Py_INCREF(ret);
482 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100483}
484
485 static PyObject *
486AlwaysTrue(PyObject *self UNUSED)
487{
488 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100489 PyObject *ret = Py_True;
490 Py_INCREF(ret);
491 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100492}
493
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494/***************/
495
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200496static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200497 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200498 {"write", (PyCFunction)OutputWrite, METH_O, ""},
499 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100500 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
501 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
502 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
503 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
504 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
505 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200506 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200507 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200508 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200509};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200510
511static OutputObject Output =
512{
513 PyObject_HEAD_INIT(&OutputType)
514 0,
515 0
516};
517
518static OutputObject Error =
519{
520 PyObject_HEAD_INIT(&OutputType)
521 0,
522 1
523};
524
525 static int
526PythonIO_Init_io(void)
527{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200528 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
529 return -1;
530 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
531 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200532
533 if (PyErr_Occurred())
534 {
535 EMSG(_("E264: Python: Error initialising I/O objects"));
536 return -1;
537 }
538
539 return 0;
540}
541
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200542typedef struct
543{
544 PyObject_HEAD
545 PyObject *module;
546} LoaderObject;
547static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200548
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200549 static void
550LoaderDestructor(LoaderObject *self)
551{
552 Py_DECREF(self->module);
553 DESTRUCTOR_FINISH(self);
554}
555
556 static PyObject *
557LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
558{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200559 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200560
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200561 Py_INCREF(ret);
562 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200563}
564
565static struct PyMethodDef LoaderMethods[] = {
566 /* name, function, calling, doc */
567 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
568 { NULL, NULL, 0, NULL}
569};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200570
571/* Check to see whether a Vim error has been reported, or a keyboard
572 * interrupt has been detected.
573 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200574
575 static void
576VimTryStart(void)
577{
578 ++trylevel;
579}
580
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200581 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200582VimTryEnd(void)
583{
584 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100585 /* Without this it stops processing all subsequent Vim script commands and
586 * generates strange error messages if I e.g. try calling Test() in a cycle
587 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200588 did_emsg = FALSE;
589 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200590 if (got_int)
591 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100592 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100593 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100594 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200596 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200597 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100598 else if (msg_list != NULL && *msg_list != NULL)
599 {
600 int should_free;
601 char_u *msg;
602
603 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
604
605 if (msg == NULL)
606 {
607 PyErr_NoMemory();
608 return -1;
609 }
610
611 PyErr_SetVim((char *) msg);
612
613 free_global_msglist();
614
615 if (should_free)
616 vim_free(msg);
617
618 return -1;
619 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200620 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200621 return (PyErr_Occurred() ? -1 : 0);
622 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200623 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200624 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100625 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200626 return -1;
627 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100628 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200629 else
630 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200631 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200632 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200633 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200634 }
635}
636
637 static int
638VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200639{
640 if (got_int)
641 {
642 PyErr_SetNone(PyExc_KeyboardInterrupt);
643 return 1;
644 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200645 return 0;
646}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200647
648/* Vim module - Implementation
649 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200650
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200651 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200653{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200654 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200655 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200656 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200657
Bram Moolenaar389a1792013-06-23 13:00:44 +0200658 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659 return NULL;
660
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200661 Py_BEGIN_ALLOW_THREADS
662 Python_Lock_Vim();
663
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200664 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200665 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200666 update_screen(VALID);
667
668 Python_Release_Vim();
669 Py_END_ALLOW_THREADS
670
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200671 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200672 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200675
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200676 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200677 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200678 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679}
680
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200681/*
682 * Function to translate a typval_T into a PyObject; this will recursively
683 * translate lists/dictionaries into their Python equivalents.
684 *
685 * The depth parameter is to avoid infinite recursion, set it to 1 when
686 * you call VimToPython.
687 */
688 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200689VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200690{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200691 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200693 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200694
695 /* Avoid infinite recursion */
696 if (depth > 100)
697 {
698 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200699 ret = Py_None;
700 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701 }
702
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200703 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 * then and we can use it again. */
705 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
706 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
707 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200708 sprintf(ptrBuf, "%p",
709 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
710 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200714 Py_INCREF(ret);
715 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200716 }
717 }
718
719 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200720 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200721 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200722 else if (our_tv->v_type == VAR_NUMBER)
723 {
724 char buf[NUMBUFLEN];
725
726 /* For backwards compatibility numbers are stored as strings. */
727 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200728 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200729 }
730# ifdef FEAT_FLOAT
731 else if (our_tv->v_type == VAR_FLOAT)
732 {
733 char buf[NUMBUFLEN];
734
735 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200736 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
738# endif
739 else if (our_tv->v_type == VAR_LIST)
740 {
741 list_T *list = our_tv->vval.v_list;
742 listitem_T *curr;
743
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200744 if (list == NULL)
745 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200746
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200747 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200748 return NULL;
749
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200751 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200752 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200753 return NULL;
754 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200755
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200756 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
757 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200758 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200759 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200761 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200762 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200763 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200764 {
765 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200766 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200767 return NULL;
768 }
769 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770 }
771 }
772 else if (our_tv->v_type == VAR_DICT)
773 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200774
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100775 hashtab_T *ht;
776 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200777 hashitem_T *hi;
778 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100779
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 if (our_tv->vval.v_dict == NULL)
781 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100782 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200783
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 return NULL;
786
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200787 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200788 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200789 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200790 return NULL;
791 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200792
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100793 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200794 for (hi = ht->ht_array; todo > 0; ++hi)
795 {
796 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200798 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200799
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200801 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200802 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200804 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200806 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200807 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200808 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200809 Py_DECREF(newObj);
810 return NULL;
811 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200812 }
813 }
814 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100815 else if (our_tv->v_type == VAR_SPECIAL)
816 {
817 if (our_tv->vval.v_number == VVAL_FALSE)
818 {
819 ret = Py_False;
820 Py_INCREF(ret);
821 }
822 else if (our_tv->vval.v_number == VVAL_TRUE)
823 {
824 ret = Py_True;
825 Py_INCREF(ret);
826 }
827 else
828 {
829 Py_INCREF(Py_None);
830 ret = Py_None;
831 }
832 return ret;
833 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200834 else
835 {
836 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200837 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200838 }
839
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200840 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200842
843 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200844VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200845{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200846 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200847 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200848 PyObject *string;
849 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200850 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200851 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200852
Bram Moolenaar389a1792013-06-23 13:00:44 +0200853 if (!PyArg_ParseTuple(args, "O", &string))
854 return NULL;
855
856 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200857 return NULL;
858
859 Py_BEGIN_ALLOW_THREADS
860 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200861 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200862 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200863 Python_Release_Vim();
864 Py_END_ALLOW_THREADS
865
Bram Moolenaar389a1792013-06-23 13:00:44 +0200866 Py_XDECREF(todecref);
867
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200868 if (VimTryEnd())
869 return NULL;
870
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200871 if (our_tv == NULL)
872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200873 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 return NULL;
875 }
876
877 /* Convert the Vim type into a Python type. Create a dictionary that's
878 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200879 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200880 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200881 else
882 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200883 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200884 Py_DECREF(lookup_dict);
885 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200886
887
888 Py_BEGIN_ALLOW_THREADS
889 Python_Lock_Vim();
890 free_tv(our_tv);
891 Python_Release_Vim();
892 Py_END_ALLOW_THREADS
893
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200894 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200895}
896
Bram Moolenaardb913952012-06-29 12:54:53 +0200897static PyObject *ConvertToPyObject(typval_T *);
898
899 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200900VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200901{
Bram Moolenaardb913952012-06-29 12:54:53 +0200902 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200903 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200904 char_u *expr;
905 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200906
Bram Moolenaar389a1792013-06-23 13:00:44 +0200907 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200908 return NULL;
909
910 Py_BEGIN_ALLOW_THREADS
911 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200912 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200913 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200914 Python_Release_Vim();
915 Py_END_ALLOW_THREADS
916
Bram Moolenaar389a1792013-06-23 13:00:44 +0200917 Py_XDECREF(todecref);
918
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200919 if (VimTryEnd())
920 return NULL;
921
Bram Moolenaardb913952012-06-29 12:54:53 +0200922 if (our_tv == NULL)
923 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200924 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200925 return NULL;
926 }
927
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200928 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200929 Py_BEGIN_ALLOW_THREADS
930 Python_Lock_Vim();
931 free_tv(our_tv);
932 Python_Release_Vim();
933 Py_END_ALLOW_THREADS
934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200935 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200936}
937
938 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200939VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200940{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200941 char_u *str;
942 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200943 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200944
Bram Moolenaar389a1792013-06-23 13:00:44 +0200945 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200946 return NULL;
947
Bram Moolenaara54bf402012-12-05 16:30:07 +0100948#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200949 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100950#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200951 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100952#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953
954 Py_XDECREF(todecref);
955
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200956 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200957}
958
Bram Moolenaarf4258302013-06-02 18:20:17 +0200959 static PyObject *
960_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
961{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200962 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200963 PyObject *newwd;
964 PyObject *todecref;
965 char_u *new_dir;
966
Bram Moolenaard4209d22013-06-05 20:34:15 +0200967 if (_chdir == NULL)
968 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200969 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200970 return NULL;
971
972 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
973 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200974 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200975 return NULL;
976 }
977
978 if (!(new_dir = StringToChars(newwd, &todecref)))
979 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200980 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200981 Py_DECREF(newwd);
982 return NULL;
983 }
984
985 VimTryStart();
986
987 if (vim_chdir(new_dir))
988 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200989 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200990 Py_DECREF(newwd);
991 Py_XDECREF(todecref);
992
993 if (VimTryEnd())
994 return NULL;
995
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200996 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200997 return NULL;
998 }
999
1000 Py_DECREF(newwd);
1001 Py_XDECREF(todecref);
1002
1003 post_chdir(FALSE);
1004
1005 if (VimTryEnd())
1006 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001007 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001008 return NULL;
1009 }
1010
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001011 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001012}
1013
1014 static PyObject *
1015VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1016{
1017 return _VimChdir(py_chdir, args, kwargs);
1018}
1019
1020 static PyObject *
1021VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1022{
1023 return _VimChdir(py_fchdir, args, kwargs);
1024}
1025
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001026typedef struct {
1027 PyObject *callable;
1028 PyObject *result;
1029} map_rtp_data;
1030
1031 static void
1032map_rtp_callback(char_u *path, void *_data)
1033{
1034 void **data = (void **) _data;
1035 PyObject *pathObject;
1036 map_rtp_data *mr_data = *((map_rtp_data **) data);
1037
Bram Moolenaar41009372013-07-01 22:03:04 +02001038 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001039 {
1040 *data = NULL;
1041 return;
1042 }
1043
1044 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1045 pathObject, NULL);
1046
1047 Py_DECREF(pathObject);
1048
1049 if (!mr_data->result || mr_data->result != Py_None)
1050 *data = NULL;
1051 else
1052 {
1053 Py_DECREF(mr_data->result);
1054 mr_data->result = NULL;
1055 }
1056}
1057
1058 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001059VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001060{
1061 map_rtp_data data;
1062
Bram Moolenaar389a1792013-06-23 13:00:44 +02001063 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001064 data.result = NULL;
1065
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001066 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001067
1068 if (data.result == NULL)
1069 {
1070 if (PyErr_Occurred())
1071 return NULL;
1072 else
1073 {
1074 Py_INCREF(Py_None);
1075 return Py_None;
1076 }
1077 }
1078 return data.result;
1079}
1080
1081/*
1082 * _vim_runtimepath_ special path implementation.
1083 */
1084
1085 static void
1086map_finder_callback(char_u *path, void *_data)
1087{
1088 void **data = (void **) _data;
1089 PyObject *list = *((PyObject **) data);
1090 PyObject *pathObject1, *pathObject2;
1091 char *pathbuf;
1092 size_t pathlen;
1093
1094 pathlen = STRLEN(path);
1095
1096#if PY_MAJOR_VERSION < 3
1097# define PY_MAIN_DIR_STRING "python2"
1098#else
1099# define PY_MAIN_DIR_STRING "python3"
1100#endif
1101#define PY_ALTERNATE_DIR_STRING "pythonx"
1102
1103#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1104 if (!(pathbuf = PyMem_New(char,
1105 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1106 {
1107 PyErr_NoMemory();
1108 *data = NULL;
1109 return;
1110 }
1111
1112 mch_memmove(pathbuf, path, pathlen + 1);
1113 add_pathsep((char_u *) pathbuf);
1114
1115 pathlen = STRLEN(pathbuf);
1116 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1117 PYTHONX_STRING_LENGTH + 1);
1118
1119 if (!(pathObject1 = PyString_FromString(pathbuf)))
1120 {
1121 *data = NULL;
1122 PyMem_Free(pathbuf);
1123 return;
1124 }
1125
1126 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1127 PYTHONX_STRING_LENGTH + 1);
1128
1129 if (!(pathObject2 = PyString_FromString(pathbuf)))
1130 {
1131 Py_DECREF(pathObject1);
1132 PyMem_Free(pathbuf);
1133 *data = NULL;
1134 return;
1135 }
1136
1137 PyMem_Free(pathbuf);
1138
1139 if (PyList_Append(list, pathObject1)
1140 || PyList_Append(list, pathObject2))
1141 *data = NULL;
1142
1143 Py_DECREF(pathObject1);
1144 Py_DECREF(pathObject2);
1145}
1146
1147 static PyObject *
1148Vim_GetPaths(PyObject *self UNUSED)
1149{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001150 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001151
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001152 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001153 return NULL;
1154
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001155 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001156
1157 if (PyErr_Occurred())
1158 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001159 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001160 return NULL;
1161 }
1162
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001163 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001164}
1165
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001166 static PyObject *
1167call_load_module(char *name, int len, PyObject *find_module_result)
1168{
1169 PyObject *fd, *pathname, *description;
1170
Bram Moolenaarc476e522013-06-23 13:46:40 +02001171 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001172 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001173 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001174 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001175 Py_TYPE_NAME(find_module_result));
1176 return NULL;
1177 }
1178 if (PyTuple_GET_SIZE(find_module_result) != 3)
1179 {
1180 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001181 N_("expected 3-tuple as imp.find_module() result, but got "
1182 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001183 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001184 return NULL;
1185 }
1186
1187 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1188 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1189 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1190 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001191 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001192 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001193 return NULL;
1194 }
1195
1196 return PyObject_CallFunction(py_load_module,
1197 "s#OOO", name, len, fd, pathname, description);
1198}
1199
1200 static PyObject *
1201find_module(char *fullname, char *tail, PyObject *new_path)
1202{
1203 PyObject *find_module_result;
1204 PyObject *module;
1205 char *dot;
1206
Bram Moolenaar41009372013-07-01 22:03:04 +02001207 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001208 {
1209 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001210 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001211 * first component
1212 */
1213 PyObject *newest_path;
1214 int partlen = (int) (dot - 1 - tail);
1215
1216 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1217 "s#O", tail, partlen, new_path)))
1218 return NULL;
1219
1220 if (!(module = call_load_module(
1221 fullname,
1222 ((int) (tail - fullname)) + partlen,
1223 find_module_result)))
1224 {
1225 Py_DECREF(find_module_result);
1226 return NULL;
1227 }
1228
1229 Py_DECREF(find_module_result);
1230
1231 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1232 {
1233 Py_DECREF(module);
1234 return NULL;
1235 }
1236
1237 Py_DECREF(module);
1238
1239 module = find_module(fullname, dot + 1, newest_path);
1240
1241 Py_DECREF(newest_path);
1242
1243 return module;
1244 }
1245 else
1246 {
1247 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1248 "sO", tail, new_path)))
1249 return NULL;
1250
1251 if (!(module = call_load_module(
1252 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001253 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001254 find_module_result)))
1255 {
1256 Py_DECREF(find_module_result);
1257 return NULL;
1258 }
1259
1260 Py_DECREF(find_module_result);
1261
1262 return module;
1263 }
1264}
1265
1266 static PyObject *
1267FinderFindModule(PyObject *self, PyObject *args)
1268{
1269 char *fullname;
1270 PyObject *module;
1271 PyObject *new_path;
1272 LoaderObject *loader;
1273
1274 if (!PyArg_ParseTuple(args, "s", &fullname))
1275 return NULL;
1276
1277 if (!(new_path = Vim_GetPaths(self)))
1278 return NULL;
1279
1280 module = find_module(fullname, fullname, new_path);
1281
1282 Py_DECREF(new_path);
1283
1284 if (!module)
1285 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001286 if (PyErr_Occurred())
1287 {
1288 if (PyErr_ExceptionMatches(PyExc_ImportError))
1289 PyErr_Clear();
1290 else
1291 return NULL;
1292 }
1293
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001294 Py_INCREF(Py_None);
1295 return Py_None;
1296 }
1297
1298 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1299 {
1300 Py_DECREF(module);
1301 return NULL;
1302 }
1303
1304 loader->module = module;
1305
1306 return (PyObject *) loader;
1307}
1308
1309 static PyObject *
1310VimPathHook(PyObject *self UNUSED, PyObject *args)
1311{
1312 char *path;
1313
1314 if (PyArg_ParseTuple(args, "s", &path)
1315 && STRCMP(path, vim_special_path) == 0)
1316 {
1317 Py_INCREF(vim_module);
1318 return vim_module;
1319 }
1320
1321 PyErr_Clear();
1322 PyErr_SetNone(PyExc_ImportError);
1323 return NULL;
1324}
1325
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326/*
1327 * Vim module - Definitions
1328 */
1329
1330static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001331 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001332 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001333 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001334 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1335 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001336 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1337 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001338 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001339 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001340 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1341 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1342 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001343};
1344
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001346 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001347 */
1348
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001349static PyTypeObject IterType;
1350
1351typedef PyObject *(*nextfun)(void **);
1352typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001353typedef int (*traversefun)(void *, visitproc, void *);
1354typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001355
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001356/* Main purpose of this object is removing the need for do python
1357 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1358 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001359
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001360typedef struct
1361{
1362 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001363 void *cur;
1364 nextfun next;
1365 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 traversefun traverse;
1367 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001368} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001369
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001370 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001371IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1372 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001373{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001374 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001375
Bram Moolenaar774267b2013-05-21 20:51:59 +02001376 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001377 self->cur = start;
1378 self->next = next;
1379 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001380 self->traverse = traverse;
1381 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001382
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001383 return (PyObject *)(self);
1384}
1385
1386 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001387IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001388{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001389 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001390 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001391 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001392}
1393
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001394 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001395IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001396{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001397 if (self->traverse != NULL)
1398 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001399 else
1400 return 0;
1401}
1402
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001403/* Mac OSX defines clear() somewhere. */
1404#ifdef clear
1405# undef clear
1406#endif
1407
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001408 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001409IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001410{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001411 if (self->clear != NULL)
1412 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001413 else
1414 return 0;
1415}
1416
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001417 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001418IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001419{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001420 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001421}
1422
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001423 static PyObject *
1424IterIter(PyObject *self)
1425{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001426 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001427 return self;
1428}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001429
Bram Moolenaardb913952012-06-29 12:54:53 +02001430typedef struct pylinkedlist_S {
1431 struct pylinkedlist_S *pll_next;
1432 struct pylinkedlist_S *pll_prev;
1433 PyObject *pll_obj;
1434} pylinkedlist_T;
1435
1436static pylinkedlist_T *lastdict = NULL;
1437static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001438static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001439
1440 static void
1441pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1442{
1443 if (ref->pll_prev == NULL)
1444 {
1445 if (ref->pll_next == NULL)
1446 {
1447 *last = NULL;
1448 return;
1449 }
1450 }
1451 else
1452 ref->pll_prev->pll_next = ref->pll_next;
1453
1454 if (ref->pll_next == NULL)
1455 *last = ref->pll_prev;
1456 else
1457 ref->pll_next->pll_prev = ref->pll_prev;
1458}
1459
1460 static void
1461pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1462{
1463 if (*last == NULL)
1464 ref->pll_prev = NULL;
1465 else
1466 {
1467 (*last)->pll_next = ref;
1468 ref->pll_prev = *last;
1469 }
1470 ref->pll_next = NULL;
1471 ref->pll_obj = self;
1472 *last = ref;
1473}
1474
1475static PyTypeObject DictionaryType;
1476
1477typedef struct
1478{
1479 PyObject_HEAD
1480 dict_T *dict;
1481 pylinkedlist_T ref;
1482} DictionaryObject;
1483
Bram Moolenaara9922d62013-05-30 13:01:18 +02001484static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1485
1486#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1487
Bram Moolenaardb913952012-06-29 12:54:53 +02001488 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001489DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001490{
1491 DictionaryObject *self;
1492
Bram Moolenaara9922d62013-05-30 13:01:18 +02001493 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001494 if (self == NULL)
1495 return NULL;
1496 self->dict = dict;
1497 ++dict->dv_refcount;
1498
1499 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1500
1501 return (PyObject *)(self);
1502}
1503
Bram Moolenaara9922d62013-05-30 13:01:18 +02001504 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001505py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001506{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001507 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001508
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001509 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001510 {
1511 PyErr_NoMemory();
1512 return NULL;
1513 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001514 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001515
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001516 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001517}
1518
1519 static PyObject *
1520DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1521{
1522 DictionaryObject *self;
1523 dict_T *dict;
1524
1525 if (!(dict = py_dict_alloc()))
1526 return NULL;
1527
1528 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1529
1530 --dict->dv_refcount;
1531
1532 if (kwargs || PyTuple_Size(args))
1533 {
1534 PyObject *tmp;
1535 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1536 {
1537 Py_DECREF(self);
1538 return NULL;
1539 }
1540
1541 Py_DECREF(tmp);
1542 }
1543
1544 return (PyObject *)(self);
1545}
1546
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001547 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001548DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001549{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001550 pyll_remove(&self->ref, &lastdict);
1551 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001552
1553 DESTRUCTOR_FINISH(self);
1554}
1555
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001556static char *DictionaryAttrs[] = {
1557 "locked", "scope",
1558 NULL
1559};
1560
1561 static PyObject *
1562DictionaryDir(PyObject *self)
1563{
1564 return ObjectDir(self, DictionaryAttrs);
1565}
1566
Bram Moolenaardb913952012-06-29 12:54:53 +02001567 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001568DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001569{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001570 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001571 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001572 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001573 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001574 return -1;
1575 }
1576
1577 if (strcmp(name, "locked") == 0)
1578 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001579 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001580 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001581 PyErr_SET_STRING(PyExc_TypeError,
1582 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001583 return -1;
1584 }
1585 else
1586 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001587 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001588 if (istrue == -1)
1589 return -1;
1590 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001591 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001592 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001593 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001594 }
1595 return 0;
1596 }
1597 else
1598 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001599 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001600 return -1;
1601 }
1602}
1603
1604 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001605DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001606{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001607 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001608}
1609
Bram Moolenaara9922d62013-05-30 13:01:18 +02001610#define DICT_FLAG_HAS_DEFAULT 0x01
1611#define DICT_FLAG_POP 0x02
1612#define DICT_FLAG_NONE_DEFAULT 0x04
1613#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1614#define DICT_FLAG_RETURN_PAIR 0x10
1615
Bram Moolenaardb913952012-06-29 12:54:53 +02001616 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001617_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001618{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001619 PyObject *keyObject;
1620 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001621 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001622 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001623 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001624 dict_T *dict = self->dict;
1625 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001626 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001627
Bram Moolenaara9922d62013-05-30 13:01:18 +02001628 if (flags & DICT_FLAG_HAS_DEFAULT)
1629 {
1630 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1631 return NULL;
1632 }
1633 else
1634 keyObject = args;
1635
1636 if (flags & DICT_FLAG_RETURN_BOOL)
1637 defObject = Py_False;
1638
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001639 if (!(key = StringToChars(keyObject, &todecref)))
1640 return NULL;
1641
1642 if (*key == NUL)
1643 {
1644 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001645 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001646 return NULL;
1647 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001648
Bram Moolenaara9922d62013-05-30 13:01:18 +02001649 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001650
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001651 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001652
Bram Moolenaara9922d62013-05-30 13:01:18 +02001653 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001654 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001655 if (defObject)
1656 {
1657 Py_INCREF(defObject);
1658 return defObject;
1659 }
1660 else
1661 {
1662 PyErr_SetObject(PyExc_KeyError, keyObject);
1663 return NULL;
1664 }
1665 }
1666 else if (flags & DICT_FLAG_RETURN_BOOL)
1667 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001668 ret = Py_True;
1669 Py_INCREF(ret);
1670 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001671 }
1672
1673 di = dict_lookup(hi);
1674
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001675 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001676 return NULL;
1677
1678 if (flags & DICT_FLAG_POP)
1679 {
1680 if (dict->dv_lock)
1681 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001682 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001683 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001684 return NULL;
1685 }
1686
1687 hash_remove(&dict->dv_hashtab, hi);
1688 dictitem_free(di);
1689 }
1690
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001691 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692}
1693
1694 static PyObject *
1695DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1696{
1697 return _DictionaryItem(self, keyObject, 0);
1698}
1699
1700 static int
1701DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1702{
1703 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001704 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001706 if (rObj == NULL)
1707 return -1;
1708
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001709 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001710
Bram Moolenaardee2e312013-06-23 16:35:47 +02001711 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001712
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001713 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001714}
1715
1716typedef struct
1717{
1718 hashitem_T *ht_array;
1719 long_u ht_used;
1720 hashtab_T *ht;
1721 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001722 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001723} dictiterinfo_T;
1724
1725 static PyObject *
1726DictionaryIterNext(dictiterinfo_T **dii)
1727{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001728 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001729
1730 if (!(*dii)->todo)
1731 return NULL;
1732
1733 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1734 (*dii)->ht->ht_used != (*dii)->ht_used)
1735 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001736 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001737 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001738 return NULL;
1739 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001740
Bram Moolenaara9922d62013-05-30 13:01:18 +02001741 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1742 ++((*dii)->hi);
1743
1744 --((*dii)->todo);
1745
Bram Moolenaar41009372013-07-01 22:03:04 +02001746 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747 return NULL;
1748
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001749 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001750}
1751
1752 static PyObject *
1753DictionaryIter(DictionaryObject *self)
1754{
1755 dictiterinfo_T *dii;
1756 hashtab_T *ht;
1757
1758 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1759 {
1760 PyErr_NoMemory();
1761 return NULL;
1762 }
1763
1764 ht = &self->dict->dv_hashtab;
1765 dii->ht_array = ht->ht_array;
1766 dii->ht_used = ht->ht_used;
1767 dii->ht = ht;
1768 dii->hi = dii->ht_array;
1769 dii->todo = dii->ht_used;
1770
1771 return IterNew(dii,
1772 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1773 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001774}
1775
1776 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001777DictionaryAssItem(
1778 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001779{
1780 char_u *key;
1781 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001782 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001783 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001784 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001785
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001786 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001787 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001788 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001789 return -1;
1790 }
1791
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001792 if (!(key = StringToChars(keyObject, &todecref)))
1793 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001794
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001795 if (*key == NUL)
1796 {
1797 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001798 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001799 return -1;
1800 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001801
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001802 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001803
1804 if (valObject == NULL)
1805 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001806 hashitem_T *hi;
1807
Bram Moolenaardb913952012-06-29 12:54:53 +02001808 if (di == NULL)
1809 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001810 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001811 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001812 return -1;
1813 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001814 hi = hash_find(&dict->dv_hashtab, di->di_key);
1815 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001816 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001817 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001818 return 0;
1819 }
1820
1821 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001822 {
1823 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001824 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001825 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001826
1827 if (di == NULL)
1828 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001830 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001831 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001832 PyErr_NoMemory();
1833 return -1;
1834 }
1835 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001836 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001837
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001838 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001839 {
1840 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001841 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001842 RAISE_KEY_ADD_FAIL(key);
1843 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001844 return -1;
1845 }
1846 }
1847 else
1848 clear_tv(&di->di_tv);
1849
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001850 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001851
1852 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001853 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001854 return 0;
1855}
1856
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857typedef PyObject *(*hi_to_py)(hashitem_T *);
1858
Bram Moolenaardb913952012-06-29 12:54:53 +02001859 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001860DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001861{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001862 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001863 long_u todo = dict->dv_hashtab.ht_used;
1864 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001865 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001867 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001868
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001869 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1871 {
1872 if (!HASHITEM_EMPTY(hi))
1873 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001874 if (!(newObj = hiconvert(hi)))
1875 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001876 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001877 return NULL;
1878 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001879 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001880 --todo;
1881 ++i;
1882 }
1883 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001884 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001885}
1886
Bram Moolenaara9922d62013-05-30 13:01:18 +02001887 static PyObject *
1888dict_key(hashitem_T *hi)
1889{
1890 return PyBytes_FromString((char *)(hi->hi_key));
1891}
1892
1893 static PyObject *
1894DictionaryListKeys(DictionaryObject *self)
1895{
1896 return DictionaryListObjects(self, dict_key);
1897}
1898
1899 static PyObject *
1900dict_val(hashitem_T *hi)
1901{
1902 dictitem_T *di;
1903
1904 di = dict_lookup(hi);
1905 return ConvertToPyObject(&di->di_tv);
1906}
1907
1908 static PyObject *
1909DictionaryListValues(DictionaryObject *self)
1910{
1911 return DictionaryListObjects(self, dict_val);
1912}
1913
1914 static PyObject *
1915dict_item(hashitem_T *hi)
1916{
1917 PyObject *keyObject;
1918 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001919 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001920
1921 if (!(keyObject = dict_key(hi)))
1922 return NULL;
1923
1924 if (!(valObject = dict_val(hi)))
1925 {
1926 Py_DECREF(keyObject);
1927 return NULL;
1928 }
1929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001930 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001931
1932 Py_DECREF(keyObject);
1933 Py_DECREF(valObject);
1934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001935 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001936}
1937
1938 static PyObject *
1939DictionaryListItems(DictionaryObject *self)
1940{
1941 return DictionaryListObjects(self, dict_item);
1942}
1943
1944 static PyObject *
1945DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1946{
1947 dict_T *dict = self->dict;
1948
1949 if (dict->dv_lock)
1950 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001951 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001952 return NULL;
1953 }
1954
1955 if (kwargs)
1956 {
1957 typval_T tv;
1958
1959 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1960 return NULL;
1961
1962 VimTryStart();
1963 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1964 clear_tv(&tv);
1965 if (VimTryEnd())
1966 return NULL;
1967 }
1968 else
1969 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001970 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001971
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001972 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001973 return NULL;
1974
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001975 if (obj == NULL)
1976 {
1977 Py_INCREF(Py_None);
1978 return Py_None;
1979 }
1980
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001981 if (PyObject_HasAttrString(obj, "keys"))
1982 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001983 else
1984 {
1985 PyObject *iterator;
1986 PyObject *item;
1987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001988 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001989 return NULL;
1990
1991 while ((item = PyIter_Next(iterator)))
1992 {
1993 PyObject *fast;
1994 PyObject *keyObject;
1995 PyObject *valObject;
1996 PyObject *todecref;
1997 char_u *key;
1998 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01001999 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002000
2001 if (!(fast = PySequence_Fast(item, "")))
2002 {
2003 Py_DECREF(iterator);
2004 Py_DECREF(item);
2005 return NULL;
2006 }
2007
2008 Py_DECREF(item);
2009
2010 if (PySequence_Fast_GET_SIZE(fast) != 2)
2011 {
2012 Py_DECREF(iterator);
2013 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002014 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002015 N_("expected sequence element of size 2, "
2016 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002017 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002018 return NULL;
2019 }
2020
2021 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2022
2023 if (!(key = StringToChars(keyObject, &todecref)))
2024 {
2025 Py_DECREF(iterator);
2026 Py_DECREF(fast);
2027 return NULL;
2028 }
2029
2030 di = dictitem_alloc(key);
2031
2032 Py_XDECREF(todecref);
2033
2034 if (di == NULL)
2035 {
2036 Py_DECREF(fast);
2037 Py_DECREF(iterator);
2038 PyErr_NoMemory();
2039 return NULL;
2040 }
2041 di->di_tv.v_lock = 0;
2042 di->di_tv.v_type = VAR_UNKNOWN;
2043
2044 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2045
2046 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2047 {
2048 Py_DECREF(iterator);
2049 Py_DECREF(fast);
2050 dictitem_free(di);
2051 return NULL;
2052 }
2053
2054 Py_DECREF(fast);
2055
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002056 hi = hash_find(&dict->dv_hashtab, di->di_key);
2057 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002058 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002059 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002060 Py_DECREF(iterator);
2061 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002062 return NULL;
2063 }
2064 }
2065
2066 Py_DECREF(iterator);
2067
2068 /* Iterator may have finished due to an exception */
2069 if (PyErr_Occurred())
2070 return NULL;
2071 }
2072 }
2073 Py_INCREF(Py_None);
2074 return Py_None;
2075}
2076
2077 static PyObject *
2078DictionaryGet(DictionaryObject *self, PyObject *args)
2079{
2080 return _DictionaryItem(self, args,
2081 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2082}
2083
2084 static PyObject *
2085DictionaryPop(DictionaryObject *self, PyObject *args)
2086{
2087 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2088}
2089
2090 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002091DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002092{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002093 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002094 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002095 PyObject *valObject;
2096 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002097
Bram Moolenaarde71b562013-06-02 17:41:54 +02002098 if (self->dict->dv_hashtab.ht_used == 0)
2099 {
2100 PyErr_SetNone(PyExc_KeyError);
2101 return NULL;
2102 }
2103
2104 hi = self->dict->dv_hashtab.ht_array;
2105 while (HASHITEM_EMPTY(hi))
2106 ++hi;
2107
2108 di = dict_lookup(hi);
2109
2110 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002111 return NULL;
2112
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002113 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002114 {
2115 Py_DECREF(valObject);
2116 return NULL;
2117 }
2118
2119 hash_remove(&self->dict->dv_hashtab, hi);
2120 dictitem_free(di);
2121
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002122 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002123}
2124
2125 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002126DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002127{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002128 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2129}
2130
2131static PySequenceMethods DictionaryAsSeq = {
2132 0, /* sq_length */
2133 0, /* sq_concat */
2134 0, /* sq_repeat */
2135 0, /* sq_item */
2136 0, /* sq_slice */
2137 0, /* sq_ass_item */
2138 0, /* sq_ass_slice */
2139 (objobjproc) DictionaryContains, /* sq_contains */
2140 0, /* sq_inplace_concat */
2141 0, /* sq_inplace_repeat */
2142};
2143
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002144static PyMappingMethods DictionaryAsMapping = {
2145 (lenfunc) DictionaryLength,
2146 (binaryfunc) DictionaryItem,
2147 (objobjargproc) DictionaryAssItem,
2148};
2149
Bram Moolenaardb913952012-06-29 12:54:53 +02002150static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002151 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002152 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2153 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2154 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2155 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2156 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002157 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002158 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002159 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2160 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002161};
2162
2163static PyTypeObject ListType;
2164
2165typedef struct
2166{
2167 PyObject_HEAD
2168 list_T *list;
2169 pylinkedlist_T ref;
2170} ListObject;
2171
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002172#define NEW_LIST(list) ListNew(&ListType, list)
2173
Bram Moolenaardb913952012-06-29 12:54:53 +02002174 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002175ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002176{
2177 ListObject *self;
2178
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002179 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002180 if (self == NULL)
2181 return NULL;
2182 self->list = list;
2183 ++list->lv_refcount;
2184
2185 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2186
2187 return (PyObject *)(self);
2188}
2189
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002190 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002191py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002192{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002193 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002194
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002195 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002196 {
2197 PyErr_NoMemory();
2198 return NULL;
2199 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002200 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002201
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002202 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002203}
2204
2205 static int
2206list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2207{
2208 PyObject *iterator;
2209 PyObject *item;
2210 listitem_T *li;
2211
2212 if (!(iterator = PyObject_GetIter(obj)))
2213 return -1;
2214
2215 while ((item = PyIter_Next(iterator)))
2216 {
2217 if (!(li = listitem_alloc()))
2218 {
2219 PyErr_NoMemory();
2220 Py_DECREF(item);
2221 Py_DECREF(iterator);
2222 return -1;
2223 }
2224 li->li_tv.v_lock = 0;
2225 li->li_tv.v_type = VAR_UNKNOWN;
2226
2227 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2228 {
2229 Py_DECREF(item);
2230 Py_DECREF(iterator);
2231 listitem_free(li);
2232 return -1;
2233 }
2234
2235 Py_DECREF(item);
2236
2237 list_append(l, li);
2238 }
2239
2240 Py_DECREF(iterator);
2241
2242 /* Iterator may have finished due to an exception */
2243 if (PyErr_Occurred())
2244 return -1;
2245
2246 return 0;
2247}
2248
2249 static PyObject *
2250ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2251{
2252 list_T *list;
2253 PyObject *obj = NULL;
2254
2255 if (kwargs)
2256 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002257 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002258 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002259 return NULL;
2260 }
2261
2262 if (!PyArg_ParseTuple(args, "|O", &obj))
2263 return NULL;
2264
2265 if (!(list = py_list_alloc()))
2266 return NULL;
2267
2268 if (obj)
2269 {
2270 PyObject *lookup_dict;
2271
2272 if (!(lookup_dict = PyDict_New()))
2273 {
2274 list_unref(list);
2275 return NULL;
2276 }
2277
2278 if (list_py_concat(list, obj, lookup_dict) == -1)
2279 {
2280 Py_DECREF(lookup_dict);
2281 list_unref(list);
2282 return NULL;
2283 }
2284
2285 Py_DECREF(lookup_dict);
2286 }
2287
2288 return ListNew(subtype, list);
2289}
2290
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002291 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002292ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002293{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002294 pyll_remove(&self->ref, &lastlist);
2295 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002296
2297 DESTRUCTOR_FINISH(self);
2298}
2299
Bram Moolenaardb913952012-06-29 12:54:53 +02002300 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002301ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002302{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002303 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002304}
2305
2306 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002307ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002308{
2309 listitem_T *li;
2310
Bram Moolenaard6e39182013-05-21 18:30:34 +02002311 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002312 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002313 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002314 return NULL;
2315 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002316 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002317 if (li == NULL)
2318 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002319 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002320 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002321 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002322 return NULL;
2323 }
2324 return ConvertToPyObject(&li->li_tv);
2325}
2326
Bram Moolenaardb913952012-06-29 12:54:53 +02002327 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002328ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2329 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002330{
2331 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002332 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002333
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002334 if (step == 0)
2335 {
2336 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2337 return NULL;
2338 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002339
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002340 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002341 if (list == NULL)
2342 return NULL;
2343
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002344 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002345 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002346 PyObject *item;
2347
2348 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002349 if (item == NULL)
2350 {
2351 Py_DECREF(list);
2352 return NULL;
2353 }
2354
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002355 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002356 }
2357
2358 return list;
2359}
2360
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002361 static PyObject *
2362ListItem(ListObject *self, PyObject* idx)
2363{
2364#if PY_MAJOR_VERSION < 3
2365 if (PyInt_Check(idx))
2366 {
2367 long _idx = PyInt_AsLong(idx);
2368 return ListIndex(self, _idx);
2369 }
2370 else
2371#endif
2372 if (PyLong_Check(idx))
2373 {
2374 long _idx = PyLong_AsLong(idx);
2375 return ListIndex(self, _idx);
2376 }
2377 else if (PySlice_Check(idx))
2378 {
2379 Py_ssize_t start, stop, step, slicelen;
2380
Bram Moolenaar922a4662014-03-30 16:11:43 +02002381 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002382 &start, &stop, &step, &slicelen) < 0)
2383 return NULL;
2384 return ListSlice(self, start, step, slicelen);
2385 }
2386 else
2387 {
2388 RAISE_INVALID_INDEX_TYPE(idx);
2389 return NULL;
2390 }
2391}
2392
2393 static void
2394list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2395 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2396{
2397 while (numreplaced--)
2398 {
2399 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2400 listitem_remove(l, lis[slicelen + numreplaced]);
2401 }
2402 while (numadded--)
2403 {
2404 listitem_T *next;
2405
2406 next = lastaddedli->li_prev;
2407 listitem_remove(l, lastaddedli);
2408 lastaddedli = next;
2409 }
2410}
2411
2412 static int
2413ListAssSlice(ListObject *self, Py_ssize_t first,
2414 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2415{
2416 PyObject *iterator;
2417 PyObject *item;
2418 listitem_T *li;
2419 listitem_T *lastaddedli = NULL;
2420 listitem_T *next;
2421 typval_T v;
2422 list_T *l = self->list;
2423 PyInt i;
2424 PyInt j;
2425 PyInt numreplaced = 0;
2426 PyInt numadded = 0;
2427 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002428 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002429
2430 size = ListLength(self);
2431
2432 if (l->lv_lock)
2433 {
2434 RAISE_LOCKED_LIST;
2435 return -1;
2436 }
2437
2438 if (step == 0)
2439 {
2440 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2441 return -1;
2442 }
2443
2444 if (step != 1 && slicelen == 0)
2445 {
2446 /* Nothing to do. Only error out if obj has some items. */
2447 int ret = 0;
2448
2449 if (obj == NULL)
2450 return 0;
2451
2452 if (!(iterator = PyObject_GetIter(obj)))
2453 return -1;
2454
2455 if ((item = PyIter_Next(iterator)))
2456 {
2457 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002458 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002459 "to extended slice"), 0);
2460 Py_DECREF(item);
2461 ret = -1;
2462 }
2463 Py_DECREF(iterator);
2464 return ret;
2465 }
2466
2467 if (obj != NULL)
2468 /* XXX May allocate zero bytes. */
2469 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2470 {
2471 PyErr_NoMemory();
2472 return -1;
2473 }
2474
2475 if (first == size)
2476 li = NULL;
2477 else
2478 {
2479 li = list_find(l, (long) first);
2480 if (li == NULL)
2481 {
2482 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2483 (int)first);
2484 if (obj != NULL)
2485 PyMem_Free(lis);
2486 return -1;
2487 }
2488 i = slicelen;
2489 while (i-- && li != NULL)
2490 {
2491 j = step;
2492 next = li;
2493 if (step > 0)
2494 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2495 else
2496 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2497
2498 if (obj == NULL)
2499 listitem_remove(l, li);
2500 else
2501 lis[slicelen - i - 1] = li;
2502
2503 li = next;
2504 }
2505 if (li == NULL && i != -1)
2506 {
2507 PyErr_SET_VIM(N_("internal error: not enough list items"));
2508 if (obj != NULL)
2509 PyMem_Free(lis);
2510 return -1;
2511 }
2512 }
2513
2514 if (obj == NULL)
2515 return 0;
2516
2517 if (!(iterator = PyObject_GetIter(obj)))
2518 {
2519 PyMem_Free(lis);
2520 return -1;
2521 }
2522
2523 i = 0;
2524 while ((item = PyIter_Next(iterator)))
2525 {
2526 if (ConvertFromPyObject(item, &v) == -1)
2527 {
2528 Py_DECREF(iterator);
2529 Py_DECREF(item);
2530 PyMem_Free(lis);
2531 return -1;
2532 }
2533 Py_DECREF(item);
2534 if (list_insert_tv(l, &v, numreplaced < slicelen
2535 ? lis[numreplaced]
2536 : li) == FAIL)
2537 {
2538 clear_tv(&v);
2539 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2540 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2541 PyMem_Free(lis);
2542 return -1;
2543 }
2544 if (numreplaced < slicelen)
2545 {
2546 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002547 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002548 numreplaced++;
2549 }
2550 else
2551 {
2552 if (li)
2553 lastaddedli = li->li_prev;
2554 else
2555 lastaddedli = l->lv_last;
2556 numadded++;
2557 }
2558 clear_tv(&v);
2559 if (step != 1 && i >= slicelen)
2560 {
2561 Py_DECREF(iterator);
2562 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002563 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002564 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002565 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2566 PyMem_Free(lis);
2567 return -1;
2568 }
2569 ++i;
2570 }
2571 Py_DECREF(iterator);
2572
2573 if (step != 1 && i != slicelen)
2574 {
2575 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002576 N_("attempt to assign sequence of size %d to extended slice "
2577 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002578 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2579 PyMem_Free(lis);
2580 return -1;
2581 }
2582
2583 if (PyErr_Occurred())
2584 {
2585 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2586 PyMem_Free(lis);
2587 return -1;
2588 }
2589
2590 for (i = 0; i < numreplaced; i++)
2591 listitem_free(lis[i]);
2592 if (step == 1)
2593 for (i = numreplaced; i < slicelen; i++)
2594 listitem_remove(l, lis[i]);
2595
2596 PyMem_Free(lis);
2597
2598 return 0;
2599}
2600
2601 static int
2602ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2603{
2604 typval_T tv;
2605 list_T *l = self->list;
2606 listitem_T *li;
2607 Py_ssize_t length = ListLength(self);
2608
2609 if (l->lv_lock)
2610 {
2611 RAISE_LOCKED_LIST;
2612 return -1;
2613 }
2614 if (index > length || (index == length && obj == NULL))
2615 {
2616 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2617 return -1;
2618 }
2619
2620 if (obj == NULL)
2621 {
2622 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002623 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002624 clear_tv(&li->li_tv);
2625 vim_free(li);
2626 return 0;
2627 }
2628
2629 if (ConvertFromPyObject(obj, &tv) == -1)
2630 return -1;
2631
2632 if (index == length)
2633 {
2634 if (list_append_tv(l, &tv) == FAIL)
2635 {
2636 clear_tv(&tv);
2637 PyErr_SET_VIM(N_("failed to add item to list"));
2638 return -1;
2639 }
2640 }
2641 else
2642 {
2643 li = list_find(l, (long) index);
2644 clear_tv(&li->li_tv);
2645 copy_tv(&tv, &li->li_tv);
2646 clear_tv(&tv);
2647 }
2648 return 0;
2649}
2650
2651 static Py_ssize_t
2652ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2653{
2654#if PY_MAJOR_VERSION < 3
2655 if (PyInt_Check(idx))
2656 {
2657 long _idx = PyInt_AsLong(idx);
2658 return ListAssIndex(self, _idx, obj);
2659 }
2660 else
2661#endif
2662 if (PyLong_Check(idx))
2663 {
2664 long _idx = PyLong_AsLong(idx);
2665 return ListAssIndex(self, _idx, obj);
2666 }
2667 else if (PySlice_Check(idx))
2668 {
2669 Py_ssize_t start, stop, step, slicelen;
2670
Bram Moolenaar922a4662014-03-30 16:11:43 +02002671 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002672 &start, &stop, &step, &slicelen) < 0)
2673 return -1;
2674 return ListAssSlice(self, start, step, slicelen,
2675 obj);
2676 }
2677 else
2678 {
2679 RAISE_INVALID_INDEX_TYPE(idx);
2680 return -1;
2681 }
2682}
2683
2684 static PyObject *
2685ListConcatInPlace(ListObject *self, PyObject *obj)
2686{
2687 list_T *l = self->list;
2688 PyObject *lookup_dict;
2689
2690 if (l->lv_lock)
2691 {
2692 RAISE_LOCKED_LIST;
2693 return NULL;
2694 }
2695
2696 if (!(lookup_dict = PyDict_New()))
2697 return NULL;
2698
2699 if (list_py_concat(l, obj, lookup_dict) == -1)
2700 {
2701 Py_DECREF(lookup_dict);
2702 return NULL;
2703 }
2704 Py_DECREF(lookup_dict);
2705
2706 Py_INCREF(self);
2707 return (PyObject *)(self);
2708}
2709
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002710typedef struct
2711{
2712 listwatch_T lw;
2713 list_T *list;
2714} listiterinfo_T;
2715
2716 static void
2717ListIterDestruct(listiterinfo_T *lii)
2718{
2719 list_rem_watch(lii->list, &lii->lw);
2720 PyMem_Free(lii);
2721}
2722
2723 static PyObject *
2724ListIterNext(listiterinfo_T **lii)
2725{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002726 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002727
2728 if (!((*lii)->lw.lw_item))
2729 return NULL;
2730
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002731 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002732 return NULL;
2733
2734 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2735
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002736 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002737}
2738
2739 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002740ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002741{
2742 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002743 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002744
2745 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2746 {
2747 PyErr_NoMemory();
2748 return NULL;
2749 }
2750
2751 list_add_watch(l, &lii->lw);
2752 lii->lw.lw_item = l->lv_first;
2753 lii->list = l;
2754
2755 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002756 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2757 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002758}
2759
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002760static char *ListAttrs[] = {
2761 "locked",
2762 NULL
2763};
2764
2765 static PyObject *
2766ListDir(PyObject *self)
2767{
2768 return ObjectDir(self, ListAttrs);
2769}
2770
Bram Moolenaar66b79852012-09-21 14:00:35 +02002771 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002772ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002773{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002774 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002775 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002776 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002777 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002778 return -1;
2779 }
2780
2781 if (strcmp(name, "locked") == 0)
2782 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002783 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002784 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002785 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002786 return -1;
2787 }
2788 else
2789 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002790 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002791 if (istrue == -1)
2792 return -1;
2793 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002794 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002795 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002796 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002797 }
2798 return 0;
2799 }
2800 else
2801 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002802 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002803 return -1;
2804 }
2805}
2806
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002807static PySequenceMethods ListAsSeq = {
2808 (lenfunc) ListLength, /* sq_length, len(x) */
2809 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2810 0, /* RangeRepeat, sq_repeat, x*n */
2811 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2812 0, /* was_sq_slice, x[i:j] */
2813 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2814 0, /* was_sq_ass_slice, x[i:j]=v */
2815 0, /* sq_contains */
2816 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2817 0, /* sq_inplace_repeat */
2818};
2819
2820static PyMappingMethods ListAsMapping = {
2821 /* mp_length */ (lenfunc) ListLength,
2822 /* mp_subscript */ (binaryfunc) ListItem,
2823 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2824};
2825
Bram Moolenaardb913952012-06-29 12:54:53 +02002826static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002827 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2828 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2829 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002830};
2831
2832typedef struct
2833{
2834 PyObject_HEAD
2835 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002836 int argc;
2837 typval_T *argv;
2838 dict_T *self;
2839 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002840 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002841} FunctionObject;
2842
2843static PyTypeObject FunctionType;
2844
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002845#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2846 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002847
Bram Moolenaardb913952012-06-29 12:54:53 +02002848 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002849FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002850 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002851{
2852 FunctionObject *self;
2853
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002854 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2855
Bram Moolenaardb913952012-06-29 12:54:53 +02002856 if (self == NULL)
2857 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002858
2859 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002860 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002861 if (!translated_function_exists(name))
2862 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002863 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002864 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002865 return NULL;
2866 }
2867 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002868 }
2869 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002870 if ((self->name = get_expanded_name(name,
2871 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2872 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002873 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002874 PyErr_FORMAT(PyExc_ValueError,
2875 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002876 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002877 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002878
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002879 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002880 self->argc = argc;
2881 self->argv = argv;
2882 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002883 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002884
2885 if (self->argv || self->self)
2886 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2887
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002888 return (PyObject *)(self);
2889}
2890
2891 static PyObject *
2892FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2893{
2894 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002895 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002896 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002897 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002898 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002899 typval_T selfdicttv;
2900 typval_T argstv;
2901 list_T *argslist = NULL;
2902 dict_T *selfdict = NULL;
2903 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002904 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002905 typval_T *argv = NULL;
2906 typval_T *curtv;
2907 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002908
Bram Moolenaar8110a092016-04-14 15:56:09 +02002909 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002910 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002911 selfdictObject = PyDict_GetItemString(kwargs, "self");
2912 if (selfdictObject != NULL)
2913 {
2914 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2915 return NULL;
2916 selfdict = selfdicttv.vval.v_dict;
2917 }
2918 argsObject = PyDict_GetItemString(kwargs, "args");
2919 if (argsObject != NULL)
2920 {
2921 if (ConvertFromPySequence(argsObject, &argstv) == -1)
2922 {
2923 dict_unref(selfdict);
2924 return NULL;
2925 }
2926 argslist = argstv.vval.v_list;
2927
2928 argc = argslist->lv_len;
2929 if (argc != 0)
2930 {
2931 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02002932 if (argv == NULL)
2933 {
2934 PyErr_NoMemory();
2935 dict_unref(selfdict);
2936 list_unref(argslist);
2937 return NULL;
2938 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02002939 curtv = argv;
2940 for (li = argslist->lv_first; li != NULL; li = li->li_next)
2941 copy_tv(&li->li_tv, curtv++);
2942 }
2943 list_unref(argslist);
2944 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002945 if (selfdict != NULL)
2946 {
2947 auto_rebind = FALSE;
2948 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
2949 if (autoRebindObject != NULL)
2950 {
2951 auto_rebind = PyObject_IsTrue(autoRebindObject);
2952 if (auto_rebind == -1)
2953 {
2954 dict_unref(selfdict);
2955 list_unref(argslist);
2956 return NULL;
2957 }
2958 }
2959 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002960 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002961
Bram Moolenaar389a1792013-06-23 13:00:44 +02002962 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02002963 {
2964 dict_unref(selfdict);
2965 while (argc--)
2966 clear_tv(&argv[argc]);
2967 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002968 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002969 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002970
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002971 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002972
Bram Moolenaar389a1792013-06-23 13:00:44 +02002973 PyMem_Free(name);
2974
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002975 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002976}
2977
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002978 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002979FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002980{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002981 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002982 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002983 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002984 for (i = 0; i < self->argc; ++i)
2985 clear_tv(&self->argv[i]);
2986 PyMem_Free(self->argv);
2987 dict_unref(self->self);
2988 if (self->argv || self->self)
2989 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002990
2991 DESTRUCTOR_FINISH(self);
2992}
2993
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002994static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002995 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002996 NULL
2997};
2998
2999 static PyObject *
3000FunctionDir(PyObject *self)
3001{
3002 return ObjectDir(self, FunctionAttrs);
3003}
3004
Bram Moolenaardb913952012-06-29 12:54:53 +02003005 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003006FunctionAttr(FunctionObject *self, char *name)
3007{
3008 list_T *list;
3009 int i;
3010 if (strcmp(name, "name") == 0)
3011 return PyString_FromString((char *)(self->name));
3012 else if (strcmp(name, "args") == 0)
3013 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003014 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003015 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003016
Bram Moolenaar8110a092016-04-14 15:56:09 +02003017 for (i = 0; i < self->argc; ++i)
3018 list_append_tv(list, &self->argv[i]);
3019 return NEW_LIST(list);
3020 }
3021 else if (strcmp(name, "self") == 0)
3022 return self->self == NULL
3023 ? AlwaysNone(NULL)
3024 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003025 else if (strcmp(name, "auto_rebind") == 0)
3026 return self->auto_rebind
3027 ? AlwaysTrue(NULL)
3028 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003029 else if (strcmp(name, "__members__") == 0)
3030 return ObjectDir(NULL, FunctionAttrs);
3031 return NULL;
3032}
3033
3034/* Populate partial_T given function object.
3035 *
3036 * "exported" should be set to true when it is needed to construct a partial
3037 * that may be stored in a variable (i.e. may be freed by Vim).
3038 */
3039 static void
3040set_partial(FunctionObject *self, partial_T *pt, int exported)
3041{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003042 int i;
3043
3044 pt->pt_name = self->name;
3045 if (self->argv)
3046 {
3047 pt->pt_argc = self->argc;
3048 if (exported)
3049 {
3050 pt->pt_argv = (typval_T *)alloc_clear(
3051 sizeof(typval_T) * self->argc);
3052 for (i = 0; i < pt->pt_argc; ++i)
3053 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3054 }
3055 else
3056 pt->pt_argv = self->argv;
3057 }
3058 else
3059 {
3060 pt->pt_argc = 0;
3061 pt->pt_argv = NULL;
3062 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003063 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003064 pt->pt_dict = self->self;
3065 if (exported && self->self)
3066 ++pt->pt_dict->dv_refcount;
3067 if (exported)
3068 pt->pt_name = vim_strsave(pt->pt_name);
3069 pt->pt_refcount = 1;
3070}
3071
3072 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003073FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003074{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003075 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003076 typval_T args;
3077 typval_T selfdicttv;
3078 typval_T rettv;
3079 dict_T *selfdict = NULL;
3080 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003081 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003082 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003083 partial_T pt;
3084 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003085
Bram Moolenaar8110a092016-04-14 15:56:09 +02003086 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003087 return NULL;
3088
3089 if (kwargs != NULL)
3090 {
3091 selfdictObject = PyDict_GetItemString(kwargs, "self");
3092 if (selfdictObject != NULL)
3093 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003094 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003095 {
3096 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003097 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003098 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003099 selfdict = selfdicttv.vval.v_dict;
3100 }
3101 }
3102
Bram Moolenaar8110a092016-04-14 15:56:09 +02003103 if (self->argv || self->self)
3104 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003105 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003106 set_partial(self, &pt, FALSE);
3107 pt_ptr = &pt;
3108 }
3109
Bram Moolenaar71700b82013-05-15 17:49:05 +02003110 Py_BEGIN_ALLOW_THREADS
3111 Python_Lock_Vim();
3112
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003113 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003114 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003115
3116 Python_Release_Vim();
3117 Py_END_ALLOW_THREADS
3118
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003119 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003120 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003121 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003122 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003123 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003124 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003125 }
3126 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003127 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003128
Bram Moolenaardb913952012-06-29 12:54:53 +02003129 clear_tv(&args);
3130 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003131 if (selfdict != NULL)
3132 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003133
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003134 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003135}
3136
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003137 static PyObject *
3138FunctionRepr(FunctionObject *self)
3139{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003140 PyObject *ret;
3141 garray_T repr_ga;
3142 int i;
3143 char_u *tofree = NULL;
3144 typval_T tv;
3145 char_u numbuf[NUMBUFLEN];
3146
3147 ga_init2(&repr_ga, (int)sizeof(char), 70);
3148 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3149 if (self->name)
3150 ga_concat(&repr_ga, self->name);
3151 else
3152 ga_concat(&repr_ga, (char_u *)"<NULL>");
3153 ga_append(&repr_ga, '\'');
3154 if (self->argv)
3155 {
3156 ga_concat(&repr_ga, (char_u *)", args=[");
3157 ++emsg_silent;
3158 for (i = 0; i < self->argc; i++)
3159 {
3160 if (i != 0)
3161 ga_concat(&repr_ga, (char_u *)", ");
3162 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3163 get_copyID()));
3164 vim_free(tofree);
3165 }
3166 --emsg_silent;
3167 ga_append(&repr_ga, ']');
3168 }
3169 if (self->self)
3170 {
3171 ga_concat(&repr_ga, (char_u *)", self=");
3172 tv.v_type = VAR_DICT;
3173 tv.vval.v_dict = self->self;
3174 ++emsg_silent;
3175 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3176 --emsg_silent;
3177 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003178 if (self->auto_rebind)
3179 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003180 }
3181 ga_append(&repr_ga, '>');
3182 ret = PyString_FromString((char *)repr_ga.ga_data);
3183 ga_clear(&repr_ga);
3184 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003185}
3186
Bram Moolenaardb913952012-06-29 12:54:53 +02003187static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003188 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3189 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003190};
3191
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003192/*
3193 * Options object
3194 */
3195
3196static PyTypeObject OptionsType;
3197
3198typedef int (*checkfun)(void *);
3199
3200typedef struct
3201{
3202 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003203 int opt_type;
3204 void *from;
3205 checkfun Check;
3206 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003207} OptionsObject;
3208
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003209 static int
3210dummy_check(void *arg UNUSED)
3211{
3212 return 0;
3213}
3214
3215 static PyObject *
3216OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3217{
3218 OptionsObject *self;
3219
Bram Moolenaar774267b2013-05-21 20:51:59 +02003220 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003221 if (self == NULL)
3222 return NULL;
3223
3224 self->opt_type = opt_type;
3225 self->from = from;
3226 self->Check = Check;
3227 self->fromObj = fromObj;
3228 if (fromObj)
3229 Py_INCREF(fromObj);
3230
3231 return (PyObject *)(self);
3232}
3233
3234 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003235OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003236{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003237 PyObject_GC_UnTrack((void *)(self));
3238 Py_XDECREF(self->fromObj);
3239 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003240}
3241
3242 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003243OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003244{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003245 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003246 return 0;
3247}
3248
3249 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003250OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003251{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003252 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003253 return 0;
3254}
3255
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003256 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003257OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003258{
3259 char_u *key;
3260 int flags;
3261 long numval;
3262 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003263 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003264
Bram Moolenaard6e39182013-05-21 18:30:34 +02003265 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003266 return NULL;
3267
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003268 if (!(key = StringToChars(keyObject, &todecref)))
3269 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003270
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003271 if (*key == NUL)
3272 {
3273 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003274 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003275 return NULL;
3276 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003277
3278 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003279 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003280
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003281 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003282
3283 if (flags == 0)
3284 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003285 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003286 return NULL;
3287 }
3288
3289 if (flags & SOPT_UNSET)
3290 {
3291 Py_INCREF(Py_None);
3292 return Py_None;
3293 }
3294 else if (flags & SOPT_BOOL)
3295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003296 PyObject *ret;
3297 ret = numval ? Py_True : Py_False;
3298 Py_INCREF(ret);
3299 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003300 }
3301 else if (flags & SOPT_NUM)
3302 return PyInt_FromLong(numval);
3303 else if (flags & SOPT_STRING)
3304 {
3305 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003306 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003307 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003308 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003309 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003310 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003311 else
3312 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003313 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003314 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003315 return NULL;
3316 }
3317 }
3318 else
3319 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003320 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003321 return NULL;
3322 }
3323}
3324
3325 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003326OptionsContains(OptionsObject *self, PyObject *keyObject)
3327{
3328 char_u *key;
3329 PyObject *todecref;
3330
3331 if (!(key = StringToChars(keyObject, &todecref)))
3332 return -1;
3333
3334 if (*key == NUL)
3335 {
3336 Py_XDECREF(todecref);
3337 return 0;
3338 }
3339
3340 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3341 {
3342 Py_XDECREF(todecref);
3343 return 1;
3344 }
3345 else
3346 {
3347 Py_XDECREF(todecref);
3348 return 0;
3349 }
3350}
3351
3352typedef struct
3353{
3354 void *lastoption;
3355 int opt_type;
3356} optiterinfo_T;
3357
3358 static PyObject *
3359OptionsIterNext(optiterinfo_T **oii)
3360{
3361 char_u *name;
3362
3363 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3364 return PyString_FromString((char *)name);
3365
3366 return NULL;
3367}
3368
3369 static PyObject *
3370OptionsIter(OptionsObject *self)
3371{
3372 optiterinfo_T *oii;
3373
3374 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3375 {
3376 PyErr_NoMemory();
3377 return NULL;
3378 }
3379
3380 oii->opt_type = self->opt_type;
3381 oii->lastoption = NULL;
3382
3383 return IterNew(oii,
3384 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3385 NULL, NULL);
3386}
3387
3388 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003389set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003390{
3391 char_u *errmsg;
3392
3393 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3394 {
3395 if (VimTryEnd())
3396 return FAIL;
3397 PyErr_SetVim((char *)errmsg);
3398 return FAIL;
3399 }
3400 return OK;
3401}
3402
3403 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003404set_option_value_for(
3405 char_u *key,
3406 int numval,
3407 char_u *stringval,
3408 int opt_flags,
3409 int opt_type,
3410 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003411{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003412 win_T *save_curwin = NULL;
3413 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003414 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003415 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003416
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003417 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003418 switch (opt_type)
3419 {
3420 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003421 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003422 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003423 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003424 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003425 if (VimTryEnd())
3426 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003427 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003428 return -1;
3429 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003430 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3431 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003432 break;
3433 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003434 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003435 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003436 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003437 break;
3438 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003439 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003440 break;
3441 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003442 if (set_ret == FAIL)
3443 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003444 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003445}
3446
3447 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003448OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003449{
3450 char_u *key;
3451 int flags;
3452 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003453 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003454 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003455
Bram Moolenaard6e39182013-05-21 18:30:34 +02003456 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003457 return -1;
3458
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003459 if (!(key = StringToChars(keyObject, &todecref)))
3460 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003461
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003462 if (*key == NUL)
3463 {
3464 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003465 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003466 return -1;
3467 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003468
3469 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003470 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003471
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003472 if (flags == 0)
3473 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003474 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003475 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003476 return -1;
3477 }
3478
3479 if (valObject == NULL)
3480 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003481 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003482 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003483 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003484 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003485 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003486 return -1;
3487 }
3488 else if (!(flags & SOPT_GLOBAL))
3489 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003490 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003491 N_("unable to unset option %s "
3492 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003493 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003494 return -1;
3495 }
3496 else
3497 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003498 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003499 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003500 return 0;
3501 }
3502 }
3503
Bram Moolenaard6e39182013-05-21 18:30:34 +02003504 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003505
3506 if (flags & SOPT_BOOL)
3507 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003508 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003509
Bram Moolenaarb983f752013-05-15 16:11:50 +02003510 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003511 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003512 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003513 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003514 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003515 }
3516 else if (flags & SOPT_NUM)
3517 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003518 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003519
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003520 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003521 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003522 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003523 return -1;
3524 }
3525
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003526 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003527 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003528 }
3529 else
3530 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003531 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003532 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003533
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003534 if ((val = StringToChars(valObject, &todecref2)))
3535 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003536 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003537 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003538 Py_XDECREF(todecref2);
3539 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003541 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003542 }
3543
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003544 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003545
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003546 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003547}
3548
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003549static PySequenceMethods OptionsAsSeq = {
3550 0, /* sq_length */
3551 0, /* sq_concat */
3552 0, /* sq_repeat */
3553 0, /* sq_item */
3554 0, /* sq_slice */
3555 0, /* sq_ass_item */
3556 0, /* sq_ass_slice */
3557 (objobjproc) OptionsContains, /* sq_contains */
3558 0, /* sq_inplace_concat */
3559 0, /* sq_inplace_repeat */
3560};
3561
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003562static PyMappingMethods OptionsAsMapping = {
3563 (lenfunc) NULL,
3564 (binaryfunc) OptionsItem,
3565 (objobjargproc) OptionsAssItem,
3566};
3567
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003568/* Tabpage object
3569 */
3570
3571typedef struct
3572{
3573 PyObject_HEAD
3574 tabpage_T *tab;
3575} TabPageObject;
3576
3577static PyObject *WinListNew(TabPageObject *tabObject);
3578
3579static PyTypeObject TabPageType;
3580
3581 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003582CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003583{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003584 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003585 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003586 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003587 return -1;
3588 }
3589
3590 return 0;
3591}
3592
3593 static PyObject *
3594TabPageNew(tabpage_T *tab)
3595{
3596 TabPageObject *self;
3597
3598 if (TAB_PYTHON_REF(tab))
3599 {
3600 self = TAB_PYTHON_REF(tab);
3601 Py_INCREF(self);
3602 }
3603 else
3604 {
3605 self = PyObject_NEW(TabPageObject, &TabPageType);
3606 if (self == NULL)
3607 return NULL;
3608 self->tab = tab;
3609 TAB_PYTHON_REF(tab) = self;
3610 }
3611
3612 return (PyObject *)(self);
3613}
3614
3615 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003616TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003617{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3619 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003620
3621 DESTRUCTOR_FINISH(self);
3622}
3623
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003624static char *TabPageAttrs[] = {
3625 "windows", "number", "vars", "window", "valid",
3626 NULL
3627};
3628
3629 static PyObject *
3630TabPageDir(PyObject *self)
3631{
3632 return ObjectDir(self, TabPageAttrs);
3633}
3634
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003635 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003636TabPageAttrValid(TabPageObject *self, char *name)
3637{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003638 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003639
3640 if (strcmp(name, "valid") != 0)
3641 return NULL;
3642
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003643 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3644 Py_INCREF(ret);
3645 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003646}
3647
3648 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003649TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003650{
3651 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003652 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003653 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003654 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003655 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003656 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003657 else if (strcmp(name, "window") == 0)
3658 {
3659 /* For current tab window.c does not bother to set or update tp_curwin
3660 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003661 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003662 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003663 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003664 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003665 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003666 else if (strcmp(name, "__members__") == 0)
3667 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003668 return NULL;
3669}
3670
3671 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003672TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003673{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003674 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003675 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003676 else
3677 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003678 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003679
3680 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003681 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3682 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003683 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003684 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003685 }
3686}
3687
3688static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003689 /* name, function, calling, documentation */
3690 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3691 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003692};
3693
3694/*
3695 * Window list object
3696 */
3697
3698static PyTypeObject TabListType;
3699static PySequenceMethods TabListAsSeq;
3700
3701typedef struct
3702{
3703 PyObject_HEAD
3704} TabListObject;
3705
3706 static PyInt
3707TabListLength(PyObject *self UNUSED)
3708{
3709 tabpage_T *tp = first_tabpage;
3710 PyInt n = 0;
3711
3712 while (tp != NULL)
3713 {
3714 ++n;
3715 tp = tp->tp_next;
3716 }
3717
3718 return n;
3719}
3720
3721 static PyObject *
3722TabListItem(PyObject *self UNUSED, PyInt n)
3723{
3724 tabpage_T *tp;
3725
3726 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3727 if (n == 0)
3728 return TabPageNew(tp);
3729
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003730 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003731 return NULL;
3732}
3733
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003734/*
3735 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003736 */
3737
3738typedef struct
3739{
3740 PyObject_HEAD
3741 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003742 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003743} WindowObject;
3744
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003745static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003746
3747 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003748CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003749{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003750 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003751 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003752 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003753 return -1;
3754 }
3755
3756 return 0;
3757}
3758
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003759 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003760WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003761{
3762 /* We need to handle deletion of windows underneath us.
3763 * If we add a "w_python*_ref" field to the win_T structure,
3764 * then we can get at it in win_free() in vim. We then
3765 * need to create only ONE Python object per window - if
3766 * we try to create a second, just INCREF the existing one
3767 * and return it. The (single) Python object referring to
3768 * the window is stored in "w_python*_ref".
3769 * On a win_free() we set the Python object's win_T* field
3770 * to an invalid value. We trap all uses of a window
3771 * object, and reject them if the win_T* field is invalid.
3772 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003773 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003774 * w_python_ref and w_python3_ref fields respectively.
3775 */
3776
3777 WindowObject *self;
3778
3779 if (WIN_PYTHON_REF(win))
3780 {
3781 self = WIN_PYTHON_REF(win);
3782 Py_INCREF(self);
3783 }
3784 else
3785 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003786 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003787 if (self == NULL)
3788 return NULL;
3789 self->win = win;
3790 WIN_PYTHON_REF(win) = self;
3791 }
3792
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003793 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3794
Bram Moolenaar971db462013-05-12 18:44:48 +02003795 return (PyObject *)(self);
3796}
3797
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003798 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003799WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003800{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003801 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003802 if (self->win && self->win != INVALID_WINDOW_VALUE)
3803 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003804 Py_XDECREF(((PyObject *)(self->tabObject)));
3805 PyObject_GC_Del((void *)(self));
3806}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003807
Bram Moolenaar774267b2013-05-21 20:51:59 +02003808 static int
3809WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3810{
3811 Py_VISIT(((PyObject *)(self->tabObject)));
3812 return 0;
3813}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003814
Bram Moolenaar774267b2013-05-21 20:51:59 +02003815 static int
3816WindowClear(WindowObject *self)
3817{
3818 Py_CLEAR(self->tabObject);
3819 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003820}
3821
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003822 static win_T *
3823get_firstwin(TabPageObject *tabObject)
3824{
3825 if (tabObject)
3826 {
3827 if (CheckTabPage(tabObject))
3828 return NULL;
3829 /* For current tab window.c does not bother to set or update tp_firstwin
3830 */
3831 else if (tabObject->tab == curtab)
3832 return firstwin;
3833 else
3834 return tabObject->tab->tp_firstwin;
3835 }
3836 else
3837 return firstwin;
3838}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003839static char *WindowAttrs[] = {
3840 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3841 "tabpage", "valid",
3842 NULL
3843};
3844
3845 static PyObject *
3846WindowDir(PyObject *self)
3847{
3848 return ObjectDir(self, WindowAttrs);
3849}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003850
Bram Moolenaar971db462013-05-12 18:44:48 +02003851 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003852WindowAttrValid(WindowObject *self, char *name)
3853{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003854 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003855
3856 if (strcmp(name, "valid") != 0)
3857 return NULL;
3858
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003859 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3860 Py_INCREF(ret);
3861 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003862}
3863
3864 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003865WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003866{
3867 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003868 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003869 else if (strcmp(name, "cursor") == 0)
3870 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003871 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003872
3873 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3874 }
3875 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003876 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003877 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003878 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003879 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003880 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003881 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003882 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003883 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003884 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003885 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003886 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3887 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003888 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003889 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003890 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003891 return NULL;
3892 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003893 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003894 }
3895 else if (strcmp(name, "tabpage") == 0)
3896 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897 Py_INCREF(self->tabObject);
3898 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003899 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003900 else if (strcmp(name, "__members__") == 0)
3901 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003902 else
3903 return NULL;
3904}
3905
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003906 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003907WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003908{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003909 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003910 return -1;
3911
3912 if (strcmp(name, "buffer") == 0)
3913 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003914 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003915 return -1;
3916 }
3917 else if (strcmp(name, "cursor") == 0)
3918 {
3919 long lnum;
3920 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003921
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003922 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003923 return -1;
3924
Bram Moolenaard6e39182013-05-21 18:30:34 +02003925 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003926 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003927 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003928 return -1;
3929 }
3930
3931 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003932 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003933 return -1;
3934
Bram Moolenaard6e39182013-05-21 18:30:34 +02003935 self->win->w_cursor.lnum = lnum;
3936 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003937#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003938 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003939#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003940 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003941 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003942
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003943 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003944 return 0;
3945 }
3946 else if (strcmp(name, "height") == 0)
3947 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003948 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003949 win_T *savewin;
3950
Bram Moolenaardee2e312013-06-23 16:35:47 +02003951 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003952 return -1;
3953
3954#ifdef FEAT_GUI
3955 need_mouse_correct = TRUE;
3956#endif
3957 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003958 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003959
3960 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003961 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003962 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003963 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003964 return -1;
3965
3966 return 0;
3967 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003968 else if (strcmp(name, "width") == 0)
3969 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003970 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003971 win_T *savewin;
3972
Bram Moolenaardee2e312013-06-23 16:35:47 +02003973 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003974 return -1;
3975
3976#ifdef FEAT_GUI
3977 need_mouse_correct = TRUE;
3978#endif
3979 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003981
3982 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003983 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003984 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003985 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003986 return -1;
3987
3988 return 0;
3989 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003990 else
3991 {
3992 PyErr_SetString(PyExc_AttributeError, name);
3993 return -1;
3994 }
3995}
3996
3997 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003998WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003999{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004000 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004001 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004002 else
4003 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004004 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004005
Bram Moolenaar6d216452013-05-12 19:00:41 +02004006 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004007 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 (self));
4009 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004010 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011 }
4012}
4013
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004014static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004015 /* name, function, calling, documentation */
4016 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4017 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004018};
4019
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004020/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004021 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004023
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004024static PyTypeObject WinListType;
4025static PySequenceMethods WinListAsSeq;
4026
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004027typedef struct
4028{
4029 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004030 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004031} WinListObject;
4032
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004033 static PyObject *
4034WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004036 WinListObject *self;
4037
4038 self = PyObject_NEW(WinListObject, &WinListType);
4039 self->tabObject = tabObject;
4040 Py_INCREF(tabObject);
4041
4042 return (PyObject *)(self);
4043}
4044
4045 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004046WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004047{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004048 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004049
4050 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004051 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004052 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004053 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004054
4055 DESTRUCTOR_FINISH(self);
4056}
4057
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004058 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004059WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004060{
4061 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 PyInt n = 0;
4063
Bram Moolenaard6e39182013-05-21 18:30:34 +02004064 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004065 return -1;
4066
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067 while (w != NULL)
4068 {
4069 ++n;
4070 w = W_NEXT(w);
4071 }
4072
4073 return n;
4074}
4075
4076 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004077WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078{
4079 win_T *w;
4080
Bram Moolenaard6e39182013-05-21 18:30:34 +02004081 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004082 return NULL;
4083
4084 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004086 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004088 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 return NULL;
4090}
4091
4092/* Convert a Python string into a Vim line.
4093 *
4094 * The result is in allocated memory. All internal nulls are replaced by
4095 * newline characters. It is an error for the string to contain newline
4096 * characters.
4097 *
4098 * On errors, the Python exception data is set, and NULL is returned.
4099 */
4100 static char *
4101StringToLine(PyObject *obj)
4102{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004103 char *str;
4104 char *save;
4105 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004106 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004107 PyInt i;
4108 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004110 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004112 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4113 || str == NULL)
4114 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004116 else if (PyUnicode_Check(obj))
4117 {
4118 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4119 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004120
Bram Moolenaardaa27022013-06-24 22:33:30 +02004121 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004122 || str == NULL)
4123 {
4124 Py_DECREF(bytes);
4125 return NULL;
4126 }
4127 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004128 else
4129 {
4130#if PY_MAJOR_VERSION < 3
4131 PyErr_FORMAT(PyExc_TypeError,
4132 N_("expected str() or unicode() instance, but got %s"),
4133 Py_TYPE_NAME(obj));
4134#else
4135 PyErr_FORMAT(PyExc_TypeError,
4136 N_("expected bytes() or str() instance, but got %s"),
4137 Py_TYPE_NAME(obj));
4138#endif
4139 return NULL;
4140 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004141
4142 /*
4143 * Error checking: String must not contain newlines, as we
4144 * are replacing a single line, and we must replace it with
4145 * a single line.
4146 * A trailing newline is removed, so that append(f.readlines()) works.
4147 */
4148 p = memchr(str, '\n', len);
4149 if (p != NULL)
4150 {
4151 if (p == str + len - 1)
4152 --len;
4153 else
4154 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004155 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004156 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004157 return NULL;
4158 }
4159 }
4160
4161 /* Create a copy of the string, with internal nulls replaced by
4162 * newline characters, as is the vim convention.
4163 */
4164 save = (char *)alloc((unsigned)(len+1));
4165 if (save == NULL)
4166 {
4167 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004168 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004169 return NULL;
4170 }
4171
4172 for (i = 0; i < len; ++i)
4173 {
4174 if (str[i] == '\0')
4175 save[i] = '\n';
4176 else
4177 save[i] = str[i];
4178 }
4179
4180 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004181 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004182
4183 return save;
4184}
4185
4186/* Get a line from the specified buffer. The line number is
4187 * in Vim format (1-based). The line is returned as a Python
4188 * string object.
4189 */
4190 static PyObject *
4191GetBufferLine(buf_T *buf, PyInt n)
4192{
4193 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4194}
4195
4196
4197/* Get a list of lines from the specified buffer. The line numbers
4198 * are in Vim format (1-based). The range is from lo up to, but not
4199 * including, hi. The list is returned as a Python list of string objects.
4200 */
4201 static PyObject *
4202GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4203{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004204 PyInt i;
4205 PyInt n = hi - lo;
4206 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004207
4208 if (list == NULL)
4209 return NULL;
4210
4211 for (i = 0; i < n; ++i)
4212 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004213 PyObject *string = LineToString(
4214 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004215
4216 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004217 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218 {
4219 Py_DECREF(list);
4220 return NULL;
4221 }
4222
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004223 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004224 }
4225
4226 /* The ownership of the Python list is passed to the caller (ie,
4227 * the caller should Py_DECREF() the object when it is finished
4228 * with it).
4229 */
4230
4231 return list;
4232}
4233
4234/*
4235 * Check if deleting lines made the cursor position invalid.
4236 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4237 * deleted).
4238 */
4239 static void
4240py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4241{
4242 if (curwin->w_cursor.lnum >= lo)
4243 {
4244 /* Adjust the cursor position if it's in/after the changed
4245 * lines. */
4246 if (curwin->w_cursor.lnum >= hi)
4247 {
4248 curwin->w_cursor.lnum += extra;
4249 check_cursor_col();
4250 }
4251 else if (extra < 0)
4252 {
4253 curwin->w_cursor.lnum = lo;
4254 check_cursor();
4255 }
4256 else
4257 check_cursor_col();
4258 changed_cline_bef_curs();
4259 }
4260 invalidate_botline();
4261}
4262
Bram Moolenaar19e60942011-06-19 00:27:51 +02004263/*
4264 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004265 * in Vim format (1-based). The replacement line is given as
4266 * a Python string object. The object is checked for validity
4267 * and correct format. Errors are returned as a value of FAIL.
4268 * The return value is OK on success.
4269 * If OK is returned and len_change is not NULL, *len_change
4270 * is set to the change in the buffer length.
4271 */
4272 static int
4273SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4274{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004275 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004276 win_T *save_curwin = NULL;
4277 tabpage_T *save_curtab = NULL;
4278
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004279 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004280 * There are three cases:
4281 * 1. NULL, or None - this is a deletion.
4282 * 2. A string - this is a replacement.
4283 * 3. Anything else - this is an error.
4284 */
4285 if (line == Py_None || line == NULL)
4286 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004287 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004288 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004289
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004290 VimTryStart();
4291
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004293 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004294 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004295 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004296 else
4297 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004298 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004299 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004300 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004301 /* Only adjust marks if we managed to switch to a window that
4302 * holds the buffer, otherwise line numbers will be invalid. */
4303 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004304 }
4305
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004306 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004308 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004309 return FAIL;
4310
4311 if (len_change)
4312 *len_change = -1;
4313
4314 return OK;
4315 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004316 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004317 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004318 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004319
4320 if (save == NULL)
4321 return FAIL;
4322
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004323 VimTryStart();
4324
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004325 /* We do not need to free "save" if ml_replace() consumes it. */
4326 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004327 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004328
4329 if (u_savesub((linenr_T)n) == FAIL)
4330 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004331 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004332 vim_free(save);
4333 }
4334 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4335 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004336 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004337 vim_free(save);
4338 }
4339 else
4340 changed_bytes((linenr_T)n, 0);
4341
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004342 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004343
4344 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004345 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004346 check_cursor_col();
4347
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004348 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004349 return FAIL;
4350
4351 if (len_change)
4352 *len_change = 0;
4353
4354 return OK;
4355 }
4356 else
4357 {
4358 PyErr_BadArgument();
4359 return FAIL;
4360 }
4361}
4362
Bram Moolenaar19e60942011-06-19 00:27:51 +02004363/* Replace a range of lines in the specified buffer. The line numbers are in
4364 * Vim format (1-based). The range is from lo up to, but not including, hi.
4365 * The replacement lines are given as a Python list of string objects. The
4366 * list is checked for validity and correct format. Errors are returned as a
4367 * value of FAIL. The return value is OK on success.
4368 * If OK is returned and len_change is not NULL, *len_change
4369 * is set to the change in the buffer length.
4370 */
4371 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004372SetBufferLineList(
4373 buf_T *buf,
4374 PyInt lo,
4375 PyInt hi,
4376 PyObject *list,
4377 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004378{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004379 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004380 win_T *save_curwin = NULL;
4381 tabpage_T *save_curtab = NULL;
4382
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004383 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004384 * There are three cases:
4385 * 1. NULL, or None - this is a deletion.
4386 * 2. A list - this is a replacement.
4387 * 3. Anything else - this is an error.
4388 */
4389 if (list == Py_None || list == NULL)
4390 {
4391 PyInt i;
4392 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004393
4394 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004395 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004396 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004397
4398 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004399 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004400 else
4401 {
4402 for (i = 0; i < n; ++i)
4403 {
4404 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4405 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004406 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004407 break;
4408 }
4409 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004410 if (buf == curbuf && (save_curwin != NULL
4411 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004412 /* Using an existing window for the buffer, adjust the cursor
4413 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004414 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004415 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004416 /* Only adjust marks if we managed to switch to a window that
4417 * holds the buffer, otherwise line numbers will be invalid. */
4418 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004419 }
4420
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004421 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004422
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004423 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004424 return FAIL;
4425
4426 if (len_change)
4427 *len_change = -n;
4428
4429 return OK;
4430 }
4431 else if (PyList_Check(list))
4432 {
4433 PyInt i;
4434 PyInt new_len = PyList_Size(list);
4435 PyInt old_len = hi - lo;
4436 PyInt extra = 0; /* lines added to text, can be negative */
4437 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004438
4439 if (new_len == 0) /* avoid allocating zero bytes */
4440 array = NULL;
4441 else
4442 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004443 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004444 if (array == NULL)
4445 {
4446 PyErr_NoMemory();
4447 return FAIL;
4448 }
4449 }
4450
4451 for (i = 0; i < new_len; ++i)
4452 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004453 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004454
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004455 if (!(line = PyList_GetItem(list, i)) ||
4456 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004457 {
4458 while (i)
4459 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004460 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004461 return FAIL;
4462 }
4463 }
4464
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004465 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004466 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004467
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004468 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004469 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004470
4471 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004472 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004473
4474 /* If the size of the range is reducing (ie, new_len < old_len) we
4475 * need to delete some old_len. We do this at the start, by
4476 * repeatedly deleting line "lo".
4477 */
4478 if (!PyErr_Occurred())
4479 {
4480 for (i = 0; i < old_len - new_len; ++i)
4481 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4482 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004483 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004484 break;
4485 }
4486 extra -= i;
4487 }
4488
4489 /* For as long as possible, replace the existing old_len with the
4490 * new old_len. This is a more efficient operation, as it requires
4491 * less memory allocation and freeing.
4492 */
4493 if (!PyErr_Occurred())
4494 {
4495 for (i = 0; i < old_len && i < new_len; ++i)
4496 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4497 == FAIL)
4498 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004499 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004500 break;
4501 }
4502 }
4503 else
4504 i = 0;
4505
4506 /* Now we may need to insert the remaining new old_len. If we do, we
4507 * must free the strings as we finish with them (we can't pass the
4508 * responsibility to vim in this case).
4509 */
4510 if (!PyErr_Occurred())
4511 {
4512 while (i < new_len)
4513 {
4514 if (ml_append((linenr_T)(lo + i - 1),
4515 (char_u *)array[i], 0, FALSE) == FAIL)
4516 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004517 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004518 break;
4519 }
4520 vim_free(array[i]);
4521 ++i;
4522 ++extra;
4523 }
4524 }
4525
4526 /* Free any left-over old_len, as a result of an error */
4527 while (i < new_len)
4528 {
4529 vim_free(array[i]);
4530 ++i;
4531 }
4532
4533 /* Free the array of old_len. All of its contents have now
4534 * been dealt with (either freed, or the responsibility passed
4535 * to vim.
4536 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004537 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004538
4539 /* Adjust marks. Invalidate any which lie in the
4540 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004541 * Only adjust marks if we managed to switch to a window that holds
4542 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004543 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004544 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004545 (long)MAXLNUM, (long)extra);
4546 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4547
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004548 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004549 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4550
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004551 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004552 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004553
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004554 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004555 return FAIL;
4556
4557 if (len_change)
4558 *len_change = new_len - old_len;
4559
4560 return OK;
4561 }
4562 else
4563 {
4564 PyErr_BadArgument();
4565 return FAIL;
4566 }
4567}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004568
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004569/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004570 * The line number is in Vim format (1-based). The lines to be inserted are
4571 * given as a Python list of string objects or as a single string. The lines
4572 * to be added are checked for validity and correct format. Errors are
4573 * returned as a value of FAIL. The return value is OK on success.
4574 * If OK is returned and len_change is not NULL, *len_change
4575 * is set to the change in the buffer length.
4576 */
4577 static int
4578InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4579{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004580 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004581 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004582 tabpage_T *save_curtab = NULL;
4583
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004584 /* First of all, we check the type of the supplied Python object.
4585 * It must be a string or a list, or the call is in error.
4586 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004587 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004588 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004589 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004590
4591 if (str == NULL)
4592 return FAIL;
4593
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004594 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004595 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004596 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004597
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004598 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004599 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004600 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004601 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004602 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004603 /* Only adjust marks if we managed to switch to a window that
4604 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004605 appended_lines_mark((linenr_T)n, 1L);
4606
4607 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004608 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004609 update_screen(VALID);
4610
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004611 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004612 return FAIL;
4613
4614 if (len_change)
4615 *len_change = 1;
4616
4617 return OK;
4618 }
4619 else if (PyList_Check(lines))
4620 {
4621 PyInt i;
4622 PyInt size = PyList_Size(lines);
4623 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004624
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004625 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004626 if (array == NULL)
4627 {
4628 PyErr_NoMemory();
4629 return FAIL;
4630 }
4631
4632 for (i = 0; i < size; ++i)
4633 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004634 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004635
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004636 if (!(line = PyList_GetItem(lines, i)) ||
4637 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004638 {
4639 while (i)
4640 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004641 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004642 return FAIL;
4643 }
4644 }
4645
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004646 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004647 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004648 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004649
4650 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004651 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652 else
4653 {
4654 for (i = 0; i < size; ++i)
4655 {
4656 if (ml_append((linenr_T)(n + i),
4657 (char_u *)array[i], 0, FALSE) == FAIL)
4658 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004659 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004660
4661 /* Free the rest of the lines */
4662 while (i < size)
4663 vim_free(array[i++]);
4664
4665 break;
4666 }
4667 vim_free(array[i]);
4668 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004669 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004670 /* Only adjust marks if we managed to switch to a window that
4671 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004672 appended_lines_mark((linenr_T)n, (long)i);
4673 }
4674
4675 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004676 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004677 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004678 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004679
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004680 update_screen(VALID);
4681
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004682 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683 return FAIL;
4684
4685 if (len_change)
4686 *len_change = size;
4687
4688 return OK;
4689 }
4690 else
4691 {
4692 PyErr_BadArgument();
4693 return FAIL;
4694 }
4695}
4696
4697/*
4698 * Common routines for buffers and line ranges
4699 * -------------------------------------------
4700 */
4701
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004702typedef struct
4703{
4704 PyObject_HEAD
4705 buf_T *buf;
4706} BufferObject;
4707
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004709CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004710{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004711 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004713 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004714 return -1;
4715 }
4716
4717 return 0;
4718}
4719
4720 static PyObject *
4721RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4722{
4723 if (CheckBuffer(self))
4724 return NULL;
4725
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004726 if (end == -1)
4727 end = self->buf->b_ml.ml_line_count;
4728
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004729 if (n < 0)
4730 n += end - start + 1;
4731
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732 if (n < 0 || n > end - start)
4733 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004734 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735 return NULL;
4736 }
4737
4738 return GetBufferLine(self->buf, n+start);
4739}
4740
4741 static PyObject *
4742RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4743{
4744 PyInt size;
4745
4746 if (CheckBuffer(self))
4747 return NULL;
4748
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004749 if (end == -1)
4750 end = self->buf->b_ml.ml_line_count;
4751
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752 size = end - start + 1;
4753
4754 if (lo < 0)
4755 lo = 0;
4756 else if (lo > size)
4757 lo = size;
4758 if (hi < 0)
4759 hi = 0;
4760 if (hi < lo)
4761 hi = lo;
4762 else if (hi > size)
4763 hi = size;
4764
4765 return GetBufferLineList(self->buf, lo+start, hi+start);
4766}
4767
4768 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004769RBAsItem(
4770 BufferObject *self,
4771 PyInt n,
4772 PyObject *valObject,
4773 PyInt start,
4774 PyInt end,
4775 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004776{
4777 PyInt len_change;
4778
4779 if (CheckBuffer(self))
4780 return -1;
4781
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004782 if (end == -1)
4783 end = self->buf->b_ml.ml_line_count;
4784
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004785 if (n < 0)
4786 n += end - start + 1;
4787
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004788 if (n < 0 || n > end - start)
4789 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004790 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004791 return -1;
4792 }
4793
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004794 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004795 return -1;
4796
4797 if (new_end)
4798 *new_end = end + len_change;
4799
4800 return 0;
4801}
4802
Bram Moolenaar19e60942011-06-19 00:27:51 +02004803 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004804RBAsSlice(
4805 BufferObject *self,
4806 PyInt lo,
4807 PyInt hi,
4808 PyObject *valObject,
4809 PyInt start,
4810 PyInt end,
4811 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004812{
4813 PyInt size;
4814 PyInt len_change;
4815
4816 /* Self must be a valid buffer */
4817 if (CheckBuffer(self))
4818 return -1;
4819
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004820 if (end == -1)
4821 end = self->buf->b_ml.ml_line_count;
4822
Bram Moolenaar19e60942011-06-19 00:27:51 +02004823 /* Sort out the slice range */
4824 size = end - start + 1;
4825
4826 if (lo < 0)
4827 lo = 0;
4828 else if (lo > size)
4829 lo = size;
4830 if (hi < 0)
4831 hi = 0;
4832 if (hi < lo)
4833 hi = lo;
4834 else if (hi > size)
4835 hi = size;
4836
4837 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004838 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004839 return -1;
4840
4841 if (new_end)
4842 *new_end = end + len_change;
4843
4844 return 0;
4845}
4846
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004847
4848 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004849RBAppend(
4850 BufferObject *self,
4851 PyObject *args,
4852 PyInt start,
4853 PyInt end,
4854 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004855{
4856 PyObject *lines;
4857 PyInt len_change;
4858 PyInt max;
4859 PyInt n;
4860
4861 if (CheckBuffer(self))
4862 return NULL;
4863
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004864 if (end == -1)
4865 end = self->buf->b_ml.ml_line_count;
4866
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004867 max = n = end - start + 1;
4868
4869 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4870 return NULL;
4871
4872 if (n < 0 || n > max)
4873 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004874 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004875 return NULL;
4876 }
4877
4878 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4879 return NULL;
4880
4881 if (new_end)
4882 *new_end = end + len_change;
4883
4884 Py_INCREF(Py_None);
4885 return Py_None;
4886}
4887
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004888/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004889 */
4890
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004891static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004892static PySequenceMethods RangeAsSeq;
4893static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004894
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004895typedef struct
4896{
4897 PyObject_HEAD
4898 BufferObject *buf;
4899 PyInt start;
4900 PyInt end;
4901} RangeObject;
4902
4903 static PyObject *
4904RangeNew(buf_T *buf, PyInt start, PyInt end)
4905{
4906 BufferObject *bufr;
4907 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004908 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004909 if (self == NULL)
4910 return NULL;
4911
4912 bufr = (BufferObject *)BufferNew(buf);
4913 if (bufr == NULL)
4914 {
4915 Py_DECREF(self);
4916 return NULL;
4917 }
4918 Py_INCREF(bufr);
4919
4920 self->buf = bufr;
4921 self->start = start;
4922 self->end = end;
4923
4924 return (PyObject *)(self);
4925}
4926
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004927 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004928RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004929{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004930 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004931 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004932 PyObject_GC_Del((void *)(self));
4933}
4934
4935 static int
4936RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4937{
4938 Py_VISIT(((PyObject *)(self->buf)));
4939 return 0;
4940}
4941
4942 static int
4943RangeClear(RangeObject *self)
4944{
4945 Py_CLEAR(self->buf);
4946 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004947}
4948
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004949 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004950RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004951{
4952 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004953 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004954 return -1; /* ??? */
4955
Bram Moolenaard6e39182013-05-21 18:30:34 +02004956 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004957}
4958
4959 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004960RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004961{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004962 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004963}
4964
4965 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004966RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004967{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004968 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004969}
4970
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004971static char *RangeAttrs[] = {
4972 "start", "end",
4973 NULL
4974};
4975
4976 static PyObject *
4977RangeDir(PyObject *self)
4978{
4979 return ObjectDir(self, RangeAttrs);
4980}
4981
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004982 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004983RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004984{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004985 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004986}
4987
4988 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004989RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004990{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004991 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004992 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4993 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004994 else
4995 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004996 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004997
4998 if (name == NULL)
4999 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005000
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005001 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005002 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005003 }
5004}
5005
5006static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005007 /* name, function, calling, documentation */
5008 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005009 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5010 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005011};
5012
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005013static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005014static PySequenceMethods BufferAsSeq;
5015static PyMappingMethods BufferAsMapping;
5016
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005017 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005018BufferNew(buf_T *buf)
5019{
5020 /* We need to handle deletion of buffers underneath us.
5021 * If we add a "b_python*_ref" field to the buf_T structure,
5022 * then we can get at it in buf_freeall() in vim. We then
5023 * need to create only ONE Python object per buffer - if
5024 * we try to create a second, just INCREF the existing one
5025 * and return it. The (single) Python object referring to
5026 * the buffer is stored in "b_python*_ref".
5027 * Question: what to do on a buf_freeall(). We'll probably
5028 * have to either delete the Python object (DECREF it to
5029 * zero - a bad idea, as it leaves dangling refs!) or
5030 * set the buf_T * value to an invalid value (-1?), which
5031 * means we need checks in all access functions... Bah.
5032 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005033 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005034 * b_python_ref and b_python3_ref fields respectively.
5035 */
5036
5037 BufferObject *self;
5038
5039 if (BUF_PYTHON_REF(buf) != NULL)
5040 {
5041 self = BUF_PYTHON_REF(buf);
5042 Py_INCREF(self);
5043 }
5044 else
5045 {
5046 self = PyObject_NEW(BufferObject, &BufferType);
5047 if (self == NULL)
5048 return NULL;
5049 self->buf = buf;
5050 BUF_PYTHON_REF(buf) = self;
5051 }
5052
5053 return (PyObject *)(self);
5054}
5055
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005056 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005057BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005058{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005059 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5060 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005061
5062 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005063}
5064
Bram Moolenaar971db462013-05-12 18:44:48 +02005065 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005067{
5068 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005070 return -1; /* ??? */
5071
Bram Moolenaard6e39182013-05-21 18:30:34 +02005072 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005073}
5074
5075 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005076BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005077{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005078 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005079}
5080
5081 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005082BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005083{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005084 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005085}
5086
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005087static char *BufferAttrs[] = {
5088 "name", "number", "vars", "options", "valid",
5089 NULL
5090};
5091
5092 static PyObject *
5093BufferDir(PyObject *self)
5094{
5095 return ObjectDir(self, BufferAttrs);
5096}
5097
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005098 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005099BufferAttrValid(BufferObject *self, char *name)
5100{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005101 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005102
5103 if (strcmp(name, "valid") != 0)
5104 return NULL;
5105
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005106 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5107 Py_INCREF(ret);
5108 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005109}
5110
5111 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005112BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005113{
5114 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005115 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005116 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005117 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005118 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005120 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5123 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005124 else if (strcmp(name, "__members__") == 0)
5125 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005126 else
5127 return NULL;
5128}
5129
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005130 static int
5131BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5132{
5133 if (CheckBuffer(self))
5134 return -1;
5135
5136 if (strcmp(name, "name") == 0)
5137 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005138 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005139 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005140 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005141 PyObject *todecref;
5142
5143 if (!(val = StringToChars(valObject, &todecref)))
5144 return -1;
5145
5146 VimTryStart();
5147 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5148 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005149 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005150 aucmd_restbuf(&aco);
5151 Py_XDECREF(todecref);
5152 if (VimTryEnd())
5153 return -1;
5154
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005155 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005156 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005157 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005158 return -1;
5159 }
5160 return 0;
5161 }
5162 else
5163 {
5164 PyErr_SetString(PyExc_AttributeError, name);
5165 return -1;
5166 }
5167}
5168
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005170BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005171{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005173}
5174
5175 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005176BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005177{
5178 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005179 char_u *pmark;
5180 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005181 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005182 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005183
Bram Moolenaard6e39182013-05-21 18:30:34 +02005184 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005185 return NULL;
5186
Bram Moolenaar389a1792013-06-23 13:00:44 +02005187 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005188 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005189
Bram Moolenaar389a1792013-06-23 13:00:44 +02005190 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005191 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005192 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005193 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005194 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005195 return NULL;
5196 }
5197
5198 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005199
5200 Py_XDECREF(todecref);
5201
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005202 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005203 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005204 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005205 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005206 if (VimTryEnd())
5207 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005208
5209 if (posp == NULL)
5210 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005211 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005212 return NULL;
5213 }
5214
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005215 if (posp->lnum <= 0)
5216 {
5217 /* Or raise an error? */
5218 Py_INCREF(Py_None);
5219 return Py_None;
5220 }
5221
5222 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5223}
5224
5225 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005226BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005227{
5228 PyInt start;
5229 PyInt end;
5230
Bram Moolenaard6e39182013-05-21 18:30:34 +02005231 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005232 return NULL;
5233
5234 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5235 return NULL;
5236
Bram Moolenaard6e39182013-05-21 18:30:34 +02005237 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005238}
5239
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005240 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005241BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005242{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005243 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005244 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005245 else
5246 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005247 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005248
5249 if (name == NULL)
5250 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005251
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005252 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005253 }
5254}
5255
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005256static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005257 /* name, function, calling, documentation */
5258 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005259 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005260 {"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 +02005261 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5262 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005263};
5264
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005265/*
5266 * Buffer list object - Implementation
5267 */
5268
5269static PyTypeObject BufMapType;
5270
5271typedef struct
5272{
5273 PyObject_HEAD
5274} BufMapObject;
5275
5276 static PyInt
5277BufMapLength(PyObject *self UNUSED)
5278{
5279 buf_T *b = firstbuf;
5280 PyInt n = 0;
5281
5282 while (b)
5283 {
5284 ++n;
5285 b = b->b_next;
5286 }
5287
5288 return n;
5289}
5290
5291 static PyObject *
5292BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5293{
5294 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005295 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005296
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005297 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005298 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005299
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005300 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005301
5302 if (b)
5303 return BufferNew(b);
5304 else
5305 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005306 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005307 return NULL;
5308 }
5309}
5310
5311 static void
5312BufMapIterDestruct(PyObject *buffer)
5313{
5314 /* Iteration was stopped before all buffers were processed */
5315 if (buffer)
5316 {
5317 Py_DECREF(buffer);
5318 }
5319}
5320
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005321 static int
5322BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5323{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005324 if (buffer)
5325 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005326 return 0;
5327}
5328
5329 static int
5330BufMapIterClear(PyObject **buffer)
5331{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005332 if (*buffer)
5333 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005334 return 0;
5335}
5336
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005337 static PyObject *
5338BufMapIterNext(PyObject **buffer)
5339{
5340 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005341 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005342
5343 if (!*buffer)
5344 return NULL;
5345
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005346 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005347
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005348 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005349 {
5350 *buffer = NULL;
5351 return NULL;
5352 }
5353
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005354 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005355 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005356 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005357 return NULL;
5358 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005359 /* Do not increment reference: we no longer hold it (decref), but whoever
5360 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005361 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005362}
5363
5364 static PyObject *
5365BufMapIter(PyObject *self UNUSED)
5366{
5367 PyObject *buffer;
5368
5369 buffer = BufferNew(firstbuf);
5370 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005371 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5372 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005373}
5374
5375static PyMappingMethods BufMapAsMapping = {
5376 (lenfunc) BufMapLength,
5377 (binaryfunc) BufMapItem,
5378 (objobjargproc) 0,
5379};
5380
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005381/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005382 */
5383
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005384static char *CurrentAttrs[] = {
5385 "buffer", "window", "line", "range", "tabpage",
5386 NULL
5387};
5388
5389 static PyObject *
5390CurrentDir(PyObject *self)
5391{
5392 return ObjectDir(self, CurrentAttrs);
5393}
5394
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005395 static PyObject *
5396CurrentGetattr(PyObject *self UNUSED, char *name)
5397{
5398 if (strcmp(name, "buffer") == 0)
5399 return (PyObject *)BufferNew(curbuf);
5400 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005401 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005402 else if (strcmp(name, "tabpage") == 0)
5403 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005404 else if (strcmp(name, "line") == 0)
5405 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5406 else if (strcmp(name, "range") == 0)
5407 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005408 else if (strcmp(name, "__members__") == 0)
5409 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005410 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005411#if PY_MAJOR_VERSION < 3
5412 return Py_FindMethod(WindowMethods, self, name);
5413#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005414 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005415#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005416}
5417
5418 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005419CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005420{
5421 if (strcmp(name, "line") == 0)
5422 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005423 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5424 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005425 return -1;
5426
5427 return 0;
5428 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005429 else if (strcmp(name, "buffer") == 0)
5430 {
5431 int count;
5432
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005433 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005434 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005435 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005436 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005437 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005438 return -1;
5439 }
5440
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005441 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005442 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005443 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005444
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005445 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005446 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5447 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005448 if (VimTryEnd())
5449 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005450 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005451 return -1;
5452 }
5453
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005454 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005455 }
5456 else if (strcmp(name, "window") == 0)
5457 {
5458 int count;
5459
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005460 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005461 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005462 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005463 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005464 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005465 return -1;
5466 }
5467
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005468 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005469 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005470 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005471
5472 if (!count)
5473 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005474 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005475 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005476 return -1;
5477 }
5478
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005479 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005480 win_goto(((WindowObject *)(valObject))->win);
5481 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005482 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005483 if (VimTryEnd())
5484 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005485 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005486 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005487 return -1;
5488 }
5489
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005490 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005491 }
5492 else if (strcmp(name, "tabpage") == 0)
5493 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005494 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005495 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005496 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005497 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005498 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005499 return -1;
5500 }
5501
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005502 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005503 return -1;
5504
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005505 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005506 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5507 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005508 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005509 if (VimTryEnd())
5510 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005511 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005512 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005513 return -1;
5514 }
5515
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005516 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005517 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005518 else
5519 {
5520 PyErr_SetString(PyExc_AttributeError, name);
5521 return -1;
5522 }
5523}
5524
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005525static struct PyMethodDef CurrentMethods[] = {
5526 /* name, function, calling, documentation */
5527 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5528 { NULL, NULL, 0, NULL}
5529};
5530
Bram Moolenaardb913952012-06-29 12:54:53 +02005531 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005532init_range_cmd(exarg_T *eap)
5533{
5534 RangeStart = eap->line1;
5535 RangeEnd = eap->line2;
5536}
5537
5538 static void
5539init_range_eval(typval_T *rettv UNUSED)
5540{
5541 RangeStart = (PyInt) curwin->w_cursor.lnum;
5542 RangeEnd = RangeStart;
5543}
5544
5545 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005546run_cmd(const char *cmd, void *arg UNUSED
5547#ifdef PY_CAN_RECURSE
5548 , PyGILState_STATE *pygilstate UNUSED
5549#endif
5550 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005551{
Bram Moolenaar41009372013-07-01 22:03:04 +02005552 PyObject *run_ret;
5553 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5554 if (run_ret != NULL)
5555 {
5556 Py_DECREF(run_ret);
5557 }
5558 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5559 {
5560 EMSG2(_(e_py_systemexit), "python");
5561 PyErr_Clear();
5562 }
5563 else
5564 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005565}
5566
5567static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5568static int code_hdr_len = 30;
5569
5570 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005571run_do(const char *cmd, void *arg UNUSED
5572#ifdef PY_CAN_RECURSE
5573 , PyGILState_STATE *pygilstate
5574#endif
5575 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005576{
5577 PyInt lnum;
5578 size_t len;
5579 char *code;
5580 int status;
5581 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005582 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005583 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005584
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005585 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005586 {
5587 EMSG(_("cannot save undo information"));
5588 return;
5589 }
5590
5591 len = code_hdr_len + STRLEN(cmd);
5592 code = PyMem_New(char, len + 1);
5593 memcpy(code, code_hdr, code_hdr_len);
5594 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005595 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5596 status = -1;
5597 if (run_ret != NULL)
5598 {
5599 status = 0;
5600 Py_DECREF(run_ret);
5601 }
5602 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5603 {
5604 PyMem_Free(code);
5605 EMSG2(_(e_py_systemexit), "python");
5606 PyErr_Clear();
5607 return;
5608 }
5609 else
5610 PyErr_PrintEx(1);
5611
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005612 PyMem_Free(code);
5613
5614 if (status)
5615 {
5616 EMSG(_("failed to run the code"));
5617 return;
5618 }
5619
5620 status = 0;
5621 pymain = PyImport_AddModule("__main__");
5622 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005623#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005624 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005625#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005626
5627 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5628 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005629 PyObject *line;
5630 PyObject *linenr;
5631 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005632
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005633#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005634 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005635#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005636 /* Check the line number, the command my have deleted lines. */
5637 if (lnum > curbuf->b_ml.ml_line_count
5638 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005639 goto err;
5640 if (!(linenr = PyInt_FromLong((long) lnum)))
5641 {
5642 Py_DECREF(line);
5643 goto err;
5644 }
5645 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5646 Py_DECREF(line);
5647 Py_DECREF(linenr);
5648 if (!ret)
5649 goto err;
5650
Bram Moolenaara58883b2017-01-29 21:31:09 +01005651 /* Check that the command didn't switch to another buffer. */
5652 if (curbuf != was_curbuf)
5653 {
5654 Py_XDECREF(ret);
5655 goto err;
5656 }
5657
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005658 if (ret != Py_None)
5659 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005660 {
5661 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005662 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005663 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005664
5665 Py_XDECREF(ret);
5666 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005667#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005668 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005669#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005670 }
5671 goto out;
5672err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005673#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005674 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005675#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005676 PyErr_PrintEx(0);
5677 PythonIO_Flush();
5678 status = 1;
5679out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005680#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005681 if (!status)
5682 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005683#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005684 Py_DECREF(pyfunc);
5685 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5686 if (status)
5687 return;
5688 check_cursor();
5689 update_curbuf(NOT_VALID);
5690}
5691
5692 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005693run_eval(const char *cmd, typval_T *rettv
5694#ifdef PY_CAN_RECURSE
5695 , PyGILState_STATE *pygilstate UNUSED
5696#endif
5697 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005698{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005699 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005700
Bram Moolenaar41009372013-07-01 22:03:04 +02005701 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005702 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005703 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005704 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005705 {
5706 EMSG2(_(e_py_systemexit), "python");
5707 PyErr_Clear();
5708 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005709 else
5710 {
5711 if (PyErr_Occurred() && !msg_silent)
5712 PyErr_PrintEx(0);
5713 EMSG(_("E858: Eval did not return a valid python object"));
5714 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005715 }
5716 else
5717 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005718 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005719 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005720 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005721 }
5722 PyErr_Clear();
5723}
5724
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005725 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005726set_ref_in_py(const int copyID)
5727{
5728 pylinkedlist_T *cur;
5729 dict_T *dd;
5730 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005731 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005732 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005733 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005734
5735 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005737 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005738 {
5739 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5740 if (dd->dv_copyID != copyID)
5741 {
5742 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005743 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005744 }
5745 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005746 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005747
5748 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005749 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005750 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005751 {
5752 ll = ((ListObject *) (cur->pll_obj))->list;
5753 if (ll->lv_copyID != copyID)
5754 {
5755 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005756 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005757 }
5758 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005759 }
5760
Bram Moolenaar8110a092016-04-14 15:56:09 +02005761 if (lastfunc != NULL)
5762 {
5763 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5764 {
5765 func = (FunctionObject *) cur->pll_obj;
5766 if (func->self != NULL && func->self->dv_copyID != copyID)
5767 {
5768 func->self->dv_copyID = copyID;
5769 abort = abort || set_ref_in_ht(
5770 &func->self->dv_hashtab, copyID, NULL);
5771 }
5772 if (func->argc)
5773 for (i = 0; !abort && i < func->argc; ++i)
5774 abort = abort
5775 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5776 }
5777 }
5778
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005779 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005780}
5781
5782 static int
5783set_string_copy(char_u *str, typval_T *tv)
5784{
5785 tv->vval.v_string = vim_strsave(str);
5786 if (tv->vval.v_string == NULL)
5787 {
5788 PyErr_NoMemory();
5789 return -1;
5790 }
5791 return 0;
5792}
5793
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005794 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005795pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005796{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005797 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005798 char_u *key;
5799 dictitem_T *di;
5800 PyObject *keyObject;
5801 PyObject *valObject;
5802 Py_ssize_t iter = 0;
5803
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005804 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005805 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005806
5807 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005808 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005809
5810 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5811 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005812 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005813
Bram Moolenaara03e6312013-05-29 22:49:26 +02005814 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005815 {
5816 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005817 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005818 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005819
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005820 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005821 {
5822 dict_unref(dict);
5823 return -1;
5824 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005825
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005826 if (*key == NUL)
5827 {
5828 dict_unref(dict);
5829 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005830 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005831 return -1;
5832 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005833
5834 di = dictitem_alloc(key);
5835
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005836 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005837
5838 if (di == NULL)
5839 {
5840 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005841 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005842 return -1;
5843 }
5844 di->di_tv.v_lock = 0;
5845
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005846 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005847 {
5848 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005849 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005850 return -1;
5851 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005852
5853 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005854 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005855 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005856 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005857 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005858 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005859 return -1;
5860 }
5861 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005862
5863 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005864 return 0;
5865}
5866
5867 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005868pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005869{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005870 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005871 char_u *key;
5872 dictitem_T *di;
5873 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005874 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005875 PyObject *keyObject;
5876 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005877
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005878 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005879 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005880
5881 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005882 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005883
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005884 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005885 {
5886 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005887 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005888 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005889
5890 if (!(iterator = PyObject_GetIter(list)))
5891 {
5892 dict_unref(dict);
5893 Py_DECREF(list);
5894 return -1;
5895 }
5896 Py_DECREF(list);
5897
5898 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005899 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005900 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005902 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005903 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005904 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005905 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005906 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005907 return -1;
5908 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005909
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005910 if (*key == NUL)
5911 {
5912 Py_DECREF(keyObject);
5913 Py_DECREF(iterator);
5914 Py_XDECREF(todecref);
5915 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005916 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005917 return -1;
5918 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005919
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005920 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005921 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005922 Py_DECREF(keyObject);
5923 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005924 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005925 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005926 return -1;
5927 }
5928
5929 di = dictitem_alloc(key);
5930
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005931 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005932 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005933
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005934 if (di == NULL)
5935 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005936 Py_DECREF(iterator);
5937 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005938 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939 PyErr_NoMemory();
5940 return -1;
5941 }
5942 di->di_tv.v_lock = 0;
5943
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005944 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005945 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005946 Py_DECREF(iterator);
5947 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005948 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005949 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005950 return -1;
5951 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005952
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005953 Py_DECREF(valObject);
5954
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005955 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005957 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005958 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005959 dictitem_free(di);
5960 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961 return -1;
5962 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005964 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005965 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return 0;
5967}
5968
5969 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005970pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971{
5972 list_T *l;
5973
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005974 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976
5977 tv->v_type = VAR_LIST;
5978 tv->vval.v_list = l;
5979
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005980 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981 {
5982 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005984 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005986 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005987 return 0;
5988}
5989
Bram Moolenaardb913952012-06-29 12:54:53 +02005990typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5991
5992 static int
5993convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005994 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005995{
5996 PyObject *capsule;
5997 char hexBuf[sizeof(void *) * 2 + 3];
5998
Bram Moolenaar792f0e32018-02-27 17:27:13 +01005999 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006000
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006001# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006002 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006003# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006004 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006005# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006006 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006007 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006008# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006009 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006010# else
6011 capsule = PyCObject_FromVoidPtr(tv, NULL);
6012# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006013 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6014 {
6015 Py_DECREF(capsule);
6016 tv->v_type = VAR_UNKNOWN;
6017 return -1;
6018 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006019
6020 Py_DECREF(capsule);
6021
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006022 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006023 {
6024 tv->v_type = VAR_UNKNOWN;
6025 return -1;
6026 }
6027 /* As we are not using copy_tv which increments reference count we must
6028 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006029 if (tv->v_type == VAR_DICT)
6030 ++tv->vval.v_dict->dv_refcount;
6031 else if (tv->v_type == VAR_LIST)
6032 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006033 }
6034 else
6035 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006036 typval_T *v;
6037
6038# ifdef PY_USE_CAPSULE
6039 v = PyCapsule_GetPointer(capsule, NULL);
6040# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006041 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006042# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006043 copy_tv(v, tv);
6044 }
6045 return 0;
6046}
6047
6048 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006049ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6050{
6051 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006052 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006053
6054 if (!(lookup_dict = PyDict_New()))
6055 return -1;
6056
6057 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6058 {
6059 tv->v_type = VAR_DICT;
6060 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6061 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006062 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006063 }
6064 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006065 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006066 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006067 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006068 else
6069 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006070 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006071 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006072 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006073 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006074 }
6075 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006076 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006077}
6078
6079 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006080ConvertFromPySequence(PyObject *obj, typval_T *tv)
6081{
6082 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006083 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006084
6085 if (!(lookup_dict = PyDict_New()))
6086 return -1;
6087
6088 if (PyType_IsSubtype(obj->ob_type, &ListType))
6089 {
6090 tv->v_type = VAR_LIST;
6091 tv->vval.v_list = (((ListObject *)(obj))->list);
6092 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006093 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006094 }
6095 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006096 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006097 else
6098 {
6099 PyErr_FORMAT(PyExc_TypeError,
6100 N_("unable to convert %s to vim list"),
6101 Py_TYPE_NAME(obj));
6102 ret = -1;
6103 }
6104 Py_DECREF(lookup_dict);
6105 return ret;
6106}
6107
6108 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006109ConvertFromPyObject(PyObject *obj, typval_T *tv)
6110{
6111 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006112 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006113
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006114 if (!(lookup_dict = PyDict_New()))
6115 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006116 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006117 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006118 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006119}
6120
6121 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006122_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006123{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006124 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006125 {
6126 tv->v_type = VAR_DICT;
6127 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6128 ++tv->vval.v_dict->dv_refcount;
6129 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006130 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006131 {
6132 tv->v_type = VAR_LIST;
6133 tv->vval.v_list = (((ListObject *)(obj))->list);
6134 ++tv->vval.v_list->lv_refcount;
6135 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006136 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006137 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006138 FunctionObject *func = (FunctionObject *) obj;
6139 if (func->self != NULL || func->argv != NULL)
6140 {
6141 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6142 set_partial(func, pt, TRUE);
6143 tv->vval.v_partial = pt;
6144 tv->v_type = VAR_PARTIAL;
6145 }
6146 else
6147 {
6148 if (set_string_copy(func->name, tv) == -1)
6149 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006150
Bram Moolenaar8110a092016-04-14 15:56:09 +02006151 tv->v_type = VAR_FUNC;
6152 }
6153 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006154 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006155 else if (PyBytes_Check(obj))
6156 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006157 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006158
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006159 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006160 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006161 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 return -1;
6163
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006164 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 return -1;
6166
6167 tv->v_type = VAR_STRING;
6168 }
6169 else if (PyUnicode_Check(obj))
6170 {
6171 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006172 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006173
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006174 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006175 if (bytes == NULL)
6176 return -1;
6177
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006178 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006179 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006180 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006181 return -1;
6182
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006183 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006184 {
6185 Py_XDECREF(bytes);
6186 return -1;
6187 }
6188 Py_XDECREF(bytes);
6189
6190 tv->v_type = VAR_STRING;
6191 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006192#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006193 else if (PyInt_Check(obj))
6194 {
6195 tv->v_type = VAR_NUMBER;
6196 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006197 if (PyErr_Occurred())
6198 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006199 }
6200#endif
6201 else if (PyLong_Check(obj))
6202 {
6203 tv->v_type = VAR_NUMBER;
6204 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006205 if (PyErr_Occurred())
6206 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006207 }
6208 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006209 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006210#ifdef FEAT_FLOAT
6211 else if (PyFloat_Check(obj))
6212 {
6213 tv->v_type = VAR_FLOAT;
6214 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6215 }
6216#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006217 else if (PyObject_HasAttrString(obj, "keys"))
6218 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006219 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006220 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006221 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006222 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006223 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006224 else if (PyNumber_Check(obj))
6225 {
6226 PyObject *num;
6227
6228 if (!(num = PyNumber_Long(obj)))
6229 return -1;
6230
6231 tv->v_type = VAR_NUMBER;
6232 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6233
6234 Py_DECREF(num);
6235 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006236 else if (obj == Py_None)
6237 {
6238 tv->v_type = VAR_SPECIAL;
6239 tv->vval.v_number = VVAL_NONE;
6240 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006241 else
6242 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006243 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006244 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006245 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006246 return -1;
6247 }
6248 return 0;
6249}
6250
6251 static PyObject *
6252ConvertToPyObject(typval_T *tv)
6253{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006254 typval_T *argv;
6255 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 if (tv == NULL)
6257 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006258 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 return NULL;
6260 }
6261 switch (tv->v_type)
6262 {
6263 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006264 return PyBytes_FromString(tv->vval.v_string == NULL
6265 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006266 case VAR_NUMBER:
6267 return PyLong_FromLong((long) tv->vval.v_number);
6268#ifdef FEAT_FLOAT
6269 case VAR_FLOAT:
6270 return PyFloat_FromDouble((double) tv->vval.v_float);
6271#endif
6272 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006273 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006274 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006275 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006277 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006278 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006279 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006280 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006281 if (tv->vval.v_partial->pt_argc)
6282 {
6283 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6284 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6285 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6286 }
6287 else
6288 argv = NULL;
6289 if (tv->vval.v_partial->pt_dict != NULL)
6290 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006291 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006292 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006293 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006294 tv->vval.v_partial->pt_dict,
6295 tv->vval.v_partial->pt_auto);
Bram Moolenaardb913952012-06-29 12:54:53 +02006296 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006297 case VAR_CHANNEL:
6298 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006299 Py_INCREF(Py_None);
6300 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006301 case VAR_SPECIAL:
6302 switch (tv->vval.v_number)
6303 {
6304 case VVAL_FALSE: return AlwaysFalse(NULL);
6305 case VVAL_TRUE: return AlwaysTrue(NULL);
6306 case VVAL_NONE:
6307 case VVAL_NULL: return AlwaysNone(NULL);
6308 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006309 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006310 return NULL;
6311 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006312 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006313}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006314
6315typedef struct
6316{
6317 PyObject_HEAD
6318} CurrentObject;
6319static PyTypeObject CurrentType;
6320
6321 static void
6322init_structs(void)
6323{
6324 vim_memset(&OutputType, 0, sizeof(OutputType));
6325 OutputType.tp_name = "vim.message";
6326 OutputType.tp_basicsize = sizeof(OutputObject);
6327 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6328 OutputType.tp_doc = "vim message object";
6329 OutputType.tp_methods = OutputMethods;
6330#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006331 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6332 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006333 OutputType.tp_alloc = call_PyType_GenericAlloc;
6334 OutputType.tp_new = call_PyType_GenericNew;
6335 OutputType.tp_free = call_PyObject_Free;
6336#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006337 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6338 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006339#endif
6340
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006341 vim_memset(&IterType, 0, sizeof(IterType));
6342 IterType.tp_name = "vim.iter";
6343 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006344 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006345 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006346 IterType.tp_iter = (getiterfunc)IterIter;
6347 IterType.tp_iternext = (iternextfunc)IterNext;
6348 IterType.tp_dealloc = (destructor)IterDestructor;
6349 IterType.tp_traverse = (traverseproc)IterTraverse;
6350 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006351
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006352 vim_memset(&BufferType, 0, sizeof(BufferType));
6353 BufferType.tp_name = "vim.buffer";
6354 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006355 BufferType.tp_dealloc = (destructor)BufferDestructor;
6356 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006357 BufferType.tp_as_sequence = &BufferAsSeq;
6358 BufferType.tp_as_mapping = &BufferAsMapping;
6359 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6360 BufferType.tp_doc = "vim buffer object";
6361 BufferType.tp_methods = BufferMethods;
6362#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006363 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006364 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006365 BufferType.tp_alloc = call_PyType_GenericAlloc;
6366 BufferType.tp_new = call_PyType_GenericNew;
6367 BufferType.tp_free = call_PyObject_Free;
6368#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006369 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006370 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006371#endif
6372
6373 vim_memset(&WindowType, 0, sizeof(WindowType));
6374 WindowType.tp_name = "vim.window";
6375 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006376 WindowType.tp_dealloc = (destructor)WindowDestructor;
6377 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006378 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006379 WindowType.tp_doc = "vim Window object";
6380 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006381 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6382 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006383#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006384 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6385 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006386 WindowType.tp_alloc = call_PyType_GenericAlloc;
6387 WindowType.tp_new = call_PyType_GenericNew;
6388 WindowType.tp_free = call_PyObject_Free;
6389#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006390 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6391 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006392#endif
6393
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006394 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6395 TabPageType.tp_name = "vim.tabpage";
6396 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006397 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6398 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006399 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6400 TabPageType.tp_doc = "vim tab page object";
6401 TabPageType.tp_methods = TabPageMethods;
6402#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006403 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006404 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6405 TabPageType.tp_new = call_PyType_GenericNew;
6406 TabPageType.tp_free = call_PyObject_Free;
6407#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006408 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006409#endif
6410
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006411 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6412 BufMapType.tp_name = "vim.bufferlist";
6413 BufMapType.tp_basicsize = sizeof(BufMapObject);
6414 BufMapType.tp_as_mapping = &BufMapAsMapping;
6415 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006416 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006417 BufferType.tp_doc = "vim buffer list";
6418
6419 vim_memset(&WinListType, 0, sizeof(WinListType));
6420 WinListType.tp_name = "vim.windowlist";
6421 WinListType.tp_basicsize = sizeof(WinListType);
6422 WinListType.tp_as_sequence = &WinListAsSeq;
6423 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6424 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006425 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006426
6427 vim_memset(&TabListType, 0, sizeof(TabListType));
6428 TabListType.tp_name = "vim.tabpagelist";
6429 TabListType.tp_basicsize = sizeof(TabListType);
6430 TabListType.tp_as_sequence = &TabListAsSeq;
6431 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6432 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006433
6434 vim_memset(&RangeType, 0, sizeof(RangeType));
6435 RangeType.tp_name = "vim.range";
6436 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006437 RangeType.tp_dealloc = (destructor)RangeDestructor;
6438 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006439 RangeType.tp_as_sequence = &RangeAsSeq;
6440 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006441 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006442 RangeType.tp_doc = "vim Range object";
6443 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006444 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6445 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006446#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006447 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448 RangeType.tp_alloc = call_PyType_GenericAlloc;
6449 RangeType.tp_new = call_PyType_GenericNew;
6450 RangeType.tp_free = call_PyObject_Free;
6451#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006452 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006453#endif
6454
6455 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6456 CurrentType.tp_name = "vim.currentdata";
6457 CurrentType.tp_basicsize = sizeof(CurrentObject);
6458 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6459 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006460 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006461#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006462 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6463 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006464#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006465 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6466 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467#endif
6468
6469 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6470 DictionaryType.tp_name = "vim.dictionary";
6471 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006472 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006473 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006474 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006475 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006476 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6477 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006478 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6479 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6480 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006481#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006482 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6483 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006484#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006485 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6486 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006487#endif
6488
6489 vim_memset(&ListType, 0, sizeof(ListType));
6490 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006491 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006492 ListType.tp_basicsize = sizeof(ListObject);
6493 ListType.tp_as_sequence = &ListAsSeq;
6494 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006495 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006496 ListType.tp_doc = "list pushing modifications to vim structure";
6497 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006498 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006499 ListType.tp_new = (newfunc)ListConstructor;
6500 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006501#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006502 ListType.tp_getattro = (getattrofunc)ListGetattro;
6503 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 ListType.tp_getattr = (getattrfunc)ListGetattr;
6506 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006507#endif
6508
6509 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006510 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006511 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006512 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6513 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006514 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006515 FunctionType.tp_doc = "object that calls vim function";
6516 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006517 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006518 FunctionType.tp_new = (newfunc)FunctionConstructor;
6519 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006520#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006522#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006523 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006524#endif
6525
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006526 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6527 OptionsType.tp_name = "vim.options";
6528 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006529 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006530 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006531 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006532 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006533 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006534 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6535 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6536 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006537
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006538 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6539 LoaderType.tp_name = "vim.Loader";
6540 LoaderType.tp_basicsize = sizeof(LoaderObject);
6541 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6542 LoaderType.tp_doc = "vim message object";
6543 LoaderType.tp_methods = LoaderMethods;
6544 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6545
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006546#if PY_MAJOR_VERSION >= 3
6547 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6548 vimmodule.m_name = "vim";
6549 vimmodule.m_doc = "Vim Python interface\n";
6550 vimmodule.m_size = -1;
6551 vimmodule.m_methods = VimMethods;
6552#endif
6553}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006554
6555#define PYTYPE_READY(type) \
6556 if (PyType_Ready(&type)) \
6557 return -1;
6558
6559 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006560init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006561{
6562 PYTYPE_READY(IterType);
6563 PYTYPE_READY(BufferType);
6564 PYTYPE_READY(RangeType);
6565 PYTYPE_READY(WindowType);
6566 PYTYPE_READY(TabPageType);
6567 PYTYPE_READY(BufMapType);
6568 PYTYPE_READY(WinListType);
6569 PYTYPE_READY(TabListType);
6570 PYTYPE_READY(CurrentType);
6571 PYTYPE_READY(DictionaryType);
6572 PYTYPE_READY(ListType);
6573 PYTYPE_READY(FunctionType);
6574 PYTYPE_READY(OptionsType);
6575 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006576 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006577 return 0;
6578}
6579
6580 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006581init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006582{
6583 PyObject *path;
6584 PyObject *path_hook;
6585 PyObject *path_hooks;
6586
6587 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6588 return -1;
6589
6590 if (!(path_hooks = PySys_GetObject("path_hooks")))
6591 {
6592 PyErr_Clear();
6593 path_hooks = PyList_New(1);
6594 PyList_SET_ITEM(path_hooks, 0, path_hook);
6595 if (PySys_SetObject("path_hooks", path_hooks))
6596 {
6597 Py_DECREF(path_hooks);
6598 return -1;
6599 }
6600 Py_DECREF(path_hooks);
6601 }
6602 else if (PyList_Check(path_hooks))
6603 {
6604 if (PyList_Append(path_hooks, path_hook))
6605 {
6606 Py_DECREF(path_hook);
6607 return -1;
6608 }
6609 Py_DECREF(path_hook);
6610 }
6611 else
6612 {
6613 VimTryStart();
6614 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6615 "You should now do the following:\n"
6616 "- append vim.path_hook to sys.path_hooks\n"
6617 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6618 VimTryEnd(); /* Discard the error */
6619 Py_DECREF(path_hook);
6620 return 0;
6621 }
6622
6623 if (!(path = PySys_GetObject("path")))
6624 {
6625 PyErr_Clear();
6626 path = PyList_New(1);
6627 Py_INCREF(vim_special_path_object);
6628 PyList_SET_ITEM(path, 0, vim_special_path_object);
6629 if (PySys_SetObject("path", path))
6630 {
6631 Py_DECREF(path);
6632 return -1;
6633 }
6634 Py_DECREF(path);
6635 }
6636 else if (PyList_Check(path))
6637 {
6638 if (PyList_Append(path, vim_special_path_object))
6639 return -1;
6640 }
6641 else
6642 {
6643 VimTryStart();
6644 EMSG(_("Failed to set path: sys.path is not a list\n"
6645 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6646 VimTryEnd(); /* Discard the error */
6647 }
6648
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006649 return 0;
6650}
6651
6652static BufMapObject TheBufferMap =
6653{
6654 PyObject_HEAD_INIT(&BufMapType)
6655};
6656
6657static WinListObject TheWindowList =
6658{
6659 PyObject_HEAD_INIT(&WinListType)
6660 NULL
6661};
6662
6663static CurrentObject TheCurrent =
6664{
6665 PyObject_HEAD_INIT(&CurrentType)
6666};
6667
6668static TabListObject TheTabPageList =
6669{
6670 PyObject_HEAD_INIT(&TabListType)
6671};
6672
6673static struct numeric_constant {
6674 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006675 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006676} numeric_constants[] = {
6677 {"VAR_LOCKED", VAR_LOCKED},
6678 {"VAR_FIXED", VAR_FIXED},
6679 {"VAR_SCOPE", VAR_SCOPE},
6680 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6681};
6682
6683static struct object_constant {
6684 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006685 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006686} object_constants[] = {
6687 {"buffers", (PyObject *)(void *)&TheBufferMap},
6688 {"windows", (PyObject *)(void *)&TheWindowList},
6689 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6690 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006691
6692 {"Buffer", (PyObject *)&BufferType},
6693 {"Range", (PyObject *)&RangeType},
6694 {"Window", (PyObject *)&WindowType},
6695 {"TabPage", (PyObject *)&TabPageType},
6696 {"Dictionary", (PyObject *)&DictionaryType},
6697 {"List", (PyObject *)&ListType},
6698 {"Function", (PyObject *)&FunctionType},
6699 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006700 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006701};
6702
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006703#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006704 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006705 return -1;
6706
6707#define ADD_CHECKED_OBJECT(m, name, obj) \
6708 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006709 PyObject *valObject = obj; \
6710 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006711 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006712 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006713 }
6714
6715 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006716populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006717{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006718 int i;
6719 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006720 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006721 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006722
6723 for (i = 0; i < (int)(sizeof(numeric_constants)
6724 / sizeof(struct numeric_constant));
6725 ++i)
6726 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006727 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006728
6729 for (i = 0; i < (int)(sizeof(object_constants)
6730 / sizeof(struct object_constant));
6731 ++i)
6732 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006733 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006734
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006735 valObject = object_constants[i].valObject;
6736 Py_INCREF(valObject);
6737 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006738 }
6739
6740 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6741 return -1;
6742 ADD_OBJECT(m, "error", VimError);
6743
Bram Moolenaara9922d62013-05-30 13:01:18 +02006744 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6745 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006746 ADD_CHECKED_OBJECT(m, "options",
6747 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006748
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006749 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006750 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006751 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006752
Bram Moolenaar22081f42016-06-01 20:38:34 +02006753#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006754 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006755 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006756#else
6757 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6758 return -1;
6759#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006760 ADD_OBJECT(m, "_getcwd", py_getcwd)
6761
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006762 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006763 return -1;
6764 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006765 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006766 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006767 if (PyObject_SetAttrString(other_module, "chdir", attr))
6768 {
6769 Py_DECREF(attr);
6770 return -1;
6771 }
6772 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006773
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006774 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006775 {
6776 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006777 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006778 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006779 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6780 {
6781 Py_DECREF(attr);
6782 return -1;
6783 }
6784 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006785 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006786 else
6787 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006788
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006789 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6790 return -1;
6791
6792 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6793
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006794 if (!(imp = PyImport_ImportModule("imp")))
6795 return -1;
6796
6797 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6798 {
6799 Py_DECREF(imp);
6800 return -1;
6801 }
6802
6803 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6804 {
6805 Py_DECREF(py_find_module);
6806 Py_DECREF(imp);
6807 return -1;
6808 }
6809
6810 Py_DECREF(imp);
6811
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006812 ADD_OBJECT(m, "_find_module", py_find_module);
6813 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006814
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006815 return 0;
6816}