xref: /vim-8.2.3635/src/if_py_both.h (revision 2bf24176)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
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 /*
10  * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11  * Pavlov.
12  *
13  * Common code for if_python.c and if_python3.c.
14  */
15 
16 #ifdef __BORLANDC__
17 /* Disable Warning W8060: Possibly incorrect assignment in function ... */
18 # pragma warn -8060
19 #endif
20 
21 static char_u e_py_systemexit[]	= "E880: Can't handle SystemExit of %s exception in vim";
22 
23 #if PY_VERSION_HEX < 0x02050000
24 typedef int Py_ssize_t;  /* Python 2.4 and earlier don't have this type. */
25 #endif
26 
27 #ifdef FEAT_MBYTE
28 # define ENC_OPT ((char *)p_enc)
29 #else
30 # define ENC_OPT "latin1"
31 #endif
32 #define DOPY_FUNC "_vim_pydo"
33 
34 static const char *vim_special_path = "_vim_path_";
35 
36 #define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
37 #define PyErr_SetVim(str) PyErr_SetString(VimError, str)
38 #define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
39 #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)
42 
43 #define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 	? "(NULL)" \
45 	: obj->ob_type->tp_name)
46 
47 #define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
48 					    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"))
55 #define RAISE_KEY_ADD_FAIL(key) \
56     PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
57 #define RAISE_INVALID_INDEX_TYPE(idx) \
58     PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
59 	    Py_TYPE_NAME(idx));
60 
61 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
63 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
64 
65 typedef void (*rangeinitializer)(void *);
66 typedef void (*runner)(const char *, void *
67 #ifdef PY_CAN_RECURSE
68 	, PyGILState_STATE *
69 #endif
70 	);
71 
72 static int ConvertFromPyObject(PyObject *, typval_T *);
73 static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
74 static int ConvertFromPyMapping(PyObject *, typval_T *);
75 static PyObject *WindowNew(win_T *, tabpage_T *);
76 static PyObject *BufferNew (buf_T *);
77 static PyObject *LineToString(const char *);
78 
79 static PyInt RangeStart;
80 static PyInt RangeEnd;
81 
82 static PyObject *globals;
83 
84 static PyObject *py_chdir;
85 static PyObject *py_fchdir;
86 static PyObject *py_getcwd;
87 static PyObject *vim_module;
88 static PyObject *vim_special_path_object;
89 
90 static PyObject *py_find_module;
91 static PyObject *py_load_module;
92 
93 static PyObject *VimError;
94 
95 /*
96  * obtain a lock on the Vim data structures
97  */
98     static void
99 Python_Lock_Vim(void)
100 {
101 }
102 
103 /*
104  * release a lock on the Vim data structures
105  */
106     static void
107 Python_Release_Vim(void)
108 {
109 }
110 
111 /*
112  * The "todecref" argument holds a pointer to PyObject * that must be
113  * DECREF'ed after returned char_u * is no longer needed or NULL if all what
114  * was needed to generate returned value is object.
115  *
116  * Use Py_XDECREF to decrement reference count.
117  */
118     static char_u *
119 StringToChars(PyObject *obj, PyObject **todecref)
120 {
121     char_u	*str;
122 
123     if (PyBytes_Check(obj))
124     {
125 
126 	if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
127 		|| str == NULL)
128 	    return NULL;
129 
130 	*todecref = NULL;
131     }
132     else if (PyUnicode_Check(obj))
133     {
134 	PyObject	*bytes;
135 
136 	if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
137 	    return NULL;
138 
139 	if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
140 		|| str == NULL)
141 	{
142 	    Py_DECREF(bytes);
143 	    return NULL;
144 	}
145 
146 	*todecref = bytes;
147     }
148     else
149     {
150 #if PY_MAJOR_VERSION < 3
151 	PyErr_FORMAT(PyExc_TypeError,
152 		N_("expected str() or unicode() instance, but got %s"),
153 		Py_TYPE_NAME(obj));
154 #else
155 	PyErr_FORMAT(PyExc_TypeError,
156 		N_("expected bytes() or str() instance, but got %s"),
157 		Py_TYPE_NAME(obj));
158 #endif
159 	return NULL;
160     }
161 
162     return (char_u *) str;
163 }
164 
165 #define NUMBER_LONG     1
166 #define NUMBER_INT      2
167 #define NUMBER_NATURAL  4
168 #define NUMBER_UNSIGNED 8
169 
170     static int
171 NumberToLong(PyObject *obj, long *result, int flags)
172 {
173 #if PY_MAJOR_VERSION < 3
174     if (PyInt_Check(obj))
175     {
176 	*result = PyInt_AsLong(obj);
177 	if (PyErr_Occurred())
178 	    return -1;
179     }
180     else
181 #endif
182     if (PyLong_Check(obj))
183     {
184 	*result = PyLong_AsLong(obj);
185 	if (PyErr_Occurred())
186 	    return -1;
187     }
188     else if (PyNumber_Check(obj))
189     {
190 	PyObject	*num;
191 
192 	if (!(num = PyNumber_Long(obj)))
193 	    return -1;
194 
195 	*result = PyLong_AsLong(num);
196 
197 	Py_DECREF(num);
198 
199 	if (PyErr_Occurred())
200 	    return -1;
201     }
202     else
203     {
204 #if PY_MAJOR_VERSION < 3
205 	PyErr_FORMAT(PyExc_TypeError,
206 		N_("expected int(), long() or something supporting "
207 		   "coercing to long(), but got %s"),
208 		Py_TYPE_NAME(obj));
209 #else
210 	PyErr_FORMAT(PyExc_TypeError,
211 		N_("expected int() or something supporting coercing to int(), "
212 		   "but got %s"),
213 		Py_TYPE_NAME(obj));
214 #endif
215 	return -1;
216     }
217 
218     if (flags & NUMBER_INT)
219     {
220 	if (*result > INT_MAX)
221 	{
222 	    PyErr_SET_STRING(PyExc_OverflowError,
223 		    N_("value is too large to fit into C int type"));
224 	    return -1;
225 	}
226 	else if (*result < INT_MIN)
227 	{
228 	    PyErr_SET_STRING(PyExc_OverflowError,
229 		    N_("value is too small to fit into C int type"));
230 	    return -1;
231 	}
232     }
233 
234     if (flags & NUMBER_NATURAL)
235     {
236 	if (*result <= 0)
237 	{
238 	    PyErr_SET_STRING(PyExc_ValueError,
239 		    N_("number must be greater than zero"));
240 	    return -1;
241 	}
242     }
243     else if (flags & NUMBER_UNSIGNED)
244     {
245 	if (*result < 0)
246 	{
247 	    PyErr_SET_STRING(PyExc_ValueError,
248 		    N_("number must be greater or equal to zero"));
249 	    return -1;
250 	}
251     }
252 
253     return 0;
254 }
255 
256     static int
257 add_string(PyObject *list, char *s)
258 {
259     PyObject	*string;
260 
261     if (!(string = PyString_FromString(s)))
262 	return -1;
263 
264     if (PyList_Append(list, string))
265     {
266 	Py_DECREF(string);
267 	return -1;
268     }
269 
270     Py_DECREF(string);
271     return 0;
272 }
273 
274     static PyObject *
275 ObjectDir(PyObject *self, char **attributes)
276 {
277     PyMethodDef	*method;
278     char	**attr;
279     PyObject	*ret;
280 
281     if (!(ret = PyList_New(0)))
282 	return NULL;
283 
284     if (self)
285 	for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
286 	    if (add_string(ret, (char *)method->ml_name))
287 	    {
288 		Py_DECREF(ret);
289 		return NULL;
290 	    }
291 
292     for (attr = attributes ; *attr ; ++attr)
293 	if (add_string(ret, *attr))
294 	{
295 	    Py_DECREF(ret);
296 	    return NULL;
297 	}
298 
299 #if PY_MAJOR_VERSION < 3
300     if (add_string(ret, "__members__"))
301     {
302 	Py_DECREF(ret);
303 	return NULL;
304     }
305 #endif
306 
307     return ret;
308 }
309 
310 /* Output buffer management
311  */
312 
313 /* Function to write a line, points to either msg() or emsg(). */
314 typedef void (*writefn)(char_u *);
315 
316 static PyTypeObject OutputType;
317 
318 typedef struct
319 {
320     PyObject_HEAD
321     long softspace;
322     long error;
323 } OutputObject;
324 
325 static char *OutputAttrs[] = {
326     "softspace",
327     NULL
328 };
329 
330     static PyObject *
331 OutputDir(PyObject *self)
332 {
333     return ObjectDir(self, OutputAttrs);
334 }
335 
336     static int
337 OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
338 {
339     if (valObject == NULL)
340     {
341 	PyErr_SET_STRING(PyExc_AttributeError,
342 		N_("can't delete OutputObject attributes"));
343 	return -1;
344     }
345 
346     if (strcmp(name, "softspace") == 0)
347     {
348 	if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
349 	    return -1;
350 	return 0;
351     }
352 
353     PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
354     return -1;
355 }
356 
357 /* Buffer IO, we write one whole line at a time. */
358 static garray_T io_ga = {0, 0, 1, 80, NULL};
359 static writefn old_fn = NULL;
360 
361     static void
362 PythonIO_Flush(void)
363 {
364     if (old_fn != NULL && io_ga.ga_len > 0)
365     {
366 	((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
367 	old_fn((char_u *)io_ga.ga_data);
368     }
369     io_ga.ga_len = 0;
370 }
371 
372     static void
373 writer(writefn fn, char_u *str, PyInt n)
374 {
375     char_u *ptr;
376 
377     /* Flush when switching output function. */
378     if (fn != old_fn)
379 	PythonIO_Flush();
380     old_fn = fn;
381 
382     /* Write each NL separated line.  Text after the last NL is kept for
383      * writing later. */
384     while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
385     {
386 	PyInt len = ptr - str;
387 
388 	if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 	    break;
390 
391 	mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 	((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 	fn((char_u *)io_ga.ga_data);
394 	str = ptr + 1;
395 	n -= len + 1;
396 	io_ga.ga_len = 0;
397     }
398 
399     /* Put the remaining text into io_ga for later printing. */
400     if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
401     {
402 	mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 	io_ga.ga_len += (int)n;
404     }
405 }
406 
407     static int
408 write_output(OutputObject *self, PyObject *string)
409 {
410     Py_ssize_t	len = 0;
411     char	*str = NULL;
412     int		error = self->error;
413 
414     if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 	return -1;
416 
417     Py_BEGIN_ALLOW_THREADS
418     Python_Lock_Vim();
419     writer((writefn)(error ? emsg : msg), (char_u *)str, len);
420     Python_Release_Vim();
421     Py_END_ALLOW_THREADS
422     PyMem_Free(str);
423 
424     return 0;
425 }
426 
427     static PyObject *
428 OutputWrite(OutputObject *self, PyObject *string)
429 {
430     if (write_output(self, string))
431 	return NULL;
432 
433     Py_INCREF(Py_None);
434     return Py_None;
435 }
436 
437     static PyObject *
438 OutputWritelines(OutputObject *self, PyObject *seq)
439 {
440     PyObject	*iterator;
441     PyObject	*item;
442 
443     if (!(iterator = PyObject_GetIter(seq)))
444 	return NULL;
445 
446     while ((item = PyIter_Next(iterator)))
447     {
448 	if (write_output(self, item))
449 	{
450 	    Py_DECREF(iterator);
451 	    Py_DECREF(item);
452 	    return NULL;
453 	}
454 	Py_DECREF(item);
455     }
456 
457     Py_DECREF(iterator);
458 
459     /* Iterator may have finished due to an exception */
460     if (PyErr_Occurred())
461 	return NULL;
462 
463     Py_INCREF(Py_None);
464     return Py_None;
465 }
466 
467     static PyObject *
468 AlwaysNone(PyObject *self UNUSED)
469 {
470     /* do nothing */
471     Py_INCREF(Py_None);
472     return Py_None;
473 }
474 
475     static PyObject *
476 AlwaysFalse(PyObject *self UNUSED)
477 {
478     /* do nothing */
479     PyObject	*ret = Py_False;
480     Py_INCREF(ret);
481     return ret;
482 }
483 
484     static PyObject *
485 AlwaysTrue(PyObject *self UNUSED)
486 {
487     /* do nothing */
488     PyObject	*ret = Py_True;
489     Py_INCREF(ret);
490     return ret;
491 }
492 
493 /***************/
494 
495 static struct PyMethodDef OutputMethods[] = {
496     /* name,	    function,				calling,	doc */
497     {"write",	    (PyCFunction)OutputWrite,		METH_O,		""},
498     {"writelines",  (PyCFunction)OutputWritelines,	METH_O,		""},
499     {"flush",	    (PyCFunction)AlwaysNone,		METH_NOARGS,	""},
500     {"close",	    (PyCFunction)AlwaysNone,		METH_NOARGS,	""},
501     {"isatty",	    (PyCFunction)AlwaysFalse,		METH_NOARGS,	""},
502     {"readable",    (PyCFunction)AlwaysFalse,		METH_NOARGS,	""},
503     {"seekable",    (PyCFunction)AlwaysFalse,		METH_NOARGS,	""},
504     {"writable",    (PyCFunction)AlwaysTrue,		METH_NOARGS,	""},
505     {"__dir__",	    (PyCFunction)OutputDir,		METH_NOARGS,	""},
506     { NULL,	    NULL,				0,		NULL}
507 };
508 
509 static OutputObject Output =
510 {
511     PyObject_HEAD_INIT(&OutputType)
512     0,
513     0
514 };
515 
516 static OutputObject Error =
517 {
518     PyObject_HEAD_INIT(&OutputType)
519     0,
520     1
521 };
522 
523     static int
524 PythonIO_Init_io(void)
525 {
526     if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
527 	return -1;
528     if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
529 	return -1;
530 
531     if (PyErr_Occurred())
532     {
533 	EMSG(_("E264: Python: Error initialising I/O objects"));
534 	return -1;
535     }
536 
537     return 0;
538 }
539 
540 typedef struct
541 {
542     PyObject_HEAD
543     PyObject	*module;
544 } LoaderObject;
545 static PyTypeObject LoaderType;
546 
547     static void
548 LoaderDestructor(LoaderObject *self)
549 {
550     Py_DECREF(self->module);
551     DESTRUCTOR_FINISH(self);
552 }
553 
554     static PyObject *
555 LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
556 {
557     PyObject	*ret = self->module;
558 
559     Py_INCREF(ret);
560     return ret;
561 }
562 
563 static struct PyMethodDef LoaderMethods[] = {
564     /* name,	    function,				calling,	doc */
565     {"load_module", (PyCFunction)LoaderLoadModule,	METH_VARARGS,	""},
566     { NULL,	    NULL,				0,		NULL}
567 };
568 
569 /* Check to see whether a Vim error has been reported, or a keyboard
570  * interrupt has been detected.
571  */
572 
573     static void
574 VimTryStart(void)
575 {
576     ++trylevel;
577 }
578 
579     static int
580 VimTryEnd(void)
581 {
582     --trylevel;
583     /* Without this it stops processing all subsequent VimL commands and
584      * generates strange error messages if I e.g. try calling Test() in a
585      * cycle */
586     did_emsg = FALSE;
587     /* Keyboard interrupt should be preferred over anything else */
588     if (got_int)
589     {
590 	if (did_throw)
591 	    discard_current_exception();
592 	got_int = FALSE;
593 	PyErr_SetNone(PyExc_KeyboardInterrupt);
594 	return -1;
595     }
596     else if (msg_list != NULL && *msg_list != NULL)
597     {
598 	int	should_free;
599 	char_u	*msg;
600 
601 	msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
602 
603 	if (msg == NULL)
604 	{
605 	    PyErr_NoMemory();
606 	    return -1;
607 	}
608 
609 	PyErr_SetVim((char *) msg);
610 
611 	free_global_msglist();
612 
613 	if (should_free)
614 	    vim_free(msg);
615 
616 	return -1;
617     }
618     else if (!did_throw)
619 	return (PyErr_Occurred() ? -1 : 0);
620     /* Python exception is preferred over vim one; unlikely to occur though */
621     else if (PyErr_Occurred())
622     {
623 	discard_current_exception();
624 	return -1;
625     }
626     /* Finally transform VimL exception to python one */
627     else
628     {
629 	PyErr_SetVim((char *)current_exception->value);
630 	discard_current_exception();
631 	return -1;
632     }
633 }
634 
635     static int
636 VimCheckInterrupt(void)
637 {
638     if (got_int)
639     {
640 	PyErr_SetNone(PyExc_KeyboardInterrupt);
641 	return 1;
642     }
643     return 0;
644 }
645 
646 /* Vim module - Implementation
647  */
648 
649     static PyObject *
650 VimCommand(PyObject *self UNUSED, PyObject *string)
651 {
652     char_u	*cmd;
653     PyObject	*ret;
654     PyObject	*todecref;
655 
656     if (!(cmd = StringToChars(string, &todecref)))
657 	return NULL;
658 
659     Py_BEGIN_ALLOW_THREADS
660     Python_Lock_Vim();
661 
662     VimTryStart();
663     do_cmdline_cmd(cmd);
664     update_screen(VALID);
665 
666     Python_Release_Vim();
667     Py_END_ALLOW_THREADS
668 
669     if (VimTryEnd())
670 	ret = NULL;
671     else
672 	ret = Py_None;
673 
674     Py_XINCREF(ret);
675     Py_XDECREF(todecref);
676     return ret;
677 }
678 
679 /*
680  * Function to translate a typval_T into a PyObject; this will recursively
681  * translate lists/dictionaries into their Python equivalents.
682  *
683  * The depth parameter is to avoid infinite recursion, set it to 1 when
684  * you call VimToPython.
685  */
686     static PyObject *
687 VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
688 {
689     PyObject	*ret;
690     PyObject	*newObj;
691     char	ptrBuf[sizeof(void *) * 2 + 3];
692 
693     /* Avoid infinite recursion */
694     if (depth > 100)
695     {
696 	Py_INCREF(Py_None);
697 	ret = Py_None;
698 	return ret;
699     }
700 
701     /* Check if we run into a recursive loop.  The item must be in lookup_dict
702      * then and we can use it again. */
703     if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
704 	    || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
705     {
706 	sprintf(ptrBuf, "%p",
707 		our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
708 					   : (void *)our_tv->vval.v_dict);
709 
710 	if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
711 	{
712 	    Py_INCREF(ret);
713 	    return ret;
714 	}
715     }
716 
717     if (our_tv->v_type == VAR_STRING)
718 	ret = PyString_FromString(our_tv->vval.v_string == NULL
719 					? "" : (char *)our_tv->vval.v_string);
720     else if (our_tv->v_type == VAR_NUMBER)
721     {
722 	char buf[NUMBUFLEN];
723 
724 	/* For backwards compatibility numbers are stored as strings. */
725 	sprintf(buf, "%ld", (long)our_tv->vval.v_number);
726 	ret = PyString_FromString((char *)buf);
727     }
728 # ifdef FEAT_FLOAT
729     else if (our_tv->v_type == VAR_FLOAT)
730     {
731 	char buf[NUMBUFLEN];
732 
733 	sprintf(buf, "%f", our_tv->vval.v_float);
734 	ret = PyString_FromString((char *)buf);
735     }
736 # endif
737     else if (our_tv->v_type == VAR_LIST)
738     {
739 	list_T		*list = our_tv->vval.v_list;
740 	listitem_T	*curr;
741 
742 	if (list == NULL)
743 	    return NULL;
744 
745 	if (!(ret = PyList_New(0)))
746 	    return NULL;
747 
748 	if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
749 	{
750 	    Py_DECREF(ret);
751 	    return NULL;
752 	}
753 
754 	for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
755 	{
756 	    if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
757 	    {
758 		Py_DECREF(ret);
759 		return NULL;
760 	    }
761 	    if (PyList_Append(ret, newObj))
762 	    {
763 		Py_DECREF(newObj);
764 		Py_DECREF(ret);
765 		return NULL;
766 	    }
767 	    Py_DECREF(newObj);
768 	}
769     }
770     else if (our_tv->v_type == VAR_DICT)
771     {
772 
773 	hashtab_T	*ht;
774 	long_u		todo;
775 	hashitem_T	*hi;
776 	dictitem_T	*di;
777 
778 	if (our_tv->vval.v_dict == NULL)
779 	    return NULL;
780 	ht = &our_tv->vval.v_dict->dv_hashtab;
781 
782 	if (!(ret = PyDict_New()))
783 	    return NULL;
784 
785 	if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
786 	{
787 	    Py_DECREF(ret);
788 	    return NULL;
789 	}
790 
791 	todo = ht->ht_used;
792 	for (hi = ht->ht_array; todo > 0; ++hi)
793 	{
794 	    if (!HASHITEM_EMPTY(hi))
795 	    {
796 		--todo;
797 
798 		di = dict_lookup(hi);
799 		if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
800 		{
801 		    Py_DECREF(ret);
802 		    return NULL;
803 		}
804 		if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
805 		{
806 		    Py_DECREF(ret);
807 		    Py_DECREF(newObj);
808 		    return NULL;
809 		}
810 	    }
811 	}
812     }
813     else
814     {
815 	Py_INCREF(Py_None);
816 	ret = Py_None;
817     }
818 
819     return ret;
820 }
821 
822     static PyObject *
823 VimEval(PyObject *self UNUSED, PyObject *args)
824 {
825     char_u	*expr;
826     typval_T	*our_tv;
827     PyObject	*string;
828     PyObject	*todecref;
829     PyObject	*ret;
830     PyObject	*lookup_dict;
831 
832     if (!PyArg_ParseTuple(args, "O", &string))
833 	return NULL;
834 
835     if (!(expr = StringToChars(string, &todecref)))
836 	return NULL;
837 
838     Py_BEGIN_ALLOW_THREADS
839     Python_Lock_Vim();
840     VimTryStart();
841     our_tv = eval_expr(expr, NULL);
842     Python_Release_Vim();
843     Py_END_ALLOW_THREADS
844 
845     Py_XDECREF(todecref);
846 
847     if (VimTryEnd())
848 	return NULL;
849 
850     if (our_tv == NULL)
851     {
852 	PyErr_SET_VIM(N_("invalid expression"));
853 	return NULL;
854     }
855 
856     /* Convert the Vim type into a Python type.  Create a dictionary that's
857      * used to check for recursive loops. */
858     if (!(lookup_dict = PyDict_New()))
859 	ret = NULL;
860     else
861     {
862 	ret = VimToPython(our_tv, 1, lookup_dict);
863 	Py_DECREF(lookup_dict);
864     }
865 
866 
867     Py_BEGIN_ALLOW_THREADS
868     Python_Lock_Vim();
869     free_tv(our_tv);
870     Python_Release_Vim();
871     Py_END_ALLOW_THREADS
872 
873     return ret;
874 }
875 
876 static PyObject *ConvertToPyObject(typval_T *);
877 
878     static PyObject *
879 VimEvalPy(PyObject *self UNUSED, PyObject *string)
880 {
881     typval_T	*our_tv;
882     PyObject	*ret;
883     char_u	*expr;
884     PyObject	*todecref;
885 
886     if (!(expr = StringToChars(string, &todecref)))
887 	return NULL;
888 
889     Py_BEGIN_ALLOW_THREADS
890     Python_Lock_Vim();
891     VimTryStart();
892     our_tv = eval_expr(expr, NULL);
893     Python_Release_Vim();
894     Py_END_ALLOW_THREADS
895 
896     Py_XDECREF(todecref);
897 
898     if (VimTryEnd())
899 	return NULL;
900 
901     if (our_tv == NULL)
902     {
903 	PyErr_SET_VIM(N_("invalid expression"));
904 	return NULL;
905     }
906 
907     ret = ConvertToPyObject(our_tv);
908     Py_BEGIN_ALLOW_THREADS
909     Python_Lock_Vim();
910     free_tv(our_tv);
911     Python_Release_Vim();
912     Py_END_ALLOW_THREADS
913 
914     return ret;
915 }
916 
917     static PyObject *
918 VimStrwidth(PyObject *self UNUSED, PyObject *string)
919 {
920     char_u	*str;
921     PyObject	*todecref;
922     int		len;
923 
924     if (!(str = StringToChars(string, &todecref)))
925 	return NULL;
926 
927 #ifdef FEAT_MBYTE
928     len = mb_string2cells(str, (int)STRLEN(str));
929 #else
930     len = STRLEN(str);
931 #endif
932 
933     Py_XDECREF(todecref);
934 
935     return PyLong_FromLong(len);
936 }
937 
938     static PyObject *
939 _VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
940 {
941     PyObject	*ret;
942     PyObject	*newwd;
943     PyObject	*todecref;
944     char_u	*new_dir;
945 
946     if (_chdir == NULL)
947 	return NULL;
948     if (!(ret = PyObject_Call(_chdir, args, kwargs)))
949 	return NULL;
950 
951     if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
952     {
953 	Py_DECREF(ret);
954 	return NULL;
955     }
956 
957     if (!(new_dir = StringToChars(newwd, &todecref)))
958     {
959 	Py_DECREF(ret);
960 	Py_DECREF(newwd);
961 	return NULL;
962     }
963 
964     VimTryStart();
965 
966     if (vim_chdir(new_dir))
967     {
968 	Py_DECREF(ret);
969 	Py_DECREF(newwd);
970 	Py_XDECREF(todecref);
971 
972 	if (VimTryEnd())
973 	    return NULL;
974 
975 	PyErr_SET_VIM(N_("failed to change directory"));
976 	return NULL;
977     }
978 
979     Py_DECREF(newwd);
980     Py_XDECREF(todecref);
981 
982     post_chdir(FALSE);
983 
984     if (VimTryEnd())
985     {
986 	Py_DECREF(ret);
987 	return NULL;
988     }
989 
990     return ret;
991 }
992 
993     static PyObject *
994 VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
995 {
996     return _VimChdir(py_chdir, args, kwargs);
997 }
998 
999     static PyObject *
1000 VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1001 {
1002     return _VimChdir(py_fchdir, args, kwargs);
1003 }
1004 
1005 typedef struct {
1006     PyObject	*callable;
1007     PyObject	*result;
1008 } map_rtp_data;
1009 
1010     static void
1011 map_rtp_callback(char_u *path, void *_data)
1012 {
1013     void	**data = (void **) _data;
1014     PyObject	*pathObject;
1015     map_rtp_data	*mr_data = *((map_rtp_data **) data);
1016 
1017     if (!(pathObject = PyString_FromString((char *)path)))
1018     {
1019 	*data = NULL;
1020 	return;
1021     }
1022 
1023     mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1024 						   pathObject, NULL);
1025 
1026     Py_DECREF(pathObject);
1027 
1028     if (!mr_data->result || mr_data->result != Py_None)
1029 	*data = NULL;
1030     else
1031     {
1032 	Py_DECREF(mr_data->result);
1033 	mr_data->result = NULL;
1034     }
1035 }
1036 
1037     static PyObject *
1038 VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
1039 {
1040     map_rtp_data	data;
1041 
1042     data.callable = callable;
1043     data.result = NULL;
1044 
1045     do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1046 
1047     if (data.result == NULL)
1048     {
1049 	if (PyErr_Occurred())
1050 	    return NULL;
1051 	else
1052 	{
1053 	    Py_INCREF(Py_None);
1054 	    return Py_None;
1055 	}
1056     }
1057     return data.result;
1058 }
1059 
1060 /*
1061  * _vim_runtimepath_ special path implementation.
1062  */
1063 
1064     static void
1065 map_finder_callback(char_u *path, void *_data)
1066 {
1067     void	**data = (void **) _data;
1068     PyObject	*list = *((PyObject **) data);
1069     PyObject	*pathObject1, *pathObject2;
1070     char	*pathbuf;
1071     size_t	pathlen;
1072 
1073     pathlen = STRLEN(path);
1074 
1075 #if PY_MAJOR_VERSION < 3
1076 # define PY_MAIN_DIR_STRING "python2"
1077 #else
1078 # define PY_MAIN_DIR_STRING "python3"
1079 #endif
1080 #define PY_ALTERNATE_DIR_STRING "pythonx"
1081 
1082 #define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1083     if (!(pathbuf = PyMem_New(char,
1084 		    pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1085     {
1086 	PyErr_NoMemory();
1087 	*data = NULL;
1088 	return;
1089     }
1090 
1091     mch_memmove(pathbuf, path, pathlen + 1);
1092     add_pathsep((char_u *) pathbuf);
1093 
1094     pathlen = STRLEN(pathbuf);
1095     mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1096 	    PYTHONX_STRING_LENGTH + 1);
1097 
1098     if (!(pathObject1 = PyString_FromString(pathbuf)))
1099     {
1100 	*data = NULL;
1101 	PyMem_Free(pathbuf);
1102 	return;
1103     }
1104 
1105     mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1106 	    PYTHONX_STRING_LENGTH + 1);
1107 
1108     if (!(pathObject2 = PyString_FromString(pathbuf)))
1109     {
1110 	Py_DECREF(pathObject1);
1111 	PyMem_Free(pathbuf);
1112 	*data = NULL;
1113 	return;
1114     }
1115 
1116     PyMem_Free(pathbuf);
1117 
1118     if (PyList_Append(list, pathObject1)
1119 	    || PyList_Append(list, pathObject2))
1120 	*data = NULL;
1121 
1122     Py_DECREF(pathObject1);
1123     Py_DECREF(pathObject2);
1124 }
1125 
1126     static PyObject *
1127 Vim_GetPaths(PyObject *self UNUSED)
1128 {
1129     PyObject	*ret;
1130 
1131     if (!(ret = PyList_New(0)))
1132 	return NULL;
1133 
1134     do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
1135 
1136     if (PyErr_Occurred())
1137     {
1138 	Py_DECREF(ret);
1139 	return NULL;
1140     }
1141 
1142     return ret;
1143 }
1144 
1145     static PyObject *
1146 call_load_module(char *name, int len, PyObject *find_module_result)
1147 {
1148     PyObject	*fd, *pathname, *description;
1149 
1150     if (!PyTuple_Check(find_module_result))
1151     {
1152 	PyErr_FORMAT(PyExc_TypeError,
1153 		N_("expected 3-tuple as imp.find_module() result, but got %s"),
1154 		Py_TYPE_NAME(find_module_result));
1155 	return NULL;
1156     }
1157     if (PyTuple_GET_SIZE(find_module_result) != 3)
1158     {
1159 	PyErr_FORMAT(PyExc_TypeError,
1160 		N_("expected 3-tuple as imp.find_module() result, but got "
1161 		   "tuple of size %d"),
1162 		(int) PyTuple_GET_SIZE(find_module_result));
1163 	return NULL;
1164     }
1165 
1166     if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1167 	    || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1168 	    || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1169     {
1170 	PyErr_SET_STRING(PyExc_RuntimeError,
1171 		N_("internal error: imp.find_module returned tuple with NULL"));
1172 	return NULL;
1173     }
1174 
1175     return PyObject_CallFunction(py_load_module,
1176 	    "s#OOO", name, len, fd, pathname, description);
1177 }
1178 
1179     static PyObject *
1180 find_module(char *fullname, char *tail, PyObject *new_path)
1181 {
1182     PyObject	*find_module_result;
1183     PyObject	*module;
1184     char	*dot;
1185 
1186     if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
1187     {
1188 	/*
1189 	 * There is a dot in the name: call find_module recursively without the
1190 	 * first component
1191 	 */
1192 	PyObject	*newest_path;
1193 	int		partlen = (int) (dot - 1 - tail);
1194 
1195 	if (!(find_module_result = PyObject_CallFunction(py_find_module,
1196 			"s#O", tail, partlen, new_path)))
1197 	    return NULL;
1198 
1199 	if (!(module = call_load_module(
1200 			fullname,
1201 			((int) (tail - fullname)) + partlen,
1202 			find_module_result)))
1203 	{
1204 	    Py_DECREF(find_module_result);
1205 	    return NULL;
1206 	}
1207 
1208 	Py_DECREF(find_module_result);
1209 
1210 	if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1211 	{
1212 	    Py_DECREF(module);
1213 	    return NULL;
1214 	}
1215 
1216 	Py_DECREF(module);
1217 
1218 	module = find_module(fullname, dot + 1, newest_path);
1219 
1220 	Py_DECREF(newest_path);
1221 
1222 	return module;
1223     }
1224     else
1225     {
1226 	if (!(find_module_result = PyObject_CallFunction(py_find_module,
1227 			"sO", tail, new_path)))
1228 	    return NULL;
1229 
1230 	if (!(module = call_load_module(
1231 			fullname,
1232 			(int)STRLEN(fullname),
1233 			find_module_result)))
1234 	{
1235 	    Py_DECREF(find_module_result);
1236 	    return NULL;
1237 	}
1238 
1239 	Py_DECREF(find_module_result);
1240 
1241 	return module;
1242     }
1243 }
1244 
1245     static PyObject *
1246 FinderFindModule(PyObject *self, PyObject *args)
1247 {
1248     char	*fullname;
1249     PyObject	*module;
1250     PyObject	*new_path;
1251     LoaderObject	*loader;
1252 
1253     if (!PyArg_ParseTuple(args, "s", &fullname))
1254 	return NULL;
1255 
1256     if (!(new_path = Vim_GetPaths(self)))
1257 	return NULL;
1258 
1259     module = find_module(fullname, fullname, new_path);
1260 
1261     Py_DECREF(new_path);
1262 
1263     if (!module)
1264     {
1265 	if (PyErr_Occurred())
1266 	{
1267 	    if (PyErr_ExceptionMatches(PyExc_ImportError))
1268 		PyErr_Clear();
1269 	    else
1270 		return NULL;
1271 	}
1272 
1273 	Py_INCREF(Py_None);
1274 	return Py_None;
1275     }
1276 
1277     if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1278     {
1279 	Py_DECREF(module);
1280 	return NULL;
1281     }
1282 
1283     loader->module = module;
1284 
1285     return (PyObject *) loader;
1286 }
1287 
1288     static PyObject *
1289 VimPathHook(PyObject *self UNUSED, PyObject *args)
1290 {
1291     char	*path;
1292 
1293     if (PyArg_ParseTuple(args, "s", &path)
1294 	    && STRCMP(path, vim_special_path) == 0)
1295     {
1296 	Py_INCREF(vim_module);
1297 	return vim_module;
1298     }
1299 
1300     PyErr_Clear();
1301     PyErr_SetNone(PyExc_ImportError);
1302     return NULL;
1303 }
1304 
1305 /*
1306  * Vim module - Definitions
1307  */
1308 
1309 static struct PyMethodDef VimMethods[] = {
1310     /* name,	    function,			calling,			documentation */
1311     {"command",	    VimCommand,			METH_O,				"Execute a Vim ex-mode command" },
1312     {"eval",	    VimEval,			METH_VARARGS,			"Evaluate an expression using Vim evaluator" },
1313     {"bindeval",    VimEvalPy,			METH_O,				"Like eval(), but returns objects attached to vim ones"},
1314     {"strwidth",    VimStrwidth,		METH_O,				"Screen string width, counts <Tab> as having width 1"},
1315     {"chdir",	    (PyCFunction)VimChdir,	METH_VARARGS|METH_KEYWORDS,	"Change directory"},
1316     {"fchdir",	    (PyCFunction)VimFchdir,	METH_VARARGS|METH_KEYWORDS,	"Change directory"},
1317     {"foreach_rtp", VimForeachRTP,		METH_O,				"Call given callable for each path in &rtp"},
1318     {"find_module", FinderFindModule,		METH_VARARGS,			"Internal use only, returns loader object for any input it receives"},
1319     {"path_hook",   VimPathHook,		METH_VARARGS,			"Hook function to install in sys.path_hooks"},
1320     {"_get_paths",  (PyCFunction)Vim_GetPaths,	METH_NOARGS,			"Get &rtp-based additions to sys.path"},
1321     { NULL,	    NULL,			0,				NULL}
1322 };
1323 
1324 /*
1325  * Generic iterator object
1326  */
1327 
1328 static PyTypeObject IterType;
1329 
1330 typedef PyObject *(*nextfun)(void **);
1331 typedef void (*destructorfun)(void *);
1332 typedef int (*traversefun)(void *, visitproc, void *);
1333 typedef int (*clearfun)(void **);
1334 
1335 /* Main purpose of this object is removing the need for do python
1336  * initialization (i.e. PyType_Ready and setting type attributes) for a big
1337  * bunch of objects. */
1338 
1339 typedef struct
1340 {
1341     PyObject_HEAD
1342     void *cur;
1343     nextfun next;
1344     destructorfun destruct;
1345     traversefun traverse;
1346     clearfun clear;
1347 } IterObject;
1348 
1349     static PyObject *
1350 IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1351 	clearfun clear)
1352 {
1353     IterObject *self;
1354 
1355     self = PyObject_GC_New(IterObject, &IterType);
1356     self->cur = start;
1357     self->next = next;
1358     self->destruct = destruct;
1359     self->traverse = traverse;
1360     self->clear = clear;
1361 
1362     return (PyObject *)(self);
1363 }
1364 
1365     static void
1366 IterDestructor(IterObject *self)
1367 {
1368     PyObject_GC_UnTrack((void *)(self));
1369     self->destruct(self->cur);
1370     PyObject_GC_Del((void *)(self));
1371 }
1372 
1373     static int
1374 IterTraverse(IterObject *self, visitproc visit, void *arg)
1375 {
1376     if (self->traverse != NULL)
1377 	return self->traverse(self->cur, visit, arg);
1378     else
1379 	return 0;
1380 }
1381 
1382 /* Mac OSX defines clear() somewhere. */
1383 #ifdef clear
1384 # undef clear
1385 #endif
1386 
1387     static int
1388 IterClear(IterObject *self)
1389 {
1390     if (self->clear != NULL)
1391 	return self->clear(&self->cur);
1392     else
1393 	return 0;
1394 }
1395 
1396     static PyObject *
1397 IterNext(IterObject *self)
1398 {
1399     return self->next(&self->cur);
1400 }
1401 
1402     static PyObject *
1403 IterIter(PyObject *self)
1404 {
1405     Py_INCREF(self);
1406     return self;
1407 }
1408 
1409 typedef struct pylinkedlist_S {
1410     struct pylinkedlist_S	*pll_next;
1411     struct pylinkedlist_S	*pll_prev;
1412     PyObject			*pll_obj;
1413 } pylinkedlist_T;
1414 
1415 static pylinkedlist_T *lastdict = NULL;
1416 static pylinkedlist_T *lastlist = NULL;
1417 
1418     static void
1419 pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1420 {
1421     if (ref->pll_prev == NULL)
1422     {
1423 	if (ref->pll_next == NULL)
1424 	{
1425 	    *last = NULL;
1426 	    return;
1427 	}
1428     }
1429     else
1430 	ref->pll_prev->pll_next = ref->pll_next;
1431 
1432     if (ref->pll_next == NULL)
1433 	*last = ref->pll_prev;
1434     else
1435 	ref->pll_next->pll_prev = ref->pll_prev;
1436 }
1437 
1438     static void
1439 pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1440 {
1441     if (*last == NULL)
1442 	ref->pll_prev = NULL;
1443     else
1444     {
1445 	(*last)->pll_next = ref;
1446 	ref->pll_prev = *last;
1447     }
1448     ref->pll_next = NULL;
1449     ref->pll_obj = self;
1450     *last = ref;
1451 }
1452 
1453 static PyTypeObject DictionaryType;
1454 
1455 typedef struct
1456 {
1457     PyObject_HEAD
1458     dict_T	*dict;
1459     pylinkedlist_T	ref;
1460 } DictionaryObject;
1461 
1462 static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1463 
1464 #define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1465 
1466     static PyObject *
1467 DictionaryNew(PyTypeObject *subtype, dict_T *dict)
1468 {
1469     DictionaryObject	*self;
1470 
1471     self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
1472     if (self == NULL)
1473 	return NULL;
1474     self->dict = dict;
1475     ++dict->dv_refcount;
1476 
1477     pyll_add((PyObject *)(self), &self->ref, &lastdict);
1478 
1479     return (PyObject *)(self);
1480 }
1481 
1482     static dict_T *
1483 py_dict_alloc(void)
1484 {
1485     dict_T	*ret;
1486 
1487     if (!(ret = dict_alloc()))
1488     {
1489 	PyErr_NoMemory();
1490 	return NULL;
1491     }
1492     ++ret->dv_refcount;
1493 
1494     return ret;
1495 }
1496 
1497     static PyObject *
1498 DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1499 {
1500     DictionaryObject	*self;
1501     dict_T	*dict;
1502 
1503     if (!(dict = py_dict_alloc()))
1504 	return NULL;
1505 
1506     self = (DictionaryObject *) DictionaryNew(subtype, dict);
1507 
1508     --dict->dv_refcount;
1509 
1510     if (kwargs || PyTuple_Size(args))
1511     {
1512 	PyObject	*tmp;
1513 	if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1514 	{
1515 	    Py_DECREF(self);
1516 	    return NULL;
1517 	}
1518 
1519 	Py_DECREF(tmp);
1520     }
1521 
1522     return (PyObject *)(self);
1523 }
1524 
1525     static void
1526 DictionaryDestructor(DictionaryObject *self)
1527 {
1528     pyll_remove(&self->ref, &lastdict);
1529     dict_unref(self->dict);
1530 
1531     DESTRUCTOR_FINISH(self);
1532 }
1533 
1534 static char *DictionaryAttrs[] = {
1535     "locked", "scope",
1536     NULL
1537 };
1538 
1539     static PyObject *
1540 DictionaryDir(PyObject *self)
1541 {
1542     return ObjectDir(self, DictionaryAttrs);
1543 }
1544 
1545     static int
1546 DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
1547 {
1548     if (valObject == NULL)
1549     {
1550 	PyErr_SET_STRING(PyExc_AttributeError,
1551 		N_("cannot delete vim.Dictionary attributes"));
1552 	return -1;
1553     }
1554 
1555     if (strcmp(name, "locked") == 0)
1556     {
1557 	if (self->dict->dv_lock == VAR_FIXED)
1558 	{
1559 	    PyErr_SET_STRING(PyExc_TypeError,
1560 		    N_("cannot modify fixed dictionary"));
1561 	    return -1;
1562 	}
1563 	else
1564 	{
1565 	    int		istrue = PyObject_IsTrue(valObject);
1566 	    if (istrue == -1)
1567 		return -1;
1568 	    else if (istrue)
1569 		self->dict->dv_lock = VAR_LOCKED;
1570 	    else
1571 		self->dict->dv_lock = 0;
1572 	}
1573 	return 0;
1574     }
1575     else
1576     {
1577 	PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
1578 	return -1;
1579     }
1580 }
1581 
1582     static PyInt
1583 DictionaryLength(DictionaryObject *self)
1584 {
1585     return ((PyInt) (self->dict->dv_hashtab.ht_used));
1586 }
1587 
1588 #define DICT_FLAG_HAS_DEFAULT	0x01
1589 #define DICT_FLAG_POP		0x02
1590 #define DICT_FLAG_NONE_DEFAULT	0x04
1591 #define DICT_FLAG_RETURN_BOOL	0x08 /* Incompatible with DICT_FLAG_POP */
1592 #define DICT_FLAG_RETURN_PAIR	0x10
1593 
1594     static PyObject *
1595 _DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
1596 {
1597     PyObject	*keyObject;
1598     PyObject	*defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1599     PyObject	*ret;
1600     char_u	*key;
1601     dictitem_T	*di;
1602     dict_T	*dict = self->dict;
1603     hashitem_T	*hi;
1604     PyObject	*todecref;
1605 
1606     if (flags & DICT_FLAG_HAS_DEFAULT)
1607     {
1608 	if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1609 	    return NULL;
1610     }
1611     else
1612 	keyObject = args;
1613 
1614     if (flags & DICT_FLAG_RETURN_BOOL)
1615 	defObject = Py_False;
1616 
1617     if (!(key = StringToChars(keyObject, &todecref)))
1618 	return NULL;
1619 
1620     if (*key == NUL)
1621     {
1622 	RAISE_NO_EMPTY_KEYS;
1623 	Py_XDECREF(todecref);
1624 	return NULL;
1625     }
1626 
1627     hi = hash_find(&dict->dv_hashtab, key);
1628 
1629     Py_XDECREF(todecref);
1630 
1631     if (HASHITEM_EMPTY(hi))
1632     {
1633 	if (defObject)
1634 	{
1635 	    Py_INCREF(defObject);
1636 	    return defObject;
1637 	}
1638 	else
1639 	{
1640 	    PyErr_SetObject(PyExc_KeyError, keyObject);
1641 	    return NULL;
1642 	}
1643     }
1644     else if (flags & DICT_FLAG_RETURN_BOOL)
1645     {
1646 	ret = Py_True;
1647 	Py_INCREF(ret);
1648 	return ret;
1649     }
1650 
1651     di = dict_lookup(hi);
1652 
1653     if (!(ret = ConvertToPyObject(&di->di_tv)))
1654 	return NULL;
1655 
1656     if (flags & DICT_FLAG_POP)
1657     {
1658 	if (dict->dv_lock)
1659 	{
1660 	    RAISE_LOCKED_DICTIONARY;
1661 	    Py_DECREF(ret);
1662 	    return NULL;
1663 	}
1664 
1665 	hash_remove(&dict->dv_hashtab, hi);
1666 	dictitem_free(di);
1667     }
1668 
1669     return ret;
1670 }
1671 
1672     static PyObject *
1673 DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1674 {
1675     return _DictionaryItem(self, keyObject, 0);
1676 }
1677 
1678     static int
1679 DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1680 {
1681     PyObject	*rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1682     int		ret;
1683 
1684     if (rObj == NULL)
1685 	return -1;
1686 
1687     ret = (rObj == Py_True);
1688 
1689     Py_DECREF(rObj);
1690 
1691     return ret;
1692 }
1693 
1694 typedef struct
1695 {
1696     hashitem_T	*ht_array;
1697     long_u	ht_used;
1698     hashtab_T	*ht;
1699     hashitem_T	*hi;
1700     long_u	todo;
1701 } dictiterinfo_T;
1702 
1703     static PyObject *
1704 DictionaryIterNext(dictiterinfo_T **dii)
1705 {
1706     PyObject	*ret;
1707 
1708     if (!(*dii)->todo)
1709 	return NULL;
1710 
1711     if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1712 	    (*dii)->ht->ht_used != (*dii)->ht_used)
1713     {
1714 	PyErr_SET_STRING(PyExc_RuntimeError,
1715 		N_("hashtab changed during iteration"));
1716 	return NULL;
1717     }
1718 
1719     while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1720 	++((*dii)->hi);
1721 
1722     --((*dii)->todo);
1723 
1724     if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
1725 	return NULL;
1726 
1727     return ret;
1728 }
1729 
1730     static PyObject *
1731 DictionaryIter(DictionaryObject *self)
1732 {
1733     dictiterinfo_T	*dii;
1734     hashtab_T		*ht;
1735 
1736     if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1737     {
1738 	PyErr_NoMemory();
1739 	return NULL;
1740     }
1741 
1742     ht = &self->dict->dv_hashtab;
1743     dii->ht_array = ht->ht_array;
1744     dii->ht_used = ht->ht_used;
1745     dii->ht = ht;
1746     dii->hi = dii->ht_array;
1747     dii->todo = dii->ht_used;
1748 
1749     return IterNew(dii,
1750 	    (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1751 	    NULL, NULL);
1752 }
1753 
1754     static PyInt
1755 DictionaryAssItem(
1756 	DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
1757 {
1758     char_u	*key;
1759     typval_T	tv;
1760     dict_T	*dict = self->dict;
1761     dictitem_T	*di;
1762     PyObject	*todecref;
1763 
1764     if (dict->dv_lock)
1765     {
1766 	RAISE_LOCKED_DICTIONARY;
1767 	return -1;
1768     }
1769 
1770     if (!(key = StringToChars(keyObject, &todecref)))
1771 	return -1;
1772 
1773     if (*key == NUL)
1774     {
1775 	RAISE_NO_EMPTY_KEYS;
1776 	Py_XDECREF(todecref);
1777 	return -1;
1778     }
1779 
1780     di = dict_find(dict, key, -1);
1781 
1782     if (valObject == NULL)
1783     {
1784 	hashitem_T	*hi;
1785 
1786 	if (di == NULL)
1787 	{
1788 	    Py_XDECREF(todecref);
1789 	    PyErr_SetObject(PyExc_KeyError, keyObject);
1790 	    return -1;
1791 	}
1792 	hi = hash_find(&dict->dv_hashtab, di->di_key);
1793 	hash_remove(&dict->dv_hashtab, hi);
1794 	dictitem_free(di);
1795 	Py_XDECREF(todecref);
1796 	return 0;
1797     }
1798 
1799     if (ConvertFromPyObject(valObject, &tv) == -1)
1800     {
1801 	Py_XDECREF(todecref);
1802 	return -1;
1803     }
1804 
1805     if (di == NULL)
1806     {
1807 	if (!(di = dictitem_alloc(key)))
1808 	{
1809 	    Py_XDECREF(todecref);
1810 	    PyErr_NoMemory();
1811 	    return -1;
1812 	}
1813 	di->di_tv.v_lock = 0;
1814 	di->di_tv.v_type = VAR_UNKNOWN;
1815 
1816 	if (dict_add(dict, di) == FAIL)
1817 	{
1818 	    vim_free(di);
1819 	    dictitem_free(di);
1820 	    RAISE_KEY_ADD_FAIL(key);
1821 	    Py_XDECREF(todecref);
1822 	    return -1;
1823 	}
1824     }
1825     else
1826 	clear_tv(&di->di_tv);
1827 
1828     Py_XDECREF(todecref);
1829 
1830     copy_tv(&tv, &di->di_tv);
1831     clear_tv(&tv);
1832     return 0;
1833 }
1834 
1835 typedef PyObject *(*hi_to_py)(hashitem_T *);
1836 
1837     static PyObject *
1838 DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
1839 {
1840     dict_T	*dict = self->dict;
1841     long_u	todo = dict->dv_hashtab.ht_used;
1842     Py_ssize_t	i = 0;
1843     PyObject	*ret;
1844     hashitem_T	*hi;
1845     PyObject	*newObj;
1846 
1847     ret = PyList_New(todo);
1848     for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1849     {
1850 	if (!HASHITEM_EMPTY(hi))
1851 	{
1852 	    if (!(newObj = hiconvert(hi)))
1853 	    {
1854 		Py_DECREF(ret);
1855 		return NULL;
1856 	    }
1857 	    PyList_SET_ITEM(ret, i, newObj);
1858 	    --todo;
1859 	    ++i;
1860 	}
1861     }
1862     return ret;
1863 }
1864 
1865     static PyObject *
1866 dict_key(hashitem_T *hi)
1867 {
1868     return PyBytes_FromString((char *)(hi->hi_key));
1869 }
1870 
1871     static PyObject *
1872 DictionaryListKeys(DictionaryObject *self)
1873 {
1874     return DictionaryListObjects(self, dict_key);
1875 }
1876 
1877     static PyObject *
1878 dict_val(hashitem_T *hi)
1879 {
1880     dictitem_T	*di;
1881 
1882     di = dict_lookup(hi);
1883     return ConvertToPyObject(&di->di_tv);
1884 }
1885 
1886     static PyObject *
1887 DictionaryListValues(DictionaryObject *self)
1888 {
1889     return DictionaryListObjects(self, dict_val);
1890 }
1891 
1892     static PyObject *
1893 dict_item(hashitem_T *hi)
1894 {
1895     PyObject	*keyObject;
1896     PyObject	*valObject;
1897     PyObject	*ret;
1898 
1899     if (!(keyObject = dict_key(hi)))
1900 	return NULL;
1901 
1902     if (!(valObject = dict_val(hi)))
1903     {
1904 	Py_DECREF(keyObject);
1905 	return NULL;
1906     }
1907 
1908     ret = Py_BuildValue("(OO)", keyObject, valObject);
1909 
1910     Py_DECREF(keyObject);
1911     Py_DECREF(valObject);
1912 
1913     return ret;
1914 }
1915 
1916     static PyObject *
1917 DictionaryListItems(DictionaryObject *self)
1918 {
1919     return DictionaryListObjects(self, dict_item);
1920 }
1921 
1922     static PyObject *
1923 DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1924 {
1925     dict_T	*dict = self->dict;
1926 
1927     if (dict->dv_lock)
1928     {
1929 	RAISE_LOCKED_DICTIONARY;
1930 	return NULL;
1931     }
1932 
1933     if (kwargs)
1934     {
1935 	typval_T	tv;
1936 
1937 	if (ConvertFromPyMapping(kwargs, &tv) == -1)
1938 	    return NULL;
1939 
1940 	VimTryStart();
1941 	dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1942 	clear_tv(&tv);
1943 	if (VimTryEnd())
1944 	    return NULL;
1945     }
1946     else
1947     {
1948 	PyObject	*obj = NULL;
1949 
1950 	if (!PyArg_ParseTuple(args, "|O", &obj))
1951 	    return NULL;
1952 
1953 	if (obj == NULL)
1954 	{
1955 	    Py_INCREF(Py_None);
1956 	    return Py_None;
1957 	}
1958 
1959 	if (PyObject_HasAttrString(obj, "keys"))
1960 	    return DictionaryUpdate(self, NULL, obj);
1961 	else
1962 	{
1963 	    PyObject	*iterator;
1964 	    PyObject	*item;
1965 
1966 	    if (!(iterator = PyObject_GetIter(obj)))
1967 		return NULL;
1968 
1969 	    while ((item = PyIter_Next(iterator)))
1970 	    {
1971 		PyObject	*fast;
1972 		PyObject	*keyObject;
1973 		PyObject	*valObject;
1974 		PyObject	*todecref;
1975 		char_u		*key;
1976 		dictitem_T	*di;
1977 
1978 		if (!(fast = PySequence_Fast(item, "")))
1979 		{
1980 		    Py_DECREF(iterator);
1981 		    Py_DECREF(item);
1982 		    return NULL;
1983 		}
1984 
1985 		Py_DECREF(item);
1986 
1987 		if (PySequence_Fast_GET_SIZE(fast) != 2)
1988 		{
1989 		    Py_DECREF(iterator);
1990 		    Py_DECREF(fast);
1991 		    PyErr_FORMAT(PyExc_ValueError,
1992 			    N_("expected sequence element of size 2, "
1993 			    "but got sequence of size %d"),
1994 			    (int) PySequence_Fast_GET_SIZE(fast));
1995 		    return NULL;
1996 		}
1997 
1998 		keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1999 
2000 		if (!(key = StringToChars(keyObject, &todecref)))
2001 		{
2002 		    Py_DECREF(iterator);
2003 		    Py_DECREF(fast);
2004 		    return NULL;
2005 		}
2006 
2007 		di = dictitem_alloc(key);
2008 
2009 		Py_XDECREF(todecref);
2010 
2011 		if (di == NULL)
2012 		{
2013 		    Py_DECREF(fast);
2014 		    Py_DECREF(iterator);
2015 		    PyErr_NoMemory();
2016 		    return NULL;
2017 		}
2018 		di->di_tv.v_lock = 0;
2019 		di->di_tv.v_type = VAR_UNKNOWN;
2020 
2021 		valObject = PySequence_Fast_GET_ITEM(fast, 1);
2022 
2023 		if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2024 		{
2025 		    Py_DECREF(iterator);
2026 		    Py_DECREF(fast);
2027 		    dictitem_free(di);
2028 		    return NULL;
2029 		}
2030 
2031 		Py_DECREF(fast);
2032 
2033 		if (dict_add(dict, di) == FAIL)
2034 		{
2035 		    RAISE_KEY_ADD_FAIL(di->di_key);
2036 		    Py_DECREF(iterator);
2037 		    dictitem_free(di);
2038 		    return NULL;
2039 		}
2040 	    }
2041 
2042 	    Py_DECREF(iterator);
2043 
2044 	    /* Iterator may have finished due to an exception */
2045 	    if (PyErr_Occurred())
2046 		return NULL;
2047 	}
2048     }
2049     Py_INCREF(Py_None);
2050     return Py_None;
2051 }
2052 
2053     static PyObject *
2054 DictionaryGet(DictionaryObject *self, PyObject *args)
2055 {
2056     return _DictionaryItem(self, args,
2057 			    DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2058 }
2059 
2060     static PyObject *
2061 DictionaryPop(DictionaryObject *self, PyObject *args)
2062 {
2063     return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2064 }
2065 
2066     static PyObject *
2067 DictionaryPopItem(DictionaryObject *self)
2068 {
2069     hashitem_T	*hi;
2070     PyObject	*ret;
2071     PyObject	*valObject;
2072     dictitem_T	*di;
2073 
2074     if (self->dict->dv_hashtab.ht_used == 0)
2075     {
2076 	PyErr_SetNone(PyExc_KeyError);
2077 	return NULL;
2078     }
2079 
2080     hi = self->dict->dv_hashtab.ht_array;
2081     while (HASHITEM_EMPTY(hi))
2082 	++hi;
2083 
2084     di = dict_lookup(hi);
2085 
2086     if (!(valObject = ConvertToPyObject(&di->di_tv)))
2087 	return NULL;
2088 
2089     if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
2090     {
2091 	Py_DECREF(valObject);
2092 	return NULL;
2093     }
2094 
2095     hash_remove(&self->dict->dv_hashtab, hi);
2096     dictitem_free(di);
2097 
2098     return ret;
2099 }
2100 
2101     static PyObject *
2102 DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
2103 {
2104     return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2105 }
2106 
2107 static PySequenceMethods DictionaryAsSeq = {
2108     0,					/* sq_length */
2109     0,					/* sq_concat */
2110     0,					/* sq_repeat */
2111     0,					/* sq_item */
2112     0,					/* sq_slice */
2113     0,					/* sq_ass_item */
2114     0,					/* sq_ass_slice */
2115     (objobjproc) DictionaryContains,	/* sq_contains */
2116     0,					/* sq_inplace_concat */
2117     0,					/* sq_inplace_repeat */
2118 };
2119 
2120 static PyMappingMethods DictionaryAsMapping = {
2121     (lenfunc)       DictionaryLength,
2122     (binaryfunc)    DictionaryItem,
2123     (objobjargproc) DictionaryAssItem,
2124 };
2125 
2126 static struct PyMethodDef DictionaryMethods[] = {
2127     {"keys",	(PyCFunction)DictionaryListKeys,	METH_NOARGS,	""},
2128     {"values",	(PyCFunction)DictionaryListValues,	METH_NOARGS,	""},
2129     {"items",	(PyCFunction)DictionaryListItems,	METH_NOARGS,	""},
2130     {"update",	(PyCFunction)DictionaryUpdate,		METH_VARARGS|METH_KEYWORDS, ""},
2131     {"get",	(PyCFunction)DictionaryGet,		METH_VARARGS,	""},
2132     {"pop",	(PyCFunction)DictionaryPop,		METH_VARARGS,	""},
2133     {"popitem",	(PyCFunction)DictionaryPopItem,		METH_NOARGS,	""},
2134     {"has_key",	(PyCFunction)DictionaryHasKey,		METH_O,		""},
2135     {"__dir__",	(PyCFunction)DictionaryDir,		METH_NOARGS,	""},
2136     { NULL,	NULL,					0,		NULL}
2137 };
2138 
2139 static PyTypeObject ListType;
2140 
2141 typedef struct
2142 {
2143     PyObject_HEAD
2144     list_T	*list;
2145     pylinkedlist_T	ref;
2146 } ListObject;
2147 
2148 #define NEW_LIST(list) ListNew(&ListType, list)
2149 
2150     static PyObject *
2151 ListNew(PyTypeObject *subtype, list_T *list)
2152 {
2153     ListObject	*self;
2154 
2155     self = (ListObject *) subtype->tp_alloc(subtype, 0);
2156     if (self == NULL)
2157 	return NULL;
2158     self->list = list;
2159     ++list->lv_refcount;
2160 
2161     pyll_add((PyObject *)(self), &self->ref, &lastlist);
2162 
2163     return (PyObject *)(self);
2164 }
2165 
2166     static list_T *
2167 py_list_alloc(void)
2168 {
2169     list_T	*ret;
2170 
2171     if (!(ret = list_alloc()))
2172     {
2173 	PyErr_NoMemory();
2174 	return NULL;
2175     }
2176     ++ret->lv_refcount;
2177 
2178     return ret;
2179 }
2180 
2181     static int
2182 list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2183 {
2184     PyObject	*iterator;
2185     PyObject	*item;
2186     listitem_T	*li;
2187 
2188     if (!(iterator = PyObject_GetIter(obj)))
2189 	return -1;
2190 
2191     while ((item = PyIter_Next(iterator)))
2192     {
2193 	if (!(li = listitem_alloc()))
2194 	{
2195 	    PyErr_NoMemory();
2196 	    Py_DECREF(item);
2197 	    Py_DECREF(iterator);
2198 	    return -1;
2199 	}
2200 	li->li_tv.v_lock = 0;
2201 	li->li_tv.v_type = VAR_UNKNOWN;
2202 
2203 	if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2204 	{
2205 	    Py_DECREF(item);
2206 	    Py_DECREF(iterator);
2207 	    listitem_free(li);
2208 	    return -1;
2209 	}
2210 
2211 	Py_DECREF(item);
2212 
2213 	list_append(l, li);
2214     }
2215 
2216     Py_DECREF(iterator);
2217 
2218     /* Iterator may have finished due to an exception */
2219     if (PyErr_Occurred())
2220 	return -1;
2221 
2222     return 0;
2223 }
2224 
2225     static PyObject *
2226 ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2227 {
2228     list_T	*list;
2229     PyObject	*obj = NULL;
2230 
2231     if (kwargs)
2232     {
2233 	PyErr_SET_STRING(PyExc_TypeError,
2234 		N_("list constructor does not accept keyword arguments"));
2235 	return NULL;
2236     }
2237 
2238     if (!PyArg_ParseTuple(args, "|O", &obj))
2239 	return NULL;
2240 
2241     if (!(list = py_list_alloc()))
2242 	return NULL;
2243 
2244     if (obj)
2245     {
2246 	PyObject	*lookup_dict;
2247 
2248 	if (!(lookup_dict = PyDict_New()))
2249 	{
2250 	    list_unref(list);
2251 	    return NULL;
2252 	}
2253 
2254 	if (list_py_concat(list, obj, lookup_dict) == -1)
2255 	{
2256 	    Py_DECREF(lookup_dict);
2257 	    list_unref(list);
2258 	    return NULL;
2259 	}
2260 
2261 	Py_DECREF(lookup_dict);
2262     }
2263 
2264     return ListNew(subtype, list);
2265 }
2266 
2267     static void
2268 ListDestructor(ListObject *self)
2269 {
2270     pyll_remove(&self->ref, &lastlist);
2271     list_unref(self->list);
2272 
2273     DESTRUCTOR_FINISH(self);
2274 }
2275 
2276     static PyInt
2277 ListLength(ListObject *self)
2278 {
2279     return ((PyInt) (self->list->lv_len));
2280 }
2281 
2282     static PyObject *
2283 ListIndex(ListObject *self, Py_ssize_t index)
2284 {
2285     listitem_T	*li;
2286 
2287     if (index >= ListLength(self))
2288     {
2289 	PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2290 	return NULL;
2291     }
2292     li = list_find(self->list, (long) index);
2293     if (li == NULL)
2294     {
2295 	/* No more suitable format specifications in python-2.3 */
2296 	PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
2297 		(int) index);
2298 	return NULL;
2299     }
2300     return ConvertToPyObject(&li->li_tv);
2301 }
2302 
2303     static PyObject *
2304 ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2305 	  Py_ssize_t slicelen)
2306 {
2307     PyInt	i;
2308     PyObject	*list;
2309 
2310     if (step == 0)
2311     {
2312 	PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2313 	return NULL;
2314     }
2315 
2316     list = PyList_New(slicelen);
2317     if (list == NULL)
2318 	return NULL;
2319 
2320     for (i = 0; i < slicelen; ++i)
2321     {
2322 	PyObject	*item;
2323 
2324 	item = ListIndex(self, first + i*step);
2325 	if (item == NULL)
2326 	{
2327 	    Py_DECREF(list);
2328 	    return NULL;
2329 	}
2330 
2331 	PyList_SET_ITEM(list, i, item);
2332     }
2333 
2334     return list;
2335 }
2336 
2337     static PyObject *
2338 ListItem(ListObject *self, PyObject* idx)
2339 {
2340 #if PY_MAJOR_VERSION < 3
2341     if (PyInt_Check(idx))
2342     {
2343 	long _idx = PyInt_AsLong(idx);
2344 	return ListIndex(self, _idx);
2345     }
2346     else
2347 #endif
2348     if (PyLong_Check(idx))
2349     {
2350 	long _idx = PyLong_AsLong(idx);
2351 	return ListIndex(self, _idx);
2352     }
2353     else if (PySlice_Check(idx))
2354     {
2355 	Py_ssize_t start, stop, step, slicelen;
2356 
2357 	if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
2358 				 &start, &stop, &step, &slicelen) < 0)
2359 	    return NULL;
2360 	return ListSlice(self, start, step, slicelen);
2361     }
2362     else
2363     {
2364 	RAISE_INVALID_INDEX_TYPE(idx);
2365 	return NULL;
2366     }
2367 }
2368 
2369     static void
2370 list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2371 	list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2372 {
2373     while (numreplaced--)
2374     {
2375 	list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2376 	listitem_remove(l, lis[slicelen + numreplaced]);
2377     }
2378     while (numadded--)
2379     {
2380 	listitem_T	*next;
2381 
2382 	next = lastaddedli->li_prev;
2383 	listitem_remove(l, lastaddedli);
2384 	lastaddedli = next;
2385     }
2386 }
2387 
2388     static int
2389 ListAssSlice(ListObject *self, Py_ssize_t first,
2390 	     Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2391 {
2392     PyObject	*iterator;
2393     PyObject	*item;
2394     listitem_T	*li;
2395     listitem_T	*lastaddedli = NULL;
2396     listitem_T	*next;
2397     typval_T	v;
2398     list_T	*l = self->list;
2399     PyInt	i;
2400     PyInt	j;
2401     PyInt	numreplaced = 0;
2402     PyInt	numadded = 0;
2403     PyInt	size;
2404     listitem_T	**lis = NULL;
2405 
2406     size = ListLength(self);
2407 
2408     if (l->lv_lock)
2409     {
2410 	RAISE_LOCKED_LIST;
2411 	return -1;
2412     }
2413 
2414     if (step == 0)
2415     {
2416 	PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2417 	return -1;
2418     }
2419 
2420     if (step != 1 && slicelen == 0)
2421     {
2422 	/* Nothing to do. Only error out if obj has some items. */
2423 	int		ret = 0;
2424 
2425 	if (obj == NULL)
2426 	    return 0;
2427 
2428 	if (!(iterator = PyObject_GetIter(obj)))
2429 	    return -1;
2430 
2431 	if ((item = PyIter_Next(iterator)))
2432 	{
2433 	    PyErr_FORMAT(PyExc_ValueError,
2434 		    N_("attempt to assign sequence of size greater than %d "
2435 			"to extended slice"), 0);
2436 	    Py_DECREF(item);
2437 	    ret = -1;
2438 	}
2439 	Py_DECREF(iterator);
2440 	return ret;
2441     }
2442 
2443     if (obj != NULL)
2444 	/* XXX May allocate zero bytes. */
2445 	if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2446 	{
2447 	    PyErr_NoMemory();
2448 	    return -1;
2449 	}
2450 
2451     if (first == size)
2452 	li = NULL;
2453     else
2454     {
2455 	li = list_find(l, (long) first);
2456 	if (li == NULL)
2457 	{
2458 	    PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2459 		    (int)first);
2460 	    if (obj != NULL)
2461 		PyMem_Free(lis);
2462 	    return -1;
2463 	}
2464 	i = slicelen;
2465 	while (i-- && li != NULL)
2466 	{
2467 	    j = step;
2468 	    next = li;
2469 	    if (step > 0)
2470 		while (next != NULL && ((next = next->li_next) != NULL) && --j);
2471 	    else
2472 		while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2473 
2474 	    if (obj == NULL)
2475 		listitem_remove(l, li);
2476 	    else
2477 		lis[slicelen - i - 1] = li;
2478 
2479 	    li = next;
2480 	}
2481 	if (li == NULL && i != -1)
2482 	{
2483 	    PyErr_SET_VIM(N_("internal error: not enough list items"));
2484 	    if (obj != NULL)
2485 		PyMem_Free(lis);
2486 	    return -1;
2487 	}
2488     }
2489 
2490     if (obj == NULL)
2491 	return 0;
2492 
2493     if (!(iterator = PyObject_GetIter(obj)))
2494     {
2495 	PyMem_Free(lis);
2496 	return -1;
2497     }
2498 
2499     i = 0;
2500     while ((item = PyIter_Next(iterator)))
2501     {
2502 	if (ConvertFromPyObject(item, &v) == -1)
2503 	{
2504 	    Py_DECREF(iterator);
2505 	    Py_DECREF(item);
2506 	    PyMem_Free(lis);
2507 	    return -1;
2508 	}
2509 	Py_DECREF(item);
2510 	if (list_insert_tv(l, &v, numreplaced < slicelen
2511 				    ? lis[numreplaced]
2512 				    : li) == FAIL)
2513 	{
2514 	    clear_tv(&v);
2515 	    PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2516 	    list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2517 	    PyMem_Free(lis);
2518 	    return -1;
2519 	}
2520 	if (numreplaced < slicelen)
2521 	{
2522 	    lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
2523 	    vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
2524 	    numreplaced++;
2525 	}
2526 	else
2527 	{
2528 	    if (li)
2529 		lastaddedli = li->li_prev;
2530 	    else
2531 		lastaddedli = l->lv_last;
2532 	    numadded++;
2533 	}
2534 	clear_tv(&v);
2535 	if (step != 1 && i >= slicelen)
2536 	{
2537 	    Py_DECREF(iterator);
2538 	    PyErr_FORMAT(PyExc_ValueError,
2539 		    N_("attempt to assign sequence of size greater than %d "
2540 			"to extended slice"), (int) slicelen);
2541 	    list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2542 	    PyMem_Free(lis);
2543 	    return -1;
2544 	}
2545 	++i;
2546     }
2547     Py_DECREF(iterator);
2548 
2549     if (step != 1 && i != slicelen)
2550     {
2551 	PyErr_FORMAT2(PyExc_ValueError,
2552 		N_("attempt to assign sequence of size %d to extended slice "
2553 		    "of size %d"), (int) i, (int) slicelen);
2554 	list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2555 	PyMem_Free(lis);
2556 	return -1;
2557     }
2558 
2559     if (PyErr_Occurred())
2560     {
2561 	list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2562 	PyMem_Free(lis);
2563 	return -1;
2564     }
2565 
2566     for (i = 0; i < numreplaced; i++)
2567 	listitem_free(lis[i]);
2568     if (step == 1)
2569 	for (i = numreplaced; i < slicelen; i++)
2570 	    listitem_remove(l, lis[i]);
2571 
2572     PyMem_Free(lis);
2573 
2574     return 0;
2575 }
2576 
2577     static int
2578 ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2579 {
2580     typval_T	tv;
2581     list_T	*l = self->list;
2582     listitem_T	*li;
2583     Py_ssize_t	length = ListLength(self);
2584 
2585     if (l->lv_lock)
2586     {
2587 	RAISE_LOCKED_LIST;
2588 	return -1;
2589     }
2590     if (index > length || (index == length && obj == NULL))
2591     {
2592 	PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2593 	return -1;
2594     }
2595 
2596     if (obj == NULL)
2597     {
2598 	li = list_find(l, (long) index);
2599 	vimlist_remove(l, li, li);
2600 	clear_tv(&li->li_tv);
2601 	vim_free(li);
2602 	return 0;
2603     }
2604 
2605     if (ConvertFromPyObject(obj, &tv) == -1)
2606 	return -1;
2607 
2608     if (index == length)
2609     {
2610 	if (list_append_tv(l, &tv) == FAIL)
2611 	{
2612 	    clear_tv(&tv);
2613 	    PyErr_SET_VIM(N_("failed to add item to list"));
2614 	    return -1;
2615 	}
2616     }
2617     else
2618     {
2619 	li = list_find(l, (long) index);
2620 	clear_tv(&li->li_tv);
2621 	copy_tv(&tv, &li->li_tv);
2622 	clear_tv(&tv);
2623     }
2624     return 0;
2625 }
2626 
2627     static Py_ssize_t
2628 ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2629 {
2630 #if PY_MAJOR_VERSION < 3
2631     if (PyInt_Check(idx))
2632     {
2633 	long _idx = PyInt_AsLong(idx);
2634 	return ListAssIndex(self, _idx, obj);
2635     }
2636     else
2637 #endif
2638     if (PyLong_Check(idx))
2639     {
2640 	long _idx = PyLong_AsLong(idx);
2641 	return ListAssIndex(self, _idx, obj);
2642     }
2643     else if (PySlice_Check(idx))
2644     {
2645 	Py_ssize_t start, stop, step, slicelen;
2646 
2647 	if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
2648 				 &start, &stop, &step, &slicelen) < 0)
2649 	    return -1;
2650 	return ListAssSlice(self, start, step, slicelen,
2651 		obj);
2652     }
2653     else
2654     {
2655 	RAISE_INVALID_INDEX_TYPE(idx);
2656 	return -1;
2657     }
2658 }
2659 
2660     static PyObject *
2661 ListConcatInPlace(ListObject *self, PyObject *obj)
2662 {
2663     list_T	*l = self->list;
2664     PyObject	*lookup_dict;
2665 
2666     if (l->lv_lock)
2667     {
2668 	RAISE_LOCKED_LIST;
2669 	return NULL;
2670     }
2671 
2672     if (!(lookup_dict = PyDict_New()))
2673 	return NULL;
2674 
2675     if (list_py_concat(l, obj, lookup_dict) == -1)
2676     {
2677 	Py_DECREF(lookup_dict);
2678 	return NULL;
2679     }
2680     Py_DECREF(lookup_dict);
2681 
2682     Py_INCREF(self);
2683     return (PyObject *)(self);
2684 }
2685 
2686 typedef struct
2687 {
2688     listwatch_T	lw;
2689     list_T	*list;
2690 } listiterinfo_T;
2691 
2692     static void
2693 ListIterDestruct(listiterinfo_T *lii)
2694 {
2695     list_rem_watch(lii->list, &lii->lw);
2696     PyMem_Free(lii);
2697 }
2698 
2699     static PyObject *
2700 ListIterNext(listiterinfo_T **lii)
2701 {
2702     PyObject	*ret;
2703 
2704     if (!((*lii)->lw.lw_item))
2705 	return NULL;
2706 
2707     if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
2708 	return NULL;
2709 
2710     (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2711 
2712     return ret;
2713 }
2714 
2715     static PyObject *
2716 ListIter(ListObject *self)
2717 {
2718     listiterinfo_T	*lii;
2719     list_T	*l = self->list;
2720 
2721     if (!(lii = PyMem_New(listiterinfo_T, 1)))
2722     {
2723 	PyErr_NoMemory();
2724 	return NULL;
2725     }
2726 
2727     list_add_watch(l, &lii->lw);
2728     lii->lw.lw_item = l->lv_first;
2729     lii->list = l;
2730 
2731     return IterNew(lii,
2732 	    (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2733 	    NULL, NULL);
2734 }
2735 
2736 static char *ListAttrs[] = {
2737     "locked",
2738     NULL
2739 };
2740 
2741     static PyObject *
2742 ListDir(PyObject *self)
2743 {
2744     return ObjectDir(self, ListAttrs);
2745 }
2746 
2747     static int
2748 ListSetattr(ListObject *self, char *name, PyObject *valObject)
2749 {
2750     if (valObject == NULL)
2751     {
2752 	PyErr_SET_STRING(PyExc_AttributeError,
2753 		N_("cannot delete vim.List attributes"));
2754 	return -1;
2755     }
2756 
2757     if (strcmp(name, "locked") == 0)
2758     {
2759 	if (self->list->lv_lock == VAR_FIXED)
2760 	{
2761 	    PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
2762 	    return -1;
2763 	}
2764 	else
2765 	{
2766 	    int		istrue = PyObject_IsTrue(valObject);
2767 	    if (istrue == -1)
2768 		return -1;
2769 	    else if (istrue)
2770 		self->list->lv_lock = VAR_LOCKED;
2771 	    else
2772 		self->list->lv_lock = 0;
2773 	}
2774 	return 0;
2775     }
2776     else
2777     {
2778 	PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
2779 	return -1;
2780     }
2781 }
2782 
2783 static PySequenceMethods ListAsSeq = {
2784     (lenfunc)		ListLength,	 /* sq_length,	  len(x)   */
2785     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
2786     0,					 /* RangeRepeat, sq_repeat,  x*n   */
2787     (PyIntArgFunc)	ListIndex,	 /* sq_item,	  x[i]	   */
2788     0,					 /* was_sq_slice,     x[i:j]   */
2789     (PyIntObjArgProc)	ListAssIndex,	 /* sq_as_item,  x[i]=v   */
2790     0,					 /* was_sq_ass_slice, x[i:j]=v */
2791     0,					 /* sq_contains */
2792     (binaryfunc)	ListConcatInPlace,/* sq_inplace_concat */
2793     0,					 /* sq_inplace_repeat */
2794 };
2795 
2796 static PyMappingMethods ListAsMapping = {
2797     /* mp_length	*/ (lenfunc) ListLength,
2798     /* mp_subscript     */ (binaryfunc) ListItem,
2799     /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2800 };
2801 
2802 static struct PyMethodDef ListMethods[] = {
2803     {"extend",	(PyCFunction)ListConcatInPlace,	METH_O,		""},
2804     {"__dir__",	(PyCFunction)ListDir,		METH_NOARGS,	""},
2805     { NULL,	NULL,				0,		NULL}
2806 };
2807 
2808 typedef struct
2809 {
2810     PyObject_HEAD
2811     char_u	*name;
2812 } FunctionObject;
2813 
2814 static PyTypeObject FunctionType;
2815 
2816 #define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2817 
2818     static PyObject *
2819 FunctionNew(PyTypeObject *subtype, char_u *name)
2820 {
2821     FunctionObject	*self;
2822 
2823     self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2824 
2825     if (self == NULL)
2826 	return NULL;
2827 
2828     if (isdigit(*name))
2829     {
2830 	if (!translated_function_exists(name))
2831 	{
2832 	    PyErr_FORMAT(PyExc_ValueError,
2833 		    N_("unnamed function %s does not exist"), name);
2834 	    return NULL;
2835 	}
2836 	self->name = vim_strsave(name);
2837 	func_ref(self->name);
2838     }
2839     else
2840 	if ((self->name = get_expanded_name(name,
2841 				    vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2842 		== NULL)
2843 	{
2844 	    PyErr_FORMAT(PyExc_ValueError,
2845 		    N_("function %s does not exist"), name);
2846 	    return NULL;
2847 	}
2848 
2849     return (PyObject *)(self);
2850 }
2851 
2852     static PyObject *
2853 FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2854 {
2855     PyObject	*self;
2856     char_u	*name;
2857 
2858     if (kwargs)
2859     {
2860 	PyErr_SET_STRING(PyExc_TypeError,
2861 		N_("function constructor does not accept keyword arguments"));
2862 	return NULL;
2863     }
2864 
2865     if (!PyArg_ParseTuple(args, "et", "ascii", &name))
2866 	return NULL;
2867 
2868     self = FunctionNew(subtype, name);
2869 
2870     PyMem_Free(name);
2871 
2872     return self;
2873 }
2874 
2875     static void
2876 FunctionDestructor(FunctionObject *self)
2877 {
2878     func_unref(self->name);
2879     vim_free(self->name);
2880 
2881     DESTRUCTOR_FINISH(self);
2882 }
2883 
2884 static char *FunctionAttrs[] = {
2885     "softspace",
2886     NULL
2887 };
2888 
2889     static PyObject *
2890 FunctionDir(PyObject *self)
2891 {
2892     return ObjectDir(self, FunctionAttrs);
2893 }
2894 
2895     static PyObject *
2896 FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
2897 {
2898     char_u	*name = self->name;
2899     typval_T	args;
2900     typval_T	selfdicttv;
2901     typval_T	rettv;
2902     dict_T	*selfdict = NULL;
2903     PyObject	*selfdictObject;
2904     PyObject	*ret;
2905     int		error;
2906 
2907     if (ConvertFromPyObject(argsObject, &args) == -1)
2908 	return NULL;
2909 
2910     if (kwargs != NULL)
2911     {
2912 	selfdictObject = PyDict_GetItemString(kwargs, "self");
2913 	if (selfdictObject != NULL)
2914 	{
2915 	    if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2916 	    {
2917 		clear_tv(&args);
2918 		return NULL;
2919 	    }
2920 	    selfdict = selfdicttv.vval.v_dict;
2921 	}
2922     }
2923 
2924     Py_BEGIN_ALLOW_THREADS
2925     Python_Lock_Vim();
2926 
2927     VimTryStart();
2928     error = func_call(name, &args, selfdict, &rettv);
2929 
2930     Python_Release_Vim();
2931     Py_END_ALLOW_THREADS
2932 
2933     if (VimTryEnd())
2934 	ret = NULL;
2935     else if (error != OK)
2936     {
2937 	ret = NULL;
2938 	PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
2939     }
2940     else
2941 	ret = ConvertToPyObject(&rettv);
2942 
2943     clear_tv(&args);
2944     clear_tv(&rettv);
2945     if (selfdict != NULL)
2946 	clear_tv(&selfdicttv);
2947 
2948     return ret;
2949 }
2950 
2951     static PyObject *
2952 FunctionRepr(FunctionObject *self)
2953 {
2954 #ifdef Py_TRACE_REFS
2955     /* For unknown reason self->name may be NULL after calling
2956      * Finalize */
2957     return PyString_FromFormat("<vim.Function '%s'>",
2958 	    (self->name == NULL ? "<NULL>" : (char *)self->name));
2959 #else
2960     return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
2961 #endif
2962 }
2963 
2964 static struct PyMethodDef FunctionMethods[] = {
2965     {"__dir__",	(PyCFunction)FunctionDir,   METH_NOARGS,		""},
2966     { NULL,	NULL,			0,				NULL}
2967 };
2968 
2969 /*
2970  * Options object
2971  */
2972 
2973 static PyTypeObject OptionsType;
2974 
2975 typedef int (*checkfun)(void *);
2976 
2977 typedef struct
2978 {
2979     PyObject_HEAD
2980     int		opt_type;
2981     void	*from;
2982     checkfun	Check;
2983     PyObject	*fromObj;
2984 } OptionsObject;
2985 
2986     static int
2987 dummy_check(void *arg UNUSED)
2988 {
2989     return 0;
2990 }
2991 
2992     static PyObject *
2993 OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2994 {
2995     OptionsObject	*self;
2996 
2997     self = PyObject_GC_New(OptionsObject, &OptionsType);
2998     if (self == NULL)
2999 	return NULL;
3000 
3001     self->opt_type = opt_type;
3002     self->from = from;
3003     self->Check = Check;
3004     self->fromObj = fromObj;
3005     if (fromObj)
3006 	Py_INCREF(fromObj);
3007 
3008     return (PyObject *)(self);
3009 }
3010 
3011     static void
3012 OptionsDestructor(OptionsObject *self)
3013 {
3014     PyObject_GC_UnTrack((void *)(self));
3015     Py_XDECREF(self->fromObj);
3016     PyObject_GC_Del((void *)(self));
3017 }
3018 
3019     static int
3020 OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
3021 {
3022     Py_VISIT(self->fromObj);
3023     return 0;
3024 }
3025 
3026     static int
3027 OptionsClear(OptionsObject *self)
3028 {
3029     Py_CLEAR(self->fromObj);
3030     return 0;
3031 }
3032 
3033     static PyObject *
3034 OptionsItem(OptionsObject *self, PyObject *keyObject)
3035 {
3036     char_u	*key;
3037     int		flags;
3038     long	numval;
3039     char_u	*stringval;
3040     PyObject	*todecref;
3041 
3042     if (self->Check(self->from))
3043 	return NULL;
3044 
3045     if (!(key = StringToChars(keyObject, &todecref)))
3046 	return NULL;
3047 
3048     if (*key == NUL)
3049     {
3050 	RAISE_NO_EMPTY_KEYS;
3051 	Py_XDECREF(todecref);
3052 	return NULL;
3053     }
3054 
3055     flags = get_option_value_strict(key, &numval, &stringval,
3056 				    self->opt_type, self->from);
3057 
3058     Py_XDECREF(todecref);
3059 
3060     if (flags == 0)
3061     {
3062 	PyErr_SetObject(PyExc_KeyError, keyObject);
3063 	return NULL;
3064     }
3065 
3066     if (flags & SOPT_UNSET)
3067     {
3068 	Py_INCREF(Py_None);
3069 	return Py_None;
3070     }
3071     else if (flags & SOPT_BOOL)
3072     {
3073 	PyObject	*ret;
3074 	ret = numval ? Py_True : Py_False;
3075 	Py_INCREF(ret);
3076 	return ret;
3077     }
3078     else if (flags & SOPT_NUM)
3079 	return PyInt_FromLong(numval);
3080     else if (flags & SOPT_STRING)
3081     {
3082 	if (stringval)
3083 	{
3084 	    PyObject	*ret = PyBytes_FromString((char *)stringval);
3085 	    vim_free(stringval);
3086 	    return ret;
3087 	}
3088 	else
3089 	{
3090 	    PyErr_SET_STRING(PyExc_RuntimeError,
3091 		    N_("unable to get option value"));
3092 	    return NULL;
3093 	}
3094     }
3095     else
3096     {
3097 	PyErr_SET_VIM(N_("internal error: unknown option type"));
3098 	return NULL;
3099     }
3100 }
3101 
3102     static int
3103 OptionsContains(OptionsObject *self, PyObject *keyObject)
3104 {
3105     char_u	*key;
3106     PyObject	*todecref;
3107 
3108     if (!(key = StringToChars(keyObject, &todecref)))
3109 	return -1;
3110 
3111     if (*key == NUL)
3112     {
3113 	Py_XDECREF(todecref);
3114 	return 0;
3115     }
3116 
3117     if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3118     {
3119 	Py_XDECREF(todecref);
3120 	return 1;
3121     }
3122     else
3123     {
3124 	Py_XDECREF(todecref);
3125 	return 0;
3126     }
3127 }
3128 
3129 typedef struct
3130 {
3131     void	*lastoption;
3132     int		opt_type;
3133 } optiterinfo_T;
3134 
3135     static PyObject *
3136 OptionsIterNext(optiterinfo_T **oii)
3137 {
3138     char_u	*name;
3139 
3140     if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3141 	return PyString_FromString((char *)name);
3142 
3143     return NULL;
3144 }
3145 
3146     static PyObject *
3147 OptionsIter(OptionsObject *self)
3148 {
3149     optiterinfo_T	*oii;
3150 
3151     if (!(oii = PyMem_New(optiterinfo_T, 1)))
3152     {
3153 	PyErr_NoMemory();
3154 	return NULL;
3155     }
3156 
3157     oii->opt_type = self->opt_type;
3158     oii->lastoption = NULL;
3159 
3160     return IterNew(oii,
3161 	    (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3162 	    NULL, NULL);
3163 }
3164 
3165     static int
3166 set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
3167 {
3168     char_u	*errmsg;
3169 
3170     if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3171     {
3172 	if (VimTryEnd())
3173 	    return FAIL;
3174 	PyErr_SetVim((char *)errmsg);
3175 	return FAIL;
3176     }
3177     return OK;
3178 }
3179 
3180     static int
3181 set_option_value_for(
3182 	char_u *key,
3183 	int numval,
3184 	char_u *stringval,
3185 	int opt_flags,
3186 	int opt_type,
3187 	void *from)
3188 {
3189     win_T	*save_curwin = NULL;
3190     tabpage_T	*save_curtab = NULL;
3191     buf_T	*save_curbuf = NULL;
3192     int		set_ret = 0;
3193 
3194     VimTryStart();
3195     switch (opt_type)
3196     {
3197 	case SREQ_WIN:
3198 	    if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
3199 			      win_find_tabpage((win_T *)from), FALSE) == FAIL)
3200 	    {
3201 		restore_win(save_curwin, save_curtab, TRUE);
3202 		if (VimTryEnd())
3203 		    return -1;
3204 		PyErr_SET_VIM(N_("problem while switching windows"));
3205 		return -1;
3206 	    }
3207 	    set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3208 	    restore_win(save_curwin, save_curtab, TRUE);
3209 	    break;
3210 	case SREQ_BUF:
3211 	    switch_buffer(&save_curbuf, (buf_T *)from);
3212 	    set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3213 	    restore_buffer(save_curbuf);
3214 	    break;
3215 	case SREQ_GLOBAL:
3216 	    set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3217 	    break;
3218     }
3219     if (set_ret == FAIL)
3220 	return -1;
3221     return VimTryEnd();
3222 }
3223 
3224     static int
3225 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
3226 {
3227     char_u	*key;
3228     int		flags;
3229     int		opt_flags;
3230     int		ret = 0;
3231     PyObject	*todecref;
3232 
3233     if (self->Check(self->from))
3234 	return -1;
3235 
3236     if (!(key = StringToChars(keyObject, &todecref)))
3237 	return -1;
3238 
3239     if (*key == NUL)
3240     {
3241 	RAISE_NO_EMPTY_KEYS;
3242 	Py_XDECREF(todecref);
3243 	return -1;
3244     }
3245 
3246     flags = get_option_value_strict(key, NULL, NULL,
3247 				    self->opt_type, self->from);
3248 
3249     if (flags == 0)
3250     {
3251 	PyErr_SetObject(PyExc_KeyError, keyObject);
3252 	Py_XDECREF(todecref);
3253 	return -1;
3254     }
3255 
3256     if (valObject == NULL)
3257     {
3258 	if (self->opt_type == SREQ_GLOBAL)
3259 	{
3260 	    PyErr_FORMAT(PyExc_ValueError,
3261 		    N_("unable to unset global option %s"), key);
3262 	    Py_XDECREF(todecref);
3263 	    return -1;
3264 	}
3265 	else if (!(flags & SOPT_GLOBAL))
3266 	{
3267 	    PyErr_FORMAT(PyExc_ValueError,
3268 		    N_("unable to unset option %s "
3269 		       "which does not have global value"), key);
3270 	    Py_XDECREF(todecref);
3271 	    return -1;
3272 	}
3273 	else
3274 	{
3275 	    unset_global_local_option(key, self->from);
3276 	    Py_XDECREF(todecref);
3277 	    return 0;
3278 	}
3279     }
3280 
3281     opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
3282 
3283     if (flags & SOPT_BOOL)
3284     {
3285 	int	istrue = PyObject_IsTrue(valObject);
3286 
3287 	if (istrue == -1)
3288 	    ret = -1;
3289 	else
3290 	    ret = set_option_value_for(key, istrue, NULL,
3291 				    opt_flags, self->opt_type, self->from);
3292     }
3293     else if (flags & SOPT_NUM)
3294     {
3295 	long	val;
3296 
3297 	if (NumberToLong(valObject, &val, NUMBER_INT))
3298 	{
3299 	    Py_XDECREF(todecref);
3300 	    return -1;
3301 	}
3302 
3303 	ret = set_option_value_for(key, (int) val, NULL, opt_flags,
3304 				self->opt_type, self->from);
3305     }
3306     else
3307     {
3308 	char_u		*val;
3309 	PyObject	*todecref2;
3310 
3311 	if ((val = StringToChars(valObject, &todecref2)))
3312 	{
3313 	    ret = set_option_value_for(key, 0, val, opt_flags,
3314 				    self->opt_type, self->from);
3315 	    Py_XDECREF(todecref2);
3316 	}
3317 	else
3318 	    ret = -1;
3319     }
3320 
3321     Py_XDECREF(todecref);
3322 
3323     return ret;
3324 }
3325 
3326 static PySequenceMethods OptionsAsSeq = {
3327     0,					/* sq_length */
3328     0,					/* sq_concat */
3329     0,					/* sq_repeat */
3330     0,					/* sq_item */
3331     0,					/* sq_slice */
3332     0,					/* sq_ass_item */
3333     0,					/* sq_ass_slice */
3334     (objobjproc) OptionsContains,	/* sq_contains */
3335     0,					/* sq_inplace_concat */
3336     0,					/* sq_inplace_repeat */
3337 };
3338 
3339 static PyMappingMethods OptionsAsMapping = {
3340     (lenfunc)       NULL,
3341     (binaryfunc)    OptionsItem,
3342     (objobjargproc) OptionsAssItem,
3343 };
3344 
3345 /* Tabpage object
3346  */
3347 
3348 typedef struct
3349 {
3350     PyObject_HEAD
3351     tabpage_T	*tab;
3352 } TabPageObject;
3353 
3354 static PyObject *WinListNew(TabPageObject *tabObject);
3355 
3356 static PyTypeObject TabPageType;
3357 
3358     static int
3359 CheckTabPage(TabPageObject *self)
3360 {
3361     if (self->tab == INVALID_TABPAGE_VALUE)
3362     {
3363 	PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
3364 	return -1;
3365     }
3366 
3367     return 0;
3368 }
3369 
3370     static PyObject *
3371 TabPageNew(tabpage_T *tab)
3372 {
3373     TabPageObject *self;
3374 
3375     if (TAB_PYTHON_REF(tab))
3376     {
3377 	self = TAB_PYTHON_REF(tab);
3378 	Py_INCREF(self);
3379     }
3380     else
3381     {
3382 	self = PyObject_NEW(TabPageObject, &TabPageType);
3383 	if (self == NULL)
3384 	    return NULL;
3385 	self->tab = tab;
3386 	TAB_PYTHON_REF(tab) = self;
3387     }
3388 
3389     return (PyObject *)(self);
3390 }
3391 
3392     static void
3393 TabPageDestructor(TabPageObject *self)
3394 {
3395     if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3396 	TAB_PYTHON_REF(self->tab) = NULL;
3397 
3398     DESTRUCTOR_FINISH(self);
3399 }
3400 
3401 static char *TabPageAttrs[] = {
3402     "windows", "number", "vars", "window", "valid",
3403     NULL
3404 };
3405 
3406     static PyObject *
3407 TabPageDir(PyObject *self)
3408 {
3409     return ObjectDir(self, TabPageAttrs);
3410 }
3411 
3412     static PyObject *
3413 TabPageAttrValid(TabPageObject *self, char *name)
3414 {
3415     PyObject	*ret;
3416 
3417     if (strcmp(name, "valid") != 0)
3418 	return NULL;
3419 
3420     ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3421     Py_INCREF(ret);
3422     return ret;
3423 }
3424 
3425     static PyObject *
3426 TabPageAttr(TabPageObject *self, char *name)
3427 {
3428     if (strcmp(name, "windows") == 0)
3429 	return WinListNew(self);
3430     else if (strcmp(name, "number") == 0)
3431 	return PyLong_FromLong((long) get_tab_number(self->tab));
3432     else if (strcmp(name, "vars") == 0)
3433 	return NEW_DICTIONARY(self->tab->tp_vars);
3434     else if (strcmp(name, "window") == 0)
3435     {
3436 	/* For current tab window.c does not bother to set or update tp_curwin
3437 	 */
3438 	if (self->tab == curtab)
3439 	    return WindowNew(curwin, curtab);
3440 	else
3441 	    return WindowNew(self->tab->tp_curwin, self->tab);
3442     }
3443     else if (strcmp(name, "__members__") == 0)
3444 	return ObjectDir(NULL, TabPageAttrs);
3445     return NULL;
3446 }
3447 
3448     static PyObject *
3449 TabPageRepr(TabPageObject *self)
3450 {
3451     if (self->tab == INVALID_TABPAGE_VALUE)
3452 	return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
3453     else
3454     {
3455 	int	t = get_tab_number(self->tab);
3456 
3457 	if (t == 0)
3458 	    return PyString_FromFormat("<tabpage object (unknown) at %p>",
3459 					(self));
3460 	else
3461 	    return PyString_FromFormat("<tabpage %d>", t - 1);
3462     }
3463 }
3464 
3465 static struct PyMethodDef TabPageMethods[] = {
3466     /* name,	    function,			calling,	documentation */
3467     {"__dir__",	    (PyCFunction)TabPageDir,	METH_NOARGS,	""},
3468     { NULL,	    NULL,			0,		NULL}
3469 };
3470 
3471 /*
3472  * Window list object
3473  */
3474 
3475 static PyTypeObject TabListType;
3476 static PySequenceMethods TabListAsSeq;
3477 
3478 typedef struct
3479 {
3480     PyObject_HEAD
3481 } TabListObject;
3482 
3483     static PyInt
3484 TabListLength(PyObject *self UNUSED)
3485 {
3486     tabpage_T	*tp = first_tabpage;
3487     PyInt	n = 0;
3488 
3489     while (tp != NULL)
3490     {
3491 	++n;
3492 	tp = tp->tp_next;
3493     }
3494 
3495     return n;
3496 }
3497 
3498     static PyObject *
3499 TabListItem(PyObject *self UNUSED, PyInt n)
3500 {
3501     tabpage_T	*tp;
3502 
3503     for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3504 	if (n == 0)
3505 	    return TabPageNew(tp);
3506 
3507     PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
3508     return NULL;
3509 }
3510 
3511 /*
3512  * Window object
3513  */
3514 
3515 typedef struct
3516 {
3517     PyObject_HEAD
3518     win_T	*win;
3519     TabPageObject	*tabObject;
3520 } WindowObject;
3521 
3522 static PyTypeObject WindowType;
3523 
3524     static int
3525 CheckWindow(WindowObject *self)
3526 {
3527     if (self->win == INVALID_WINDOW_VALUE)
3528     {
3529 	PyErr_SET_VIM(N_("attempt to refer to deleted window"));
3530 	return -1;
3531     }
3532 
3533     return 0;
3534 }
3535 
3536     static PyObject *
3537 WindowNew(win_T *win, tabpage_T *tab)
3538 {
3539     /* We need to handle deletion of windows underneath us.
3540      * If we add a "w_python*_ref" field to the win_T structure,
3541      * then we can get at it in win_free() in vim. We then
3542      * need to create only ONE Python object per window - if
3543      * we try to create a second, just INCREF the existing one
3544      * and return it. The (single) Python object referring to
3545      * the window is stored in "w_python*_ref".
3546      * On a win_free() we set the Python object's win_T* field
3547      * to an invalid value. We trap all uses of a window
3548      * object, and reject them if the win_T* field is invalid.
3549      *
3550      * Python2 and Python3 get different fields and different objects:
3551      * w_python_ref and w_python3_ref fields respectively.
3552      */
3553 
3554     WindowObject *self;
3555 
3556     if (WIN_PYTHON_REF(win))
3557     {
3558 	self = WIN_PYTHON_REF(win);
3559 	Py_INCREF(self);
3560     }
3561     else
3562     {
3563 	self = PyObject_GC_New(WindowObject, &WindowType);
3564 	if (self == NULL)
3565 	    return NULL;
3566 	self->win = win;
3567 	WIN_PYTHON_REF(win) = self;
3568     }
3569 
3570     self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3571 
3572     return (PyObject *)(self);
3573 }
3574 
3575     static void
3576 WindowDestructor(WindowObject *self)
3577 {
3578     PyObject_GC_UnTrack((void *)(self));
3579     if (self->win && self->win != INVALID_WINDOW_VALUE)
3580 	WIN_PYTHON_REF(self->win) = NULL;
3581      Py_XDECREF(((PyObject *)(self->tabObject)));
3582     PyObject_GC_Del((void *)(self));
3583 }
3584 
3585     static int
3586 WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3587 {
3588     Py_VISIT(((PyObject *)(self->tabObject)));
3589     return 0;
3590 }
3591 
3592     static int
3593 WindowClear(WindowObject *self)
3594 {
3595     Py_CLEAR(self->tabObject);
3596     return 0;
3597 }
3598 
3599     static win_T *
3600 get_firstwin(TabPageObject *tabObject)
3601 {
3602     if (tabObject)
3603     {
3604 	if (CheckTabPage(tabObject))
3605 	    return NULL;
3606 	/* For current tab window.c does not bother to set or update tp_firstwin
3607 	 */
3608 	else if (tabObject->tab == curtab)
3609 	    return firstwin;
3610 	else
3611 	    return tabObject->tab->tp_firstwin;
3612     }
3613     else
3614 	return firstwin;
3615 }
3616 static char *WindowAttrs[] = {
3617     "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3618     "tabpage", "valid",
3619     NULL
3620 };
3621 
3622     static PyObject *
3623 WindowDir(PyObject *self)
3624 {
3625     return ObjectDir(self, WindowAttrs);
3626 }
3627 
3628     static PyObject *
3629 WindowAttrValid(WindowObject *self, char *name)
3630 {
3631     PyObject	*ret;
3632 
3633     if (strcmp(name, "valid") != 0)
3634 	return NULL;
3635 
3636     ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3637     Py_INCREF(ret);
3638     return ret;
3639 }
3640 
3641     static PyObject *
3642 WindowAttr(WindowObject *self, char *name)
3643 {
3644     if (strcmp(name, "buffer") == 0)
3645 	return (PyObject *)BufferNew(self->win->w_buffer);
3646     else if (strcmp(name, "cursor") == 0)
3647     {
3648 	pos_T *pos = &self->win->w_cursor;
3649 
3650 	return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3651     }
3652     else if (strcmp(name, "height") == 0)
3653 	return PyLong_FromLong((long)(self->win->w_height));
3654 #ifdef FEAT_WINDOWS
3655     else if (strcmp(name, "row") == 0)
3656 	return PyLong_FromLong((long)(self->win->w_winrow));
3657 #endif
3658 #ifdef FEAT_VERTSPLIT
3659     else if (strcmp(name, "width") == 0)
3660 	return PyLong_FromLong((long)(W_WIDTH(self->win)));
3661     else if (strcmp(name, "col") == 0)
3662 	return PyLong_FromLong((long)(W_WINCOL(self->win)));
3663 #endif
3664     else if (strcmp(name, "vars") == 0)
3665 	return NEW_DICTIONARY(self->win->w_vars);
3666     else if (strcmp(name, "options") == 0)
3667 	return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3668 			(PyObject *) self);
3669     else if (strcmp(name, "number") == 0)
3670     {
3671 	if (CheckTabPage(self->tabObject))
3672 	    return NULL;
3673 	return PyLong_FromLong((long)
3674 		get_win_number(self->win, get_firstwin(self->tabObject)));
3675     }
3676     else if (strcmp(name, "tabpage") == 0)
3677     {
3678 	Py_INCREF(self->tabObject);
3679 	return (PyObject *)(self->tabObject);
3680     }
3681     else if (strcmp(name, "__members__") == 0)
3682 	return ObjectDir(NULL, WindowAttrs);
3683     else
3684 	return NULL;
3685 }
3686 
3687     static int
3688 WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
3689 {
3690     if (CheckWindow(self))
3691 	return -1;
3692 
3693     if (strcmp(name, "buffer") == 0)
3694     {
3695 	PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
3696 	return -1;
3697     }
3698     else if (strcmp(name, "cursor") == 0)
3699     {
3700 	long lnum;
3701 	long col;
3702 
3703 	if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
3704 	    return -1;
3705 
3706 	if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
3707 	{
3708 	    PyErr_SET_VIM(N_("cursor position outside buffer"));
3709 	    return -1;
3710 	}
3711 
3712 	/* Check for keyboard interrupts */
3713 	if (VimCheckInterrupt())
3714 	    return -1;
3715 
3716 	self->win->w_cursor.lnum = lnum;
3717 	self->win->w_cursor.col = col;
3718 #ifdef FEAT_VIRTUALEDIT
3719 	self->win->w_cursor.coladd = 0;
3720 #endif
3721 	/* When column is out of range silently correct it. */
3722 	check_cursor_col_win(self->win);
3723 
3724 	update_screen(VALID);
3725 	return 0;
3726     }
3727     else if (strcmp(name, "height") == 0)
3728     {
3729 	long	height;
3730 	win_T	*savewin;
3731 
3732 	if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
3733 	    return -1;
3734 
3735 #ifdef FEAT_GUI
3736 	need_mouse_correct = TRUE;
3737 #endif
3738 	savewin = curwin;
3739 	curwin = self->win;
3740 
3741 	VimTryStart();
3742 	win_setheight((int) height);
3743 	curwin = savewin;
3744 	if (VimTryEnd())
3745 	    return -1;
3746 
3747 	return 0;
3748     }
3749 #ifdef FEAT_VERTSPLIT
3750     else if (strcmp(name, "width") == 0)
3751     {
3752 	long	width;
3753 	win_T	*savewin;
3754 
3755 	if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
3756 	    return -1;
3757 
3758 #ifdef FEAT_GUI
3759 	need_mouse_correct = TRUE;
3760 #endif
3761 	savewin = curwin;
3762 	curwin = self->win;
3763 
3764 	VimTryStart();
3765 	win_setwidth((int) width);
3766 	curwin = savewin;
3767 	if (VimTryEnd())
3768 	    return -1;
3769 
3770 	return 0;
3771     }
3772 #endif
3773     else
3774     {
3775 	PyErr_SetString(PyExc_AttributeError, name);
3776 	return -1;
3777     }
3778 }
3779 
3780     static PyObject *
3781 WindowRepr(WindowObject *self)
3782 {
3783     if (self->win == INVALID_WINDOW_VALUE)
3784 	return PyString_FromFormat("<window object (deleted) at %p>", (self));
3785     else
3786     {
3787 	int	w = get_win_number(self->win, firstwin);
3788 
3789 	if (w == 0)
3790 	    return PyString_FromFormat("<window object (unknown) at %p>",
3791 								      (self));
3792 	else
3793 	    return PyString_FromFormat("<window %d>", w - 1);
3794     }
3795 }
3796 
3797 static struct PyMethodDef WindowMethods[] = {
3798     /* name,	    function,			calling,	documentation */
3799     {"__dir__",	    (PyCFunction)WindowDir,	METH_NOARGS,	""},
3800     { NULL,	    NULL,			0,		NULL}
3801 };
3802 
3803 /*
3804  * Window list object
3805  */
3806 
3807 static PyTypeObject WinListType;
3808 static PySequenceMethods WinListAsSeq;
3809 
3810 typedef struct
3811 {
3812     PyObject_HEAD
3813     TabPageObject	*tabObject;
3814 } WinListObject;
3815 
3816     static PyObject *
3817 WinListNew(TabPageObject *tabObject)
3818 {
3819     WinListObject	*self;
3820 
3821     self = PyObject_NEW(WinListObject, &WinListType);
3822     self->tabObject = tabObject;
3823     Py_INCREF(tabObject);
3824 
3825     return (PyObject *)(self);
3826 }
3827 
3828     static void
3829 WinListDestructor(WinListObject *self)
3830 {
3831     TabPageObject	*tabObject = self->tabObject;
3832 
3833     if (tabObject)
3834     {
3835 	Py_DECREF((PyObject *)(tabObject));
3836     }
3837 
3838     DESTRUCTOR_FINISH(self);
3839 }
3840 
3841     static PyInt
3842 WinListLength(WinListObject *self)
3843 {
3844     win_T	*w;
3845     PyInt	n = 0;
3846 
3847     if (!(w = get_firstwin(self->tabObject)))
3848 	return -1;
3849 
3850     while (w != NULL)
3851     {
3852 	++n;
3853 	w = W_NEXT(w);
3854     }
3855 
3856     return n;
3857 }
3858 
3859     static PyObject *
3860 WinListItem(WinListObject *self, PyInt n)
3861 {
3862     win_T *w;
3863 
3864     if (!(w = get_firstwin(self->tabObject)))
3865 	return NULL;
3866 
3867     for (; w != NULL; w = W_NEXT(w), --n)
3868 	if (n == 0)
3869 	    return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
3870 
3871     PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
3872     return NULL;
3873 }
3874 
3875 /* Convert a Python string into a Vim line.
3876  *
3877  * The result is in allocated memory. All internal nulls are replaced by
3878  * newline characters. It is an error for the string to contain newline
3879  * characters.
3880  *
3881  * On errors, the Python exception data is set, and NULL is returned.
3882  */
3883     static char *
3884 StringToLine(PyObject *obj)
3885 {
3886     char	*str;
3887     char	*save;
3888     PyObject	*bytes = NULL;
3889     Py_ssize_t	len = 0;
3890     PyInt	i;
3891     char	*p;
3892 
3893     if (PyBytes_Check(obj))
3894     {
3895 	if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3896 		|| str == NULL)
3897 	    return NULL;
3898     }
3899     else if (PyUnicode_Check(obj))
3900     {
3901 	if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3902 	    return NULL;
3903 
3904 	if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3905 		|| str == NULL)
3906 	{
3907 	    Py_DECREF(bytes);
3908 	    return NULL;
3909 	}
3910     }
3911     else
3912     {
3913 #if PY_MAJOR_VERSION < 3
3914 	PyErr_FORMAT(PyExc_TypeError,
3915 		N_("expected str() or unicode() instance, but got %s"),
3916 		Py_TYPE_NAME(obj));
3917 #else
3918 	PyErr_FORMAT(PyExc_TypeError,
3919 		N_("expected bytes() or str() instance, but got %s"),
3920 		Py_TYPE_NAME(obj));
3921 #endif
3922 	return NULL;
3923     }
3924 
3925     /*
3926      * Error checking: String must not contain newlines, as we
3927      * are replacing a single line, and we must replace it with
3928      * a single line.
3929      * A trailing newline is removed, so that append(f.readlines()) works.
3930      */
3931     p = memchr(str, '\n', len);
3932     if (p != NULL)
3933     {
3934 	if (p == str + len - 1)
3935 	    --len;
3936 	else
3937 	{
3938 	    PyErr_SET_VIM(N_("string cannot contain newlines"));
3939 	    Py_XDECREF(bytes);
3940 	    return NULL;
3941 	}
3942     }
3943 
3944     /* Create a copy of the string, with internal nulls replaced by
3945      * newline characters, as is the vim convention.
3946      */
3947     save = (char *)alloc((unsigned)(len+1));
3948     if (save == NULL)
3949     {
3950 	PyErr_NoMemory();
3951 	Py_XDECREF(bytes);
3952 	return NULL;
3953     }
3954 
3955     for (i = 0; i < len; ++i)
3956     {
3957 	if (str[i] == '\0')
3958 	    save[i] = '\n';
3959 	else
3960 	    save[i] = str[i];
3961     }
3962 
3963     save[i] = '\0';
3964     Py_XDECREF(bytes);  /* Python 2 does nothing here */
3965 
3966     return save;
3967 }
3968 
3969 /* Get a line from the specified buffer. The line number is
3970  * in Vim format (1-based). The line is returned as a Python
3971  * string object.
3972  */
3973     static PyObject *
3974 GetBufferLine(buf_T *buf, PyInt n)
3975 {
3976     return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3977 }
3978 
3979 
3980 /* Get a list of lines from the specified buffer. The line numbers
3981  * are in Vim format (1-based). The range is from lo up to, but not
3982  * including, hi. The list is returned as a Python list of string objects.
3983  */
3984     static PyObject *
3985 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3986 {
3987     PyInt	i;
3988     PyInt	n = hi - lo;
3989     PyObject	*list = PyList_New(n);
3990 
3991     if (list == NULL)
3992 	return NULL;
3993 
3994     for (i = 0; i < n; ++i)
3995     {
3996 	PyObject	*string = LineToString(
3997 		(char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3998 
3999 	/* Error check - was the Python string creation OK? */
4000 	if (string == NULL)
4001 	{
4002 	    Py_DECREF(list);
4003 	    return NULL;
4004 	}
4005 
4006 	PyList_SET_ITEM(list, i, string);
4007     }
4008 
4009     /* The ownership of the Python list is passed to the caller (ie,
4010      * the caller should Py_DECREF() the object when it is finished
4011      * with it).
4012      */
4013 
4014     return list;
4015 }
4016 
4017 /*
4018  * Check if deleting lines made the cursor position invalid.
4019  * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4020  * deleted).
4021  */
4022     static void
4023 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4024 {
4025     if (curwin->w_cursor.lnum >= lo)
4026     {
4027 	/* Adjust the cursor position if it's in/after the changed
4028 	 * lines. */
4029 	if (curwin->w_cursor.lnum >= hi)
4030 	{
4031 	    curwin->w_cursor.lnum += extra;
4032 	    check_cursor_col();
4033 	}
4034 	else if (extra < 0)
4035 	{
4036 	    curwin->w_cursor.lnum = lo;
4037 	    check_cursor();
4038 	}
4039 	else
4040 	    check_cursor_col();
4041 	changed_cline_bef_curs();
4042     }
4043     invalidate_botline();
4044 }
4045 
4046 /*
4047  * Find a window that contains "buf" and switch to it.
4048  * If there is no such window, use the current window and change "curbuf".
4049  * Caller must initialize save_curbuf to NULL.
4050  * restore_win_for_buf() MUST be called later!
4051  */
4052     static void
4053 switch_to_win_for_buf(
4054     buf_T	*buf,
4055     win_T	**save_curwinp,
4056     tabpage_T	**save_curtabp,
4057     buf_T	**save_curbufp)
4058 {
4059     win_T	*wp;
4060     tabpage_T	*tp;
4061 
4062     if (find_win_for_buf(buf, &wp, &tp) == FAIL)
4063 	switch_buffer(save_curbufp, buf);
4064     else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4065     {
4066 	restore_win(*save_curwinp, *save_curtabp, TRUE);
4067 	switch_buffer(save_curbufp, buf);
4068     }
4069 }
4070 
4071     static void
4072 restore_win_for_buf(
4073     win_T	*save_curwin,
4074     tabpage_T	*save_curtab,
4075     buf_T	*save_curbuf)
4076 {
4077     if (save_curbuf == NULL)
4078 	restore_win(save_curwin, save_curtab, TRUE);
4079     else
4080 	restore_buffer(save_curbuf);
4081 }
4082 
4083 /*
4084  * Replace a line in the specified buffer. The line number is
4085  * in Vim format (1-based). The replacement line is given as
4086  * a Python string object. The object is checked for validity
4087  * and correct format. Errors are returned as a value of FAIL.
4088  * The return value is OK on success.
4089  * If OK is returned and len_change is not NULL, *len_change
4090  * is set to the change in the buffer length.
4091  */
4092     static int
4093 SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4094 {
4095     buf_T	*save_curbuf = NULL;
4096     win_T	*save_curwin = NULL;
4097     tabpage_T	*save_curtab = NULL;
4098 
4099     /* First of all, we check the type of the supplied Python object.
4100      * There are three cases:
4101      *	  1. NULL, or None - this is a deletion.
4102      *	  2. A string	   - this is a replacement.
4103      *	  3. Anything else - this is an error.
4104      */
4105     if (line == Py_None || line == NULL)
4106     {
4107 	PyErr_Clear();
4108 	switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4109 
4110 	VimTryStart();
4111 
4112 	if (u_savedel((linenr_T)n, 1L) == FAIL)
4113 	    RAISE_UNDO_FAIL;
4114 	else if (ml_delete((linenr_T)n, FALSE) == FAIL)
4115 	    RAISE_DELETE_LINE_FAIL;
4116 	else
4117 	{
4118 	    if (buf == curbuf)
4119 		py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
4120 	    if (save_curbuf == NULL)
4121 		/* Only adjust marks if we managed to switch to a window that
4122 		 * holds the buffer, otherwise line numbers will be invalid. */
4123 		deleted_lines_mark((linenr_T)n, 1L);
4124 	}
4125 
4126 	restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
4127 
4128 	if (VimTryEnd())
4129 	    return FAIL;
4130 
4131 	if (len_change)
4132 	    *len_change = -1;
4133 
4134 	return OK;
4135     }
4136     else if (PyBytes_Check(line) || PyUnicode_Check(line))
4137     {
4138 	char	*save = StringToLine(line);
4139 
4140 	if (save == NULL)
4141 	    return FAIL;
4142 
4143 	VimTryStart();
4144 
4145 	/* We do not need to free "save" if ml_replace() consumes it. */
4146 	PyErr_Clear();
4147 	switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4148 
4149 	if (u_savesub((linenr_T)n) == FAIL)
4150 	{
4151 	    RAISE_UNDO_FAIL;
4152 	    vim_free(save);
4153 	}
4154 	else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4155 	{
4156 	    RAISE_REPLACE_LINE_FAIL;
4157 	    vim_free(save);
4158 	}
4159 	else
4160 	    changed_bytes((linenr_T)n, 0);
4161 
4162 	restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
4163 
4164 	/* Check that the cursor is not beyond the end of the line now. */
4165 	if (buf == curbuf)
4166 	    check_cursor_col();
4167 
4168 	if (VimTryEnd())
4169 	    return FAIL;
4170 
4171 	if (len_change)
4172 	    *len_change = 0;
4173 
4174 	return OK;
4175     }
4176     else
4177     {
4178 	PyErr_BadArgument();
4179 	return FAIL;
4180     }
4181 }
4182 
4183 /* Replace a range of lines in the specified buffer. The line numbers are in
4184  * Vim format (1-based). The range is from lo up to, but not including, hi.
4185  * The replacement lines are given as a Python list of string objects. The
4186  * list is checked for validity and correct format. Errors are returned as a
4187  * value of FAIL.  The return value is OK on success.
4188  * If OK is returned and len_change is not NULL, *len_change
4189  * is set to the change in the buffer length.
4190  */
4191     static int
4192 SetBufferLineList(
4193 	buf_T *buf,
4194 	PyInt lo,
4195 	PyInt hi,
4196 	PyObject *list,
4197 	PyInt *len_change)
4198 {
4199     buf_T	*save_curbuf = NULL;
4200     win_T	*save_curwin = NULL;
4201     tabpage_T	*save_curtab = NULL;
4202 
4203     /* First of all, we check the type of the supplied Python object.
4204      * There are three cases:
4205      *	  1. NULL, or None - this is a deletion.
4206      *	  2. A list	   - this is a replacement.
4207      *	  3. Anything else - this is an error.
4208      */
4209     if (list == Py_None || list == NULL)
4210     {
4211 	PyInt	i;
4212 	PyInt	n = (int)(hi - lo);
4213 
4214 	PyErr_Clear();
4215 	VimTryStart();
4216 	switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4217 
4218 	if (u_savedel((linenr_T)lo, (long)n) == FAIL)
4219 	    RAISE_UNDO_FAIL;
4220 	else
4221 	{
4222 	    for (i = 0; i < n; ++i)
4223 	    {
4224 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4225 		{
4226 		    RAISE_DELETE_LINE_FAIL;
4227 		    break;
4228 		}
4229 	    }
4230 	    if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4231 		/* Using an existing window for the buffer, adjust the cursor
4232 		 * position. */
4233 		py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
4234 	    if (save_curbuf == NULL)
4235 		/* Only adjust marks if we managed to switch to a window that
4236 		 * holds the buffer, otherwise line numbers will be invalid. */
4237 		deleted_lines_mark((linenr_T)lo, (long)i);
4238 	}
4239 
4240 	restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
4241 
4242 	if (VimTryEnd())
4243 	    return FAIL;
4244 
4245 	if (len_change)
4246 	    *len_change = -n;
4247 
4248 	return OK;
4249     }
4250     else if (PyList_Check(list))
4251     {
4252 	PyInt	i;
4253 	PyInt	new_len = PyList_Size(list);
4254 	PyInt	old_len = hi - lo;
4255 	PyInt	extra = 0;	/* lines added to text, can be negative */
4256 	char	**array;
4257 
4258 	if (new_len == 0)	/* avoid allocating zero bytes */
4259 	    array = NULL;
4260 	else
4261 	{
4262 	    array = PyMem_New(char *, new_len);
4263 	    if (array == NULL)
4264 	    {
4265 		PyErr_NoMemory();
4266 		return FAIL;
4267 	    }
4268 	}
4269 
4270 	for (i = 0; i < new_len; ++i)
4271 	{
4272 	    PyObject *line;
4273 
4274 	    if (!(line = PyList_GetItem(list, i)) ||
4275 		!(array[i] = StringToLine(line)))
4276 	    {
4277 		while (i)
4278 		    vim_free(array[--i]);
4279 		PyMem_Free(array);
4280 		return FAIL;
4281 	    }
4282 	}
4283 
4284 	VimTryStart();
4285 	PyErr_Clear();
4286 
4287 	/* START of region without "return".  Must call restore_buffer()! */
4288 	switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4289 
4290 	if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
4291 	    RAISE_UNDO_FAIL;
4292 
4293 	/* If the size of the range is reducing (ie, new_len < old_len) we
4294 	 * need to delete some old_len. We do this at the start, by
4295 	 * repeatedly deleting line "lo".
4296 	 */
4297 	if (!PyErr_Occurred())
4298 	{
4299 	    for (i = 0; i < old_len - new_len; ++i)
4300 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4301 		{
4302 		    RAISE_DELETE_LINE_FAIL;
4303 		    break;
4304 		}
4305 	    extra -= i;
4306 	}
4307 
4308 	/* For as long as possible, replace the existing old_len with the
4309 	 * new old_len. This is a more efficient operation, as it requires
4310 	 * less memory allocation and freeing.
4311 	 */
4312 	if (!PyErr_Occurred())
4313 	{
4314 	    for (i = 0; i < old_len && i < new_len; ++i)
4315 		if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4316 								      == FAIL)
4317 		{
4318 		    RAISE_REPLACE_LINE_FAIL;
4319 		    break;
4320 		}
4321 	}
4322 	else
4323 	    i = 0;
4324 
4325 	/* Now we may need to insert the remaining new old_len. If we do, we
4326 	 * must free the strings as we finish with them (we can't pass the
4327 	 * responsibility to vim in this case).
4328 	 */
4329 	if (!PyErr_Occurred())
4330 	{
4331 	    while (i < new_len)
4332 	    {
4333 		if (ml_append((linenr_T)(lo + i - 1),
4334 					(char_u *)array[i], 0, FALSE) == FAIL)
4335 		{
4336 		    RAISE_INSERT_LINE_FAIL;
4337 		    break;
4338 		}
4339 		vim_free(array[i]);
4340 		++i;
4341 		++extra;
4342 	    }
4343 	}
4344 
4345 	/* Free any left-over old_len, as a result of an error */
4346 	while (i < new_len)
4347 	{
4348 	    vim_free(array[i]);
4349 	    ++i;
4350 	}
4351 
4352 	/* Free the array of old_len. All of its contents have now
4353 	 * been dealt with (either freed, or the responsibility passed
4354 	 * to vim.
4355 	 */
4356 	PyMem_Free(array);
4357 
4358 	/* Adjust marks. Invalidate any which lie in the
4359 	 * changed range, and move any in the remainder of the buffer.
4360 	 * Only adjust marks if we managed to switch to a window that holds
4361 	 * the buffer, otherwise line numbers will be invalid. */
4362 	if (save_curbuf == NULL)
4363 	    mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
4364 						  (long)MAXLNUM, (long)extra);
4365 	changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4366 
4367 	if (buf == curbuf)
4368 	    py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4369 
4370 	/* END of region without "return". */
4371 	restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
4372 
4373 	if (VimTryEnd())
4374 	    return FAIL;
4375 
4376 	if (len_change)
4377 	    *len_change = new_len - old_len;
4378 
4379 	return OK;
4380     }
4381     else
4382     {
4383 	PyErr_BadArgument();
4384 	return FAIL;
4385     }
4386 }
4387 
4388 /* Insert a number of lines into the specified buffer after the specified line.
4389  * The line number is in Vim format (1-based). The lines to be inserted are
4390  * given as a Python list of string objects or as a single string. The lines
4391  * to be added are checked for validity and correct format. Errors are
4392  * returned as a value of FAIL.  The return value is OK on success.
4393  * If OK is returned and len_change is not NULL, *len_change
4394  * is set to the change in the buffer length.
4395  */
4396     static int
4397 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4398 {
4399     buf_T	*save_curbuf = NULL;
4400     win_T	*save_curwin = NULL;
4401     tabpage_T	*save_curtab = NULL;
4402 
4403     /* First of all, we check the type of the supplied Python object.
4404      * It must be a string or a list, or the call is in error.
4405      */
4406     if (PyBytes_Check(lines) || PyUnicode_Check(lines))
4407     {
4408 	char		*str = StringToLine(lines);
4409 
4410 	if (str == NULL)
4411 	    return FAIL;
4412 
4413 	PyErr_Clear();
4414 	VimTryStart();
4415 	switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4416 
4417 	if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
4418 	    RAISE_UNDO_FAIL;
4419 	else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
4420 	    RAISE_INSERT_LINE_FAIL;
4421 	else if (save_curbuf == NULL)
4422 	    /* Only adjust marks if we managed to switch to a window that
4423 	     * holds the buffer, otherwise line numbers will be invalid. */
4424 	    appended_lines_mark((linenr_T)n, 1L);
4425 
4426 	vim_free(str);
4427 	restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
4428 	update_screen(VALID);
4429 
4430 	if (VimTryEnd())
4431 	    return FAIL;
4432 
4433 	if (len_change)
4434 	    *len_change = 1;
4435 
4436 	return OK;
4437     }
4438     else if (PyList_Check(lines))
4439     {
4440 	PyInt	i;
4441 	PyInt	size = PyList_Size(lines);
4442 	char	**array;
4443 
4444 	array = PyMem_New(char *, size);
4445 	if (array == NULL)
4446 	{
4447 	    PyErr_NoMemory();
4448 	    return FAIL;
4449 	}
4450 
4451 	for (i = 0; i < size; ++i)
4452 	{
4453 	    PyObject *line;
4454 
4455 	    if (!(line = PyList_GetItem(lines, i)) ||
4456 		!(array[i] = StringToLine(line)))
4457 	    {
4458 		while (i)
4459 		    vim_free(array[--i]);
4460 		PyMem_Free(array);
4461 		return FAIL;
4462 	    }
4463 	}
4464 
4465 	PyErr_Clear();
4466 	VimTryStart();
4467 	switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4468 
4469 	if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
4470 	    RAISE_UNDO_FAIL;
4471 	else
4472 	{
4473 	    for (i = 0; i < size; ++i)
4474 	    {
4475 		if (ml_append((linenr_T)(n + i),
4476 					(char_u *)array[i], 0, FALSE) == FAIL)
4477 		{
4478 		    RAISE_INSERT_LINE_FAIL;
4479 
4480 		    /* Free the rest of the lines */
4481 		    while (i < size)
4482 			vim_free(array[i++]);
4483 
4484 		    break;
4485 		}
4486 		vim_free(array[i]);
4487 	    }
4488 	    if (i > 0 && save_curbuf == NULL)
4489 		/* Only adjust marks if we managed to switch to a window that
4490 		 * holds the buffer, otherwise line numbers will be invalid. */
4491 		appended_lines_mark((linenr_T)n, (long)i);
4492 	}
4493 
4494 	/* Free the array of lines. All of its contents have now
4495 	 * been freed. */
4496 	PyMem_Free(array);
4497 	restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
4498 
4499 	update_screen(VALID);
4500 
4501 	if (VimTryEnd())
4502 	    return FAIL;
4503 
4504 	if (len_change)
4505 	    *len_change = size;
4506 
4507 	return OK;
4508     }
4509     else
4510     {
4511 	PyErr_BadArgument();
4512 	return FAIL;
4513     }
4514 }
4515 
4516 /*
4517  * Common routines for buffers and line ranges
4518  * -------------------------------------------
4519  */
4520 
4521 typedef struct
4522 {
4523     PyObject_HEAD
4524     buf_T *buf;
4525 } BufferObject;
4526 
4527     static int
4528 CheckBuffer(BufferObject *self)
4529 {
4530     if (self->buf == INVALID_BUFFER_VALUE)
4531     {
4532 	PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
4533 	return -1;
4534     }
4535 
4536     return 0;
4537 }
4538 
4539     static PyObject *
4540 RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4541 {
4542     if (CheckBuffer(self))
4543 	return NULL;
4544 
4545     if (end == -1)
4546 	end = self->buf->b_ml.ml_line_count;
4547 
4548     if (n < 0)
4549 	n += end - start + 1;
4550 
4551     if (n < 0 || n > end - start)
4552     {
4553 	PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
4554 	return NULL;
4555     }
4556 
4557     return GetBufferLine(self->buf, n+start);
4558 }
4559 
4560     static PyObject *
4561 RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4562 {
4563     PyInt size;
4564 
4565     if (CheckBuffer(self))
4566 	return NULL;
4567 
4568     if (end == -1)
4569 	end = self->buf->b_ml.ml_line_count;
4570 
4571     size = end - start + 1;
4572 
4573     if (lo < 0)
4574 	lo = 0;
4575     else if (lo > size)
4576 	lo = size;
4577     if (hi < 0)
4578 	hi = 0;
4579     if (hi < lo)
4580 	hi = lo;
4581     else if (hi > size)
4582 	hi = size;
4583 
4584     return GetBufferLineList(self->buf, lo+start, hi+start);
4585 }
4586 
4587     static PyInt
4588 RBAsItem(
4589 	BufferObject *self,
4590 	PyInt n,
4591 	PyObject *valObject,
4592 	PyInt start,
4593 	PyInt end,
4594 	PyInt *new_end)
4595 {
4596     PyInt len_change;
4597 
4598     if (CheckBuffer(self))
4599 	return -1;
4600 
4601     if (end == -1)
4602 	end = self->buf->b_ml.ml_line_count;
4603 
4604     if (n < 0)
4605 	n += end - start + 1;
4606 
4607     if (n < 0 || n > end - start)
4608     {
4609 	PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
4610 	return -1;
4611     }
4612 
4613     if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
4614 	return -1;
4615 
4616     if (new_end)
4617 	*new_end = end + len_change;
4618 
4619     return 0;
4620 }
4621 
4622     static PyInt
4623 RBAsSlice(
4624 	BufferObject *self,
4625 	PyInt lo,
4626 	PyInt hi,
4627 	PyObject *valObject,
4628 	PyInt start,
4629 	PyInt end,
4630 	PyInt *new_end)
4631 {
4632     PyInt size;
4633     PyInt len_change;
4634 
4635     /* Self must be a valid buffer */
4636     if (CheckBuffer(self))
4637 	return -1;
4638 
4639     if (end == -1)
4640 	end = self->buf->b_ml.ml_line_count;
4641 
4642     /* Sort out the slice range */
4643     size = end - start + 1;
4644 
4645     if (lo < 0)
4646 	lo = 0;
4647     else if (lo > size)
4648 	lo = size;
4649     if (hi < 0)
4650 	hi = 0;
4651     if (hi < lo)
4652 	hi = lo;
4653     else if (hi > size)
4654 	hi = size;
4655 
4656     if (SetBufferLineList(self->buf, lo + start, hi + start,
4657 						valObject, &len_change) == FAIL)
4658 	return -1;
4659 
4660     if (new_end)
4661 	*new_end = end + len_change;
4662 
4663     return 0;
4664 }
4665 
4666 
4667     static PyObject *
4668 RBAppend(
4669 	BufferObject *self,
4670 	PyObject *args,
4671 	PyInt start,
4672 	PyInt end,
4673 	PyInt *new_end)
4674 {
4675     PyObject *lines;
4676     PyInt len_change;
4677     PyInt max;
4678     PyInt n;
4679 
4680     if (CheckBuffer(self))
4681 	return NULL;
4682 
4683     if (end == -1)
4684 	end = self->buf->b_ml.ml_line_count;
4685 
4686     max = n = end - start + 1;
4687 
4688     if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4689 	return NULL;
4690 
4691     if (n < 0 || n > max)
4692     {
4693 	PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
4694 	return NULL;
4695     }
4696 
4697     if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4698 	return NULL;
4699 
4700     if (new_end)
4701 	*new_end = end + len_change;
4702 
4703     Py_INCREF(Py_None);
4704     return Py_None;
4705 }
4706 
4707 /* Range object
4708  */
4709 
4710 static PyTypeObject RangeType;
4711 static PySequenceMethods RangeAsSeq;
4712 static PyMappingMethods RangeAsMapping;
4713 
4714 typedef struct
4715 {
4716     PyObject_HEAD
4717     BufferObject *buf;
4718     PyInt start;
4719     PyInt end;
4720 } RangeObject;
4721 
4722     static PyObject *
4723 RangeNew(buf_T *buf, PyInt start, PyInt end)
4724 {
4725     BufferObject *bufr;
4726     RangeObject *self;
4727     self = PyObject_GC_New(RangeObject, &RangeType);
4728     if (self == NULL)
4729 	return NULL;
4730 
4731     bufr = (BufferObject *)BufferNew(buf);
4732     if (bufr == NULL)
4733     {
4734 	Py_DECREF(self);
4735 	return NULL;
4736     }
4737     Py_INCREF(bufr);
4738 
4739     self->buf = bufr;
4740     self->start = start;
4741     self->end = end;
4742 
4743     return (PyObject *)(self);
4744 }
4745 
4746     static void
4747 RangeDestructor(RangeObject *self)
4748 {
4749     PyObject_GC_UnTrack((void *)(self));
4750     Py_XDECREF(self->buf);
4751     PyObject_GC_Del((void *)(self));
4752 }
4753 
4754     static int
4755 RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4756 {
4757     Py_VISIT(((PyObject *)(self->buf)));
4758     return 0;
4759 }
4760 
4761     static int
4762 RangeClear(RangeObject *self)
4763 {
4764     Py_CLEAR(self->buf);
4765     return 0;
4766 }
4767 
4768     static PyInt
4769 RangeLength(RangeObject *self)
4770 {
4771     /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
4772     if (CheckBuffer(self->buf))
4773 	return -1; /* ??? */
4774 
4775     return (self->end - self->start + 1);
4776 }
4777 
4778     static PyObject *
4779 RangeItem(RangeObject *self, PyInt n)
4780 {
4781     return RBItem(self->buf, n, self->start, self->end);
4782 }
4783 
4784     static PyObject *
4785 RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
4786 {
4787     return RBSlice(self->buf, lo, hi, self->start, self->end);
4788 }
4789 
4790 static char *RangeAttrs[] = {
4791     "start", "end",
4792     NULL
4793 };
4794 
4795     static PyObject *
4796 RangeDir(PyObject *self)
4797 {
4798     return ObjectDir(self, RangeAttrs);
4799 }
4800 
4801     static PyObject *
4802 RangeAppend(RangeObject *self, PyObject *args)
4803 {
4804     return RBAppend(self->buf, args, self->start, self->end, &self->end);
4805 }
4806 
4807     static PyObject *
4808 RangeRepr(RangeObject *self)
4809 {
4810     if (self->buf->buf == INVALID_BUFFER_VALUE)
4811 	return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4812 				    (self));
4813     else
4814     {
4815 	char *name = (char *)self->buf->buf->b_fname;
4816 
4817 	if (name == NULL)
4818 	    name = "";
4819 
4820 	return PyString_FromFormat("<range %s (%d:%d)>",
4821 				    name, (int)self->start, (int)self->end);
4822     }
4823 }
4824 
4825 static struct PyMethodDef RangeMethods[] = {
4826     /* name,	function,			calling,	documentation */
4827     {"append",	(PyCFunction)RangeAppend,	METH_VARARGS,	"Append data to the Vim range" },
4828     {"__dir__",	(PyCFunction)RangeDir,		METH_NOARGS,	""},
4829     { NULL,	NULL,				0,		NULL}
4830 };
4831 
4832 static PyTypeObject BufferType;
4833 static PySequenceMethods BufferAsSeq;
4834 static PyMappingMethods BufferAsMapping;
4835 
4836     static PyObject *
4837 BufferNew(buf_T *buf)
4838 {
4839     /* We need to handle deletion of buffers underneath us.
4840      * If we add a "b_python*_ref" field to the buf_T structure,
4841      * then we can get at it in buf_freeall() in vim. We then
4842      * need to create only ONE Python object per buffer - if
4843      * we try to create a second, just INCREF the existing one
4844      * and return it. The (single) Python object referring to
4845      * the buffer is stored in "b_python*_ref".
4846      * Question: what to do on a buf_freeall(). We'll probably
4847      * have to either delete the Python object (DECREF it to
4848      * zero - a bad idea, as it leaves dangling refs!) or
4849      * set the buf_T * value to an invalid value (-1?), which
4850      * means we need checks in all access functions... Bah.
4851      *
4852      * Python2 and Python3 get different fields and different objects:
4853      * b_python_ref and b_python3_ref fields respectively.
4854      */
4855 
4856     BufferObject *self;
4857 
4858     if (BUF_PYTHON_REF(buf) != NULL)
4859     {
4860 	self = BUF_PYTHON_REF(buf);
4861 	Py_INCREF(self);
4862     }
4863     else
4864     {
4865 	self = PyObject_NEW(BufferObject, &BufferType);
4866 	if (self == NULL)
4867 	    return NULL;
4868 	self->buf = buf;
4869 	BUF_PYTHON_REF(buf) = self;
4870     }
4871 
4872     return (PyObject *)(self);
4873 }
4874 
4875     static void
4876 BufferDestructor(BufferObject *self)
4877 {
4878     if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4879 	BUF_PYTHON_REF(self->buf) = NULL;
4880 
4881     DESTRUCTOR_FINISH(self);
4882 }
4883 
4884     static PyInt
4885 BufferLength(BufferObject *self)
4886 {
4887     /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
4888     if (CheckBuffer(self))
4889 	return -1; /* ??? */
4890 
4891     return (PyInt)(self->buf->b_ml.ml_line_count);
4892 }
4893 
4894     static PyObject *
4895 BufferItem(BufferObject *self, PyInt n)
4896 {
4897     return RBItem(self, n, 1, -1);
4898 }
4899 
4900     static PyObject *
4901 BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
4902 {
4903     return RBSlice(self, lo, hi, 1, -1);
4904 }
4905 
4906 static char *BufferAttrs[] = {
4907     "name", "number", "vars", "options", "valid",
4908     NULL
4909 };
4910 
4911     static PyObject *
4912 BufferDir(PyObject *self)
4913 {
4914     return ObjectDir(self, BufferAttrs);
4915 }
4916 
4917     static PyObject *
4918 BufferAttrValid(BufferObject *self, char *name)
4919 {
4920     PyObject	*ret;
4921 
4922     if (strcmp(name, "valid") != 0)
4923 	return NULL;
4924 
4925     ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4926     Py_INCREF(ret);
4927     return ret;
4928 }
4929 
4930     static PyObject *
4931 BufferAttr(BufferObject *self, char *name)
4932 {
4933     if (strcmp(name, "name") == 0)
4934 	return PyString_FromString((self->buf->b_ffname == NULL
4935 				    ? "" : (char *)self->buf->b_ffname));
4936     else if (strcmp(name, "number") == 0)
4937 	return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
4938     else if (strcmp(name, "vars") == 0)
4939 	return NEW_DICTIONARY(self->buf->b_vars);
4940     else if (strcmp(name, "options") == 0)
4941 	return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4942 			(PyObject *) self);
4943     else if (strcmp(name, "__members__") == 0)
4944 	return ObjectDir(NULL, BufferAttrs);
4945     else
4946 	return NULL;
4947 }
4948 
4949     static int
4950 BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4951 {
4952     if (CheckBuffer(self))
4953 	return -1;
4954 
4955     if (strcmp(name, "name") == 0)
4956     {
4957 	char_u		*val;
4958 	aco_save_T	aco;
4959 	int		ren_ret;
4960 	PyObject	*todecref;
4961 
4962 	if (!(val = StringToChars(valObject, &todecref)))
4963 	    return -1;
4964 
4965 	VimTryStart();
4966 	/* Using aucmd_*: autocommands will be executed by rename_buffer */
4967 	aucmd_prepbuf(&aco, self->buf);
4968 	ren_ret = rename_buffer(val);
4969 	aucmd_restbuf(&aco);
4970 	Py_XDECREF(todecref);
4971 	if (VimTryEnd())
4972 	    return -1;
4973 
4974 	if (ren_ret == FAIL)
4975 	{
4976 	    PyErr_SET_VIM(N_("failed to rename buffer"));
4977 	    return -1;
4978 	}
4979 	return 0;
4980     }
4981     else
4982     {
4983 	PyErr_SetString(PyExc_AttributeError, name);
4984 	return -1;
4985     }
4986 }
4987 
4988     static PyObject *
4989 BufferAppend(BufferObject *self, PyObject *args)
4990 {
4991     return RBAppend(self, args, 1, -1, NULL);
4992 }
4993 
4994     static PyObject *
4995 BufferMark(BufferObject *self, PyObject *pmarkObject)
4996 {
4997     pos_T	*posp;
4998     char_u	*pmark;
4999     char_u	mark;
5000     buf_T	*savebuf;
5001     PyObject	*todecref;
5002 
5003     if (CheckBuffer(self))
5004 	return NULL;
5005 
5006     if (!(pmark = StringToChars(pmarkObject, &todecref)))
5007 	return NULL;
5008 
5009     if (pmark[0] == '\0' || pmark[1] != '\0')
5010     {
5011 	PyErr_SET_STRING(PyExc_ValueError,
5012 		N_("mark name must be a single character"));
5013 	Py_XDECREF(todecref);
5014 	return NULL;
5015     }
5016 
5017     mark = *pmark;
5018 
5019     Py_XDECREF(todecref);
5020 
5021     VimTryStart();
5022     switch_buffer(&savebuf, self->buf);
5023     posp = getmark(mark, FALSE);
5024     restore_buffer(savebuf);
5025     if (VimTryEnd())
5026 	return NULL;
5027 
5028     if (posp == NULL)
5029     {
5030 	PyErr_SET_VIM(N_("invalid mark name"));
5031 	return NULL;
5032     }
5033 
5034     if (posp->lnum <= 0)
5035     {
5036 	/* Or raise an error? */
5037 	Py_INCREF(Py_None);
5038 	return Py_None;
5039     }
5040 
5041     return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5042 }
5043 
5044     static PyObject *
5045 BufferRange(BufferObject *self, PyObject *args)
5046 {
5047     PyInt start;
5048     PyInt end;
5049 
5050     if (CheckBuffer(self))
5051 	return NULL;
5052 
5053     if (!PyArg_ParseTuple(args, "nn", &start, &end))
5054 	return NULL;
5055 
5056     return RangeNew(self->buf, start, end);
5057 }
5058 
5059     static PyObject *
5060 BufferRepr(BufferObject *self)
5061 {
5062     if (self->buf == INVALID_BUFFER_VALUE)
5063 	return PyString_FromFormat("<buffer object (deleted) at %p>", self);
5064     else
5065     {
5066 	char	*name = (char *)self->buf->b_fname;
5067 
5068 	if (name == NULL)
5069 	    name = "";
5070 
5071 	return PyString_FromFormat("<buffer %s>", name);
5072     }
5073 }
5074 
5075 static struct PyMethodDef BufferMethods[] = {
5076     /* name,	    function,			calling,	documentation */
5077     {"append",	    (PyCFunction)BufferAppend,	METH_VARARGS,	"Append data to Vim buffer" },
5078     {"mark",	    (PyCFunction)BufferMark,	METH_O,		"Return (row,col) representing position of named mark" },
5079     {"range",	    (PyCFunction)BufferRange,	METH_VARARGS,	"Return a range object which represents the part of the given buffer between line numbers s and e" },
5080     {"__dir__",	    (PyCFunction)BufferDir,	METH_NOARGS,	""},
5081     { NULL,	    NULL,			0,		NULL}
5082 };
5083 
5084 /*
5085  * Buffer list object - Implementation
5086  */
5087 
5088 static PyTypeObject BufMapType;
5089 
5090 typedef struct
5091 {
5092     PyObject_HEAD
5093 } BufMapObject;
5094 
5095     static PyInt
5096 BufMapLength(PyObject *self UNUSED)
5097 {
5098     buf_T	*b = firstbuf;
5099     PyInt	n = 0;
5100 
5101     while (b)
5102     {
5103 	++n;
5104 	b = b->b_next;
5105     }
5106 
5107     return n;
5108 }
5109 
5110     static PyObject *
5111 BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5112 {
5113     buf_T	*b;
5114     long	bnr;
5115 
5116     if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
5117 	return NULL;
5118 
5119     b = buflist_findnr((int) bnr);
5120 
5121     if (b)
5122 	return BufferNew(b);
5123     else
5124     {
5125 	PyErr_SetObject(PyExc_KeyError, keyObject);
5126 	return NULL;
5127     }
5128 }
5129 
5130     static void
5131 BufMapIterDestruct(PyObject *buffer)
5132 {
5133     /* Iteration was stopped before all buffers were processed */
5134     if (buffer)
5135     {
5136 	Py_DECREF(buffer);
5137     }
5138 }
5139 
5140     static int
5141 BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5142 {
5143     if (buffer)
5144 	Py_VISIT(buffer);
5145     return 0;
5146 }
5147 
5148     static int
5149 BufMapIterClear(PyObject **buffer)
5150 {
5151     if (*buffer)
5152 	Py_CLEAR(*buffer);
5153     return 0;
5154 }
5155 
5156     static PyObject *
5157 BufMapIterNext(PyObject **buffer)
5158 {
5159     PyObject	*next;
5160     PyObject	*ret;
5161 
5162     if (!*buffer)
5163 	return NULL;
5164 
5165     ret = *buffer;
5166 
5167     if (CheckBuffer((BufferObject *)(ret)))
5168     {
5169 	*buffer = NULL;
5170 	return NULL;
5171     }
5172 
5173     if (!((BufferObject *)(ret))->buf->b_next)
5174 	next = NULL;
5175     else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
5176 	return NULL;
5177     *buffer = next;
5178     /* Do not increment reference: we no longer hold it (decref), but whoever
5179      * on other side will hold (incref). Decref+incref = nothing. */
5180     return ret;
5181 }
5182 
5183     static PyObject *
5184 BufMapIter(PyObject *self UNUSED)
5185 {
5186     PyObject *buffer;
5187 
5188     buffer = BufferNew(firstbuf);
5189     return IterNew(buffer,
5190 	    (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5191 	    (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
5192 }
5193 
5194 static PyMappingMethods BufMapAsMapping = {
5195     (lenfunc)       BufMapLength,
5196     (binaryfunc)    BufMapItem,
5197     (objobjargproc) 0,
5198 };
5199 
5200 /* Current items object
5201  */
5202 
5203 static char *CurrentAttrs[] = {
5204     "buffer", "window", "line", "range", "tabpage",
5205     NULL
5206 };
5207 
5208     static PyObject *
5209 CurrentDir(PyObject *self)
5210 {
5211     return ObjectDir(self, CurrentAttrs);
5212 }
5213 
5214     static PyObject *
5215 CurrentGetattr(PyObject *self UNUSED, char *name)
5216 {
5217     if (strcmp(name, "buffer") == 0)
5218 	return (PyObject *)BufferNew(curbuf);
5219     else if (strcmp(name, "window") == 0)
5220 	return (PyObject *)WindowNew(curwin, curtab);
5221     else if (strcmp(name, "tabpage") == 0)
5222 	return (PyObject *)TabPageNew(curtab);
5223     else if (strcmp(name, "line") == 0)
5224 	return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5225     else if (strcmp(name, "range") == 0)
5226 	return RangeNew(curbuf, RangeStart, RangeEnd);
5227     else if (strcmp(name, "__members__") == 0)
5228 	return ObjectDir(NULL, CurrentAttrs);
5229     else
5230 #if PY_MAJOR_VERSION < 3
5231 	return Py_FindMethod(WindowMethods, self, name);
5232 #else
5233 	return NULL;
5234 #endif
5235 }
5236 
5237     static int
5238 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
5239 {
5240     if (strcmp(name, "line") == 0)
5241     {
5242 	if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5243 			  NULL) == FAIL)
5244 	    return -1;
5245 
5246 	return 0;
5247     }
5248     else if (strcmp(name, "buffer") == 0)
5249     {
5250 	int count;
5251 
5252 	if (valObject->ob_type != &BufferType)
5253 	{
5254 	    PyErr_FORMAT(PyExc_TypeError,
5255 		    N_("expected vim.Buffer object, but got %s"),
5256 		    Py_TYPE_NAME(valObject));
5257 	    return -1;
5258 	}
5259 
5260 	if (CheckBuffer((BufferObject *)(valObject)))
5261 	    return -1;
5262 	count = ((BufferObject *)(valObject))->buf->b_fnum;
5263 
5264 	VimTryStart();
5265 	if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5266 	{
5267 	    if (VimTryEnd())
5268 		return -1;
5269 	    PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
5270 	    return -1;
5271 	}
5272 
5273 	return VimTryEnd();
5274     }
5275     else if (strcmp(name, "window") == 0)
5276     {
5277 	int count;
5278 
5279 	if (valObject->ob_type != &WindowType)
5280 	{
5281 	    PyErr_FORMAT(PyExc_TypeError,
5282 		    N_("expected vim.Window object, but got %s"),
5283 		    Py_TYPE_NAME(valObject));
5284 	    return -1;
5285 	}
5286 
5287 	if (CheckWindow((WindowObject *)(valObject)))
5288 	    return -1;
5289 	count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
5290 
5291 	if (!count)
5292 	{
5293 	    PyErr_SET_STRING(PyExc_ValueError,
5294 		    N_("failed to find window in the current tab page"));
5295 	    return -1;
5296 	}
5297 
5298 	VimTryStart();
5299 	win_goto(((WindowObject *)(valObject))->win);
5300 	if (((WindowObject *)(valObject))->win != curwin)
5301 	{
5302 	    if (VimTryEnd())
5303 		return -1;
5304 	    PyErr_SET_STRING(PyExc_RuntimeError,
5305 		    N_("did not switch to the specified window"));
5306 	    return -1;
5307 	}
5308 
5309 	return VimTryEnd();
5310     }
5311     else if (strcmp(name, "tabpage") == 0)
5312     {
5313 	if (valObject->ob_type != &TabPageType)
5314 	{
5315 	    PyErr_FORMAT(PyExc_TypeError,
5316 		    N_("expected vim.TabPage object, but got %s"),
5317 		    Py_TYPE_NAME(valObject));
5318 	    return -1;
5319 	}
5320 
5321 	if (CheckTabPage((TabPageObject *)(valObject)))
5322 	    return -1;
5323 
5324 	VimTryStart();
5325 	goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5326 	if (((TabPageObject *)(valObject))->tab != curtab)
5327 	{
5328 	    if (VimTryEnd())
5329 		return -1;
5330 	    PyErr_SET_STRING(PyExc_RuntimeError,
5331 		    N_("did not switch to the specified tab page"));
5332 	    return -1;
5333 	}
5334 
5335 	return VimTryEnd();
5336     }
5337     else
5338     {
5339 	PyErr_SetString(PyExc_AttributeError, name);
5340 	return -1;
5341     }
5342 }
5343 
5344 static struct PyMethodDef CurrentMethods[] = {
5345     /* name,	    function,			calling,	documentation */
5346     {"__dir__",	    (PyCFunction)CurrentDir,	METH_NOARGS,	""},
5347     { NULL,	    NULL,			0,		NULL}
5348 };
5349 
5350     static void
5351 init_range_cmd(exarg_T *eap)
5352 {
5353     RangeStart = eap->line1;
5354     RangeEnd = eap->line2;
5355 }
5356 
5357     static void
5358 init_range_eval(typval_T *rettv UNUSED)
5359 {
5360     RangeStart = (PyInt) curwin->w_cursor.lnum;
5361     RangeEnd = RangeStart;
5362 }
5363 
5364     static void
5365 run_cmd(const char *cmd, void *arg UNUSED
5366 #ifdef PY_CAN_RECURSE
5367 	, PyGILState_STATE *pygilstate UNUSED
5368 #endif
5369 	)
5370 {
5371     PyObject	*run_ret;
5372     run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5373     if (run_ret != NULL)
5374     {
5375 	Py_DECREF(run_ret);
5376     }
5377     else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5378     {
5379 	EMSG2(_(e_py_systemexit), "python");
5380 	PyErr_Clear();
5381     }
5382     else
5383 	PyErr_PrintEx(1);
5384 }
5385 
5386 static const char	*code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5387 static int		code_hdr_len = 30;
5388 
5389     static void
5390 run_do(const char *cmd, void *arg UNUSED
5391 #ifdef PY_CAN_RECURSE
5392 	, PyGILState_STATE *pygilstate
5393 #endif
5394 	)
5395 {
5396     PyInt	lnum;
5397     size_t	len;
5398     char	*code;
5399     int		status;
5400     PyObject	*pyfunc, *pymain;
5401     PyObject	*run_ret;
5402 
5403     if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
5404     {
5405 	EMSG(_("cannot save undo information"));
5406 	return;
5407     }
5408 
5409     len = code_hdr_len + STRLEN(cmd);
5410     code = PyMem_New(char, len + 1);
5411     memcpy(code, code_hdr, code_hdr_len);
5412     STRCPY(code + code_hdr_len, cmd);
5413     run_ret = PyRun_String(code, Py_file_input, globals, globals);
5414     status = -1;
5415     if (run_ret != NULL)
5416     {
5417 	status = 0;
5418 	Py_DECREF(run_ret);
5419     }
5420     else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5421     {
5422 	PyMem_Free(code);
5423 	EMSG2(_(e_py_systemexit), "python");
5424 	PyErr_Clear();
5425 	return;
5426     }
5427     else
5428 	PyErr_PrintEx(1);
5429 
5430     PyMem_Free(code);
5431 
5432     if (status)
5433     {
5434 	EMSG(_("failed to run the code"));
5435 	return;
5436     }
5437 
5438     status = 0;
5439     pymain = PyImport_AddModule("__main__");
5440     pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
5441 #ifdef PY_CAN_RECURSE
5442     PyGILState_Release(*pygilstate);
5443 #endif
5444 
5445     for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5446     {
5447 	PyObject	*line;
5448 	PyObject	*linenr;
5449 	PyObject	*ret;
5450 
5451 #ifdef PY_CAN_RECURSE
5452 	*pygilstate = PyGILState_Ensure();
5453 #endif
5454 	if (!(line = GetBufferLine(curbuf, lnum)))
5455 	    goto err;
5456 	if (!(linenr = PyInt_FromLong((long) lnum)))
5457 	{
5458 	    Py_DECREF(line);
5459 	    goto err;
5460 	}
5461 	ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5462 	Py_DECREF(line);
5463 	Py_DECREF(linenr);
5464 	if (!ret)
5465 	    goto err;
5466 
5467 	if (ret != Py_None)
5468 	    if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5469 		goto err;
5470 
5471 	Py_XDECREF(ret);
5472 	PythonIO_Flush();
5473 #ifdef PY_CAN_RECURSE
5474 	PyGILState_Release(*pygilstate);
5475 #endif
5476     }
5477     goto out;
5478 err:
5479 #ifdef PY_CAN_RECURSE
5480     *pygilstate = PyGILState_Ensure();
5481 #endif
5482     PyErr_PrintEx(0);
5483     PythonIO_Flush();
5484     status = 1;
5485 out:
5486 #ifdef PY_CAN_RECURSE
5487     if (!status)
5488 	*pygilstate = PyGILState_Ensure();
5489 #endif
5490     Py_DECREF(pyfunc);
5491     PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5492     if (status)
5493 	return;
5494     check_cursor();
5495     update_curbuf(NOT_VALID);
5496 }
5497 
5498     static void
5499 run_eval(const char *cmd, typval_T *rettv
5500 #ifdef PY_CAN_RECURSE
5501 	, PyGILState_STATE *pygilstate UNUSED
5502 #endif
5503 	)
5504 {
5505     PyObject	*run_ret;
5506 
5507     run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
5508     if (run_ret == NULL)
5509     {
5510 	if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5511 	{
5512 	    EMSG2(_(e_py_systemexit), "python");
5513 	    PyErr_Clear();
5514 	}
5515 	else
5516 	{
5517 	    if (PyErr_Occurred() && !msg_silent)
5518 		PyErr_PrintEx(0);
5519 	    EMSG(_("E858: Eval did not return a valid python object"));
5520 	}
5521     }
5522     else
5523     {
5524 	if (ConvertFromPyObject(run_ret, rettv) == -1)
5525 	    EMSG(_("E859: Failed to convert returned python object to vim value"));
5526 	Py_DECREF(run_ret);
5527     }
5528     PyErr_Clear();
5529 }
5530 
5531     static int
5532 set_ref_in_py(const int copyID)
5533 {
5534     pylinkedlist_T	*cur;
5535     dict_T	*dd;
5536     list_T	*ll;
5537     int		abort = FALSE;
5538 
5539     if (lastdict != NULL)
5540     {
5541 	for(cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
5542 	{
5543 	    dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5544 	    if (dd->dv_copyID != copyID)
5545 	    {
5546 		dd->dv_copyID = copyID;
5547 		abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
5548 	    }
5549 	}
5550     }
5551 
5552     if (lastlist != NULL)
5553     {
5554 	for(cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
5555 	{
5556 	    ll = ((ListObject *) (cur->pll_obj))->list;
5557 	    if (ll->lv_copyID != copyID)
5558 	    {
5559 		ll->lv_copyID = copyID;
5560 		abort = abort || set_ref_in_list(ll, copyID, NULL);
5561 	    }
5562 	}
5563     }
5564 
5565     return abort;
5566 }
5567 
5568     static int
5569 set_string_copy(char_u *str, typval_T *tv)
5570 {
5571     tv->vval.v_string = vim_strsave(str);
5572     if (tv->vval.v_string == NULL)
5573     {
5574 	PyErr_NoMemory();
5575 	return -1;
5576     }
5577     return 0;
5578 }
5579 
5580     static int
5581 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
5582 {
5583     dict_T	*dict;
5584     char_u	*key;
5585     dictitem_T	*di;
5586     PyObject	*keyObject;
5587     PyObject	*valObject;
5588     Py_ssize_t	iter = 0;
5589 
5590     if (!(dict = py_dict_alloc()))
5591 	return -1;
5592 
5593     tv->v_type = VAR_DICT;
5594     tv->vval.v_dict = dict;
5595 
5596     while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5597     {
5598 	PyObject	*todecref = NULL;
5599 
5600 	if (keyObject == NULL || valObject == NULL)
5601 	{
5602 	    dict_unref(dict);
5603 	    return -1;
5604 	}
5605 
5606 	if (!(key = StringToChars(keyObject, &todecref)))
5607 	{
5608 	    dict_unref(dict);
5609 	    return -1;
5610 	}
5611 
5612 	if (*key == NUL)
5613 	{
5614 	    dict_unref(dict);
5615 	    Py_XDECREF(todecref);
5616 	    RAISE_NO_EMPTY_KEYS;
5617 	    return -1;
5618 	}
5619 
5620 	di = dictitem_alloc(key);
5621 
5622 	Py_XDECREF(todecref);
5623 
5624 	if (di == NULL)
5625 	{
5626 	    PyErr_NoMemory();
5627 	    dict_unref(dict);
5628 	    return -1;
5629 	}
5630 	di->di_tv.v_lock = 0;
5631 
5632 	if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
5633 	{
5634 	    vim_free(di);
5635 	    dict_unref(dict);
5636 	    return -1;
5637 	}
5638 
5639 	if (dict_add(dict, di) == FAIL)
5640 	{
5641 	    RAISE_KEY_ADD_FAIL(di->di_key);
5642 	    clear_tv(&di->di_tv);
5643 	    vim_free(di);
5644 	    dict_unref(dict);
5645 	    return -1;
5646 	}
5647     }
5648 
5649     --dict->dv_refcount;
5650     return 0;
5651 }
5652 
5653     static int
5654 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
5655 {
5656     dict_T	*dict;
5657     char_u	*key;
5658     dictitem_T	*di;
5659     PyObject	*list;
5660     PyObject	*iterator;
5661     PyObject	*keyObject;
5662     PyObject	*valObject;
5663 
5664     if (!(dict = py_dict_alloc()))
5665 	return -1;
5666 
5667     tv->v_type = VAR_DICT;
5668     tv->vval.v_dict = dict;
5669 
5670     if (!(list = PyMapping_Keys(obj)))
5671     {
5672 	dict_unref(dict);
5673 	return -1;
5674     }
5675 
5676     if (!(iterator = PyObject_GetIter(list)))
5677     {
5678 	dict_unref(dict);
5679 	Py_DECREF(list);
5680 	return -1;
5681     }
5682     Py_DECREF(list);
5683 
5684     while ((keyObject = PyIter_Next(iterator)))
5685     {
5686 	PyObject	*todecref;
5687 
5688 	if (!(key = StringToChars(keyObject, &todecref)))
5689 	{
5690 	    Py_DECREF(keyObject);
5691 	    Py_DECREF(iterator);
5692 	    dict_unref(dict);
5693 	    return -1;
5694 	}
5695 
5696 	if (*key == NUL)
5697 	{
5698 	    Py_DECREF(keyObject);
5699 	    Py_DECREF(iterator);
5700 	    Py_XDECREF(todecref);
5701 	    dict_unref(dict);
5702 	    RAISE_NO_EMPTY_KEYS;
5703 	    return -1;
5704 	}
5705 
5706 	if (!(valObject = PyObject_GetItem(obj, keyObject)))
5707 	{
5708 	    Py_DECREF(keyObject);
5709 	    Py_DECREF(iterator);
5710 	    Py_XDECREF(todecref);
5711 	    dict_unref(dict);
5712 	    return -1;
5713 	}
5714 
5715 	di = dictitem_alloc(key);
5716 
5717 	Py_DECREF(keyObject);
5718 	Py_XDECREF(todecref);
5719 
5720 	if (di == NULL)
5721 	{
5722 	    Py_DECREF(iterator);
5723 	    Py_DECREF(valObject);
5724 	    dict_unref(dict);
5725 	    PyErr_NoMemory();
5726 	    return -1;
5727 	}
5728 	di->di_tv.v_lock = 0;
5729 
5730 	if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
5731 	{
5732 	    Py_DECREF(iterator);
5733 	    Py_DECREF(valObject);
5734 	    vim_free(di);
5735 	    dict_unref(dict);
5736 	    return -1;
5737 	}
5738 
5739 	Py_DECREF(valObject);
5740 
5741 	if (dict_add(dict, di) == FAIL)
5742 	{
5743 	    RAISE_KEY_ADD_FAIL(di->di_key);
5744 	    Py_DECREF(iterator);
5745 	    dictitem_free(di);
5746 	    dict_unref(dict);
5747 	    return -1;
5748 	}
5749     }
5750     Py_DECREF(iterator);
5751     --dict->dv_refcount;
5752     return 0;
5753 }
5754 
5755     static int
5756 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
5757 {
5758     list_T	*l;
5759 
5760     if (!(l = py_list_alloc()))
5761 	return -1;
5762 
5763     tv->v_type = VAR_LIST;
5764     tv->vval.v_list = l;
5765 
5766     if (list_py_concat(l, obj, lookup_dict) == -1)
5767     {
5768 	list_unref(l);
5769 	return -1;
5770     }
5771 
5772     --l->lv_refcount;
5773     return 0;
5774 }
5775 
5776 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5777 
5778     static int
5779 convert_dl(PyObject *obj, typval_T *tv,
5780 				    pytotvfunc py_to_tv, PyObject *lookup_dict)
5781 {
5782     PyObject	*capsule;
5783     char	hexBuf[sizeof(void *) * 2 + 3];
5784 
5785     sprintf(hexBuf, "%p", obj);
5786 
5787 # ifdef PY_USE_CAPSULE
5788     capsule = PyDict_GetItemString(lookup_dict, hexBuf);
5789 # else
5790     capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
5791 # endif
5792     if (capsule == NULL)
5793     {
5794 # ifdef PY_USE_CAPSULE
5795 	capsule = PyCapsule_New(tv, NULL, NULL);
5796 # else
5797 	capsule = PyCObject_FromVoidPtr(tv, NULL);
5798 # endif
5799 	if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5800 	{
5801 	    Py_DECREF(capsule);
5802 	    tv->v_type = VAR_UNKNOWN;
5803 	    return -1;
5804 	}
5805 
5806 	Py_DECREF(capsule);
5807 
5808 	if (py_to_tv(obj, tv, lookup_dict) == -1)
5809 	{
5810 	    tv->v_type = VAR_UNKNOWN;
5811 	    return -1;
5812 	}
5813 	/* As we are not using copy_tv which increments reference count we must
5814 	 * do it ourself. */
5815 	switch(tv->v_type)
5816 	{
5817 	    case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5818 	    case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5819 	}
5820     }
5821     else
5822     {
5823 	typval_T	*v;
5824 
5825 # ifdef PY_USE_CAPSULE
5826 	v = PyCapsule_GetPointer(capsule, NULL);
5827 # else
5828 	v = PyCObject_AsVoidPtr(capsule);
5829 # endif
5830 	copy_tv(v, tv);
5831     }
5832     return 0;
5833 }
5834 
5835     static int
5836 ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5837 {
5838     PyObject	*lookup_dict;
5839     int		ret;
5840 
5841     if (!(lookup_dict = PyDict_New()))
5842 	return -1;
5843 
5844     if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5845     {
5846 	tv->v_type = VAR_DICT;
5847 	tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5848 	++tv->vval.v_dict->dv_refcount;
5849 	ret = 0;
5850     }
5851     else if (PyDict_Check(obj))
5852 	ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5853     else if (PyMapping_Check(obj))
5854 	ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5855     else
5856     {
5857 	PyErr_FORMAT(PyExc_TypeError,
5858 		N_("unable to convert %s to vim dictionary"),
5859 		Py_TYPE_NAME(obj));
5860 	ret = -1;
5861     }
5862     Py_DECREF(lookup_dict);
5863     return ret;
5864 }
5865 
5866     static int
5867 ConvertFromPyObject(PyObject *obj, typval_T *tv)
5868 {
5869     PyObject	*lookup_dict;
5870     int		ret;
5871 
5872     if (!(lookup_dict = PyDict_New()))
5873 	return -1;
5874     ret = _ConvertFromPyObject(obj, tv, lookup_dict);
5875     Py_DECREF(lookup_dict);
5876     return ret;
5877 }
5878 
5879     static int
5880 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
5881 {
5882     if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5883     {
5884 	tv->v_type = VAR_DICT;
5885 	tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5886 	++tv->vval.v_dict->dv_refcount;
5887     }
5888     else if (PyType_IsSubtype(obj->ob_type, &ListType))
5889     {
5890 	tv->v_type = VAR_LIST;
5891 	tv->vval.v_list = (((ListObject *)(obj))->list);
5892 	++tv->vval.v_list->lv_refcount;
5893     }
5894     else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
5895     {
5896 	if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5897 	    return -1;
5898 
5899 	tv->v_type = VAR_FUNC;
5900 	func_ref(tv->vval.v_string);
5901     }
5902     else if (PyBytes_Check(obj))
5903     {
5904 	char_u	*str;
5905 
5906 	if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
5907 	    return -1;
5908 	if (str == NULL)
5909 	    return -1;
5910 
5911 	if (set_string_copy(str, tv) == -1)
5912 	    return -1;
5913 
5914 	tv->v_type = VAR_STRING;
5915     }
5916     else if (PyUnicode_Check(obj))
5917     {
5918 	PyObject	*bytes;
5919 	char_u	*str;
5920 
5921 	bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
5922 	if (bytes == NULL)
5923 	    return -1;
5924 
5925 	if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
5926 	    return -1;
5927 	if (str == NULL)
5928 	    return -1;
5929 
5930 	if (set_string_copy(str, tv))
5931 	{
5932 	    Py_XDECREF(bytes);
5933 	    return -1;
5934 	}
5935 	Py_XDECREF(bytes);
5936 
5937 	tv->v_type = VAR_STRING;
5938     }
5939 #if PY_MAJOR_VERSION < 3
5940     else if (PyInt_Check(obj))
5941     {
5942 	tv->v_type = VAR_NUMBER;
5943 	tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
5944 	if (PyErr_Occurred())
5945 	    return -1;
5946     }
5947 #endif
5948     else if (PyLong_Check(obj))
5949     {
5950 	tv->v_type = VAR_NUMBER;
5951 	tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
5952 	if (PyErr_Occurred())
5953 	    return -1;
5954     }
5955     else if (PyDict_Check(obj))
5956 	return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5957 #ifdef FEAT_FLOAT
5958     else if (PyFloat_Check(obj))
5959     {
5960 	tv->v_type = VAR_FLOAT;
5961 	tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5962     }
5963 #endif
5964     else if (PyObject_HasAttrString(obj, "keys"))
5965 	return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5966     /* PyObject_GetIter can create built-in iterator for any sequence object */
5967     else if (PyIter_Check(obj) || PySequence_Check(obj))
5968 	return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
5969     else if (PyMapping_Check(obj))
5970 	return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5971     else if (PyNumber_Check(obj))
5972     {
5973 	PyObject	*num;
5974 
5975 	if (!(num = PyNumber_Long(obj)))
5976 	    return -1;
5977 
5978 	tv->v_type = VAR_NUMBER;
5979 	tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5980 
5981 	Py_DECREF(num);
5982     }
5983     else
5984     {
5985 	PyErr_FORMAT(PyExc_TypeError,
5986 		N_("unable to convert %s to vim structure"),
5987 		Py_TYPE_NAME(obj));
5988 	return -1;
5989     }
5990     return 0;
5991 }
5992 
5993     static PyObject *
5994 ConvertToPyObject(typval_T *tv)
5995 {
5996     if (tv == NULL)
5997     {
5998 	PyErr_SET_VIM(N_("internal error: NULL reference passed"));
5999 	return NULL;
6000     }
6001     switch (tv->v_type)
6002     {
6003 	case VAR_STRING:
6004 	    return PyBytes_FromString(tv->vval.v_string == NULL
6005 					    ? "" : (char *)tv->vval.v_string);
6006 	case VAR_NUMBER:
6007 	    return PyLong_FromLong((long) tv->vval.v_number);
6008 #ifdef FEAT_FLOAT
6009 	case VAR_FLOAT:
6010 	    return PyFloat_FromDouble((double) tv->vval.v_float);
6011 #endif
6012 	case VAR_LIST:
6013 	    return NEW_LIST(tv->vval.v_list);
6014 	case VAR_DICT:
6015 	    return NEW_DICTIONARY(tv->vval.v_dict);
6016 	case VAR_FUNC:
6017 	    return NEW_FUNCTION(tv->vval.v_string == NULL
6018 					  ? (char_u *)"" : tv->vval.v_string);
6019 	case VAR_UNKNOWN:
6020 	    Py_INCREF(Py_None);
6021 	    return Py_None;
6022 	default:
6023 	    PyErr_SET_VIM(N_("internal error: invalid value type"));
6024 	    return NULL;
6025     }
6026 }
6027 
6028 typedef struct
6029 {
6030     PyObject_HEAD
6031 } CurrentObject;
6032 static PyTypeObject CurrentType;
6033 
6034     static void
6035 init_structs(void)
6036 {
6037     vim_memset(&OutputType, 0, sizeof(OutputType));
6038     OutputType.tp_name = "vim.message";
6039     OutputType.tp_basicsize = sizeof(OutputObject);
6040     OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6041     OutputType.tp_doc = "vim message object";
6042     OutputType.tp_methods = OutputMethods;
6043 #if PY_MAJOR_VERSION >= 3
6044     OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6045     OutputType.tp_setattro = (setattrofunc)OutputSetattro;
6046     OutputType.tp_alloc = call_PyType_GenericAlloc;
6047     OutputType.tp_new = call_PyType_GenericNew;
6048     OutputType.tp_free = call_PyObject_Free;
6049 #else
6050     OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6051     OutputType.tp_setattr = (setattrfunc)OutputSetattr;
6052 #endif
6053 
6054     vim_memset(&IterType, 0, sizeof(IterType));
6055     IterType.tp_name = "vim.iter";
6056     IterType.tp_basicsize = sizeof(IterObject);
6057     IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
6058     IterType.tp_doc = "generic iterator object";
6059     IterType.tp_iter = (getiterfunc)IterIter;
6060     IterType.tp_iternext = (iternextfunc)IterNext;
6061     IterType.tp_dealloc = (destructor)IterDestructor;
6062     IterType.tp_traverse = (traverseproc)IterTraverse;
6063     IterType.tp_clear = (inquiry)IterClear;
6064 
6065     vim_memset(&BufferType, 0, sizeof(BufferType));
6066     BufferType.tp_name = "vim.buffer";
6067     BufferType.tp_basicsize = sizeof(BufferType);
6068     BufferType.tp_dealloc = (destructor)BufferDestructor;
6069     BufferType.tp_repr = (reprfunc)BufferRepr;
6070     BufferType.tp_as_sequence = &BufferAsSeq;
6071     BufferType.tp_as_mapping = &BufferAsMapping;
6072     BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6073     BufferType.tp_doc = "vim buffer object";
6074     BufferType.tp_methods = BufferMethods;
6075 #if PY_MAJOR_VERSION >= 3
6076     BufferType.tp_getattro = (getattrofunc)BufferGetattro;
6077     BufferType.tp_setattro = (setattrofunc)BufferSetattro;
6078     BufferType.tp_alloc = call_PyType_GenericAlloc;
6079     BufferType.tp_new = call_PyType_GenericNew;
6080     BufferType.tp_free = call_PyObject_Free;
6081 #else
6082     BufferType.tp_getattr = (getattrfunc)BufferGetattr;
6083     BufferType.tp_setattr = (setattrfunc)BufferSetattr;
6084 #endif
6085 
6086     vim_memset(&WindowType, 0, sizeof(WindowType));
6087     WindowType.tp_name = "vim.window";
6088     WindowType.tp_basicsize = sizeof(WindowObject);
6089     WindowType.tp_dealloc = (destructor)WindowDestructor;
6090     WindowType.tp_repr = (reprfunc)WindowRepr;
6091     WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
6092     WindowType.tp_doc = "vim Window object";
6093     WindowType.tp_methods = WindowMethods;
6094     WindowType.tp_traverse = (traverseproc)WindowTraverse;
6095     WindowType.tp_clear = (inquiry)WindowClear;
6096 #if PY_MAJOR_VERSION >= 3
6097     WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6098     WindowType.tp_setattro = (setattrofunc)WindowSetattro;
6099     WindowType.tp_alloc = call_PyType_GenericAlloc;
6100     WindowType.tp_new = call_PyType_GenericNew;
6101     WindowType.tp_free = call_PyObject_Free;
6102 #else
6103     WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6104     WindowType.tp_setattr = (setattrfunc)WindowSetattr;
6105 #endif
6106 
6107     vim_memset(&TabPageType, 0, sizeof(TabPageType));
6108     TabPageType.tp_name = "vim.tabpage";
6109     TabPageType.tp_basicsize = sizeof(TabPageObject);
6110     TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6111     TabPageType.tp_repr = (reprfunc)TabPageRepr;
6112     TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6113     TabPageType.tp_doc = "vim tab page object";
6114     TabPageType.tp_methods = TabPageMethods;
6115 #if PY_MAJOR_VERSION >= 3
6116     TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
6117     TabPageType.tp_alloc = call_PyType_GenericAlloc;
6118     TabPageType.tp_new = call_PyType_GenericNew;
6119     TabPageType.tp_free = call_PyObject_Free;
6120 #else
6121     TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
6122 #endif
6123 
6124     vim_memset(&BufMapType, 0, sizeof(BufMapType));
6125     BufMapType.tp_name = "vim.bufferlist";
6126     BufMapType.tp_basicsize = sizeof(BufMapObject);
6127     BufMapType.tp_as_mapping = &BufMapAsMapping;
6128     BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
6129     BufMapType.tp_iter = BufMapIter;
6130     BufferType.tp_doc = "vim buffer list";
6131 
6132     vim_memset(&WinListType, 0, sizeof(WinListType));
6133     WinListType.tp_name = "vim.windowlist";
6134     WinListType.tp_basicsize = sizeof(WinListType);
6135     WinListType.tp_as_sequence = &WinListAsSeq;
6136     WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6137     WinListType.tp_doc = "vim window list";
6138     WinListType.tp_dealloc = (destructor)WinListDestructor;
6139 
6140     vim_memset(&TabListType, 0, sizeof(TabListType));
6141     TabListType.tp_name = "vim.tabpagelist";
6142     TabListType.tp_basicsize = sizeof(TabListType);
6143     TabListType.tp_as_sequence = &TabListAsSeq;
6144     TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6145     TabListType.tp_doc = "vim tab page list";
6146 
6147     vim_memset(&RangeType, 0, sizeof(RangeType));
6148     RangeType.tp_name = "vim.range";
6149     RangeType.tp_basicsize = sizeof(RangeObject);
6150     RangeType.tp_dealloc = (destructor)RangeDestructor;
6151     RangeType.tp_repr = (reprfunc)RangeRepr;
6152     RangeType.tp_as_sequence = &RangeAsSeq;
6153     RangeType.tp_as_mapping = &RangeAsMapping;
6154     RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
6155     RangeType.tp_doc = "vim Range object";
6156     RangeType.tp_methods = RangeMethods;
6157     RangeType.tp_traverse = (traverseproc)RangeTraverse;
6158     RangeType.tp_clear = (inquiry)RangeClear;
6159 #if PY_MAJOR_VERSION >= 3
6160     RangeType.tp_getattro = (getattrofunc)RangeGetattro;
6161     RangeType.tp_alloc = call_PyType_GenericAlloc;
6162     RangeType.tp_new = call_PyType_GenericNew;
6163     RangeType.tp_free = call_PyObject_Free;
6164 #else
6165     RangeType.tp_getattr = (getattrfunc)RangeGetattr;
6166 #endif
6167 
6168     vim_memset(&CurrentType, 0, sizeof(CurrentType));
6169     CurrentType.tp_name = "vim.currentdata";
6170     CurrentType.tp_basicsize = sizeof(CurrentObject);
6171     CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6172     CurrentType.tp_doc = "vim current object";
6173     CurrentType.tp_methods = CurrentMethods;
6174 #if PY_MAJOR_VERSION >= 3
6175     CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6176     CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
6177 #else
6178     CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6179     CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
6180 #endif
6181 
6182     vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6183     DictionaryType.tp_name = "vim.dictionary";
6184     DictionaryType.tp_basicsize = sizeof(DictionaryObject);
6185     DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
6186     DictionaryType.tp_as_sequence = &DictionaryAsSeq;
6187     DictionaryType.tp_as_mapping = &DictionaryAsMapping;
6188     DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
6189     DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6190     DictionaryType.tp_methods = DictionaryMethods;
6191     DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6192     DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6193     DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
6194 #if PY_MAJOR_VERSION >= 3
6195     DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6196     DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
6197 #else
6198     DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6199     DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
6200 #endif
6201 
6202     vim_memset(&ListType, 0, sizeof(ListType));
6203     ListType.tp_name = "vim.list";
6204     ListType.tp_dealloc = (destructor)ListDestructor;
6205     ListType.tp_basicsize = sizeof(ListObject);
6206     ListType.tp_as_sequence = &ListAsSeq;
6207     ListType.tp_as_mapping = &ListAsMapping;
6208     ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
6209     ListType.tp_doc = "list pushing modifications to vim structure";
6210     ListType.tp_methods = ListMethods;
6211     ListType.tp_iter = (getiterfunc)ListIter;
6212     ListType.tp_new = (newfunc)ListConstructor;
6213     ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
6214 #if PY_MAJOR_VERSION >= 3
6215     ListType.tp_getattro = (getattrofunc)ListGetattro;
6216     ListType.tp_setattro = (setattrofunc)ListSetattro;
6217 #else
6218     ListType.tp_getattr = (getattrfunc)ListGetattr;
6219     ListType.tp_setattr = (setattrfunc)ListSetattr;
6220 #endif
6221 
6222     vim_memset(&FunctionType, 0, sizeof(FunctionType));
6223     FunctionType.tp_name = "vim.function";
6224     FunctionType.tp_basicsize = sizeof(FunctionObject);
6225     FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6226     FunctionType.tp_call = (ternaryfunc)FunctionCall;
6227     FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
6228     FunctionType.tp_doc = "object that calls vim function";
6229     FunctionType.tp_methods = FunctionMethods;
6230     FunctionType.tp_repr = (reprfunc)FunctionRepr;
6231     FunctionType.tp_new = (newfunc)FunctionConstructor;
6232     FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
6233 #if PY_MAJOR_VERSION >= 3
6234     FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
6235 #else
6236     FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
6237 #endif
6238 
6239     vim_memset(&OptionsType, 0, sizeof(OptionsType));
6240     OptionsType.tp_name = "vim.options";
6241     OptionsType.tp_basicsize = sizeof(OptionsObject);
6242     OptionsType.tp_as_sequence = &OptionsAsSeq;
6243     OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
6244     OptionsType.tp_doc = "object for manipulating options";
6245     OptionsType.tp_iter = (getiterfunc)OptionsIter;
6246     OptionsType.tp_as_mapping = &OptionsAsMapping;
6247     OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6248     OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6249     OptionsType.tp_clear = (inquiry)OptionsClear;
6250 
6251     vim_memset(&LoaderType, 0, sizeof(LoaderType));
6252     LoaderType.tp_name = "vim.Loader";
6253     LoaderType.tp_basicsize = sizeof(LoaderObject);
6254     LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6255     LoaderType.tp_doc = "vim message object";
6256     LoaderType.tp_methods = LoaderMethods;
6257     LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6258 
6259 #if PY_MAJOR_VERSION >= 3
6260     vim_memset(&vimmodule, 0, sizeof(vimmodule));
6261     vimmodule.m_name = "vim";
6262     vimmodule.m_doc = "Vim Python interface\n";
6263     vimmodule.m_size = -1;
6264     vimmodule.m_methods = VimMethods;
6265 #endif
6266 }
6267 
6268 #define PYTYPE_READY(type) \
6269     if (PyType_Ready(&type)) \
6270 	return -1;
6271 
6272     static int
6273 init_types(void)
6274 {
6275     PYTYPE_READY(IterType);
6276     PYTYPE_READY(BufferType);
6277     PYTYPE_READY(RangeType);
6278     PYTYPE_READY(WindowType);
6279     PYTYPE_READY(TabPageType);
6280     PYTYPE_READY(BufMapType);
6281     PYTYPE_READY(WinListType);
6282     PYTYPE_READY(TabListType);
6283     PYTYPE_READY(CurrentType);
6284     PYTYPE_READY(DictionaryType);
6285     PYTYPE_READY(ListType);
6286     PYTYPE_READY(FunctionType);
6287     PYTYPE_READY(OptionsType);
6288     PYTYPE_READY(OutputType);
6289     PYTYPE_READY(LoaderType);
6290     return 0;
6291 }
6292 
6293     static int
6294 init_sys_path(void)
6295 {
6296     PyObject	*path;
6297     PyObject	*path_hook;
6298     PyObject	*path_hooks;
6299 
6300     if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6301 	return -1;
6302 
6303     if (!(path_hooks = PySys_GetObject("path_hooks")))
6304     {
6305 	PyErr_Clear();
6306 	path_hooks = PyList_New(1);
6307 	PyList_SET_ITEM(path_hooks, 0, path_hook);
6308 	if (PySys_SetObject("path_hooks", path_hooks))
6309 	{
6310 	    Py_DECREF(path_hooks);
6311 	    return -1;
6312 	}
6313 	Py_DECREF(path_hooks);
6314     }
6315     else if (PyList_Check(path_hooks))
6316     {
6317 	if (PyList_Append(path_hooks, path_hook))
6318 	{
6319 	    Py_DECREF(path_hook);
6320 	    return -1;
6321 	}
6322 	Py_DECREF(path_hook);
6323     }
6324     else
6325     {
6326 	VimTryStart();
6327 	EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6328 	       "You should now do the following:\n"
6329 	       "- append vim.path_hook to sys.path_hooks\n"
6330 	       "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6331 	VimTryEnd(); /* Discard the error */
6332 	Py_DECREF(path_hook);
6333 	return 0;
6334     }
6335 
6336     if (!(path = PySys_GetObject("path")))
6337     {
6338 	PyErr_Clear();
6339 	path = PyList_New(1);
6340 	Py_INCREF(vim_special_path_object);
6341 	PyList_SET_ITEM(path, 0, vim_special_path_object);
6342 	if (PySys_SetObject("path", path))
6343 	{
6344 	    Py_DECREF(path);
6345 	    return -1;
6346 	}
6347 	Py_DECREF(path);
6348     }
6349     else if (PyList_Check(path))
6350     {
6351 	if (PyList_Append(path, vim_special_path_object))
6352 	    return -1;
6353     }
6354     else
6355     {
6356 	VimTryStart();
6357 	EMSG(_("Failed to set path: sys.path is not a list\n"
6358 	       "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6359 	VimTryEnd(); /* Discard the error */
6360     }
6361 
6362     return 0;
6363 }
6364 
6365 static BufMapObject TheBufferMap =
6366 {
6367     PyObject_HEAD_INIT(&BufMapType)
6368 };
6369 
6370 static WinListObject TheWindowList =
6371 {
6372     PyObject_HEAD_INIT(&WinListType)
6373     NULL
6374 };
6375 
6376 static CurrentObject TheCurrent =
6377 {
6378     PyObject_HEAD_INIT(&CurrentType)
6379 };
6380 
6381 static TabListObject TheTabPageList =
6382 {
6383     PyObject_HEAD_INIT(&TabListType)
6384 };
6385 
6386 static struct numeric_constant {
6387     char	*name;
6388     int		val;
6389 } numeric_constants[] = {
6390     {"VAR_LOCKED",	VAR_LOCKED},
6391     {"VAR_FIXED",	VAR_FIXED},
6392     {"VAR_SCOPE",	VAR_SCOPE},
6393     {"VAR_DEF_SCOPE",	VAR_DEF_SCOPE},
6394 };
6395 
6396 static struct object_constant {
6397     char	*name;
6398     PyObject	*valObject;
6399 } object_constants[] = {
6400     {"buffers",  (PyObject *)(void *)&TheBufferMap},
6401     {"windows",  (PyObject *)(void *)&TheWindowList},
6402     {"tabpages", (PyObject *)(void *)&TheTabPageList},
6403     {"current",  (PyObject *)(void *)&TheCurrent},
6404 
6405     {"Buffer",     (PyObject *)&BufferType},
6406     {"Range",      (PyObject *)&RangeType},
6407     {"Window",     (PyObject *)&WindowType},
6408     {"TabPage",    (PyObject *)&TabPageType},
6409     {"Dictionary", (PyObject *)&DictionaryType},
6410     {"List",       (PyObject *)&ListType},
6411     {"Function",   (PyObject *)&FunctionType},
6412     {"Options",    (PyObject *)&OptionsType},
6413     {"_Loader",    (PyObject *)&LoaderType},
6414 };
6415 
6416 #define ADD_OBJECT(m, name, obj) \
6417     if (PyModule_AddObject(m, name, obj)) \
6418 	return -1;
6419 
6420 #define ADD_CHECKED_OBJECT(m, name, obj) \
6421     { \
6422 	PyObject	*valObject = obj; \
6423 	if (!valObject) \
6424 	    return -1; \
6425 	ADD_OBJECT(m, name, valObject); \
6426     }
6427 
6428     static int
6429 populate_module(PyObject *m)
6430 {
6431     int		i;
6432     PyObject	*other_module;
6433     PyObject	*attr;
6434     PyObject	*imp;
6435 
6436     for (i = 0; i < (int)(sizeof(numeric_constants)
6437 					   / sizeof(struct numeric_constant));
6438 	    ++i)
6439 	ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
6440 		PyInt_FromLong(numeric_constants[i].val));
6441 
6442     for (i = 0; i < (int)(sizeof(object_constants)
6443 					    / sizeof(struct object_constant));
6444 	    ++i)
6445     {
6446 	PyObject	*valObject;
6447 
6448 	valObject = object_constants[i].valObject;
6449 	Py_INCREF(valObject);
6450 	ADD_OBJECT(m, object_constants[i].name, valObject);
6451     }
6452 
6453     if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6454 	return -1;
6455     ADD_OBJECT(m, "error", VimError);
6456 
6457     ADD_CHECKED_OBJECT(m, "vars",  NEW_DICTIONARY(&globvardict));
6458     ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
6459     ADD_CHECKED_OBJECT(m, "options",
6460 	    OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
6461 
6462     if (!(other_module = PyImport_ImportModule("os")))
6463 	return -1;
6464     ADD_OBJECT(m, "os", other_module);
6465 
6466     if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
6467 	return -1;
6468     ADD_OBJECT(m, "_getcwd", py_getcwd)
6469 
6470     if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
6471 	return -1;
6472     ADD_OBJECT(m, "_chdir", py_chdir);
6473     if (!(attr = PyObject_GetAttrString(m, "chdir")))
6474 	return -1;
6475     if (PyObject_SetAttrString(other_module, "chdir", attr))
6476     {
6477 	Py_DECREF(attr);
6478 	return -1;
6479     }
6480     Py_DECREF(attr);
6481 
6482     if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
6483     {
6484 	ADD_OBJECT(m, "_fchdir", py_fchdir);
6485 	if (!(attr = PyObject_GetAttrString(m, "fchdir")))
6486 	    return -1;
6487 	if (PyObject_SetAttrString(other_module, "fchdir", attr))
6488 	{
6489 	    Py_DECREF(attr);
6490 	    return -1;
6491 	}
6492 	Py_DECREF(attr);
6493     }
6494     else
6495 	PyErr_Clear();
6496 
6497     if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6498 	return -1;
6499 
6500     ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6501 
6502     if (!(imp = PyImport_ImportModule("imp")))
6503 	return -1;
6504 
6505     if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6506     {
6507 	Py_DECREF(imp);
6508 	return -1;
6509     }
6510 
6511     if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6512     {
6513 	Py_DECREF(py_find_module);
6514 	Py_DECREF(imp);
6515 	return -1;
6516     }
6517 
6518     Py_DECREF(imp);
6519 
6520     ADD_OBJECT(m, "_find_module", py_find_module);
6521     ADD_OBJECT(m, "_load_module", py_load_module);
6522 
6523     return 0;
6524 }
6525