xref: /vim-8.2.3635/src/if_python.c (revision 8ea05de6)
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 // uncomment this if used with the debug version of python.
25 // Checked on 2.7.4.
26 // #define Py_DEBUG
27 // Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
28 // uncomment this if used with the debug version of python, but without its
29 // allocator
30 // #define Py_DEBUG_NO_PYMALLOC
31 
32 // Python.h defines _POSIX_THREADS itself (if needed)
33 #ifdef _POSIX_THREADS
34 # undef _POSIX_THREADS
35 #endif
36 
37 #if defined(MSWIN) && defined(HAVE_FCNTL_H)
38 # undef HAVE_FCNTL_H
39 #endif
40 
41 #ifdef _DEBUG
42 # undef _DEBUG
43 #endif
44 
45 #ifdef HAVE_STRFTIME
46 # undef HAVE_STRFTIME
47 #endif
48 #ifdef HAVE_STRING_H
49 # undef HAVE_STRING_H
50 #endif
51 #ifdef HAVE_PUTENV
52 # undef HAVE_PUTENV
53 #endif
54 #ifdef HAVE_STDARG_H
55 # undef HAVE_STDARG_H	// Python's config.h defines it as well.
56 #endif
57 #ifdef _POSIX_C_SOURCE
58 # undef _POSIX_C_SOURCE	// pyconfig.h defines it as well.
59 #endif
60 #ifdef _XOPEN_SOURCE
61 # undef _XOPEN_SOURCE	// pyconfig.h defines it as well.
62 #endif
63 
64 #define PY_SSIZE_T_CLEAN
65 
66 #include <Python.h>
67 
68 #if !defined(PY_VERSION_HEX) || PY_VERSION_HEX < 0x02050000
69 # undef PY_SSIZE_T_CLEAN
70 #endif
71 
72 #undef main // Defined in python.h - aargh
73 #undef HAVE_FCNTL_H // Clash with os_win32.h
74 
75 // Perhaps leave this out for Python 2.6, which supports bytes?
76 #define PyBytes_FromString      PyString_FromString
77 #define PyBytes_Check		PyString_Check
78 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
79 #define PyBytes_FromStringAndSize   PyString_FromStringAndSize
80 
81 #if !defined(FEAT_PYTHON) && defined(PROTO)
82 // Use this to be able to generate prototypes without python being used.
83 # define PyObject Py_ssize_t
84 # define PyThreadState Py_ssize_t
85 # define PyTypeObject Py_ssize_t
86 struct PyMethodDef { Py_ssize_t a; };
87 # define PySequenceMethods Py_ssize_t
88 #endif
89 
90 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
91 # define PY_USE_CAPSULE
92 #endif
93 
94 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
95 # define PyInt Py_ssize_t
96 # define PyInquiry lenfunc
97 # define PyIntArgFunc ssizeargfunc
98 # define PyIntIntArgFunc ssizessizeargfunc
99 # define PyIntObjArgProc ssizeobjargproc
100 # define PyIntIntObjArgProc ssizessizeobjargproc
101 # define Py_ssize_t_fmt "n"
102 #else
103 # define PyInt int
104 # define lenfunc inquiry
105 # define PyInquiry inquiry
106 # define PyIntArgFunc intargfunc
107 # define PyIntIntArgFunc intintargfunc
108 # define PyIntObjArgProc intobjargproc
109 # define PyIntIntObjArgProc intintobjargproc
110 # define Py_ssize_t_fmt "i"
111 #endif
112 #define Py_bytes_fmt "s"
113 
114 // Parser flags
115 #define single_input	256
116 #define file_input	257
117 #define eval_input	258
118 
119 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
120   // Python 2.3: can invoke ":python" recursively.
121 # define PY_CAN_RECURSE
122 #endif
123 
124 #if defined(DYNAMIC_PYTHON) || defined(PROTO)
125 # ifndef DYNAMIC_PYTHON
126 #  define HINSTANCE long_u		// for generating prototypes
127 # endif
128 
129 # ifndef MSWIN
130 #  include <dlfcn.h>
131 #  define FARPROC void*
132 #  define HINSTANCE void*
133 #  if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
134 #   define load_dll(n) dlopen((n), RTLD_LAZY)
135 #  else
136 #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
137 #  endif
138 #  define close_dll dlclose
139 #  define symbol_from_dll dlsym
140 # else
141 #  define load_dll vimLoadLib
142 #  define close_dll FreeLibrary
143 #  define symbol_from_dll GetProcAddress
144 # endif
145 
146 // This makes if_python.c compile without warnings against Python 2.5
147 // on Win32 and Win64.
148 # undef PyRun_SimpleString
149 # undef PyRun_String
150 # undef PyArg_Parse
151 # undef PyArg_ParseTuple
152 # undef Py_BuildValue
153 # undef Py_InitModule4
154 # undef Py_InitModule4_64
155 # undef PyObject_CallMethod
156 # undef PyObject_CallFunction
157 
158 /*
159  * Wrapper defines
160  */
161 # define PyArg_Parse dll_PyArg_Parse
162 # define PyArg_ParseTuple dll_PyArg_ParseTuple
163 # define PyMem_Free dll_PyMem_Free
164 # define PyMem_Malloc dll_PyMem_Malloc
165 # define PyDict_SetItemString dll_PyDict_SetItemString
166 # define PyErr_BadArgument dll_PyErr_BadArgument
167 # define PyErr_NewException dll_PyErr_NewException
168 # define PyErr_Clear dll_PyErr_Clear
169 # define PyErr_Format dll_PyErr_Format
170 # define PyErr_PrintEx dll_PyErr_PrintEx
171 # define PyErr_NoMemory dll_PyErr_NoMemory
172 # define PyErr_Occurred dll_PyErr_Occurred
173 # define PyErr_SetNone dll_PyErr_SetNone
174 # define PyErr_SetString dll_PyErr_SetString
175 # define PyErr_SetObject dll_PyErr_SetObject
176 # define PyErr_ExceptionMatches dll_PyErr_ExceptionMatches
177 # define PyEval_InitThreads dll_PyEval_InitThreads
178 # define PyEval_RestoreThread dll_PyEval_RestoreThread
179 # define PyEval_SaveThread dll_PyEval_SaveThread
180 # ifdef PY_CAN_RECURSE
181 #  define PyGILState_Ensure dll_PyGILState_Ensure
182 #  define PyGILState_Release dll_PyGILState_Release
183 # endif
184 # define PyInt_AsLong dll_PyInt_AsLong
185 # define PyInt_FromLong dll_PyInt_FromLong
186 # define PyLong_AsLong dll_PyLong_AsLong
187 # define PyLong_FromLong dll_PyLong_FromLong
188 # define PyBool_Type (*dll_PyBool_Type)
189 # define PyInt_Type (*dll_PyInt_Type)
190 # define PyLong_Type (*dll_PyLong_Type)
191 # define PyList_GetItem dll_PyList_GetItem
192 # define PyList_Append dll_PyList_Append
193 # define PyList_Insert dll_PyList_Insert
194 # define PyList_New dll_PyList_New
195 # define PyList_SetItem dll_PyList_SetItem
196 # define PyList_Size dll_PyList_Size
197 # define PyList_Type (*dll_PyList_Type)
198 # define PySequence_Check dll_PySequence_Check
199 # define PySequence_Size dll_PySequence_Size
200 # define PySequence_GetItem dll_PySequence_GetItem
201 # define PySequence_Fast dll_PySequence_Fast
202 # define PyTuple_Size dll_PyTuple_Size
203 # define PyTuple_GetItem dll_PyTuple_GetItem
204 # define PyTuple_Type (*dll_PyTuple_Type)
205 # define PySlice_GetIndicesEx dll_PySlice_GetIndicesEx
206 # define PyImport_ImportModule dll_PyImport_ImportModule
207 # define PyDict_New dll_PyDict_New
208 # define PyDict_GetItemString dll_PyDict_GetItemString
209 # define PyDict_Next dll_PyDict_Next
210 # define PyDict_Type (*dll_PyDict_Type)
211 # ifdef PyMapping_Keys
212 #  define PY_NO_MAPPING_KEYS
213 # else
214 #  define PyMapping_Keys dll_PyMapping_Keys
215 # endif
216 # define PyObject_GetItem dll_PyObject_GetItem
217 # define PyObject_CallMethod dll_PyObject_CallMethod
218 # define PyMapping_Check dll_PyMapping_Check
219 # define PyIter_Next dll_PyIter_Next
220 # define PyModule_GetDict dll_PyModule_GetDict
221 # define PyModule_AddObject dll_PyModule_AddObject
222 # define PyRun_SimpleString dll_PyRun_SimpleString
223 # define PyRun_String dll_PyRun_String
224 # define PyObject_GetAttrString dll_PyObject_GetAttrString
225 # define PyObject_HasAttrString dll_PyObject_HasAttrString
226 # define PyObject_SetAttrString dll_PyObject_SetAttrString
227 # define PyObject_CallFunctionObjArgs dll_PyObject_CallFunctionObjArgs
228 # define PyObject_CallFunction dll_PyObject_CallFunction
229 # define PyObject_Call dll_PyObject_Call
230 # define PyObject_Repr dll_PyObject_Repr
231 # define PyString_AsString dll_PyString_AsString
232 # define PyString_AsStringAndSize dll_PyString_AsStringAndSize
233 # define PyString_FromString dll_PyString_FromString
234 # define PyString_FromFormat dll_PyString_FromFormat
235 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize
236 # define PyString_Size dll_PyString_Size
237 # define PyString_Type (*dll_PyString_Type)
238 # define PyUnicode_Type (*dll_PyUnicode_Type)
239 # undef PyUnicode_AsEncodedString
240 # define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
241 # define PyFloat_AsDouble dll_PyFloat_AsDouble
242 # define PyFloat_FromDouble dll_PyFloat_FromDouble
243 # define PyFloat_Type (*dll_PyFloat_Type)
244 # define PyNumber_Check dll_PyNumber_Check
245 # define PyNumber_Long dll_PyNumber_Long
246 # define PyImport_AddModule (*dll_PyImport_AddModule)
247 # define PySys_SetObject dll_PySys_SetObject
248 # define PySys_GetObject dll_PySys_GetObject
249 # define PySys_SetArgv dll_PySys_SetArgv
250 # define PyType_Type (*dll_PyType_Type)
251 # define PyFile_Type (*dll_PyFile_Type)
252 # define PySlice_Type (*dll_PySlice_Type)
253 # define PyType_Ready (*dll_PyType_Ready)
254 # define PyType_GenericAlloc dll_PyType_GenericAlloc
255 # define Py_BuildValue dll_Py_BuildValue
256 # define Py_FindMethod dll_Py_FindMethod
257 # define Py_InitModule4 dll_Py_InitModule4
258 # define Py_SetPythonHome dll_Py_SetPythonHome
259 # define Py_Initialize dll_Py_Initialize
260 # define Py_Finalize dll_Py_Finalize
261 # define Py_IsInitialized dll_Py_IsInitialized
262 # define _PyObject_New dll__PyObject_New
263 # define _PyObject_GC_New dll__PyObject_GC_New
264 # ifdef PyObject_GC_Del
265 #  define Py_underscore_GC
266 #  define _PyObject_GC_Del dll__PyObject_GC_Del
267 #  define _PyObject_GC_UnTrack dll__PyObject_GC_UnTrack
268 # else
269 #  define PyObject_GC_Del dll_PyObject_GC_Del
270 #  define PyObject_GC_UnTrack dll_PyObject_GC_UnTrack
271 # endif
272 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
273 #  define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
274 # endif
275 # define _Py_NoneStruct (*dll__Py_NoneStruct)
276 # define _Py_ZeroStruct (*dll__Py_ZeroStruct)
277 # define _Py_TrueStruct (*dll__Py_TrueStruct)
278 # define PyObject_Init dll__PyObject_Init
279 # define PyObject_GetIter dll_PyObject_GetIter
280 # define PyObject_IsTrue dll_PyObject_IsTrue
281 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
282 #  define PyType_IsSubtype dll_PyType_IsSubtype
283 #  ifdef Py_DEBUG
284 #   define _Py_NegativeRefcount dll__Py_NegativeRefcount
285 #   define _Py_RefTotal (*dll__Py_RefTotal)
286 #   define _Py_Dealloc dll__Py_Dealloc
287 #  endif
288 # endif
289 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
290 #  if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
291 #   define _PyObject_DebugMalloc dll__PyObject_DebugMalloc
292 #   define _PyObject_DebugFree dll__PyObject_DebugFree
293 #  else
294 #   define PyObject_Malloc dll_PyObject_Malloc
295 #   define PyObject_Free dll_PyObject_Free
296 #  endif
297 # endif
298 # ifdef PY_USE_CAPSULE
299 #  define PyCapsule_New dll_PyCapsule_New
300 #  define PyCapsule_GetPointer dll_PyCapsule_GetPointer
301 # else
302 #  define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
303 #  define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
304 # endif
305 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
306 #  define Py_NoSiteFlag (*dll_Py_NoSiteFlag)
307 # endif
308 
309 /*
310  * Pointers for dynamic link
311  */
312 static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
313 static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
314 static int(*dll_PyMem_Free)(void *);
315 static void* (*dll_PyMem_Malloc)(size_t);
316 static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
317 static int(*dll_PyErr_BadArgument)(void);
318 static PyObject *(*dll_PyErr_NewException)(char *, PyObject *, PyObject *);
319 static void(*dll_PyErr_Clear)(void);
320 static PyObject*(*dll_PyErr_Format)(PyObject *, const char *, ...);
321 static void(*dll_PyErr_PrintEx)(int);
322 static PyObject*(*dll_PyErr_NoMemory)(void);
323 static PyObject*(*dll_PyErr_Occurred)(void);
324 static void(*dll_PyErr_SetNone)(PyObject *);
325 static void(*dll_PyErr_SetString)(PyObject *, const char *);
326 static void(*dll_PyErr_SetObject)(PyObject *, PyObject *);
327 static int(*dll_PyErr_ExceptionMatches)(PyObject *);
328 static void(*dll_PyEval_InitThreads)(void);
329 static void(*dll_PyEval_RestoreThread)(PyThreadState *);
330 static PyThreadState*(*dll_PyEval_SaveThread)(void);
331 # ifdef PY_CAN_RECURSE
332 static PyGILState_STATE	(*dll_PyGILState_Ensure)(void);
333 static void (*dll_PyGILState_Release)(PyGILState_STATE);
334 # endif
335 static long(*dll_PyInt_AsLong)(PyObject *);
336 static PyObject*(*dll_PyInt_FromLong)(long);
337 static long(*dll_PyLong_AsLong)(PyObject *);
338 static PyObject*(*dll_PyLong_FromLong)(long);
339 static PyTypeObject* dll_PyBool_Type;
340 static PyTypeObject* dll_PyInt_Type;
341 static PyTypeObject* dll_PyLong_Type;
342 static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
343 static int(*dll_PyList_Append)(PyObject *, PyObject *);
344 static int(*dll_PyList_Insert)(PyObject *, PyInt, PyObject *);
345 static PyObject*(*dll_PyList_New)(PyInt size);
346 static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
347 static PyInt(*dll_PyList_Size)(PyObject *);
348 static PyTypeObject* dll_PyList_Type;
349 static int (*dll_PySequence_Check)(PyObject *);
350 static PyInt(*dll_PySequence_Size)(PyObject *);
351 static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
352 static PyObject*(*dll_PySequence_Fast)(PyObject *, const char *);
353 static PyInt(*dll_PyTuple_Size)(PyObject *);
354 static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
355 static PyTypeObject* dll_PyTuple_Type;
356 static int (*dll_PySlice_GetIndicesEx)(PySliceObject *r, PyInt length,
357 		     PyInt *start, PyInt *stop, PyInt *step,
358 		     PyInt *slicelen);
359 static PyObject*(*dll_PyImport_ImportModule)(const char *);
360 static PyObject*(*dll_PyDict_New)(void);
361 static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
362 static int (*dll_PyDict_Next)(PyObject *, PyInt *, PyObject **, PyObject **);
363 static PyTypeObject* dll_PyDict_Type;
364 # ifndef PY_NO_MAPPING_KEYS
365 static PyObject* (*dll_PyMapping_Keys)(PyObject *);
366 # endif
367 static PyObject* (*dll_PyObject_GetItem)(PyObject *, PyObject *);
368 static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
369 static int (*dll_PyMapping_Check)(PyObject *);
370 static PyObject* (*dll_PyIter_Next)(PyObject *);
371 static PyObject*(*dll_PyModule_GetDict)(PyObject *);
372 static int(*dll_PyModule_AddObject)(PyObject *, const char *, PyObject *);
373 static int(*dll_PyRun_SimpleString)(char *);
374 static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
375 static PyObject* (*dll_PyObject_GetAttrString)(PyObject *, const char *);
376 static int (*dll_PyObject_HasAttrString)(PyObject *, const char *);
377 static int (*dll_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
378 static PyObject* (*dll_PyObject_CallFunctionObjArgs)(PyObject *, ...);
379 static PyObject* (*dll_PyObject_CallFunction)(PyObject *, char *, ...);
380 static PyObject* (*dll_PyObject_Call)(PyObject *, PyObject *, PyObject *);
381 static PyObject* (*dll_PyObject_Repr)(PyObject *);
382 static char*(*dll_PyString_AsString)(PyObject *);
383 static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, PyInt *);
384 static PyObject*(*dll_PyString_FromString)(const char *);
385 static PyObject*(*dll_PyString_FromFormat)(const char *, ...);
386 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
387 static PyInt(*dll_PyString_Size)(PyObject *);
388 static PyTypeObject* dll_PyString_Type;
389 static PyTypeObject* dll_PyUnicode_Type;
390 static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
391 static double(*dll_PyFloat_AsDouble)(PyObject *);
392 static PyObject*(*dll_PyFloat_FromDouble)(double);
393 static PyTypeObject* dll_PyFloat_Type;
394 static int(*dll_PyNumber_Check)(PyObject *);
395 static PyObject*(*dll_PyNumber_Long)(PyObject *);
396 static int(*dll_PySys_SetObject)(char *, PyObject *);
397 static PyObject *(*dll_PySys_GetObject)(char *);
398 static int(*dll_PySys_SetArgv)(int, char **);
399 static PyTypeObject* dll_PyType_Type;
400 static PyTypeObject* dll_PyFile_Type;
401 static PyTypeObject* dll_PySlice_Type;
402 static int (*dll_PyType_Ready)(PyTypeObject *type);
403 static PyObject* (*dll_PyType_GenericAlloc)(PyTypeObject *type, PyInt nitems);
404 static PyObject*(*dll_Py_BuildValue)(char *, ...);
405 static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
406 static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
407 static PyObject*(*dll_PyImport_AddModule)(char *);
408 static void(*dll_Py_SetPythonHome)(char *home);
409 static void(*dll_Py_Initialize)(void);
410 static void(*dll_Py_Finalize)(void);
411 static int(*dll_Py_IsInitialized)(void);
412 static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
413 static PyObject*(*dll__PyObject_GC_New)(PyTypeObject *);
414 # ifdef Py_underscore_GC
415 static void(*dll__PyObject_GC_Del)(void *);
416 static void(*dll__PyObject_GC_UnTrack)(void *);
417 # else
418 static void(*dll_PyObject_GC_Del)(void *);
419 static void(*dll_PyObject_GC_UnTrack)(void *);
420 # endif
421 static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
422 static PyObject* (*dll_PyObject_GetIter)(PyObject *);
423 static int (*dll_PyObject_IsTrue)(PyObject *);
424 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
425 static iternextfunc dll__PyObject_NextNotImplemented;
426 # endif
427 static PyObject* dll__Py_NoneStruct;
428 static PyObject* _Py_ZeroStruct;
429 static PyObject* dll__Py_TrueStruct;
430 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
431 static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
432 #  ifdef Py_DEBUG
433 static void (*dll__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
434 static PyInt* dll__Py_RefTotal;
435 static void (*dll__Py_Dealloc)(PyObject *obj);
436 #  endif
437 # endif
438 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
439 #  if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
440 static void (*dll__PyObject_DebugFree)(void*);
441 static void* (*dll__PyObject_DebugMalloc)(size_t);
442 #  else
443 static void* (*dll_PyObject_Malloc)(size_t);
444 static void (*dll_PyObject_Free)(void*);
445 #  endif
446 # endif
447 # ifdef PY_USE_CAPSULE
448 static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
449 static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
450 # else
451 static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
452 static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
453 # endif
454 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
455 static int* dll_Py_NoSiteFlag;
456 # endif
457 
458 static HINSTANCE hinstPython = 0; // Instance of python.dll
459 
460 // Imported exception objects
461 static PyObject *imp_PyExc_AttributeError;
462 static PyObject *imp_PyExc_IndexError;
463 static PyObject *imp_PyExc_KeyError;
464 static PyObject *imp_PyExc_KeyboardInterrupt;
465 static PyObject *imp_PyExc_TypeError;
466 static PyObject *imp_PyExc_ValueError;
467 static PyObject *imp_PyExc_SystemExit;
468 static PyObject *imp_PyExc_RuntimeError;
469 static PyObject *imp_PyExc_ImportError;
470 static PyObject *imp_PyExc_OverflowError;
471 
472 # define PyExc_AttributeError imp_PyExc_AttributeError
473 # define PyExc_IndexError imp_PyExc_IndexError
474 # define PyExc_KeyError imp_PyExc_KeyError
475 # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
476 # define PyExc_TypeError imp_PyExc_TypeError
477 # define PyExc_ValueError imp_PyExc_ValueError
478 # define PyExc_SystemExit imp_PyExc_SystemExit
479 # define PyExc_RuntimeError imp_PyExc_RuntimeError
480 # define PyExc_ImportError imp_PyExc_ImportError
481 # define PyExc_OverflowError imp_PyExc_OverflowError
482 
483 /*
484  * Table of name to function pointer of python.
485  */
486 # define PYTHON_PROC FARPROC
487 static struct
488 {
489     char *name;
490     PYTHON_PROC *ptr;
491 } python_funcname_table[] =
492 {
493 # ifndef PY_SSIZE_T_CLEAN
494     {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
495     {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
496     {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
497 # else
498     {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
499     {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
500     {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
501 # endif
502     {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
503     {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
504     {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
505     {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
506     {"PyErr_NewException", (PYTHON_PROC*)&dll_PyErr_NewException},
507     {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
508     {"PyErr_Format", (PYTHON_PROC*)&dll_PyErr_Format},
509     {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
510     {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
511     {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
512     {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
513     {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
514     {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject},
515     {"PyErr_ExceptionMatches", (PYTHON_PROC*)&dll_PyErr_ExceptionMatches},
516     {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
517     {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
518     {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
519 # ifdef PY_CAN_RECURSE
520     {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
521     {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
522 # endif
523     {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
524     {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
525     {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
526     {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
527     {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
528     {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
529     {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
530     {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
531     {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
532     {"PyList_Insert", (PYTHON_PROC*)&dll_PyList_Insert},
533     {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
534     {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
535     {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
536     {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
537     {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
538     {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
539     {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
540     {"PySequence_Fast", (PYTHON_PROC*)&dll_PySequence_Fast},
541     {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
542     {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
543     {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
544     {"PySlice_GetIndicesEx", (PYTHON_PROC*)&dll_PySlice_GetIndicesEx},
545     {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
546     {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
547     {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
548     {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
549     {"PyDict_Type", (PYTHON_PROC*)&dll_PyDict_Type},
550 # ifndef PY_NO_MAPPING_KEYS
551     {"PyMapping_Keys", (PYTHON_PROC*)&dll_PyMapping_Keys},
552 # endif
553     {"PyObject_GetItem", (PYTHON_PROC*)&dll_PyObject_GetItem},
554     {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
555     {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
556     {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
557     {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
558     {"PyModule_AddObject", (PYTHON_PROC*)&dll_PyModule_AddObject},
559     {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
560     {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
561     {"PyObject_GetAttrString", (PYTHON_PROC*)&dll_PyObject_GetAttrString},
562     {"PyObject_HasAttrString", (PYTHON_PROC*)&dll_PyObject_HasAttrString},
563     {"PyObject_SetAttrString", (PYTHON_PROC*)&dll_PyObject_SetAttrString},
564     {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&dll_PyObject_CallFunctionObjArgs},
565     {"PyObject_CallFunction", (PYTHON_PROC*)&dll_PyObject_CallFunction},
566     {"PyObject_Call", (PYTHON_PROC*)&dll_PyObject_Call},
567     {"PyObject_Repr", (PYTHON_PROC*)&dll_PyObject_Repr},
568     {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
569     {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
570     {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
571     {"PyString_FromFormat", (PYTHON_PROC*)&dll_PyString_FromFormat},
572     {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
573     {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
574     {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
575     {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
576     {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
577     {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
578     {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
579     {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
580     {"PyNumber_Check", (PYTHON_PROC*)&dll_PyNumber_Check},
581     {"PyNumber_Long", (PYTHON_PROC*)&dll_PyNumber_Long},
582     {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
583     {"PySys_GetObject", (PYTHON_PROC*)&dll_PySys_GetObject},
584     {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
585     {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
586     {"PyFile_Type", (PYTHON_PROC*)&dll_PyFile_Type},
587     {"PySlice_Type", (PYTHON_PROC*)&dll_PySlice_Type},
588     {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
589     {"PyType_GenericAlloc", (PYTHON_PROC*)&dll_PyType_GenericAlloc},
590     {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
591     {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
592     {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
593     {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
594     {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
595     {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
596     {"_PyObject_GC_New", (PYTHON_PROC*)&dll__PyObject_GC_New},
597 # ifdef Py_underscore_GC
598     {"_PyObject_GC_Del", (PYTHON_PROC*)&dll__PyObject_GC_Del},
599     {"_PyObject_GC_UnTrack", (PYTHON_PROC*)&dll__PyObject_GC_UnTrack},
600 # else
601     {"PyObject_GC_Del", (PYTHON_PROC*)&dll_PyObject_GC_Del},
602     {"PyObject_GC_UnTrack", (PYTHON_PROC*)&dll_PyObject_GC_UnTrack},
603 # endif
604     {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
605     {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
606     {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue},
607 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
608     {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
609 # endif
610     {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
611     {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
612     {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
613 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
614 #  ifdef Py_DEBUG
615     {"_Py_NegativeRefcount", (PYTHON_PROC*)&dll__Py_NegativeRefcount},
616     {"_Py_RefTotal", (PYTHON_PROC*)&dll__Py_RefTotal},
617     {"_Py_Dealloc", (PYTHON_PROC*)&dll__Py_Dealloc},
618 #  endif
619     {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
620 # endif
621 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
622 #  if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
623     {"_PyObject_DebugFree", (PYTHON_PROC*)&dll__PyObject_DebugFree},
624     {"_PyObject_DebugMalloc", (PYTHON_PROC*)&dll__PyObject_DebugMalloc},
625 #  else
626     {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
627     {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
628 #  endif
629 # endif
630 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
631 	&& SIZEOF_SIZE_T != VIM_SIZEOF_INT
632 #  ifdef Py_DEBUG
633     {"Py_InitModule4TraceRefs_64", (PYTHON_PROC*)&dll_Py_InitModule4},
634 #  else
635     {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
636 #  endif
637 # else
638 #  ifdef Py_DEBUG
639     {"Py_InitModule4TraceRefs", (PYTHON_PROC*)&dll_Py_InitModule4},
640 #  else
641     {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
642 #  endif
643 # endif
644 # ifdef PY_USE_CAPSULE
645     {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
646     {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
647 # else
648     {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
649     {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
650 # endif
651 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
652     {"Py_NoSiteFlag", (PYTHON_PROC*)&dll_Py_NoSiteFlag},
653 # endif
654     {"", NULL},
655 };
656 
657 /*
658  * Load library and get all pointers.
659  * Parameter 'libname' provides name of DLL.
660  * Return OK or FAIL.
661  */
662     static int
663 python_runtime_link_init(char *libname, int verbose)
664 {
665     int i;
666     PYTHON_PROC *ucs_as_encoded_string =
667 				   (PYTHON_PROC*)&py_PyUnicode_AsEncodedString;
668 
669 # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
670     // Can't have Python and Python3 loaded at the same time.
671     // It cause a crash, because RTLD_GLOBAL is needed for
672     // standard C extension libraries of one or both python versions.
673     if (python3_loaded())
674     {
675 	if (verbose)
676 	    emsg(_("E836: This Vim cannot execute :python after using :py3"));
677 	return FAIL;
678     }
679 # endif
680 
681     if (hinstPython)
682 	return OK;
683     hinstPython = load_dll(libname);
684     if (!hinstPython)
685     {
686 	if (verbose)
687 	    semsg(_(e_loadlib), libname);
688 	return FAIL;
689     }
690 
691     for (i = 0; python_funcname_table[i].ptr; ++i)
692     {
693 	if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
694 			python_funcname_table[i].name)) == NULL)
695 	{
696 	    close_dll(hinstPython);
697 	    hinstPython = 0;
698 	    if (verbose)
699 		semsg(_(e_loadfunc), python_funcname_table[i].name);
700 	    return FAIL;
701 	}
702     }
703 
704     // Load unicode functions separately as only the ucs2 or the ucs4 functions
705     // will be present in the library.
706     *ucs_as_encoded_string = symbol_from_dll(hinstPython,
707 					     "PyUnicodeUCS2_AsEncodedString");
708     if (*ucs_as_encoded_string == NULL)
709 	*ucs_as_encoded_string = symbol_from_dll(hinstPython,
710 					     "PyUnicodeUCS4_AsEncodedString");
711     if (*ucs_as_encoded_string == NULL)
712     {
713 	close_dll(hinstPython);
714 	hinstPython = 0;
715 	if (verbose)
716 	    semsg(_(e_loadfunc), "PyUnicode_UCSX_*");
717 	return FAIL;
718     }
719 
720     return OK;
721 }
722 
723 /*
724  * If python is enabled (there is installed python on Windows system) return
725  * TRUE, else FALSE.
726  */
727     int
728 python_enabled(int verbose)
729 {
730     return python_runtime_link_init((char *)p_pydll, verbose) == OK;
731 }
732 
733 /*
734  * Load the standard Python exceptions - don't import the symbols from the
735  * DLL, as this can cause errors (importing data symbols is not reliable).
736  */
737     static void
738 get_exceptions(void)
739 {
740     PyObject *exmod = PyImport_ImportModule("exceptions");
741     PyObject *exdict = PyModule_GetDict(exmod);
742     imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
743     imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
744     imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
745     imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
746     imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
747     imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
748     imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
749     imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
750     imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
751     imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
752     Py_XINCREF(imp_PyExc_AttributeError);
753     Py_XINCREF(imp_PyExc_IndexError);
754     Py_XINCREF(imp_PyExc_KeyError);
755     Py_XINCREF(imp_PyExc_KeyboardInterrupt);
756     Py_XINCREF(imp_PyExc_TypeError);
757     Py_XINCREF(imp_PyExc_ValueError);
758     Py_XINCREF(imp_PyExc_SystemExit);
759     Py_XINCREF(imp_PyExc_RuntimeError);
760     Py_XINCREF(imp_PyExc_ImportError);
761     Py_XINCREF(imp_PyExc_OverflowError);
762     Py_XDECREF(exmod);
763 }
764 #endif // DYNAMIC_PYTHON
765 
766 static int initialised = 0;
767 #define PYINITIALISED initialised
768 static int python_end_called = FALSE;
769 
770 #define DESTRUCTOR_FINISH(self) self->ob_type->tp_free((PyObject*)self);
771 
772 #define WIN_PYTHON_REF(win) win->w_python_ref
773 #define BUF_PYTHON_REF(buf) buf->b_python_ref
774 #define TAB_PYTHON_REF(tab) tab->tp_python_ref
775 
776 static PyObject *OutputGetattr(PyObject *, char *);
777 static PyObject *BufferGetattr(PyObject *, char *);
778 static PyObject *WindowGetattr(PyObject *, char *);
779 static PyObject *TabPageGetattr(PyObject *, char *);
780 static PyObject *RangeGetattr(PyObject *, char *);
781 static PyObject *DictionaryGetattr(PyObject *, char*);
782 static PyObject *ListGetattr(PyObject *, char *);
783 static PyObject *FunctionGetattr(PyObject *, char *);
784 
785 #ifndef Py_VISIT
786 # define Py_VISIT(obj) visit(obj, arg)
787 #endif
788 #ifndef Py_CLEAR
789 # define Py_CLEAR(obj) \
790     { \
791 	Py_XDECREF(obj); \
792 	obj = NULL; \
793     }
794 #endif
795 
796 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
797     static void *
798 py_memsave(void *p, size_t len)
799 {
800     void	*r;
801 
802     if (!(r = PyMem_Malloc(len)))
803 	return NULL;
804     mch_memmove(r, p, len);
805     return r;
806 }
807 
808 # define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
809 #endif
810 
811 typedef PySliceObject PySliceObject_T;
812 
813 /*
814  * Include the code shared with if_python3.c
815  */
816 #include "if_py_both.h"
817 
818 
819 ///////////////////////////////////////////////////////
820 // Internal function prototypes.
821 
822 static int PythonMod_Init(void);
823 
824 
825 ///////////////////////////////////////////////////////
826 // 1. Python interpreter main program.
827 
828 #if PYTHON_API_VERSION < 1007 // Python 1.4
829 typedef PyObject PyThreadState;
830 #endif
831 
832 #ifndef PY_CAN_RECURSE
833 static PyThreadState *saved_python_thread = NULL;
834 
835 /*
836  * Suspend a thread of the Python interpreter, other threads are allowed to
837  * run.
838  */
839     static void
840 Python_SaveThread(void)
841 {
842     saved_python_thread = PyEval_SaveThread();
843 }
844 
845 /*
846  * Restore a thread of the Python interpreter, waits for other threads to
847  * block.
848  */
849     static void
850 Python_RestoreThread(void)
851 {
852     PyEval_RestoreThread(saved_python_thread);
853     saved_python_thread = NULL;
854 }
855 #endif
856 
857     void
858 python_end(void)
859 {
860     static int recurse = 0;
861 
862     // If a crash occurs while doing this, don't try again.
863     if (recurse != 0)
864 	return;
865 
866     python_end_called = TRUE;
867     ++recurse;
868 
869 #ifdef DYNAMIC_PYTHON
870     if (hinstPython && Py_IsInitialized())
871     {
872 # ifdef PY_CAN_RECURSE
873 	PyGILState_Ensure();
874 # else
875 	Python_RestoreThread();	    // enter python
876 # endif
877 	Py_Finalize();
878     }
879 #else
880     if (Py_IsInitialized())
881     {
882 # ifdef PY_CAN_RECURSE
883 	PyGILState_Ensure();
884 # else
885 	Python_RestoreThread();	    // enter python
886 # endif
887 	Py_Finalize();
888     }
889 #endif
890 
891     --recurse;
892 }
893 
894 #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
895     int
896 python_loaded(void)
897 {
898     return (hinstPython != 0);
899 }
900 #endif
901 
902 static char *py_home_buf = NULL;
903 
904     static int
905 Python_Init(void)
906 {
907     if (!initialised)
908     {
909 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
910 	PyObject *site;
911 #endif
912 
913 #ifdef DYNAMIC_PYTHON
914 	if (!python_enabled(TRUE))
915 	{
916 	    emsg(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
917 	    goto fail;
918 	}
919 #endif
920 
921 	if (*p_pyhome != NUL)
922 	{
923 	    // The string must not change later, make a copy in static memory.
924 	    py_home_buf = (char *)vim_strsave(p_pyhome);
925 	    if (py_home_buf != NULL)
926 		Py_SetPythonHome(py_home_buf);
927 	}
928 #ifdef PYTHON_HOME
929 	else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
930 	    Py_SetPythonHome(PYTHON_HOME);
931 #endif
932 
933 	init_structs();
934 
935 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
936 	// Disable implicit 'import site', because it may cause Vim to exit
937 	// when it can't be found.
938 	Py_NoSiteFlag++;
939 #endif
940 
941 	Py_Initialize();
942 
943 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
944 	// 'import site' explicitly.
945 	site = PyImport_ImportModule("site");
946 	if (site == NULL)
947 	{
948 	    emsg(_("E887: Sorry, this command is disabled, the Python's site module could not be loaded."));
949 	    goto fail;
950 	}
951 	Py_DECREF(site);
952 #endif
953 
954 	// Initialise threads, and below save the state using
955 	// PyEval_SaveThread.  Without the call to PyEval_SaveThread, thread
956 	// specific state (such as the system trace hook), will be lost
957 	// between invocations of Python code.
958 	PyEval_InitThreads();
959 #ifdef DYNAMIC_PYTHON
960 	get_exceptions();
961 #endif
962 
963 	if (PythonIO_Init_io())
964 	    goto fail;
965 
966 	if (PythonMod_Init())
967 	    goto fail;
968 
969 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
970 
971 	// Remove the element from sys.path that was added because of our
972 	// argv[0] value in PythonMod_Init().  Previously we used an empty
973 	// string, but depending on the OS we then get an empty entry or
974 	// the current directory in sys.path.
975 	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
976 
977 	// lock is created and acquired in PyEval_InitThreads() and thread
978 	// state is created in Py_Initialize()
979 	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
980 	// (python must have threads enabled!)
981 	// so the following does both: unlock GIL and save thread state in TLS
982 	// without deleting thread state
983 #ifndef PY_CAN_RECURSE
984 	saved_python_thread =
985 #endif
986 	    PyEval_SaveThread();
987 
988 	initialised = 1;
989     }
990 
991     return 0;
992 
993 fail:
994     // We call PythonIO_Flush() here to print any Python errors.
995     // This is OK, as it is possible to call this function even
996     // if PythonIO_Init_io() has not completed successfully (it will
997     // not do anything in this case).
998     PythonIO_Flush();
999     return -1;
1000 }
1001 
1002 /*
1003  * External interface
1004  */
1005     static void
1006 DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
1007 {
1008 #ifndef PY_CAN_RECURSE
1009     static int		recursive = 0;
1010 #endif
1011 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1012     char		*saved_locale;
1013 #endif
1014 #ifdef PY_CAN_RECURSE
1015     PyGILState_STATE	pygilstate;
1016 #endif
1017 
1018 #ifndef PY_CAN_RECURSE
1019     if (recursive)
1020     {
1021 	emsg(_("E659: Cannot invoke Python recursively"));
1022 	return;
1023     }
1024     ++recursive;
1025 #endif
1026     if (python_end_called)
1027 	return;
1028 
1029     if (Python_Init())
1030 	goto theend;
1031 
1032     init_range(arg);
1033 
1034     Python_Release_Vim();	    // leave Vim
1035 
1036 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1037     // Python only works properly when the LC_NUMERIC locale is "C".
1038     saved_locale = setlocale(LC_NUMERIC, NULL);
1039     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
1040 	saved_locale = NULL;
1041     else
1042     {
1043 	// Need to make a copy, value may change when setting new locale.
1044 	saved_locale = (char *) PY_STRSAVE(saved_locale);
1045 	(void)setlocale(LC_NUMERIC, "C");
1046     }
1047 #endif
1048 
1049 #ifdef PY_CAN_RECURSE
1050     pygilstate = PyGILState_Ensure();
1051 #else
1052     Python_RestoreThread();	    // enter python
1053 #endif
1054 
1055     run((char *) cmd, arg
1056 #ifdef PY_CAN_RECURSE
1057 	    , &pygilstate
1058 #endif
1059 	    );
1060 
1061 #ifdef PY_CAN_RECURSE
1062     PyGILState_Release(pygilstate);
1063 #else
1064     Python_SaveThread();	    // leave python
1065 #endif
1066 
1067 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1068     if (saved_locale != NULL)
1069     {
1070 	(void)setlocale(LC_NUMERIC, saved_locale);
1071 	PyMem_Free(saved_locale);
1072     }
1073 #endif
1074 
1075     Python_Lock_Vim();		    // enter vim
1076     PythonIO_Flush();
1077 
1078 theend:
1079 #ifndef PY_CAN_RECURSE
1080     --recursive;
1081 #endif
1082     return;
1083 }
1084 
1085 /*
1086  * ":python"
1087  */
1088     void
1089 ex_python(exarg_T *eap)
1090 {
1091     char_u *script;
1092 
1093     script = script_get(eap, eap->arg);
1094     if (!eap->skip)
1095     {
1096 	if (p_pyx == 0)
1097 	    p_pyx = 2;
1098 
1099 	DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1100 		(rangeinitializer) init_range_cmd,
1101 		(runner) run_cmd,
1102 		(void *) eap);
1103     }
1104     vim_free(script);
1105 }
1106 
1107 #define BUFFER_SIZE 1024
1108 
1109 /*
1110  * ":pyfile"
1111  */
1112     void
1113 ex_pyfile(exarg_T *eap)
1114 {
1115     static char buffer[BUFFER_SIZE];
1116     const char *file = (char *)eap->arg;
1117     char *p;
1118 
1119     if (p_pyx == 0)
1120 	p_pyx = 2;
1121 
1122     // Have to do it like this. PyRun_SimpleFile requires you to pass a
1123     // stdio file pointer, but Vim and the Python DLL are compiled with
1124     // different options under Windows, meaning that stdio pointers aren't
1125     // compatible between the two. Yuk.
1126     //
1127     // Put the string "execfile('file')" into buffer. But, we need to
1128     // escape any backslashes or single quotes in the file name, so that
1129     // Python won't mangle the file name.
1130     strcpy(buffer, "execfile('");
1131     p = buffer + 10; // size of "execfile('"
1132 
1133     while (*file && p < buffer + (BUFFER_SIZE - 3))
1134     {
1135 	if (*file == '\\' || *file == '\'')
1136 	    *p++ = '\\';
1137 	*p++ = *file++;
1138     }
1139 
1140     // If we didn't finish the file name, we hit a buffer overflow
1141     if (*file != '\0')
1142 	return;
1143 
1144     // Put in the terminating "')" and a null
1145     *p++ = '\'';
1146     *p++ = ')';
1147     *p++ = '\0';
1148 
1149     // Execute the file
1150     DoPyCommand(buffer,
1151 	    (rangeinitializer) init_range_cmd,
1152 	    (runner) run_cmd,
1153 	    (void *) eap);
1154 }
1155 
1156     void
1157 ex_pydo(exarg_T *eap)
1158 {
1159     if (p_pyx == 0)
1160 	p_pyx = 2;
1161 
1162     DoPyCommand((char *)eap->arg,
1163 	    (rangeinitializer) init_range_cmd,
1164 	    (runner)run_do,
1165 	    (void *)eap);
1166 }
1167 
1168 ///////////////////////////////////////////////////////
1169 // 2. Python output stream: writes output via [e]msg().
1170 
1171 // Implementation functions
1172 
1173     static PyObject *
1174 OutputGetattr(PyObject *self, char *name)
1175 {
1176     if (strcmp(name, "softspace") == 0)
1177 	return PyInt_FromLong(((OutputObject *)(self))->softspace);
1178     else if (strcmp(name, "__members__") == 0)
1179 	return ObjectDir(NULL, OutputAttrs);
1180     else if (strcmp(name, "errors") == 0)
1181 	return PyString_FromString("strict");
1182     else if (strcmp(name, "encoding") == 0)
1183 	return PyString_FromString(ENC_OPT);
1184     return Py_FindMethod(OutputMethods, self, name);
1185 }
1186 
1187 ///////////////////////////////////////////////////////
1188 // 3. Implementation of the Vim module for Python
1189 
1190 // Window type - Implementation functions
1191 // --------------------------------------
1192 
1193 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1194 
1195 // Buffer type - Implementation functions
1196 // --------------------------------------
1197 
1198 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1199 
1200 static int BufferAssItem(PyObject *, PyInt, PyObject *);
1201 static int BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
1202 
1203 // Line range type - Implementation functions
1204 // --------------------------------------
1205 
1206 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1207 
1208 static int RangeAssItem(PyObject *, PyInt, PyObject *);
1209 static int RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
1210 
1211 // Current objects type - Implementation functions
1212 // -----------------------------------------------
1213 
1214 static PySequenceMethods BufferAsSeq = {
1215     (PyInquiry)		BufferLength,	    // sq_length,    len(x)
1216     (binaryfunc)	0,		    // BufferConcat, sq_concat, x+y
1217     (PyIntArgFunc)	0,		    // BufferRepeat, sq_repeat, x*n
1218     (PyIntArgFunc)	BufferItem,	    // sq_item,      x[i]
1219     (PyIntIntArgFunc)	BufferSlice,	    // sq_slice,     x[i:j]
1220     (PyIntObjArgProc)	BufferAssItem,	    // sq_ass_item,  x[i]=v
1221     (PyIntIntObjArgProc) BufferAssSlice,    // sq_ass_slice, x[i:j]=v
1222     (objobjproc)	0,
1223     (binaryfunc)	0,
1224     0,
1225 };
1226 
1227 // Buffer object - Implementation
1228 
1229     static PyObject *
1230 BufferGetattr(PyObject *self, char *name)
1231 {
1232     PyObject *r;
1233 
1234     if ((r = BufferAttrValid((BufferObject *)(self), name)))
1235 	return r;
1236 
1237     if (CheckBuffer((BufferObject *)(self)))
1238 	return NULL;
1239 
1240     r = BufferAttr((BufferObject *)(self), name);
1241     if (r || PyErr_Occurred())
1242 	return r;
1243     else
1244 	return Py_FindMethod(BufferMethods, self, name);
1245 }
1246 
1247 //////////////////
1248 
1249     static int
1250 BufferAssItem(PyObject *self, PyInt n, PyObject *val)
1251 {
1252     return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
1253 }
1254 
1255     static int
1256 BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1257 {
1258     return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
1259 }
1260 
1261 static PySequenceMethods RangeAsSeq = {
1262     (PyInquiry)		RangeLength,	      // sq_length,    len(x)
1263     (binaryfunc)	0, /* RangeConcat, */ // sq_concat,    x+y
1264     (PyIntArgFunc)	0, /* RangeRepeat, */ // sq_repeat,    x*n
1265     (PyIntArgFunc)	RangeItem,	      // sq_item,      x[i]
1266     (PyIntIntArgFunc)	RangeSlice,	      // sq_slice,     x[i:j]
1267     (PyIntObjArgProc)	RangeAssItem,	      // sq_ass_item,  x[i]=v
1268     (PyIntIntObjArgProc) RangeAssSlice,	      // sq_ass_slice, x[i:j]=v
1269     (objobjproc)	0,
1270 #if PY_MAJOR_VERSION >= 2
1271     (binaryfunc)	0,
1272     0,
1273 #endif
1274 };
1275 
1276 // Line range object - Implementation
1277 
1278     static PyObject *
1279 RangeGetattr(PyObject *self, char *name)
1280 {
1281     if (strcmp(name, "start") == 0)
1282 	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
1283     else if (strcmp(name, "end") == 0)
1284 	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
1285     else if (strcmp(name, "__members__") == 0)
1286 	return ObjectDir(NULL, RangeAttrs);
1287     else
1288 	return Py_FindMethod(RangeMethods, self, name);
1289 }
1290 
1291 ////////////////
1292 
1293     static int
1294 RangeAssItem(PyObject *self, PyInt n, PyObject *val)
1295 {
1296     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1297 		     ((RangeObject *)(self))->start,
1298 		     ((RangeObject *)(self))->end,
1299 		     &((RangeObject *)(self))->end);
1300 }
1301 
1302     static int
1303 RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1304 {
1305     return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1306 		      ((RangeObject *)(self))->start,
1307 		      ((RangeObject *)(self))->end,
1308 		      &((RangeObject *)(self))->end);
1309 }
1310 
1311 // TabPage object - Implementation
1312 
1313     static PyObject *
1314 TabPageGetattr(PyObject *self, char *name)
1315 {
1316     PyObject *r;
1317 
1318     if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1319 	return r;
1320 
1321     if (CheckTabPage((TabPageObject *)(self)))
1322 	return NULL;
1323 
1324     r = TabPageAttr((TabPageObject *)(self), name);
1325     if (r || PyErr_Occurred())
1326 	return r;
1327     else
1328 	return Py_FindMethod(TabPageMethods, self, name);
1329 }
1330 
1331 // Window object - Implementation
1332 
1333     static PyObject *
1334 WindowGetattr(PyObject *self, char *name)
1335 {
1336     PyObject *r;
1337 
1338     if ((r = WindowAttrValid((WindowObject *)(self), name)))
1339 	return r;
1340 
1341     if (CheckWindow((WindowObject *)(self)))
1342 	return NULL;
1343 
1344     r = WindowAttr((WindowObject *)(self), name);
1345     if (r || PyErr_Occurred())
1346 	return r;
1347     else
1348 	return Py_FindMethod(WindowMethods, self, name);
1349 }
1350 
1351 // Tab page list object - Definitions
1352 
1353 static PySequenceMethods TabListAsSeq = {
1354     (PyInquiry)		TabListLength,	    // sq_length,    len(x)
1355     (binaryfunc)	0,		    // sq_concat,    x+y
1356     (PyIntArgFunc)	0,		    // sq_repeat,    x*n
1357     (PyIntArgFunc)	TabListItem,	    // sq_item,      x[i]
1358     (PyIntIntArgFunc)	0,		    // sq_slice,     x[i:j]
1359     (PyIntObjArgProc)	0,		    // sq_ass_item,  x[i]=v
1360     (PyIntIntObjArgProc) 0,		    // sq_ass_slice, x[i:j]=v
1361     (objobjproc)	0,
1362 #if PY_MAJOR_VERSION >= 2
1363     (binaryfunc)	0,
1364     0,
1365 #endif
1366 };
1367 
1368 // Window list object - Definitions
1369 
1370 static PySequenceMethods WinListAsSeq = {
1371     (PyInquiry)		WinListLength,	    // sq_length,    len(x)
1372     (binaryfunc)	0,		    // sq_concat,    x+y
1373     (PyIntArgFunc)	0,		    // sq_repeat,    x*n
1374     (PyIntArgFunc)	WinListItem,	    // sq_item,      x[i]
1375     (PyIntIntArgFunc)	0,		    // sq_slice,     x[i:j]
1376     (PyIntObjArgProc)	0,		    // sq_ass_item,  x[i]=v
1377     (PyIntIntObjArgProc) 0,		    // sq_ass_slice, x[i:j]=v
1378     (objobjproc)	0,
1379 #if PY_MAJOR_VERSION >= 2
1380     (binaryfunc)	0,
1381     0,
1382 #endif
1383 };
1384 
1385 // External interface
1386 
1387     void
1388 python_buffer_free(buf_T *buf)
1389 {
1390     if (BUF_PYTHON_REF(buf) != NULL)
1391     {
1392 	BufferObject *bp = BUF_PYTHON_REF(buf);
1393 	bp->buf = INVALID_BUFFER_VALUE;
1394 	BUF_PYTHON_REF(buf) = NULL;
1395     }
1396 }
1397 
1398     void
1399 python_window_free(win_T *win)
1400 {
1401     if (WIN_PYTHON_REF(win) != NULL)
1402     {
1403 	WindowObject *wp = WIN_PYTHON_REF(win);
1404 	wp->win = INVALID_WINDOW_VALUE;
1405 	WIN_PYTHON_REF(win) = NULL;
1406     }
1407 }
1408 
1409     void
1410 python_tabpage_free(tabpage_T *tab)
1411 {
1412     if (TAB_PYTHON_REF(tab) != NULL)
1413     {
1414 	TabPageObject *tp = TAB_PYTHON_REF(tab);
1415 	tp->tab = INVALID_TABPAGE_VALUE;
1416 	TAB_PYTHON_REF(tab) = NULL;
1417     }
1418 }
1419 
1420     static int
1421 PythonMod_Init(void)
1422 {
1423     // The special value is removed from sys.path in Python_Init().
1424     static char	*(argv[2]) = {"/must>not&exist/foo", NULL};
1425 
1426     if (init_types())
1427 	return -1;
1428 
1429     // Set sys.argv[] to avoid a crash in warn().
1430     PySys_SetArgv(1, argv);
1431 
1432     vim_module = Py_InitModule4("vim", VimMethods, (char *)NULL,
1433 				(PyObject *)NULL, PYTHON_API_VERSION);
1434 
1435     if (populate_module(vim_module))
1436 	return -1;
1437 
1438     if (init_sys_path())
1439 	return -1;
1440 
1441     return 0;
1442 }
1443 
1444 //////////////////////////////////////////////////////////////////////////
1445 // 4. Utility functions for handling the interface between Vim and Python.
1446 
1447 // Convert a Vim line into a Python string.
1448 // All internal newlines are replaced by null characters.
1449 //
1450 // On errors, the Python exception data is set, and NULL is returned.
1451     static PyObject *
1452 LineToString(const char *str)
1453 {
1454     PyObject *result;
1455     PyInt len = strlen(str);
1456     char *p;
1457 
1458     // Allocate an Python string object, with uninitialised contents. We
1459     // must do it this way, so that we can modify the string in place
1460     // later. See the Python source, Objects/stringobject.c for details.
1461     result = PyString_FromStringAndSize(NULL, len);
1462     if (result == NULL)
1463 	return NULL;
1464 
1465     p = PyString_AsString(result);
1466 
1467     while (*str)
1468     {
1469 	if (*str == '\n')
1470 	    *p = '\0';
1471 	else
1472 	    *p = *str;
1473 
1474 	++p;
1475 	++str;
1476     }
1477 
1478     return result;
1479 }
1480 
1481     static PyObject *
1482 DictionaryGetattr(PyObject *self, char *name)
1483 {
1484     DictionaryObject	*this = ((DictionaryObject *) (self));
1485 
1486     if (strcmp(name, "locked") == 0)
1487 	return PyInt_FromLong(this->dict->dv_lock);
1488     else if (strcmp(name, "scope") == 0)
1489 	return PyInt_FromLong(this->dict->dv_scope);
1490     else if (strcmp(name, "__members__") == 0)
1491 	return ObjectDir(NULL, DictionaryAttrs);
1492 
1493     return Py_FindMethod(DictionaryMethods, self, name);
1494 }
1495 
1496     static PyObject *
1497 ListGetattr(PyObject *self, char *name)
1498 {
1499     if (strcmp(name, "locked") == 0)
1500 	return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1501     else if (strcmp(name, "__members__") == 0)
1502 	return ObjectDir(NULL, ListAttrs);
1503 
1504     return Py_FindMethod(ListMethods, self, name);
1505 }
1506 
1507     static PyObject *
1508 FunctionGetattr(PyObject *self, char *name)
1509 {
1510     PyObject	*r;
1511 
1512     r = FunctionAttr((FunctionObject *)(self), name);
1513 
1514     if (r || PyErr_Occurred())
1515 	return r;
1516     else
1517 	return Py_FindMethod(FunctionMethods, self, name);
1518 }
1519 
1520     void
1521 do_pyeval(char_u *str, typval_T *rettv)
1522 {
1523     DoPyCommand((char *) str,
1524 	    (rangeinitializer) init_range_eval,
1525 	    (runner) run_eval,
1526 	    (void *) rettv);
1527     if (rettv->v_type == VAR_UNKNOWN)
1528     {
1529 	rettv->v_type = VAR_NUMBER;
1530 	rettv->vval.v_number = 0;
1531     }
1532 }
1533 
1534 // Don't generate a prototype for the next function, it generates an error on
1535 // newer Python versions.
1536 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1537 
1538     char *
1539 Py_GetProgramName(void)
1540 {
1541     return "vim";
1542 }
1543 #endif // Python 1.4
1544 
1545     int
1546 set_ref_in_python(int copyID)
1547 {
1548     return set_ref_in_py(copyID);
1549 }
1550