xref: /vim-8.2.3635/src/if_python.c (revision 0ee8df9c)
1 /* vi:set ts=8 sts=4 sw=4:
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.
11  * Changes for Unix by David Leonard.
12  *
13  * This consists of four parts:
14  * 1. Python interpreter main program
15  * 2. Python output stream: writes output via [e]msg().
16  * 3. Implementation of the Vim module for Python
17  * 4. Utility functions for handling the interface between Vim and Python.
18  */
19 
20 #include "vim.h"
21 
22 #include <limits.h>
23 
24 /* Python.h defines _POSIX_THREADS itself (if needed) */
25 #ifdef _POSIX_THREADS
26 # undef _POSIX_THREADS
27 #endif
28 
29 #if defined(_WIN32) && defined(HAVE_FCNTL_H)
30 # undef HAVE_FCNTL_H
31 #endif
32 
33 #ifdef _DEBUG
34 # undef _DEBUG
35 #endif
36 
37 #ifdef HAVE_STDARG_H
38 # undef HAVE_STDARG_H	/* Python's config.h defines it as well. */
39 #endif
40 #ifdef _POSIX_C_SOURCE
41 # undef _POSIX_C_SOURCE	/* pyconfig.h defines it as well. */
42 #endif
43 #ifdef _XOPEN_SOURCE
44 # undef _XOPEN_SOURCE	/* pyconfig.h defines it as well. */
45 #endif
46 
47 #define PY_SSIZE_T_CLEAN
48 
49 #include <Python.h>
50 #if defined(MACOS) && !defined(MACOS_X_UNIX)
51 # include "macglue.h"
52 # include <CodeFragments.h>
53 #endif
54 #undef main /* Defined in python.h - aargh */
55 #undef HAVE_FCNTL_H /* Clash with os_win32.h */
56 
57 static void init_structs(void);
58 
59 #if !defined(FEAT_PYTHON) && defined(PROTO)
60 /* Use this to be able to generate prototypes without python being used. */
61 # define PyObject Py_ssize_t
62 # define PyThreadState Py_ssize_t
63 # define PyTypeObject Py_ssize_t
64 struct PyMethodDef { Py_ssize_t a; };
65 # define PySequenceMethods Py_ssize_t
66 #endif
67 
68 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
69 # define PyInt Py_ssize_t
70 # define PyInquiry lenfunc
71 # define PyIntArgFunc ssizeargfunc
72 # define PyIntIntArgFunc ssizessizeargfunc
73 # define PyIntObjArgProc ssizeobjargproc
74 # define PyIntIntObjArgProc ssizessizeobjargproc
75 # define Py_ssize_t_fmt "n"
76 #else
77 # define PyInt int
78 # define PyInquiry inquiry
79 # define PyIntArgFunc intargfunc
80 # define PyIntIntArgFunc intintargfunc
81 # define PyIntObjArgProc intobjargproc
82 # define PyIntIntObjArgProc intintobjargproc
83 # define Py_ssize_t_fmt "i"
84 #endif
85 
86 /* Parser flags */
87 #define single_input	256
88 #define file_input	257
89 #define eval_input	258
90 
91 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
92   /* Python 2.3: can invoke ":python" recursively. */
93 # define PY_CAN_RECURSE
94 #endif
95 
96 # if defined(DYNAMIC_PYTHON) || defined(PROTO)
97 #  ifndef DYNAMIC_PYTHON
98 #   define HINSTANCE long_u		/* for generating prototypes */
99 #  endif
100 
101 # ifndef WIN3264
102 #  include <dlfcn.h>
103 #  define FARPROC void*
104 #  define HINSTANCE void*
105 #  ifdef FEAT_PYTHON3
106    /* Don't use RTLD_GLOBAL, it may cause a crash if both :python and :py3 are
107     * used. But without it importing may fail, e.g., for termios. */
108 #   define load_dll(n) dlopen((n), RTLD_LAZY)
109 #  else
110 #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
111 #  endif
112 #  define close_dll dlclose
113 #  define symbol_from_dll dlsym
114 # else
115 #  define load_dll LoadLibrary
116 #  define close_dll FreeLibrary
117 #  define symbol_from_dll GetProcAddress
118 # endif
119 
120 /* This makes if_python.c compile without warnings against Python 2.5
121  * on Win32 and Win64. */
122 # undef PyRun_SimpleString
123 # undef PyArg_Parse
124 # undef PyArg_ParseTuple
125 # undef Py_BuildValue
126 # undef Py_InitModule4
127 # undef Py_InitModule4_64
128 
129 /*
130  * Wrapper defines
131  */
132 # define PyArg_Parse dll_PyArg_Parse
133 # define PyArg_ParseTuple dll_PyArg_ParseTuple
134 # define PyDict_SetItemString dll_PyDict_SetItemString
135 # define PyErr_BadArgument dll_PyErr_BadArgument
136 # define PyErr_Clear dll_PyErr_Clear
137 # define PyErr_NoMemory dll_PyErr_NoMemory
138 # define PyErr_Occurred dll_PyErr_Occurred
139 # define PyErr_SetNone dll_PyErr_SetNone
140 # define PyErr_SetString dll_PyErr_SetString
141 # define PyEval_InitThreads dll_PyEval_InitThreads
142 # define PyEval_RestoreThread dll_PyEval_RestoreThread
143 # define PyEval_SaveThread dll_PyEval_SaveThread
144 # ifdef PY_CAN_RECURSE
145 #  define PyGILState_Ensure dll_PyGILState_Ensure
146 #  define PyGILState_Release dll_PyGILState_Release
147 # endif
148 # define PyInt_AsLong dll_PyInt_AsLong
149 # define PyInt_FromLong dll_PyInt_FromLong
150 # define PyInt_Type (*dll_PyInt_Type)
151 # define PyList_GetItem dll_PyList_GetItem
152 # define PyList_Append dll_PyList_Append
153 # define PyList_New dll_PyList_New
154 # define PyList_SetItem dll_PyList_SetItem
155 # define PyList_Size dll_PyList_Size
156 # define PyList_Type (*dll_PyList_Type)
157 # define PyImport_ImportModule dll_PyImport_ImportModule
158 # define PyDict_New dll_PyDict_New
159 # define PyDict_GetItemString dll_PyDict_GetItemString
160 # define PyModule_GetDict dll_PyModule_GetDict
161 # define PyRun_SimpleString dll_PyRun_SimpleString
162 # define PyString_AsString dll_PyString_AsString
163 # define PyString_FromString dll_PyString_FromString
164 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize
165 # define PyString_Size dll_PyString_Size
166 # define PyString_Type (*dll_PyString_Type)
167 # define PySys_SetObject dll_PySys_SetObject
168 # define PySys_SetArgv dll_PySys_SetArgv
169 # define PyType_Type (*dll_PyType_Type)
170 # define Py_BuildValue dll_Py_BuildValue
171 # define Py_FindMethod dll_Py_FindMethod
172 # define Py_InitModule4 dll_Py_InitModule4
173 # define Py_Initialize dll_Py_Initialize
174 # define Py_Finalize dll_Py_Finalize
175 # define Py_IsInitialized dll_Py_IsInitialized
176 # define _PyObject_New dll__PyObject_New
177 # define _Py_NoneStruct (*dll__Py_NoneStruct)
178 # define PyObject_Init dll__PyObject_Init
179 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
180 #  define PyType_IsSubtype dll_PyType_IsSubtype
181 # endif
182 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
183 #  define PyObject_Malloc dll_PyObject_Malloc
184 #  define PyObject_Free dll_PyObject_Free
185 # endif
186 
187 /*
188  * Pointers for dynamic link
189  */
190 static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
191 static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
192 static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
193 static int(*dll_PyErr_BadArgument)(void);
194 static void(*dll_PyErr_Clear)(void);
195 static PyObject*(*dll_PyErr_NoMemory)(void);
196 static PyObject*(*dll_PyErr_Occurred)(void);
197 static void(*dll_PyErr_SetNone)(PyObject *);
198 static void(*dll_PyErr_SetString)(PyObject *, const char *);
199 static void(*dll_PyEval_InitThreads)(void);
200 static void(*dll_PyEval_RestoreThread)(PyThreadState *);
201 static PyThreadState*(*dll_PyEval_SaveThread)(void);
202 # ifdef PY_CAN_RECURSE
203 static PyGILState_STATE	(*dll_PyGILState_Ensure)(void);
204 static void (*dll_PyGILState_Release)(PyGILState_STATE);
205 #endif
206 static long(*dll_PyInt_AsLong)(PyObject *);
207 static PyObject*(*dll_PyInt_FromLong)(long);
208 static PyTypeObject* dll_PyInt_Type;
209 static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
210 static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
211 static PyObject*(*dll_PyList_New)(PyInt size);
212 static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
213 static PyInt(*dll_PyList_Size)(PyObject *);
214 static PyTypeObject* dll_PyList_Type;
215 static PyObject*(*dll_PyImport_ImportModule)(const char *);
216 static PyObject*(*dll_PyDict_New)(void);
217 static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
218 static PyObject*(*dll_PyModule_GetDict)(PyObject *);
219 static int(*dll_PyRun_SimpleString)(char *);
220 static char*(*dll_PyString_AsString)(PyObject *);
221 static PyObject*(*dll_PyString_FromString)(const char *);
222 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
223 static PyInt(*dll_PyString_Size)(PyObject *);
224 static PyTypeObject* dll_PyString_Type;
225 static int(*dll_PySys_SetObject)(char *, PyObject *);
226 static int(*dll_PySys_SetArgv)(int, char **);
227 static PyTypeObject* dll_PyType_Type;
228 static PyObject*(*dll_Py_BuildValue)(char *, ...);
229 static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
230 static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
231 static void(*dll_Py_Initialize)(void);
232 static void(*dll_Py_Finalize)(void);
233 static int(*dll_Py_IsInitialized)(void);
234 static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
235 static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
236 static PyObject* dll__Py_NoneStruct;
237 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
238 static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
239 # endif
240 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
241 static void* (*dll_PyObject_Malloc)(size_t);
242 static void (*dll_PyObject_Free)(void*);
243 # endif
244 
245 static HINSTANCE hinstPython = 0; /* Instance of python.dll */
246 
247 /* Imported exception objects */
248 static PyObject *imp_PyExc_AttributeError;
249 static PyObject *imp_PyExc_IndexError;
250 static PyObject *imp_PyExc_KeyboardInterrupt;
251 static PyObject *imp_PyExc_TypeError;
252 static PyObject *imp_PyExc_ValueError;
253 
254 # define PyExc_AttributeError imp_PyExc_AttributeError
255 # define PyExc_IndexError imp_PyExc_IndexError
256 # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
257 # define PyExc_TypeError imp_PyExc_TypeError
258 # define PyExc_ValueError imp_PyExc_ValueError
259 
260 /*
261  * Table of name to function pointer of python.
262  */
263 # define PYTHON_PROC FARPROC
264 static struct
265 {
266     char *name;
267     PYTHON_PROC *ptr;
268 } python_funcname_table[] =
269 {
270     {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
271     {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
272     {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
273     {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
274     {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
275     {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
276     {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
277     {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
278     {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
279     {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
280     {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
281     {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
282 # ifdef PY_CAN_RECURSE
283     {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
284     {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
285 # endif
286     {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
287     {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
288     {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
289     {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
290     {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
291     {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
292     {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
293     {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
294     {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
295     {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
296     {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
297     {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
298     {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
299     {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
300     {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
301     {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
302     {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
303     {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
304     {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
305     {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
306     {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
307     {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
308     {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
309     {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
310 # if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT
311     {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
312 # else
313     {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
314 # endif
315     {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
316     {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
317     {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
318     {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
319     {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
320     {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
321 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
322     {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
323 # endif
324 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
325     {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
326     {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
327 # endif
328     {"", NULL},
329 };
330 
331 /*
332  * Free python.dll
333  */
334     static void
335 end_dynamic_python(void)
336 {
337     if (hinstPython)
338     {
339 	close_dll(hinstPython);
340 	hinstPython = 0;
341     }
342 }
343 
344 /*
345  * Load library and get all pointers.
346  * Parameter 'libname' provides name of DLL.
347  * Return OK or FAIL.
348  */
349     static int
350 python_runtime_link_init(char *libname, int verbose)
351 {
352     int i;
353 
354 #if 0  /* this should be OK now that we don't use RTLD_GLOBAL */
355 #if defined(UNIX) && defined(FEAT_PYTHON3)
356     /* Can't have Python and Python3 loaded at the same time, it may cause a
357      * crash. */
358     if (python3_loaded())
359     {
360 	EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
361 	return FAIL;
362     }
363 #endif
364 #endif
365 
366     if (hinstPython)
367 	return OK;
368     hinstPython = load_dll(libname);
369     if (!hinstPython)
370     {
371 	if (verbose)
372 	    EMSG2(_(e_loadlib), libname);
373 	return FAIL;
374     }
375 
376     for (i = 0; python_funcname_table[i].ptr; ++i)
377     {
378 	if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
379 			python_funcname_table[i].name)) == NULL)
380 	{
381 	    close_dll(hinstPython);
382 	    hinstPython = 0;
383 	    if (verbose)
384 		EMSG2(_(e_loadfunc), python_funcname_table[i].name);
385 	    return FAIL;
386 	}
387     }
388     return OK;
389 }
390 
391 /*
392  * If python is enabled (there is installed python on Windows system) return
393  * TRUE, else FALSE.
394  */
395     int
396 python_enabled(int verbose)
397 {
398     return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
399 }
400 
401 /*
402  * Load the standard Python exceptions - don't import the symbols from the
403  * DLL, as this can cause errors (importing data symbols is not reliable).
404  */
405     static void
406 get_exceptions(void)
407 {
408     PyObject *exmod = PyImport_ImportModule("exceptions");
409     PyObject *exdict = PyModule_GetDict(exmod);
410     imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
411     imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
412     imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
413     imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
414     imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
415     Py_XINCREF(imp_PyExc_AttributeError);
416     Py_XINCREF(imp_PyExc_IndexError);
417     Py_XINCREF(imp_PyExc_KeyboardInterrupt);
418     Py_XINCREF(imp_PyExc_TypeError);
419     Py_XINCREF(imp_PyExc_ValueError);
420     Py_XDECREF(exmod);
421 }
422 #endif /* DYNAMIC_PYTHON */
423 
424 static PyObject *BufferNew (buf_T *);
425 static PyObject *WindowNew(win_T *);
426 static PyObject *LineToString(const char *);
427 
428 static PyTypeObject RangeType;
429 
430 /*
431  * Include the code shared with if_python3.c
432  */
433 #include "if_py_both.h"
434 
435 
436 /******************************************************
437  * Internal function prototypes.
438  */
439 
440 static PyInt RangeStart;
441 static PyInt RangeEnd;
442 
443 static void PythonIO_Flush(void);
444 static int PythonIO_Init(void);
445 static int PythonMod_Init(void);
446 
447 /* Utility functions for the vim/python interface
448  * ----------------------------------------------
449  */
450 
451 static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
452 
453 
454 /******************************************************
455  * 1. Python interpreter main program.
456  */
457 
458 static int initialised = 0;
459 
460 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */
461 typedef PyObject PyThreadState;
462 #endif
463 
464 #ifdef PY_CAN_RECURSE
465 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
466 #else
467 static PyThreadState *saved_python_thread = NULL;
468 #endif
469 
470 /*
471  * Suspend a thread of the Python interpreter, other threads are allowed to
472  * run.
473  */
474     static void
475 Python_SaveThread(void)
476 {
477 #ifdef PY_CAN_RECURSE
478     PyGILState_Release(pygilstate);
479 #else
480     saved_python_thread = PyEval_SaveThread();
481 #endif
482 }
483 
484 /*
485  * Restore a thread of the Python interpreter, waits for other threads to
486  * block.
487  */
488     static void
489 Python_RestoreThread(void)
490 {
491 #ifdef PY_CAN_RECURSE
492     pygilstate = PyGILState_Ensure();
493 #else
494     PyEval_RestoreThread(saved_python_thread);
495     saved_python_thread = NULL;
496 #endif
497 }
498 
499     void
500 python_end()
501 {
502     static int recurse = 0;
503 
504     /* If a crash occurs while doing this, don't try again. */
505     if (recurse != 0)
506 	return;
507 
508     ++recurse;
509 
510 #ifdef DYNAMIC_PYTHON
511     if (hinstPython && Py_IsInitialized())
512     {
513 	Python_RestoreThread();	    /* enter python */
514 	Py_Finalize();
515     }
516     end_dynamic_python();
517 #else
518     if (Py_IsInitialized())
519     {
520 	Python_RestoreThread();	    /* enter python */
521 	Py_Finalize();
522     }
523 #endif
524 
525     --recurse;
526 }
527 
528 #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
529     int
530 python_loaded()
531 {
532     return (hinstPython != 0);
533 }
534 #endif
535 
536     static int
537 Python_Init(void)
538 {
539     if (!initialised)
540     {
541 #ifdef DYNAMIC_PYTHON
542 	if (!python_enabled(TRUE))
543 	{
544 	    EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
545 	    goto fail;
546 	}
547 #endif
548 
549 	init_structs();
550 
551 #if !defined(MACOS) || defined(MACOS_X_UNIX)
552 	Py_Initialize();
553 #else
554 	PyMac_Initialize();
555 #endif
556 	/* initialise threads */
557 	PyEval_InitThreads();
558 
559 #ifdef DYNAMIC_PYTHON
560 	get_exceptions();
561 #endif
562 
563 	if (PythonIO_Init())
564 	    goto fail;
565 
566 	if (PythonMod_Init())
567 	    goto fail;
568 
569 	/* Remove the element from sys.path that was added because of our
570 	 * argv[0] value in PythonMod_Init().  Previously we used an empty
571 	 * string, but dependinding on the OS we then get an empty entry or
572 	 * the current directory in sys.path. */
573 	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
574 
575 	/* the first python thread is vim's, release the lock */
576 	Python_SaveThread();
577 
578 	initialised = 1;
579     }
580 
581     return 0;
582 
583 fail:
584     /* We call PythonIO_Flush() here to print any Python errors.
585      * This is OK, as it is possible to call this function even
586      * if PythonIO_Init() has not completed successfully (it will
587      * not do anything in this case).
588      */
589     PythonIO_Flush();
590     return -1;
591 }
592 
593 /*
594  * External interface
595  */
596     static void
597 DoPythonCommand(exarg_T *eap, const char *cmd)
598 {
599 #ifndef PY_CAN_RECURSE
600     static int		recursive = 0;
601 #endif
602 #if defined(MACOS) && !defined(MACOS_X_UNIX)
603     GrafPtr		oldPort;
604 #endif
605 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
606     char		*saved_locale;
607 #endif
608 
609 #ifndef PY_CAN_RECURSE
610     if (recursive)
611     {
612 	EMSG(_("E659: Cannot invoke Python recursively"));
613 	return;
614     }
615     ++recursive;
616 #endif
617 
618 #if defined(MACOS) && !defined(MACOS_X_UNIX)
619     GetPort(&oldPort);
620     /* Check if the Python library is available */
621     if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
622 	goto theend;
623 #endif
624     if (Python_Init())
625 	goto theend;
626 
627     RangeStart = eap->line1;
628     RangeEnd = eap->line2;
629     Python_Release_Vim();	    /* leave vim */
630 
631 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
632     /* Python only works properly when the LC_NUMERIC locale is "C". */
633     saved_locale = setlocale(LC_NUMERIC, NULL);
634     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
635 	saved_locale = NULL;
636     else
637     {
638 	/* Need to make a copy, value may change when setting new locale. */
639 	saved_locale = (char *)vim_strsave((char_u *)saved_locale);
640 	(void)setlocale(LC_NUMERIC, "C");
641     }
642 #endif
643 
644     Python_RestoreThread();	    /* enter python */
645 
646     PyRun_SimpleString((char *)(cmd));
647 
648     Python_SaveThread();	    /* leave python */
649 
650 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
651     if (saved_locale != NULL)
652     {
653 	(void)setlocale(LC_NUMERIC, saved_locale);
654 	vim_free(saved_locale);
655     }
656 #endif
657 
658     Python_Lock_Vim();		    /* enter vim */
659     PythonIO_Flush();
660 #if defined(MACOS) && !defined(MACOS_X_UNIX)
661     SetPort(oldPort);
662 #endif
663 
664 theend:
665 #ifndef PY_CAN_RECURSE
666     --recursive;
667 #endif
668     return;	    /* keeps lint happy */
669 }
670 
671 /*
672  * ":python"
673  */
674     void
675 ex_python(exarg_T *eap)
676 {
677     char_u *script;
678 
679     script = script_get(eap, eap->arg);
680     if (!eap->skip)
681     {
682 	if (script == NULL)
683 	    DoPythonCommand(eap, (char *)eap->arg);
684 	else
685 	    DoPythonCommand(eap, (char *)script);
686     }
687     vim_free(script);
688 }
689 
690 #define BUFFER_SIZE 1024
691 
692 /*
693  * ":pyfile"
694  */
695     void
696 ex_pyfile(exarg_T *eap)
697 {
698     static char buffer[BUFFER_SIZE];
699     const char *file = (char *)eap->arg;
700     char *p;
701 
702     /* Have to do it like this. PyRun_SimpleFile requires you to pass a
703      * stdio file pointer, but Vim and the Python DLL are compiled with
704      * different options under Windows, meaning that stdio pointers aren't
705      * compatible between the two. Yuk.
706      *
707      * Put the string "execfile('file')" into buffer. But, we need to
708      * escape any backslashes or single quotes in the file name, so that
709      * Python won't mangle the file name.
710      */
711     strcpy(buffer, "execfile('");
712     p = buffer + 10; /* size of "execfile('" */
713 
714     while (*file && p < buffer + (BUFFER_SIZE - 3))
715     {
716 	if (*file == '\\' || *file == '\'')
717 	    *p++ = '\\';
718 	*p++ = *file++;
719     }
720 
721     /* If we didn't finish the file name, we hit a buffer overflow */
722     if (*file != '\0')
723 	return;
724 
725     /* Put in the terminating "')" and a null */
726     *p++ = '\'';
727     *p++ = ')';
728     *p++ = '\0';
729 
730     /* Execute the file */
731     DoPythonCommand(eap, buffer);
732 }
733 
734 /******************************************************
735  * 2. Python output stream: writes output via [e]msg().
736  */
737 
738 /* Implementation functions
739  */
740 
741     static PyObject *
742 OutputGetattr(PyObject *self, char *name)
743 {
744     if (strcmp(name, "softspace") == 0)
745 	return PyInt_FromLong(((OutputObject *)(self))->softspace);
746 
747     return Py_FindMethod(OutputMethods, self, name);
748 }
749 
750     static int
751 OutputSetattr(PyObject *self, char *name, PyObject *val)
752 {
753     if (val == NULL) {
754 	PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
755 	return -1;
756     }
757 
758     if (strcmp(name, "softspace") == 0)
759     {
760 	if (!PyInt_Check(val)) {
761 	    PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
762 	    return -1;
763 	}
764 
765 	((OutputObject *)(self))->softspace = PyInt_AsLong(val);
766 	return 0;
767     }
768 
769     PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
770     return -1;
771 }
772 
773 /***************/
774 
775     static int
776 PythonIO_Init(void)
777 {
778     /* Fixups... */
779     OutputType.ob_type = &PyType_Type;
780 
781     return PythonIO_Init_io();
782 }
783 
784 /******************************************************
785  * 3. Implementation of the Vim module for Python
786  */
787 
788 /* Window type - Implementation functions
789  * --------------------------------------
790  */
791 
792 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
793 
794 static void WindowDestructor(PyObject *);
795 static PyObject *WindowGetattr(PyObject *, char *);
796 
797 /* Buffer type - Implementation functions
798  * --------------------------------------
799  */
800 
801 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
802 
803 static void BufferDestructor(PyObject *);
804 static PyObject *BufferGetattr(PyObject *, char *);
805 static PyObject *BufferRepr(PyObject *);
806 
807 static PyInt BufferLength(PyObject *);
808 static PyObject *BufferItem(PyObject *, PyInt);
809 static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
810 static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
811 static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
812 
813 /* Line range type - Implementation functions
814  * --------------------------------------
815  */
816 
817 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
818 
819 static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
820 static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
821 
822 /* Current objects type - Implementation functions
823  * -----------------------------------------------
824  */
825 
826 static PyObject *CurrentGetattr(PyObject *, char *);
827 static int CurrentSetattr(PyObject *, char *, PyObject *);
828 
829 /* Common routines for buffers and line ranges
830  * -------------------------------------------
831  */
832 
833     static PyInt
834 RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
835 {
836     PyInt size;
837     PyInt len_change;
838 
839     /* Self must be a valid buffer */
840     if (CheckBuffer(self))
841 	return -1;
842 
843     /* Sort out the slice range */
844     size = end - start + 1;
845 
846     if (lo < 0)
847 	lo = 0;
848     else if (lo > size)
849 	lo = size;
850     if (hi < 0)
851 	hi = 0;
852     if (hi < lo)
853 	hi = lo;
854     else if (hi > size)
855 	hi = size;
856 
857     if (SetBufferLineList(self->buf, lo + start, hi + start,
858 						    val, &len_change) == FAIL)
859 	return -1;
860 
861     if (new_end)
862 	*new_end = end + len_change;
863 
864     return 0;
865 }
866 
867 static PySequenceMethods BufferAsSeq = {
868     (PyInquiry)		BufferLength,	    /* sq_length,    len(x)   */
869     (binaryfunc)	0, /* BufferConcat, */	     /* sq_concat,    x+y      */
870     (PyIntArgFunc)	0, /* BufferRepeat, */	     /* sq_repeat,    x*n      */
871     (PyIntArgFunc)	BufferItem,	    /* sq_item,      x[i]     */
872     (PyIntIntArgFunc)	BufferSlice,	    /* sq_slice,     x[i:j]   */
873     (PyIntObjArgProc)	BufferAssItem,	    /* sq_ass_item,  x[i]=v   */
874     (PyIntIntObjArgProc)	BufferAssSlice,     /* sq_ass_slice, x[i:j]=v */
875 };
876 
877 static PyTypeObject BufferType = {
878     PyObject_HEAD_INIT(0)
879     0,
880     "buffer",
881     sizeof(BufferObject),
882     0,
883 
884     (destructor)    BufferDestructor,	/* tp_dealloc,	refcount==0  */
885     (printfunc)     0,			/* tp_print,	print x      */
886     (getattrfunc)   BufferGetattr,	/* tp_getattr,	x.attr	     */
887     (setattrfunc)   0,			/* tp_setattr,	x.attr=v     */
888     (cmpfunc)	    0,			/* tp_compare,	x>y	     */
889     (reprfunc)	    BufferRepr,		/* tp_repr,	`x`, print x */
890 
891     0,		    /* as number */
892     &BufferAsSeq,   /* as sequence */
893     0,		    /* as mapping */
894 
895     (hashfunc) 0,			/* tp_hash, dict(x) */
896     (ternaryfunc) 0,			/* tp_call, x()     */
897     (reprfunc) 0,			/* tp_str,  str(x)  */
898 };
899 
900 /* Buffer object - Implementation
901  */
902 
903     static PyObject *
904 BufferNew(buf_T *buf)
905 {
906     /* We need to handle deletion of buffers underneath us.
907      * If we add a "b_python_ref" field to the buf_T structure,
908      * then we can get at it in buf_freeall() in vim. We then
909      * need to create only ONE Python object per buffer - if
910      * we try to create a second, just INCREF the existing one
911      * and return it. The (single) Python object referring to
912      * the buffer is stored in "b_python_ref".
913      * Question: what to do on a buf_freeall(). We'll probably
914      * have to either delete the Python object (DECREF it to
915      * zero - a bad idea, as it leaves dangling refs!) or
916      * set the buf_T * value to an invalid value (-1?), which
917      * means we need checks in all access functions... Bah.
918      */
919 
920     BufferObject *self;
921 
922     if (buf->b_python_ref != NULL)
923     {
924 	self = buf->b_python_ref;
925 	Py_INCREF(self);
926     }
927     else
928     {
929 	self = PyObject_NEW(BufferObject, &BufferType);
930 	if (self == NULL)
931 	    return NULL;
932 	self->buf = buf;
933 	buf->b_python_ref = self;
934     }
935 
936     return (PyObject *)(self);
937 }
938 
939     static void
940 BufferDestructor(PyObject *self)
941 {
942     BufferObject *this = (BufferObject *)(self);
943 
944     if (this->buf && this->buf != INVALID_BUFFER_VALUE)
945 	this->buf->b_python_ref = NULL;
946 
947     Py_DECREF(self);
948 }
949 
950     static PyObject *
951 BufferGetattr(PyObject *self, char *name)
952 {
953     BufferObject *this = (BufferObject *)(self);
954 
955     if (CheckBuffer(this))
956 	return NULL;
957 
958     if (strcmp(name, "name") == 0)
959 	return Py_BuildValue("s", this->buf->b_ffname);
960     else if (strcmp(name, "number") == 0)
961 	return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
962     else if (strcmp(name,"__members__") == 0)
963 	return Py_BuildValue("[ss]", "name", "number");
964     else
965 	return Py_FindMethod(BufferMethods, self, name);
966 }
967 
968     static PyObject *
969 BufferRepr(PyObject *self)
970 {
971     static char repr[100];
972     BufferObject *this = (BufferObject *)(self);
973 
974     if (this->buf == INVALID_BUFFER_VALUE)
975     {
976 	vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
977 	return PyString_FromString(repr);
978     }
979     else
980     {
981 	char *name = (char *)this->buf->b_fname;
982 	PyInt len;
983 
984 	if (name == NULL)
985 	    name = "";
986 	len = strlen(name);
987 
988 	if (len > 35)
989 	    name = name + (35 - len);
990 
991 	vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
992 
993 	return PyString_FromString(repr);
994     }
995 }
996 
997 /******************/
998 
999     static PyInt
1000 BufferLength(PyObject *self)
1001 {
1002     /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1003     if (CheckBuffer((BufferObject *)(self)))
1004 	return -1; /* ??? */
1005 
1006     return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1007 }
1008 
1009     static PyObject *
1010 BufferItem(PyObject *self, PyInt n)
1011 {
1012     return RBItem((BufferObject *)(self), n, 1,
1013 		  (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1014 }
1015 
1016     static PyObject *
1017 BufferSlice(PyObject *self, PyInt lo, PyInt hi)
1018 {
1019     return RBSlice((BufferObject *)(self), lo, hi, 1,
1020 		   (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1021 }
1022 
1023     static PyInt
1024 BufferAssItem(PyObject *self, PyInt n, PyObject *val)
1025 {
1026     return RBAsItem((BufferObject *)(self), n, val, 1,
1027 		     (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1028 		     NULL);
1029 }
1030 
1031     static PyInt
1032 BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1033 {
1034     return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
1035 		      (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1036 		      NULL);
1037 }
1038 
1039 static PySequenceMethods RangeAsSeq = {
1040     (PyInquiry)		RangeLength,	    /* sq_length,    len(x)   */
1041     (binaryfunc)	0, /* RangeConcat, */	     /* sq_concat,    x+y      */
1042     (PyIntArgFunc)	0, /* RangeRepeat, */	     /* sq_repeat,    x*n      */
1043     (PyIntArgFunc)	RangeItem,	    /* sq_item,      x[i]     */
1044     (PyIntIntArgFunc)	RangeSlice,	    /* sq_slice,     x[i:j]   */
1045     (PyIntObjArgProc)	RangeAssItem,	    /* sq_ass_item,  x[i]=v   */
1046     (PyIntIntObjArgProc)	RangeAssSlice,	    /* sq_ass_slice, x[i:j]=v */
1047 };
1048 
1049 /* Line range object - Implementation
1050  */
1051 
1052     static void
1053 RangeDestructor(PyObject *self)
1054 {
1055     Py_DECREF(((RangeObject *)(self))->buf);
1056     Py_DECREF(self);
1057 }
1058 
1059     static PyObject *
1060 RangeGetattr(PyObject *self, char *name)
1061 {
1062     if (strcmp(name, "start") == 0)
1063 	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
1064     else if (strcmp(name, "end") == 0)
1065 	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
1066     else
1067 	return Py_FindMethod(RangeMethods, self, name);
1068 }
1069 
1070 /****************/
1071 
1072     static PyInt
1073 RangeAssItem(PyObject *self, PyInt n, PyObject *val)
1074 {
1075     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1076 		     ((RangeObject *)(self))->start,
1077 		     ((RangeObject *)(self))->end,
1078 		     &((RangeObject *)(self))->end);
1079 }
1080 
1081     static PyInt
1082 RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1083 {
1084     return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
1085 		      ((RangeObject *)(self))->start,
1086 		      ((RangeObject *)(self))->end,
1087 		      &((RangeObject *)(self))->end);
1088 }
1089 
1090 /* Buffer list object - Definitions
1091  */
1092 
1093 typedef struct
1094 {
1095     PyObject_HEAD
1096 } BufListObject;
1097 
1098 static PySequenceMethods BufListAsSeq = {
1099     (PyInquiry)		BufListLength,	    /* sq_length,    len(x)   */
1100     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1101     (PyIntArgFunc)	0,		    /* sq_repeat,    x*n      */
1102     (PyIntArgFunc)	BufListItem,	    /* sq_item,      x[i]     */
1103     (PyIntIntArgFunc)	0,		    /* sq_slice,     x[i:j]   */
1104     (PyIntObjArgProc)	0,		    /* sq_ass_item,  x[i]=v   */
1105     (PyIntIntObjArgProc)	0,		    /* sq_ass_slice, x[i:j]=v */
1106 };
1107 
1108 static PyTypeObject BufListType = {
1109     PyObject_HEAD_INIT(0)
1110     0,
1111     "buffer list",
1112     sizeof(BufListObject),
1113     0,
1114 
1115     (destructor)    0,			/* tp_dealloc,	refcount==0  */
1116     (printfunc)     0,			/* tp_print,	print x      */
1117     (getattrfunc)   0,			/* tp_getattr,	x.attr	     */
1118     (setattrfunc)   0,			/* tp_setattr,	x.attr=v     */
1119     (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1120     (reprfunc)	    0,			/* tp_repr,	`x`, print x */
1121 
1122     0,		    /* as number */
1123     &BufListAsSeq,  /* as sequence */
1124     0,		    /* as mapping */
1125 
1126     (hashfunc) 0,			/* tp_hash, dict(x) */
1127     (ternaryfunc) 0,			/* tp_call, x()     */
1128     (reprfunc) 0,			/* tp_str,  str(x)  */
1129 };
1130 
1131 /* Window object - Definitions
1132  */
1133 
1134 static struct PyMethodDef WindowMethods[] = {
1135     /* name,	    function,		calling,    documentation */
1136     { NULL,	    NULL,		0,	    NULL }
1137 };
1138 
1139 static PyTypeObject WindowType = {
1140     PyObject_HEAD_INIT(0)
1141     0,
1142     "window",
1143     sizeof(WindowObject),
1144     0,
1145 
1146     (destructor)    WindowDestructor,	/* tp_dealloc,	refcount==0  */
1147     (printfunc)     0,			/* tp_print,	print x      */
1148     (getattrfunc)   WindowGetattr,	/* tp_getattr,	x.attr	     */
1149     (setattrfunc)   WindowSetattr,	/* tp_setattr,	x.attr=v     */
1150     (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1151     (reprfunc)	    WindowRepr,		/* tp_repr,	`x`, print x */
1152 
1153     0,		    /* as number */
1154     0,		    /* as sequence */
1155     0,		    /* as mapping */
1156 
1157     (hashfunc) 0,			/* tp_hash, dict(x) */
1158     (ternaryfunc) 0,			/* tp_call, x()     */
1159     (reprfunc) 0,			/* tp_str,  str(x)  */
1160 };
1161 
1162 /* Window object - Implementation
1163  */
1164 
1165     static PyObject *
1166 WindowNew(win_T *win)
1167 {
1168     /* We need to handle deletion of windows underneath us.
1169      * If we add a "w_python_ref" field to the win_T structure,
1170      * then we can get at it in win_free() in vim. We then
1171      * need to create only ONE Python object per window - if
1172      * we try to create a second, just INCREF the existing one
1173      * and return it. The (single) Python object referring to
1174      * the window is stored in "w_python_ref".
1175      * On a win_free() we set the Python object's win_T* field
1176      * to an invalid value. We trap all uses of a window
1177      * object, and reject them if the win_T* field is invalid.
1178      */
1179 
1180     WindowObject *self;
1181 
1182     if (win->w_python_ref)
1183     {
1184 	self = win->w_python_ref;
1185 	Py_INCREF(self);
1186     }
1187     else
1188     {
1189 	self = PyObject_NEW(WindowObject, &WindowType);
1190 	if (self == NULL)
1191 	    return NULL;
1192 	self->win = win;
1193 	win->w_python_ref = self;
1194     }
1195 
1196     return (PyObject *)(self);
1197 }
1198 
1199     static void
1200 WindowDestructor(PyObject *self)
1201 {
1202     WindowObject *this = (WindowObject *)(self);
1203 
1204     if (this->win && this->win != INVALID_WINDOW_VALUE)
1205 	this->win->w_python_ref = NULL;
1206 
1207     Py_DECREF(self);
1208 }
1209 
1210     static PyObject *
1211 WindowGetattr(PyObject *self, char *name)
1212 {
1213     WindowObject *this = (WindowObject *)(self);
1214 
1215     if (CheckWindow(this))
1216 	return NULL;
1217 
1218     if (strcmp(name, "buffer") == 0)
1219 	return (PyObject *)BufferNew(this->win->w_buffer);
1220     else if (strcmp(name, "cursor") == 0)
1221     {
1222 	pos_T *pos = &this->win->w_cursor;
1223 
1224 	return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1225     }
1226     else if (strcmp(name, "height") == 0)
1227 	return Py_BuildValue("l", (long)(this->win->w_height));
1228 #ifdef FEAT_VERTSPLIT
1229     else if (strcmp(name, "width") == 0)
1230 	return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1231 #endif
1232     else if (strcmp(name,"__members__") == 0)
1233 	return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1234     else
1235 	return Py_FindMethod(WindowMethods, self, name);
1236 }
1237 
1238 /* Window list object - Definitions
1239  */
1240 
1241 typedef struct
1242 {
1243     PyObject_HEAD
1244 }
1245 WinListObject;
1246 
1247 static PySequenceMethods WinListAsSeq = {
1248     (PyInquiry)		WinListLength,	    /* sq_length,    len(x)   */
1249     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1250     (PyIntArgFunc)	0,		    /* sq_repeat,    x*n      */
1251     (PyIntArgFunc)	WinListItem,	    /* sq_item,      x[i]     */
1252     (PyIntIntArgFunc)	0,		    /* sq_slice,     x[i:j]   */
1253     (PyIntObjArgProc)	0,		    /* sq_ass_item,  x[i]=v   */
1254     (PyIntIntObjArgProc)	0,		    /* sq_ass_slice, x[i:j]=v */
1255 };
1256 
1257 static PyTypeObject WinListType = {
1258     PyObject_HEAD_INIT(0)
1259     0,
1260     "window list",
1261     sizeof(WinListObject),
1262     0,
1263 
1264     (destructor)    0,			/* tp_dealloc,	refcount==0  */
1265     (printfunc)     0,			/* tp_print,	print x      */
1266     (getattrfunc)   0,			/* tp_getattr,	x.attr	     */
1267     (setattrfunc)   0,			/* tp_setattr,	x.attr=v     */
1268     (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1269     (reprfunc)	    0,			/* tp_repr,	`x`, print x */
1270 
1271     0,		    /* as number */
1272     &WinListAsSeq,  /* as sequence */
1273     0,		    /* as mapping */
1274 
1275     (hashfunc) 0,			/* tp_hash, dict(x) */
1276     (ternaryfunc) 0,			/* tp_call, x()     */
1277     (reprfunc) 0,			/* tp_str,  str(x)  */
1278 };
1279 
1280 /* Current items object - Definitions
1281  */
1282 
1283 typedef struct
1284 {
1285     PyObject_HEAD
1286 } CurrentObject;
1287 
1288 static PyTypeObject CurrentType = {
1289     PyObject_HEAD_INIT(0)
1290     0,
1291     "current data",
1292     sizeof(CurrentObject),
1293     0,
1294 
1295     (destructor)    0,			/* tp_dealloc,	refcount==0  */
1296     (printfunc)     0,			/* tp_print,	print x      */
1297     (getattrfunc)   CurrentGetattr,	/* tp_getattr,	x.attr	     */
1298     (setattrfunc)   CurrentSetattr,	/* tp_setattr,	x.attr=v     */
1299     (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1300     (reprfunc)	    0,			/* tp_repr,	`x`, print x */
1301 
1302     0,		    /* as number */
1303     0,		    /* as sequence */
1304     0,		    /* as mapping */
1305 
1306     (hashfunc) 0,			/* tp_hash, dict(x) */
1307     (ternaryfunc) 0,			/* tp_call, x()     */
1308     (reprfunc) 0,			/* tp_str,  str(x)  */
1309 };
1310 
1311 /* Current items object - Implementation
1312  */
1313     static PyObject *
1314 CurrentGetattr(PyObject *self UNUSED, char *name)
1315 {
1316     if (strcmp(name, "buffer") == 0)
1317 	return (PyObject *)BufferNew(curbuf);
1318     else if (strcmp(name, "window") == 0)
1319 	return (PyObject *)WindowNew(curwin);
1320     else if (strcmp(name, "line") == 0)
1321 	return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
1322     else if (strcmp(name, "range") == 0)
1323 	return RangeNew(curbuf, RangeStart, RangeEnd);
1324     else if (strcmp(name,"__members__") == 0)
1325 	return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1326     else
1327     {
1328 	PyErr_SetString(PyExc_AttributeError, name);
1329 	return NULL;
1330     }
1331 }
1332 
1333     static int
1334 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
1335 {
1336     if (strcmp(name, "line") == 0)
1337     {
1338 	if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
1339 	    return -1;
1340 
1341 	return 0;
1342     }
1343     else
1344     {
1345 	PyErr_SetString(PyExc_AttributeError, name);
1346 	return -1;
1347     }
1348 }
1349 
1350 /* External interface
1351  */
1352 
1353     void
1354 python_buffer_free(buf_T *buf)
1355 {
1356     if (buf->b_python_ref != NULL)
1357     {
1358 	BufferObject *bp = buf->b_python_ref;
1359 	bp->buf = INVALID_BUFFER_VALUE;
1360 	buf->b_python_ref = NULL;
1361     }
1362 }
1363 
1364 #if defined(FEAT_WINDOWS) || defined(PROTO)
1365     void
1366 python_window_free(win_T *win)
1367 {
1368     if (win->w_python_ref != NULL)
1369     {
1370 	WindowObject *wp = win->w_python_ref;
1371 	wp->win = INVALID_WINDOW_VALUE;
1372 	win->w_python_ref = NULL;
1373     }
1374 }
1375 #endif
1376 
1377 static BufListObject TheBufferList =
1378 {
1379     PyObject_HEAD_INIT(&BufListType)
1380 };
1381 
1382 static WinListObject TheWindowList =
1383 {
1384     PyObject_HEAD_INIT(&WinListType)
1385 };
1386 
1387 static CurrentObject TheCurrent =
1388 {
1389     PyObject_HEAD_INIT(&CurrentType)
1390 };
1391 
1392     static int
1393 PythonMod_Init(void)
1394 {
1395     PyObject *mod;
1396     PyObject *dict;
1397     /* The special value is removed from sys.path in Python_Init(). */
1398     static char *(argv[2]) = {"/must>not&exist/foo", NULL};
1399 
1400     /* Fixups... */
1401     BufferType.ob_type = &PyType_Type;
1402     RangeType.ob_type = &PyType_Type;
1403     WindowType.ob_type = &PyType_Type;
1404     BufListType.ob_type = &PyType_Type;
1405     WinListType.ob_type = &PyType_Type;
1406     CurrentType.ob_type = &PyType_Type;
1407 
1408     /* Set sys.argv[] to avoid a crash in warn(). */
1409     PySys_SetArgv(1, argv);
1410 
1411     mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
1412     dict = PyModule_GetDict(mod);
1413 
1414     VimError = Py_BuildValue("s", "vim.error");
1415 
1416     PyDict_SetItemString(dict, "error", VimError);
1417     PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1418     PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1419     PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
1420 
1421     if (PyErr_Occurred())
1422 	return -1;
1423 
1424     return 0;
1425 }
1426 
1427 /*************************************************************************
1428  * 4. Utility functions for handling the interface between Vim and Python.
1429  */
1430 
1431 /* Replace a range of lines in the specified buffer. The line numbers are in
1432  * Vim format (1-based). The range is from lo up to, but not including, hi.
1433  * The replacement lines are given as a Python list of string objects. The
1434  * list is checked for validity and correct format. Errors are returned as a
1435  * value of FAIL.  The return value is OK on success.
1436  * If OK is returned and len_change is not NULL, *len_change
1437  * is set to the change in the buffer length.
1438  */
1439     static int
1440 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
1441 {
1442     /* First of all, we check the thpe of the supplied Python object.
1443      * There are three cases:
1444      *	  1. NULL, or None - this is a deletion.
1445      *	  2. A list	   - this is a replacement.
1446      *	  3. Anything else - this is an error.
1447      */
1448     if (list == Py_None || list == NULL)
1449     {
1450 	PyInt	i;
1451 	PyInt	n = (int)(hi - lo);
1452 	buf_T	*savebuf = curbuf;
1453 
1454 	PyErr_Clear();
1455 	curbuf = buf;
1456 
1457 	if (u_savedel((linenr_T)lo, (long)n) == FAIL)
1458 	    PyErr_SetVim(_("cannot save undo information"));
1459 	else
1460 	{
1461 	    for (i = 0; i < n; ++i)
1462 	    {
1463 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1464 		{
1465 		    PyErr_SetVim(_("cannot delete line"));
1466 		    break;
1467 		}
1468 	    }
1469 	    if (buf == curwin->w_buffer)
1470 		py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
1471 	    deleted_lines_mark((linenr_T)lo, (long)i);
1472 	}
1473 
1474 	curbuf = savebuf;
1475 
1476 	if (PyErr_Occurred() || VimErrorCheck())
1477 	    return FAIL;
1478 
1479 	if (len_change)
1480 	    *len_change = -n;
1481 
1482 	return OK;
1483     }
1484     else if (PyList_Check(list))
1485     {
1486 	PyInt	i;
1487 	PyInt	new_len = PyList_Size(list);
1488 	PyInt	old_len = hi - lo;
1489 	PyInt	extra = 0;	/* lines added to text, can be negative */
1490 	char	**array;
1491 	buf_T	*savebuf;
1492 
1493 	if (new_len == 0)	/* avoid allocating zero bytes */
1494 	    array = NULL;
1495 	else
1496 	{
1497 	    array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
1498 	    if (array == NULL)
1499 	    {
1500 		PyErr_NoMemory();
1501 		return FAIL;
1502 	    }
1503 	}
1504 
1505 	for (i = 0; i < new_len; ++i)
1506 	{
1507 	    PyObject *line = PyList_GetItem(list, i);
1508 
1509 	    array[i] = StringToLine(line);
1510 	    if (array[i] == NULL)
1511 	    {
1512 		while (i)
1513 		    vim_free(array[--i]);
1514 		vim_free(array);
1515 		return FAIL;
1516 	    }
1517 	}
1518 
1519 	savebuf = curbuf;
1520 
1521 	PyErr_Clear();
1522 	curbuf = buf;
1523 
1524 	if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
1525 	    PyErr_SetVim(_("cannot save undo information"));
1526 
1527 	/* If the size of the range is reducing (ie, new_len < old_len) we
1528 	 * need to delete some old_len. We do this at the start, by
1529 	 * repeatedly deleting line "lo".
1530 	 */
1531 	if (!PyErr_Occurred())
1532 	{
1533 	    for (i = 0; i < old_len - new_len; ++i)
1534 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1535 		{
1536 		    PyErr_SetVim(_("cannot delete line"));
1537 		    break;
1538 		}
1539 	    extra -= i;
1540 	}
1541 
1542 	/* For as long as possible, replace the existing old_len with the
1543 	 * new old_len. This is a more efficient operation, as it requires
1544 	 * less memory allocation and freeing.
1545 	 */
1546 	if (!PyErr_Occurred())
1547 	{
1548 	    for (i = 0; i < old_len && i < new_len; ++i)
1549 		if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
1550 								      == FAIL)
1551 		{
1552 		    PyErr_SetVim(_("cannot replace line"));
1553 		    break;
1554 		}
1555 	}
1556 	else
1557 	    i = 0;
1558 
1559 	/* Now we may need to insert the remaining new old_len. If we do, we
1560 	 * must free the strings as we finish with them (we can't pass the
1561 	 * responsibility to vim in this case).
1562 	 */
1563 	if (!PyErr_Occurred())
1564 	{
1565 	    while (i < new_len)
1566 	    {
1567 		if (ml_append((linenr_T)(lo + i - 1),
1568 					(char_u *)array[i], 0, FALSE) == FAIL)
1569 		{
1570 		    PyErr_SetVim(_("cannot insert line"));
1571 		    break;
1572 		}
1573 		vim_free(array[i]);
1574 		++i;
1575 		++extra;
1576 	    }
1577 	}
1578 
1579 	/* Free any left-over old_len, as a result of an error */
1580 	while (i < new_len)
1581 	{
1582 	    vim_free(array[i]);
1583 	    ++i;
1584 	}
1585 
1586 	/* Free the array of old_len. All of its contents have now
1587 	 * been dealt with (either freed, or the responsibility passed
1588 	 * to vim.
1589 	 */
1590 	vim_free(array);
1591 
1592 	/* Adjust marks. Invalidate any which lie in the
1593 	 * changed range, and move any in the remainder of the buffer.
1594 	 */
1595 	mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
1596 						  (long)MAXLNUM, (long)extra);
1597 	changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
1598 
1599 	if (buf == curwin->w_buffer)
1600 	    py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
1601 
1602 	curbuf = savebuf;
1603 
1604 	if (PyErr_Occurred() || VimErrorCheck())
1605 	    return FAIL;
1606 
1607 	if (len_change)
1608 	    *len_change = new_len - old_len;
1609 
1610 	return OK;
1611     }
1612     else
1613     {
1614 	PyErr_BadArgument();
1615 	return FAIL;
1616     }
1617 }
1618 
1619 /* Convert a Vim line into a Python string.
1620  * All internal newlines are replaced by null characters.
1621  *
1622  * On errors, the Python exception data is set, and NULL is returned.
1623  */
1624     static PyObject *
1625 LineToString(const char *str)
1626 {
1627     PyObject *result;
1628     PyInt len = strlen(str);
1629     char *p;
1630 
1631     /* Allocate an Python string object, with uninitialised contents. We
1632      * must do it this way, so that we can modify the string in place
1633      * later. See the Python source, Objects/stringobject.c for details.
1634      */
1635     result = PyString_FromStringAndSize(NULL, len);
1636     if (result == NULL)
1637 	return NULL;
1638 
1639     p = PyString_AsString(result);
1640 
1641     while (*str)
1642     {
1643 	if (*str == '\n')
1644 	    *p = '\0';
1645 	else
1646 	    *p = *str;
1647 
1648 	++p;
1649 	++str;
1650     }
1651 
1652     return result;
1653 }
1654 
1655 
1656 /* Don't generate a prototype for the next function, it generates an error on
1657  * newer Python versions. */
1658 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1659 
1660     char *
1661 Py_GetProgramName(void)
1662 {
1663     return "vim";
1664 }
1665 #endif /* Python 1.4 */
1666 
1667     static void
1668 init_structs(void)
1669 {
1670     vim_memset(&OutputType, 0, sizeof(OutputType));
1671     OutputType.tp_name = "message";
1672     OutputType.tp_basicsize = sizeof(OutputObject);
1673     OutputType.tp_getattr = OutputGetattr;
1674     OutputType.tp_setattr = OutputSetattr;
1675 
1676     vim_memset(&RangeType, 0, sizeof(RangeType));
1677     RangeType.tp_name = "range";
1678     RangeType.tp_basicsize = sizeof(RangeObject);
1679     RangeType.tp_dealloc = RangeDestructor;
1680     RangeType.tp_getattr = RangeGetattr;
1681     RangeType.tp_repr = RangeRepr;
1682     RangeType.tp_as_sequence = &RangeAsSeq;
1683 }
1684