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