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