xref: /vim-8.2.3635/src/if_python.c (revision 1a3e5747)
1db913953SBram Moolenaar /* vi:set ts=8 sts=4 sw=4 noet:
2071d4279SBram Moolenaar  *
3071d4279SBram Moolenaar  * VIM - Vi IMproved	by Bram Moolenaar
4071d4279SBram Moolenaar  *
5071d4279SBram Moolenaar  * Do ":help uganda"  in Vim to read copying and usage conditions.
6071d4279SBram Moolenaar  * Do ":help credits" in Vim to see a list of people who contributed.
7071d4279SBram Moolenaar  * See README.txt for an overview of the Vim source code.
8071d4279SBram Moolenaar  */
9071d4279SBram Moolenaar /*
10071d4279SBram Moolenaar  * Python extensions by Paul Moore.
11071d4279SBram Moolenaar  * Changes for Unix by David Leonard.
12071d4279SBram Moolenaar  *
13071d4279SBram Moolenaar  * This consists of four parts:
14071d4279SBram Moolenaar  * 1. Python interpreter main program
15071d4279SBram Moolenaar  * 2. Python output stream: writes output via [e]msg().
16071d4279SBram Moolenaar  * 3. Implementation of the Vim module for Python
17071d4279SBram Moolenaar  * 4. Utility functions for handling the interface between Vim and Python.
18071d4279SBram Moolenaar  */
19071d4279SBram Moolenaar 
20071d4279SBram Moolenaar #include "vim.h"
21071d4279SBram Moolenaar 
22071d4279SBram Moolenaar #include <limits.h>
23071d4279SBram Moolenaar 
242ab2e860SBram Moolenaar // uncomment this if used with the debug version of python.
252ab2e860SBram Moolenaar // Checked on 2.7.4.
262ab2e860SBram Moolenaar // #define Py_DEBUG
272ab2e860SBram Moolenaar // Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
282ab2e860SBram Moolenaar // uncomment this if used with the debug version of python, but without its
292ab2e860SBram Moolenaar // allocator
302ab2e860SBram Moolenaar // #define Py_DEBUG_NO_PYMALLOC
310014a53aSBram Moolenaar 
322ab2e860SBram Moolenaar // Python.h defines _POSIX_THREADS itself (if needed)
33071d4279SBram Moolenaar #ifdef _POSIX_THREADS
34071d4279SBram Moolenaar # undef _POSIX_THREADS
35071d4279SBram Moolenaar #endif
36071d4279SBram Moolenaar 
374f97475dSBram Moolenaar #if defined(MSWIN) && defined(HAVE_FCNTL_H)
38071d4279SBram Moolenaar # undef HAVE_FCNTL_H
39071d4279SBram Moolenaar #endif
40071d4279SBram Moolenaar 
41071d4279SBram Moolenaar #ifdef _DEBUG
42071d4279SBram Moolenaar # undef _DEBUG
43071d4279SBram Moolenaar #endif
44071d4279SBram Moolenaar 
456aa2cd4bSBram Moolenaar #ifdef HAVE_STRFTIME
466aa2cd4bSBram Moolenaar # undef HAVE_STRFTIME
476aa2cd4bSBram Moolenaar #endif
486aa2cd4bSBram Moolenaar #ifdef HAVE_STRING_H
496aa2cd4bSBram Moolenaar # undef HAVE_STRING_H
506aa2cd4bSBram Moolenaar #endif
516aa2cd4bSBram Moolenaar #ifdef HAVE_PUTENV
526aa2cd4bSBram Moolenaar # undef HAVE_PUTENV
536aa2cd4bSBram Moolenaar #endif
54071d4279SBram Moolenaar #ifdef HAVE_STDARG_H
552ab2e860SBram Moolenaar # undef HAVE_STDARG_H	// Python's config.h defines it as well.
56071d4279SBram Moolenaar #endif
57be2c9ae9SBram Moolenaar #ifdef _POSIX_C_SOURCE
582ab2e860SBram Moolenaar # undef _POSIX_C_SOURCE	// pyconfig.h defines it as well.
59be2c9ae9SBram Moolenaar #endif
60be2c9ae9SBram Moolenaar #ifdef _XOPEN_SOURCE
612ab2e860SBram Moolenaar # undef _XOPEN_SOURCE	// pyconfig.h defines it as well.
62be2c9ae9SBram Moolenaar #endif
63071d4279SBram Moolenaar 
640bdda37fSBram Moolenaar #define PY_SSIZE_T_CLEAN
650bdda37fSBram Moolenaar 
66071d4279SBram Moolenaar #include <Python.h>
670bdda37fSBram Moolenaar 
680bdda37fSBram Moolenaar #if !defined(PY_VERSION_HEX) || PY_VERSION_HEX < 0x02050000
690bdda37fSBram Moolenaar # undef PY_SSIZE_T_CLEAN
700bdda37fSBram Moolenaar #endif
710bdda37fSBram Moolenaar 
722e2f52a4SBram Moolenaar // these are NULL for Python 2
732e2f52a4SBram Moolenaar #define ERRORS_DECODE_ARG NULL
742e2f52a4SBram Moolenaar #define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
752e2f52a4SBram Moolenaar 
762ab2e860SBram Moolenaar #undef main // Defined in python.h - aargh
772ab2e860SBram Moolenaar #undef HAVE_FCNTL_H // Clash with os_win32.h
78071d4279SBram Moolenaar 
7965951258SBram Moolenaar // Perhaps leave this out for Python 2.6, which supports bytes?
80db913953SBram Moolenaar #define PyBytes_FromString      PyString_FromString
81335e0b69SBram Moolenaar #define PyBytes_Check		PyString_Check
82808c2bc8SBram Moolenaar #define PyBytes_AsStringAndSize PyString_AsStringAndSize
8365951258SBram Moolenaar #define PyBytes_FromStringAndSize   PyString_FromStringAndSize
8419e60943SBram Moolenaar 
85071d4279SBram Moolenaar #if !defined(FEAT_PYTHON) && defined(PROTO)
862ab2e860SBram Moolenaar // Use this to be able to generate prototypes without python being used.
87e7cb9cf6SBram Moolenaar # define PyObject Py_ssize_t
88e7cb9cf6SBram Moolenaar # define PyThreadState Py_ssize_t
89e7cb9cf6SBram Moolenaar # define PyTypeObject Py_ssize_t
90e7cb9cf6SBram Moolenaar struct PyMethodDef { Py_ssize_t a; };
91e7cb9cf6SBram Moolenaar # define PySequenceMethods Py_ssize_t
92071d4279SBram Moolenaar #endif
93071d4279SBram Moolenaar 
942afa3238SBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
952afa3238SBram Moolenaar # define PY_USE_CAPSULE
962afa3238SBram Moolenaar #endif
972afa3238SBram Moolenaar 
982c45e945SBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
992c45e945SBram Moolenaar # define PyInt Py_ssize_t
1002c45e945SBram Moolenaar # define PyInquiry lenfunc
1012c45e945SBram Moolenaar # define PyIntArgFunc ssizeargfunc
1022c45e945SBram Moolenaar # define PyIntIntArgFunc ssizessizeargfunc
1032c45e945SBram Moolenaar # define PyIntObjArgProc ssizeobjargproc
1042c45e945SBram Moolenaar # define PyIntIntObjArgProc ssizessizeobjargproc
105e7cb9cf6SBram Moolenaar # define Py_ssize_t_fmt "n"
1062c45e945SBram Moolenaar #else
1072c45e945SBram Moolenaar # define PyInt int
1084d1da49cSBram Moolenaar # define lenfunc inquiry
1092c45e945SBram Moolenaar # define PyInquiry inquiry
1102c45e945SBram Moolenaar # define PyIntArgFunc intargfunc
1112c45e945SBram Moolenaar # define PyIntIntArgFunc intintargfunc
1122c45e945SBram Moolenaar # define PyIntObjArgProc intobjargproc
1132c45e945SBram Moolenaar # define PyIntIntObjArgProc intintobjargproc
114e7cb9cf6SBram Moolenaar # define Py_ssize_t_fmt "i"
1152c45e945SBram Moolenaar #endif
116a9922d62SBram Moolenaar #define Py_bytes_fmt "s"
1172c45e945SBram Moolenaar 
1182ab2e860SBram Moolenaar // Parser flags
119071d4279SBram Moolenaar #define single_input	256
120071d4279SBram Moolenaar #define file_input	257
121071d4279SBram Moolenaar #define eval_input	258
122071d4279SBram Moolenaar 
123071d4279SBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
1242ab2e860SBram Moolenaar   // Python 2.3: can invoke ":python" recursively.
125071d4279SBram Moolenaar # define PY_CAN_RECURSE
126071d4279SBram Moolenaar #endif
127071d4279SBram Moolenaar 
128071d4279SBram Moolenaar #if defined(DYNAMIC_PYTHON) || defined(PROTO)
129071d4279SBram Moolenaar # ifndef DYNAMIC_PYTHON
1302ab2e860SBram Moolenaar #  define HINSTANCE long_u		// for generating prototypes
131071d4279SBram Moolenaar # endif
132071d4279SBram Moolenaar 
1334f97475dSBram Moolenaar # ifndef MSWIN
134bd5e15fdSBram Moolenaar #  include <dlfcn.h>
135bd5e15fdSBram Moolenaar #  define FARPROC void*
136bd5e15fdSBram Moolenaar #  define HINSTANCE void*
137644d37b8SBram Moolenaar #  if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
138b61f95c3SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY)
139b61f95c3SBram Moolenaar #  else
140fa5d1e63SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
141b61f95c3SBram Moolenaar #  endif
142bd5e15fdSBram Moolenaar #  define close_dll dlclose
143bd5e15fdSBram Moolenaar #  define symbol_from_dll dlsym
144*1a3e5747SMartin Tournoij #  define load_dll_error dlerror
145bd5e15fdSBram Moolenaar # else
146ebbcb824SBram Moolenaar #  define load_dll vimLoadLib
147bd5e15fdSBram Moolenaar #  define close_dll FreeLibrary
148bd5e15fdSBram Moolenaar #  define symbol_from_dll GetProcAddress
149*1a3e5747SMartin Tournoij #  define load_dll_error GetWin32Error
150bd5e15fdSBram Moolenaar # endif
151bd5e15fdSBram Moolenaar 
1522ab2e860SBram Moolenaar // This makes if_python.c compile without warnings against Python 2.5
1532ab2e860SBram Moolenaar // on Win32 and Win64.
154e7cb9cf6SBram Moolenaar # undef PyRun_SimpleString
155db913953SBram Moolenaar # undef PyRun_String
156e7cb9cf6SBram Moolenaar # undef PyArg_Parse
157e7cb9cf6SBram Moolenaar # undef PyArg_ParseTuple
158e7cb9cf6SBram Moolenaar # undef Py_BuildValue
159e7cb9cf6SBram Moolenaar # undef Py_InitModule4
160e7cb9cf6SBram Moolenaar # undef Py_InitModule4_64
161db913953SBram Moolenaar # undef PyObject_CallMethod
1629f3685a5SBram Moolenaar # undef PyObject_CallFunction
163e7cb9cf6SBram Moolenaar 
164071d4279SBram Moolenaar /*
165071d4279SBram Moolenaar  * Wrapper defines
166071d4279SBram Moolenaar  */
167071d4279SBram Moolenaar # define PyArg_Parse dll_PyArg_Parse
168071d4279SBram Moolenaar # define PyArg_ParseTuple dll_PyArg_ParseTuple
16919e60943SBram Moolenaar # define PyMem_Free dll_PyMem_Free
170db913953SBram Moolenaar # define PyMem_Malloc dll_PyMem_Malloc
171071d4279SBram Moolenaar # define PyDict_SetItemString dll_PyDict_SetItemString
172071d4279SBram Moolenaar # define PyErr_BadArgument dll_PyErr_BadArgument
173d5f729caSBram Moolenaar # define PyErr_NewException dll_PyErr_NewException
174071d4279SBram Moolenaar # define PyErr_Clear dll_PyErr_Clear
175c476e52fSBram Moolenaar # define PyErr_Format dll_PyErr_Format
1764d36987cSBram Moolenaar # define PyErr_PrintEx dll_PyErr_PrintEx
177071d4279SBram Moolenaar # define PyErr_NoMemory dll_PyErr_NoMemory
178071d4279SBram Moolenaar # define PyErr_Occurred dll_PyErr_Occurred
179071d4279SBram Moolenaar # define PyErr_SetNone dll_PyErr_SetNone
180071d4279SBram Moolenaar # define PyErr_SetString dll_PyErr_SetString
1814d188da2SBram Moolenaar # define PyErr_SetObject dll_PyErr_SetObject
182c09a6d6cSBram Moolenaar # define PyErr_ExceptionMatches dll_PyErr_ExceptionMatches
183071d4279SBram Moolenaar # define PyEval_InitThreads dll_PyEval_InitThreads
184071d4279SBram Moolenaar # define PyEval_RestoreThread dll_PyEval_RestoreThread
185071d4279SBram Moolenaar # define PyEval_SaveThread dll_PyEval_SaveThread
186071d4279SBram Moolenaar # ifdef PY_CAN_RECURSE
187071d4279SBram Moolenaar #  define PyGILState_Ensure dll_PyGILState_Ensure
188071d4279SBram Moolenaar #  define PyGILState_Release dll_PyGILState_Release
189071d4279SBram Moolenaar # endif
190071d4279SBram Moolenaar # define PyInt_AsLong dll_PyInt_AsLong
191071d4279SBram Moolenaar # define PyInt_FromLong dll_PyInt_FromLong
192db913953SBram Moolenaar # define PyLong_AsLong dll_PyLong_AsLong
193db913953SBram Moolenaar # define PyLong_FromLong dll_PyLong_FromLong
19466b7985eSBram Moolenaar # define PyBool_Type (*dll_PyBool_Type)
195071d4279SBram Moolenaar # define PyInt_Type (*dll_PyInt_Type)
196db913953SBram Moolenaar # define PyLong_Type (*dll_PyLong_Type)
197071d4279SBram Moolenaar # define PyList_GetItem dll_PyList_GetItem
1980ac9379aSBram Moolenaar # define PyList_Append dll_PyList_Append
199c09a6d6cSBram Moolenaar # define PyList_Insert dll_PyList_Insert
200071d4279SBram Moolenaar # define PyList_New dll_PyList_New
201071d4279SBram Moolenaar # define PyList_SetItem dll_PyList_SetItem
202071d4279SBram Moolenaar # define PyList_Size dll_PyList_Size
203071d4279SBram Moolenaar # define PyList_Type (*dll_PyList_Type)
204db913953SBram Moolenaar # define PySequence_Check dll_PySequence_Check
205db913953SBram Moolenaar # define PySequence_Size dll_PySequence_Size
206db913953SBram Moolenaar # define PySequence_GetItem dll_PySequence_GetItem
207a9922d62SBram Moolenaar # define PySequence_Fast dll_PySequence_Fast
208db913953SBram Moolenaar # define PyTuple_Size dll_PyTuple_Size
209db913953SBram Moolenaar # define PyTuple_GetItem dll_PyTuple_GetItem
210db913953SBram Moolenaar # define PyTuple_Type (*dll_PyTuple_Type)
211063a46baSBram Moolenaar # define PySlice_GetIndicesEx dll_PySlice_GetIndicesEx
212071d4279SBram Moolenaar # define PyImport_ImportModule dll_PyImport_ImportModule
2130ac9379aSBram Moolenaar # define PyDict_New dll_PyDict_New
214071d4279SBram Moolenaar # define PyDict_GetItemString dll_PyDict_GetItemString
215db913953SBram Moolenaar # define PyDict_Next dll_PyDict_Next
2163e734ea2SBram Moolenaar # define PyDict_Type (*dll_PyDict_Type)
217bcb40977SBram Moolenaar # ifdef PyMapping_Keys
218bcb40977SBram Moolenaar #  define PY_NO_MAPPING_KEYS
219db913953SBram Moolenaar # else
220bcb40977SBram Moolenaar #  define PyMapping_Keys dll_PyMapping_Keys
221db913953SBram Moolenaar # endif
222bcb40977SBram Moolenaar # define PyObject_GetItem dll_PyObject_GetItem
223db913953SBram Moolenaar # define PyObject_CallMethod dll_PyObject_CallMethod
224db913953SBram Moolenaar # define PyMapping_Check dll_PyMapping_Check
225db913953SBram Moolenaar # define PyIter_Next dll_PyIter_Next
226071d4279SBram Moolenaar # define PyModule_GetDict dll_PyModule_GetDict
227f9c9b32bSBram Moolenaar # define PyModule_AddObject dll_PyModule_AddObject
228071d4279SBram Moolenaar # define PyRun_SimpleString dll_PyRun_SimpleString
229db913953SBram Moolenaar # define PyRun_String dll_PyRun_String
230d620aa9bSBram Moolenaar # define PyObject_GetAttrString dll_PyObject_GetAttrString
231a9922d62SBram Moolenaar # define PyObject_HasAttrString dll_PyObject_HasAttrString
232d620aa9bSBram Moolenaar # define PyObject_SetAttrString dll_PyObject_SetAttrString
233d620aa9bSBram Moolenaar # define PyObject_CallFunctionObjArgs dll_PyObject_CallFunctionObjArgs
2349f3685a5SBram Moolenaar # define PyObject_CallFunction dll_PyObject_CallFunction
235f4258308SBram Moolenaar # define PyObject_Call dll_PyObject_Call
236141be8a5SBram Moolenaar # define PyObject_Repr dll_PyObject_Repr
237071d4279SBram Moolenaar # define PyString_AsString dll_PyString_AsString
238cdab9051SBram Moolenaar # define PyString_AsStringAndSize dll_PyString_AsStringAndSize
239071d4279SBram Moolenaar # define PyString_FromString dll_PyString_FromString
2401a3b5695SBram Moolenaar # define PyString_FromFormat dll_PyString_FromFormat
241071d4279SBram Moolenaar # define PyString_FromStringAndSize dll_PyString_FromStringAndSize
242071d4279SBram Moolenaar # define PyString_Size dll_PyString_Size
243071d4279SBram Moolenaar # define PyString_Type (*dll_PyString_Type)
244db913953SBram Moolenaar # define PyUnicode_Type (*dll_PyUnicode_Type)
245cc3e85f1SBram Moolenaar # undef PyUnicode_AsEncodedString
246cc3e85f1SBram Moolenaar # define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
247db913953SBram Moolenaar # define PyFloat_AsDouble dll_PyFloat_AsDouble
248db913953SBram Moolenaar # define PyFloat_FromDouble dll_PyFloat_FromDouble
249db913953SBram Moolenaar # define PyFloat_Type (*dll_PyFloat_Type)
250141be8a5SBram Moolenaar # define PyNumber_Check dll_PyNumber_Check
251141be8a5SBram Moolenaar # define PyNumber_Long dll_PyNumber_Long
252db913953SBram Moolenaar # define PyImport_AddModule (*dll_PyImport_AddModule)
253071d4279SBram Moolenaar # define PySys_SetObject dll_PySys_SetObject
254c09a6d6cSBram Moolenaar # define PySys_GetObject dll_PySys_GetObject
255071d4279SBram Moolenaar # define PySys_SetArgv dll_PySys_SetArgv
256071d4279SBram Moolenaar # define PyType_Type (*dll_PyType_Type)
257d4a8c98eSBram Moolenaar # define PyFile_Type (*dll_PyFile_Type)
258063a46baSBram Moolenaar # define PySlice_Type (*dll_PySlice_Type)
25930fec7bcSBram Moolenaar # define PyType_Ready (*dll_PyType_Ready)
260a9922d62SBram Moolenaar # define PyType_GenericAlloc dll_PyType_GenericAlloc
261071d4279SBram Moolenaar # define Py_BuildValue dll_Py_BuildValue
262071d4279SBram Moolenaar # define Py_FindMethod dll_Py_FindMethod
263071d4279SBram Moolenaar # define Py_InitModule4 dll_Py_InitModule4
264644d37b8SBram Moolenaar # define Py_SetPythonHome dll_Py_SetPythonHome
265071d4279SBram Moolenaar # define Py_Initialize dll_Py_Initialize
2660e21a3f6SBram Moolenaar # define Py_Finalize dll_Py_Finalize
2670e21a3f6SBram Moolenaar # define Py_IsInitialized dll_Py_IsInitialized
268071d4279SBram Moolenaar # define _PyObject_New dll__PyObject_New
269774267bbSBram Moolenaar # define _PyObject_GC_New dll__PyObject_GC_New
2703e734ea2SBram Moolenaar # ifdef PyObject_GC_Del
2713e734ea2SBram Moolenaar #  define Py_underscore_GC
2723e734ea2SBram Moolenaar #  define _PyObject_GC_Del dll__PyObject_GC_Del
2733e734ea2SBram Moolenaar #  define _PyObject_GC_UnTrack dll__PyObject_GC_UnTrack
2743e734ea2SBram Moolenaar # else
275774267bbSBram Moolenaar #  define PyObject_GC_Del dll_PyObject_GC_Del
276774267bbSBram Moolenaar #  define PyObject_GC_UnTrack dll_PyObject_GC_UnTrack
2773e734ea2SBram Moolenaar # endif
278e721122bSBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
279db913953SBram Moolenaar #  define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
280e721122bSBram Moolenaar # endif
281071d4279SBram Moolenaar # define _Py_NoneStruct (*dll__Py_NoneStruct)
28266b7985eSBram Moolenaar # define _Py_ZeroStruct (*dll__Py_ZeroStruct)
28366b7985eSBram Moolenaar # define _Py_TrueStruct (*dll__Py_TrueStruct)
284071d4279SBram Moolenaar # define PyObject_Init dll__PyObject_Init
285db913953SBram Moolenaar # define PyObject_GetIter dll_PyObject_GetIter
28603db85b3SBram Moolenaar # define PyObject_IsTrue dll_PyObject_IsTrue
287071d4279SBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
288071d4279SBram Moolenaar #  define PyType_IsSubtype dll_PyType_IsSubtype
2890014a53aSBram Moolenaar #  ifdef Py_DEBUG
2900014a53aSBram Moolenaar #   define _Py_NegativeRefcount dll__Py_NegativeRefcount
2910014a53aSBram Moolenaar #   define _Py_RefTotal (*dll__Py_RefTotal)
2920014a53aSBram Moolenaar #   define _Py_Dealloc dll__Py_Dealloc
2930014a53aSBram Moolenaar #  endif
2943e734ea2SBram Moolenaar # endif
2953e734ea2SBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
2960014a53aSBram Moolenaar #  if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
2970014a53aSBram Moolenaar #   define _PyObject_DebugMalloc dll__PyObject_DebugMalloc
2980014a53aSBram Moolenaar #   define _PyObject_DebugFree dll__PyObject_DebugFree
2990014a53aSBram Moolenaar #  else
300071d4279SBram Moolenaar #   define PyObject_Malloc dll_PyObject_Malloc
301071d4279SBram Moolenaar #   define PyObject_Free dll_PyObject_Free
302071d4279SBram Moolenaar #  endif
3030014a53aSBram Moolenaar # endif
3042afa3238SBram Moolenaar # ifdef PY_USE_CAPSULE
305db913953SBram Moolenaar #  define PyCapsule_New dll_PyCapsule_New
306db913953SBram Moolenaar #  define PyCapsule_GetPointer dll_PyCapsule_GetPointer
3072afa3238SBram Moolenaar # else
3082afa3238SBram Moolenaar #  define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
3092afa3238SBram Moolenaar #  define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
3102afa3238SBram Moolenaar # endif
31112a28d4bSBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
31212a28d4bSBram Moolenaar #  define Py_NoSiteFlag (*dll_Py_NoSiteFlag)
31312a28d4bSBram Moolenaar # endif
314071d4279SBram Moolenaar 
315071d4279SBram Moolenaar /*
316071d4279SBram Moolenaar  * Pointers for dynamic link
317071d4279SBram Moolenaar  */
318071d4279SBram Moolenaar static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
319071d4279SBram Moolenaar static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
32019e60943SBram Moolenaar static int(*dll_PyMem_Free)(void *);
321db913953SBram Moolenaar static void* (*dll_PyMem_Malloc)(size_t);
322071d4279SBram Moolenaar static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
323071d4279SBram Moolenaar static int(*dll_PyErr_BadArgument)(void);
324d5f729caSBram Moolenaar static PyObject *(*dll_PyErr_NewException)(char *, PyObject *, PyObject *);
325071d4279SBram Moolenaar static void(*dll_PyErr_Clear)(void);
326c476e52fSBram Moolenaar static PyObject*(*dll_PyErr_Format)(PyObject *, const char *, ...);
3274d36987cSBram Moolenaar static void(*dll_PyErr_PrintEx)(int);
328071d4279SBram Moolenaar static PyObject*(*dll_PyErr_NoMemory)(void);
329071d4279SBram Moolenaar static PyObject*(*dll_PyErr_Occurred)(void);
330071d4279SBram Moolenaar static void(*dll_PyErr_SetNone)(PyObject *);
331071d4279SBram Moolenaar static void(*dll_PyErr_SetString)(PyObject *, const char *);
3324d188da2SBram Moolenaar static void(*dll_PyErr_SetObject)(PyObject *, PyObject *);
333c09a6d6cSBram Moolenaar static int(*dll_PyErr_ExceptionMatches)(PyObject *);
334071d4279SBram Moolenaar static void(*dll_PyEval_InitThreads)(void);
335071d4279SBram Moolenaar static void(*dll_PyEval_RestoreThread)(PyThreadState *);
336071d4279SBram Moolenaar static PyThreadState*(*dll_PyEval_SaveThread)(void);
337071d4279SBram Moolenaar # ifdef PY_CAN_RECURSE
338071d4279SBram Moolenaar static PyGILState_STATE	(*dll_PyGILState_Ensure)(void);
339071d4279SBram Moolenaar static void (*dll_PyGILState_Release)(PyGILState_STATE);
340071d4279SBram Moolenaar # endif
341071d4279SBram Moolenaar static long(*dll_PyInt_AsLong)(PyObject *);
342071d4279SBram Moolenaar static PyObject*(*dll_PyInt_FromLong)(long);
343db913953SBram Moolenaar static long(*dll_PyLong_AsLong)(PyObject *);
344db913953SBram Moolenaar static PyObject*(*dll_PyLong_FromLong)(long);
34566b7985eSBram Moolenaar static PyTypeObject* dll_PyBool_Type;
346071d4279SBram Moolenaar static PyTypeObject* dll_PyInt_Type;
347db913953SBram Moolenaar static PyTypeObject* dll_PyLong_Type;
3482c45e945SBram Moolenaar static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
349c09a6d6cSBram Moolenaar static int(*dll_PyList_Append)(PyObject *, PyObject *);
350d5e376ebSBram Moolenaar static int(*dll_PyList_Insert)(PyObject *, PyInt, PyObject *);
3512c45e945SBram Moolenaar static PyObject*(*dll_PyList_New)(PyInt size);
3522c45e945SBram Moolenaar static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
3532c45e945SBram Moolenaar static PyInt(*dll_PyList_Size)(PyObject *);
354071d4279SBram Moolenaar static PyTypeObject* dll_PyList_Type;
355db913953SBram Moolenaar static int (*dll_PySequence_Check)(PyObject *);
356db913953SBram Moolenaar static PyInt(*dll_PySequence_Size)(PyObject *);
357db913953SBram Moolenaar static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
358a9922d62SBram Moolenaar static PyObject*(*dll_PySequence_Fast)(PyObject *, const char *);
359db913953SBram Moolenaar static PyInt(*dll_PyTuple_Size)(PyObject *);
360db913953SBram Moolenaar static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
361db913953SBram Moolenaar static PyTypeObject* dll_PyTuple_Type;
3625395e7afSBram Moolenaar static int (*dll_PySlice_GetIndicesEx)(PySliceObject *r, PyInt length,
363063a46baSBram Moolenaar 		     PyInt *start, PyInt *stop, PyInt *step,
364063a46baSBram Moolenaar 		     PyInt *slicelen);
365071d4279SBram Moolenaar static PyObject*(*dll_PyImport_ImportModule)(const char *);
3660ac9379aSBram Moolenaar static PyObject*(*dll_PyDict_New)(void);
367071d4279SBram Moolenaar static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
3683e734ea2SBram Moolenaar static int (*dll_PyDict_Next)(PyObject *, PyInt *, PyObject **, PyObject **);
3693e734ea2SBram Moolenaar static PyTypeObject* dll_PyDict_Type;
370bcb40977SBram Moolenaar # ifndef PY_NO_MAPPING_KEYS
371bcb40977SBram Moolenaar static PyObject* (*dll_PyMapping_Keys)(PyObject *);
372db913953SBram Moolenaar # endif
373bcb40977SBram Moolenaar static PyObject* (*dll_PyObject_GetItem)(PyObject *, PyObject *);
374db913953SBram Moolenaar static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
375db913953SBram Moolenaar static int (*dll_PyMapping_Check)(PyObject *);
376db913953SBram Moolenaar static PyObject* (*dll_PyIter_Next)(PyObject *);
377071d4279SBram Moolenaar static PyObject*(*dll_PyModule_GetDict)(PyObject *);
378f9c9b32bSBram Moolenaar static int(*dll_PyModule_AddObject)(PyObject *, const char *, PyObject *);
379071d4279SBram Moolenaar static int(*dll_PyRun_SimpleString)(char *);
380db913953SBram Moolenaar static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
381d620aa9bSBram Moolenaar static PyObject* (*dll_PyObject_GetAttrString)(PyObject *, const char *);
382a9922d62SBram Moolenaar static int (*dll_PyObject_HasAttrString)(PyObject *, const char *);
3830b400087SBram Moolenaar static int (*dll_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
384d620aa9bSBram Moolenaar static PyObject* (*dll_PyObject_CallFunctionObjArgs)(PyObject *, ...);
3859f3685a5SBram Moolenaar static PyObject* (*dll_PyObject_CallFunction)(PyObject *, char *, ...);
386f4258308SBram Moolenaar static PyObject* (*dll_PyObject_Call)(PyObject *, PyObject *, PyObject *);
387141be8a5SBram Moolenaar static PyObject* (*dll_PyObject_Repr)(PyObject *);
388071d4279SBram Moolenaar static char*(*dll_PyString_AsString)(PyObject *);
389d5e376ebSBram Moolenaar static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, PyInt *);
390071d4279SBram Moolenaar static PyObject*(*dll_PyString_FromString)(const char *);
3911a3b5695SBram Moolenaar static PyObject*(*dll_PyString_FromFormat)(const char *, ...);
3922c45e945SBram Moolenaar static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
3932c45e945SBram Moolenaar static PyInt(*dll_PyString_Size)(PyObject *);
394071d4279SBram Moolenaar static PyTypeObject* dll_PyString_Type;
395db913953SBram Moolenaar static PyTypeObject* dll_PyUnicode_Type;
396cc3e85f1SBram Moolenaar static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
397db913953SBram Moolenaar static double(*dll_PyFloat_AsDouble)(PyObject *);
398db913953SBram Moolenaar static PyObject*(*dll_PyFloat_FromDouble)(double);
399db913953SBram Moolenaar static PyTypeObject* dll_PyFloat_Type;
400141be8a5SBram Moolenaar static int(*dll_PyNumber_Check)(PyObject *);
401141be8a5SBram Moolenaar static PyObject*(*dll_PyNumber_Long)(PyObject *);
402071d4279SBram Moolenaar static int(*dll_PySys_SetObject)(char *, PyObject *);
403c09a6d6cSBram Moolenaar static PyObject *(*dll_PySys_GetObject)(char *);
404071d4279SBram Moolenaar static int(*dll_PySys_SetArgv)(int, char **);
405071d4279SBram Moolenaar static PyTypeObject* dll_PyType_Type;
406d4a8c98eSBram Moolenaar static PyTypeObject* dll_PyFile_Type;
407063a46baSBram Moolenaar static PyTypeObject* dll_PySlice_Type;
40830fec7bcSBram Moolenaar static int (*dll_PyType_Ready)(PyTypeObject *type);
409a9922d62SBram Moolenaar static PyObject* (*dll_PyType_GenericAlloc)(PyTypeObject *type, PyInt nitems);
410071d4279SBram Moolenaar static PyObject*(*dll_Py_BuildValue)(char *, ...);
411071d4279SBram Moolenaar static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
412071d4279SBram Moolenaar static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
413db913953SBram Moolenaar static PyObject*(*dll_PyImport_AddModule)(char *);
414644d37b8SBram Moolenaar static void(*dll_Py_SetPythonHome)(char *home);
415071d4279SBram Moolenaar static void(*dll_Py_Initialize)(void);
4160e21a3f6SBram Moolenaar static void(*dll_Py_Finalize)(void);
4170e21a3f6SBram Moolenaar static int(*dll_Py_IsInitialized)(void);
418071d4279SBram Moolenaar static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
419774267bbSBram Moolenaar static PyObject*(*dll__PyObject_GC_New)(PyTypeObject *);
4203e734ea2SBram Moolenaar # ifdef Py_underscore_GC
4213e734ea2SBram Moolenaar static void(*dll__PyObject_GC_Del)(void *);
4223e734ea2SBram Moolenaar static void(*dll__PyObject_GC_UnTrack)(void *);
4233e734ea2SBram Moolenaar # else
424774267bbSBram Moolenaar static void(*dll_PyObject_GC_Del)(void *);
425774267bbSBram Moolenaar static void(*dll_PyObject_GC_UnTrack)(void *);
4263e734ea2SBram Moolenaar # endif
427071d4279SBram Moolenaar static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
428db913953SBram Moolenaar static PyObject* (*dll_PyObject_GetIter)(PyObject *);
42903db85b3SBram Moolenaar static int (*dll_PyObject_IsTrue)(PyObject *);
430e721122bSBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
431db913953SBram Moolenaar static iternextfunc dll__PyObject_NextNotImplemented;
432e721122bSBram Moolenaar # endif
433071d4279SBram Moolenaar static PyObject* dll__Py_NoneStruct;
43466b7985eSBram Moolenaar static PyObject* _Py_ZeroStruct;
43566b7985eSBram Moolenaar static PyObject* dll__Py_TrueStruct;
436071d4279SBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
437071d4279SBram Moolenaar static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
4380014a53aSBram Moolenaar #  ifdef Py_DEBUG
4390014a53aSBram Moolenaar static void (*dll__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
4403e734ea2SBram Moolenaar static PyInt* dll__Py_RefTotal;
4410014a53aSBram Moolenaar static void (*dll__Py_Dealloc)(PyObject *obj);
4420014a53aSBram Moolenaar #  endif
4433e734ea2SBram Moolenaar # endif
4443e734ea2SBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
4450014a53aSBram Moolenaar #  if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
4460014a53aSBram Moolenaar static void (*dll__PyObject_DebugFree)(void*);
4470014a53aSBram Moolenaar static void* (*dll__PyObject_DebugMalloc)(size_t);
4480014a53aSBram Moolenaar #  else
449071d4279SBram Moolenaar static void* (*dll_PyObject_Malloc)(size_t);
450071d4279SBram Moolenaar static void (*dll_PyObject_Free)(void*);
451071d4279SBram Moolenaar #  endif
4520014a53aSBram Moolenaar # endif
4532afa3238SBram Moolenaar # ifdef PY_USE_CAPSULE
454db913953SBram Moolenaar static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
455db913953SBram Moolenaar static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
4562afa3238SBram Moolenaar # else
457221d6872SBram Moolenaar static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
458221d6872SBram Moolenaar static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
4592afa3238SBram Moolenaar # endif
46012a28d4bSBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
46112a28d4bSBram Moolenaar static int* dll_Py_NoSiteFlag;
46212a28d4bSBram Moolenaar # endif
463071d4279SBram Moolenaar 
4642ab2e860SBram Moolenaar static HINSTANCE hinstPython = 0; // Instance of python.dll
465071d4279SBram Moolenaar 
4662ab2e860SBram Moolenaar // Imported exception objects
467071d4279SBram Moolenaar static PyObject *imp_PyExc_AttributeError;
468071d4279SBram Moolenaar static PyObject *imp_PyExc_IndexError;
469af6abb9dSBram Moolenaar static PyObject *imp_PyExc_KeyError;
470071d4279SBram Moolenaar static PyObject *imp_PyExc_KeyboardInterrupt;
471071d4279SBram Moolenaar static PyObject *imp_PyExc_TypeError;
472071d4279SBram Moolenaar static PyObject *imp_PyExc_ValueError;
47341009374SBram Moolenaar static PyObject *imp_PyExc_SystemExit;
4748661b178SBram Moolenaar static PyObject *imp_PyExc_RuntimeError;
475c09a6d6cSBram Moolenaar static PyObject *imp_PyExc_ImportError;
476141be8a5SBram Moolenaar static PyObject *imp_PyExc_OverflowError;
477071d4279SBram Moolenaar 
478071d4279SBram Moolenaar # define PyExc_AttributeError imp_PyExc_AttributeError
479071d4279SBram Moolenaar # define PyExc_IndexError imp_PyExc_IndexError
480af6abb9dSBram Moolenaar # define PyExc_KeyError imp_PyExc_KeyError
481071d4279SBram Moolenaar # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
482071d4279SBram Moolenaar # define PyExc_TypeError imp_PyExc_TypeError
483071d4279SBram Moolenaar # define PyExc_ValueError imp_PyExc_ValueError
48441009374SBram Moolenaar # define PyExc_SystemExit imp_PyExc_SystemExit
4858661b178SBram Moolenaar # define PyExc_RuntimeError imp_PyExc_RuntimeError
486c09a6d6cSBram Moolenaar # define PyExc_ImportError imp_PyExc_ImportError
487141be8a5SBram Moolenaar # define PyExc_OverflowError imp_PyExc_OverflowError
488071d4279SBram Moolenaar 
489071d4279SBram Moolenaar /*
490071d4279SBram Moolenaar  * Table of name to function pointer of python.
491071d4279SBram Moolenaar  */
492071d4279SBram Moolenaar # define PYTHON_PROC FARPROC
493071d4279SBram Moolenaar static struct
494071d4279SBram Moolenaar {
495071d4279SBram Moolenaar     char *name;
496071d4279SBram Moolenaar     PYTHON_PROC *ptr;
497071d4279SBram Moolenaar } python_funcname_table[] =
498071d4279SBram Moolenaar {
499e8cdcef8SBram Moolenaar # ifndef PY_SSIZE_T_CLEAN
500071d4279SBram Moolenaar     {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
501071d4279SBram Moolenaar     {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
502e8cdcef8SBram Moolenaar     {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
503e8cdcef8SBram Moolenaar # else
504e8cdcef8SBram Moolenaar     {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
505e8cdcef8SBram Moolenaar     {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
506e8cdcef8SBram Moolenaar     {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
507e8cdcef8SBram Moolenaar # endif
50819e60943SBram Moolenaar     {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
509db913953SBram Moolenaar     {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
510071d4279SBram Moolenaar     {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
511071d4279SBram Moolenaar     {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
512d5f729caSBram Moolenaar     {"PyErr_NewException", (PYTHON_PROC*)&dll_PyErr_NewException},
513071d4279SBram Moolenaar     {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
514c476e52fSBram Moolenaar     {"PyErr_Format", (PYTHON_PROC*)&dll_PyErr_Format},
5154d36987cSBram Moolenaar     {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
516071d4279SBram Moolenaar     {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
517071d4279SBram Moolenaar     {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
518071d4279SBram Moolenaar     {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
519071d4279SBram Moolenaar     {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
5204d188da2SBram Moolenaar     {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject},
521c09a6d6cSBram Moolenaar     {"PyErr_ExceptionMatches", (PYTHON_PROC*)&dll_PyErr_ExceptionMatches},
522071d4279SBram Moolenaar     {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
523071d4279SBram Moolenaar     {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
524071d4279SBram Moolenaar     {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
525071d4279SBram Moolenaar # ifdef PY_CAN_RECURSE
526071d4279SBram Moolenaar     {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
527071d4279SBram Moolenaar     {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
528071d4279SBram Moolenaar # endif
529071d4279SBram Moolenaar     {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
530071d4279SBram Moolenaar     {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
531db913953SBram Moolenaar     {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
532db913953SBram Moolenaar     {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
53366b7985eSBram Moolenaar     {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
534071d4279SBram Moolenaar     {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
535db913953SBram Moolenaar     {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
536071d4279SBram Moolenaar     {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
5370ac9379aSBram Moolenaar     {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
538c09a6d6cSBram Moolenaar     {"PyList_Insert", (PYTHON_PROC*)&dll_PyList_Insert},
539071d4279SBram Moolenaar     {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
540071d4279SBram Moolenaar     {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
541071d4279SBram Moolenaar     {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
542071d4279SBram Moolenaar     {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
543db913953SBram Moolenaar     {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
544db913953SBram Moolenaar     {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
545a9922d62SBram Moolenaar     {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
546a9922d62SBram Moolenaar     {"PySequence_Fast", (PYTHON_PROC*)&dll_PySequence_Fast},
547db913953SBram Moolenaar     {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
548db913953SBram Moolenaar     {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
549db913953SBram Moolenaar     {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
550063a46baSBram Moolenaar     {"PySlice_GetIndicesEx", (PYTHON_PROC*)&dll_PySlice_GetIndicesEx},
551071d4279SBram Moolenaar     {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
552071d4279SBram Moolenaar     {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
553db913953SBram Moolenaar     {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
5540ac9379aSBram Moolenaar     {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
5553e734ea2SBram Moolenaar     {"PyDict_Type", (PYTHON_PROC*)&dll_PyDict_Type},
556bcb40977SBram Moolenaar # ifndef PY_NO_MAPPING_KEYS
557bcb40977SBram Moolenaar     {"PyMapping_Keys", (PYTHON_PROC*)&dll_PyMapping_Keys},
558db913953SBram Moolenaar # endif
559bcb40977SBram Moolenaar     {"PyObject_GetItem", (PYTHON_PROC*)&dll_PyObject_GetItem},
560db913953SBram Moolenaar     {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
561db913953SBram Moolenaar     {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
562db913953SBram Moolenaar     {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
563071d4279SBram Moolenaar     {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
564f9c9b32bSBram Moolenaar     {"PyModule_AddObject", (PYTHON_PROC*)&dll_PyModule_AddObject},
565071d4279SBram Moolenaar     {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
566db913953SBram Moolenaar     {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
567d620aa9bSBram Moolenaar     {"PyObject_GetAttrString", (PYTHON_PROC*)&dll_PyObject_GetAttrString},
568a9922d62SBram Moolenaar     {"PyObject_HasAttrString", (PYTHON_PROC*)&dll_PyObject_HasAttrString},
569d620aa9bSBram Moolenaar     {"PyObject_SetAttrString", (PYTHON_PROC*)&dll_PyObject_SetAttrString},
570d620aa9bSBram Moolenaar     {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&dll_PyObject_CallFunctionObjArgs},
5719f3685a5SBram Moolenaar     {"PyObject_CallFunction", (PYTHON_PROC*)&dll_PyObject_CallFunction},
572f4258308SBram Moolenaar     {"PyObject_Call", (PYTHON_PROC*)&dll_PyObject_Call},
573141be8a5SBram Moolenaar     {"PyObject_Repr", (PYTHON_PROC*)&dll_PyObject_Repr},
574071d4279SBram Moolenaar     {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
575cdab9051SBram Moolenaar     {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
576071d4279SBram Moolenaar     {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
5771a3b5695SBram Moolenaar     {"PyString_FromFormat", (PYTHON_PROC*)&dll_PyString_FromFormat},
578071d4279SBram Moolenaar     {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
579071d4279SBram Moolenaar     {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
580071d4279SBram Moolenaar     {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
581db913953SBram Moolenaar     {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
582db913953SBram Moolenaar     {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
583db913953SBram Moolenaar     {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
584db913953SBram Moolenaar     {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
585db913953SBram Moolenaar     {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
586141be8a5SBram Moolenaar     {"PyNumber_Check", (PYTHON_PROC*)&dll_PyNumber_Check},
587141be8a5SBram Moolenaar     {"PyNumber_Long", (PYTHON_PROC*)&dll_PyNumber_Long},
588071d4279SBram Moolenaar     {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
589c09a6d6cSBram Moolenaar     {"PySys_GetObject", (PYTHON_PROC*)&dll_PySys_GetObject},
590071d4279SBram Moolenaar     {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
591071d4279SBram Moolenaar     {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
592d4a8c98eSBram Moolenaar     {"PyFile_Type", (PYTHON_PROC*)&dll_PyFile_Type},
593063a46baSBram Moolenaar     {"PySlice_Type", (PYTHON_PROC*)&dll_PySlice_Type},
59430fec7bcSBram Moolenaar     {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
595a9922d62SBram Moolenaar     {"PyType_GenericAlloc", (PYTHON_PROC*)&dll_PyType_GenericAlloc},
596071d4279SBram Moolenaar     {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
597644d37b8SBram Moolenaar     {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
598071d4279SBram Moolenaar     {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
5990e21a3f6SBram Moolenaar     {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
6000e21a3f6SBram Moolenaar     {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
601071d4279SBram Moolenaar     {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
602774267bbSBram Moolenaar     {"_PyObject_GC_New", (PYTHON_PROC*)&dll__PyObject_GC_New},
6033e734ea2SBram Moolenaar # ifdef Py_underscore_GC
6043e734ea2SBram Moolenaar     {"_PyObject_GC_Del", (PYTHON_PROC*)&dll__PyObject_GC_Del},
6053e734ea2SBram Moolenaar     {"_PyObject_GC_UnTrack", (PYTHON_PROC*)&dll__PyObject_GC_UnTrack},
6063e734ea2SBram Moolenaar # else
607774267bbSBram Moolenaar     {"PyObject_GC_Del", (PYTHON_PROC*)&dll_PyObject_GC_Del},
608774267bbSBram Moolenaar     {"PyObject_GC_UnTrack", (PYTHON_PROC*)&dll_PyObject_GC_UnTrack},
6093e734ea2SBram Moolenaar # endif
610071d4279SBram Moolenaar     {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
611db913953SBram Moolenaar     {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
61203db85b3SBram Moolenaar     {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue},
613e721122bSBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
614db913953SBram Moolenaar     {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
615e721122bSBram Moolenaar # endif
616071d4279SBram Moolenaar     {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
61766b7985eSBram Moolenaar     {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
61866b7985eSBram Moolenaar     {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
619071d4279SBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
6200014a53aSBram Moolenaar #  ifdef Py_DEBUG
6210014a53aSBram Moolenaar     {"_Py_NegativeRefcount", (PYTHON_PROC*)&dll__Py_NegativeRefcount},
6220014a53aSBram Moolenaar     {"_Py_RefTotal", (PYTHON_PROC*)&dll__Py_RefTotal},
6230014a53aSBram Moolenaar     {"_Py_Dealloc", (PYTHON_PROC*)&dll__Py_Dealloc},
6240014a53aSBram Moolenaar #  endif
6253e734ea2SBram Moolenaar     {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
6263e734ea2SBram Moolenaar # endif
6273e734ea2SBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
6280014a53aSBram Moolenaar #  if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
6290014a53aSBram Moolenaar     {"_PyObject_DebugFree", (PYTHON_PROC*)&dll__PyObject_DebugFree},
6300014a53aSBram Moolenaar     {"_PyObject_DebugMalloc", (PYTHON_PROC*)&dll__PyObject_DebugMalloc},
6310014a53aSBram Moolenaar #  else
632071d4279SBram Moolenaar     {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
633071d4279SBram Moolenaar     {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
634071d4279SBram Moolenaar #  endif
6350014a53aSBram Moolenaar # endif
6360014a53aSBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
637a2aa31a9SBram Moolenaar 	&& SIZEOF_SIZE_T != VIM_SIZEOF_INT
6380014a53aSBram Moolenaar #  ifdef Py_DEBUG
6390014a53aSBram Moolenaar     {"Py_InitModule4TraceRefs_64", (PYTHON_PROC*)&dll_Py_InitModule4},
6400014a53aSBram Moolenaar #  else
6410014a53aSBram Moolenaar     {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
6420014a53aSBram Moolenaar #  endif
6430014a53aSBram Moolenaar # else
6440014a53aSBram Moolenaar #  ifdef Py_DEBUG
6450014a53aSBram Moolenaar     {"Py_InitModule4TraceRefs", (PYTHON_PROC*)&dll_Py_InitModule4},
6460014a53aSBram Moolenaar #  else
6470014a53aSBram Moolenaar     {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
6480014a53aSBram Moolenaar #  endif
6490014a53aSBram Moolenaar # endif
6502afa3238SBram Moolenaar # ifdef PY_USE_CAPSULE
651db913953SBram Moolenaar     {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
652db913953SBram Moolenaar     {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
6532afa3238SBram Moolenaar # else
6542afa3238SBram Moolenaar     {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
6552afa3238SBram Moolenaar     {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
6562afa3238SBram Moolenaar # endif
65712a28d4bSBram Moolenaar # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
65812a28d4bSBram Moolenaar     {"Py_NoSiteFlag", (PYTHON_PROC*)&dll_Py_NoSiteFlag},
65912a28d4bSBram Moolenaar # endif
660071d4279SBram Moolenaar     {"", NULL},
661071d4279SBram Moolenaar };
662071d4279SBram Moolenaar 
663071d4279SBram Moolenaar /*
664071d4279SBram Moolenaar  * Load library and get all pointers.
665071d4279SBram Moolenaar  * Parameter 'libname' provides name of DLL.
666071d4279SBram Moolenaar  * Return OK or FAIL.
667071d4279SBram Moolenaar  */
668071d4279SBram Moolenaar     static int
python_runtime_link_init(char * libname,int verbose)669071d4279SBram Moolenaar python_runtime_link_init(char *libname, int verbose)
670071d4279SBram Moolenaar {
671071d4279SBram Moolenaar     int i;
6727b24ce08SBram Moolenaar     PYTHON_PROC *ucs_as_encoded_string =
6737b24ce08SBram Moolenaar 				   (PYTHON_PROC*)&py_PyUnicode_AsEncodedString;
674071d4279SBram Moolenaar 
675644d37b8SBram Moolenaar # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
6762ab2e860SBram Moolenaar     // Can't have Python and Python3 loaded at the same time.
6772ab2e860SBram Moolenaar     // It cause a crash, because RTLD_GLOBAL is needed for
6782ab2e860SBram Moolenaar     // standard C extension libraries of one or both python versions.
6794c3a326cSBram Moolenaar     if (python3_loaded())
6804c3a326cSBram Moolenaar     {
6819dc93ae4SBram Moolenaar 	if (verbose)
682f9e3e09fSBram Moolenaar 	    emsg(_("E836: This Vim cannot execute :python after using :py3"));
6834c3a326cSBram Moolenaar 	return FAIL;
6844c3a326cSBram Moolenaar     }
6854c3a326cSBram Moolenaar # endif
6864c3a326cSBram Moolenaar 
687071d4279SBram Moolenaar     if (hinstPython)
688071d4279SBram Moolenaar 	return OK;
689bd5e15fdSBram Moolenaar     hinstPython = load_dll(libname);
690071d4279SBram Moolenaar     if (!hinstPython)
691071d4279SBram Moolenaar     {
692071d4279SBram Moolenaar 	if (verbose)
693*1a3e5747SMartin Tournoij 	    semsg(_(e_loadlib), libname, load_dll_error());
694071d4279SBram Moolenaar 	return FAIL;
695071d4279SBram Moolenaar     }
696071d4279SBram Moolenaar 
697071d4279SBram Moolenaar     for (i = 0; python_funcname_table[i].ptr; ++i)
698071d4279SBram Moolenaar     {
699bd5e15fdSBram Moolenaar 	if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
700071d4279SBram Moolenaar 			python_funcname_table[i].name)) == NULL)
701071d4279SBram Moolenaar 	{
702bd5e15fdSBram Moolenaar 	    close_dll(hinstPython);
703071d4279SBram Moolenaar 	    hinstPython = 0;
704071d4279SBram Moolenaar 	    if (verbose)
705f9e3e09fSBram Moolenaar 		semsg(_(e_loadfunc), python_funcname_table[i].name);
706071d4279SBram Moolenaar 	    return FAIL;
707071d4279SBram Moolenaar 	}
708071d4279SBram Moolenaar     }
709cc3e85f1SBram Moolenaar 
7102ab2e860SBram Moolenaar     // Load unicode functions separately as only the ucs2 or the ucs4 functions
7112ab2e860SBram Moolenaar     // will be present in the library.
7127b24ce08SBram Moolenaar     *ucs_as_encoded_string = symbol_from_dll(hinstPython,
713cc3e85f1SBram Moolenaar 					     "PyUnicodeUCS2_AsEncodedString");
7147b24ce08SBram Moolenaar     if (*ucs_as_encoded_string == NULL)
7157b24ce08SBram Moolenaar 	*ucs_as_encoded_string = symbol_from_dll(hinstPython,
716cc3e85f1SBram Moolenaar 					     "PyUnicodeUCS4_AsEncodedString");
7177b24ce08SBram Moolenaar     if (*ucs_as_encoded_string == NULL)
718cc3e85f1SBram Moolenaar     {
719cc3e85f1SBram Moolenaar 	close_dll(hinstPython);
720cc3e85f1SBram Moolenaar 	hinstPython = 0;
721cc3e85f1SBram Moolenaar 	if (verbose)
722f9e3e09fSBram Moolenaar 	    semsg(_(e_loadfunc), "PyUnicode_UCSX_*");
723cc3e85f1SBram Moolenaar 	return FAIL;
724cc3e85f1SBram Moolenaar     }
725cc3e85f1SBram Moolenaar 
726071d4279SBram Moolenaar     return OK;
727071d4279SBram Moolenaar }
728071d4279SBram Moolenaar 
729071d4279SBram Moolenaar /*
730071d4279SBram Moolenaar  * If python is enabled (there is installed python on Windows system) return
731071d4279SBram Moolenaar  * TRUE, else FALSE.
732071d4279SBram Moolenaar  */
733071d4279SBram Moolenaar     int
python_enabled(int verbose)734e7cb9cf6SBram Moolenaar python_enabled(int verbose)
735071d4279SBram Moolenaar {
73625e4fcdeSBram Moolenaar     return python_runtime_link_init((char *)p_pydll, verbose) == OK;
737071d4279SBram Moolenaar }
738071d4279SBram Moolenaar 
739ca8a4dfeSBram Moolenaar /*
740ca8a4dfeSBram Moolenaar  * Load the standard Python exceptions - don't import the symbols from the
741071d4279SBram Moolenaar  * DLL, as this can cause errors (importing data symbols is not reliable).
742071d4279SBram Moolenaar  */
743071d4279SBram Moolenaar     static void
get_exceptions(void)744ca8a4dfeSBram Moolenaar get_exceptions(void)
745071d4279SBram Moolenaar {
746071d4279SBram Moolenaar     PyObject *exmod = PyImport_ImportModule("exceptions");
747071d4279SBram Moolenaar     PyObject *exdict = PyModule_GetDict(exmod);
748071d4279SBram Moolenaar     imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
749071d4279SBram Moolenaar     imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
750af6abb9dSBram Moolenaar     imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
751071d4279SBram Moolenaar     imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
752071d4279SBram Moolenaar     imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
753071d4279SBram Moolenaar     imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
75441009374SBram Moolenaar     imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
7558661b178SBram Moolenaar     imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
756c09a6d6cSBram Moolenaar     imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
757141be8a5SBram Moolenaar     imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
758071d4279SBram Moolenaar     Py_XINCREF(imp_PyExc_AttributeError);
759071d4279SBram Moolenaar     Py_XINCREF(imp_PyExc_IndexError);
760af6abb9dSBram Moolenaar     Py_XINCREF(imp_PyExc_KeyError);
761071d4279SBram Moolenaar     Py_XINCREF(imp_PyExc_KeyboardInterrupt);
762071d4279SBram Moolenaar     Py_XINCREF(imp_PyExc_TypeError);
763071d4279SBram Moolenaar     Py_XINCREF(imp_PyExc_ValueError);
76441009374SBram Moolenaar     Py_XINCREF(imp_PyExc_SystemExit);
7658661b178SBram Moolenaar     Py_XINCREF(imp_PyExc_RuntimeError);
766c09a6d6cSBram Moolenaar     Py_XINCREF(imp_PyExc_ImportError);
767141be8a5SBram Moolenaar     Py_XINCREF(imp_PyExc_OverflowError);
768071d4279SBram Moolenaar     Py_XDECREF(exmod);
769071d4279SBram Moolenaar }
7702ab2e860SBram Moolenaar #endif // DYNAMIC_PYTHON
771071d4279SBram Moolenaar 
772db913953SBram Moolenaar static int initialised = 0;
773db913953SBram Moolenaar #define PYINITIALISED initialised
774c4f83380SBram Moolenaar static int python_end_called = FALSE;
775db913953SBram Moolenaar 
7762a0f3d3fSBram Moolenaar #define DESTRUCTOR_FINISH(self) self->ob_type->tp_free((PyObject*)self);
7774d1da49cSBram Moolenaar 
778971db467SBram Moolenaar #define WIN_PYTHON_REF(win) win->w_python_ref
779971db467SBram Moolenaar #define BUF_PYTHON_REF(buf) buf->b_python_ref
7805e538ecdSBram Moolenaar #define TAB_PYTHON_REF(tab) tab->tp_python_ref
781971db467SBram Moolenaar 
7824d1da49cSBram Moolenaar static PyObject *OutputGetattr(PyObject *, char *);
7834d1da49cSBram Moolenaar static PyObject *BufferGetattr(PyObject *, char *);
7844d1da49cSBram Moolenaar static PyObject *WindowGetattr(PyObject *, char *);
7855e538ecdSBram Moolenaar static PyObject *TabPageGetattr(PyObject *, char *);
7864d1da49cSBram Moolenaar static PyObject *RangeGetattr(PyObject *, char *);
7874d1da49cSBram Moolenaar static PyObject *DictionaryGetattr(PyObject *, char*);
7884d1da49cSBram Moolenaar static PyObject *ListGetattr(PyObject *, char *);
7894d1da49cSBram Moolenaar static PyObject *FunctionGetattr(PyObject *, char *);
7904d1da49cSBram Moolenaar 
7912a0f3d3fSBram Moolenaar #ifndef Py_VISIT
7922a0f3d3fSBram Moolenaar # define Py_VISIT(obj) visit(obj, arg)
7932a0f3d3fSBram Moolenaar #endif
7942a0f3d3fSBram Moolenaar #ifndef Py_CLEAR
7952a0f3d3fSBram Moolenaar # define Py_CLEAR(obj) \
7963e734ea2SBram Moolenaar     { \
7972a0f3d3fSBram Moolenaar 	Py_XDECREF(obj); \
7983e734ea2SBram Moolenaar 	obj = NULL; \
7993e734ea2SBram Moolenaar     }
8002a0f3d3fSBram Moolenaar #endif
8012a0f3d3fSBram Moolenaar 
802fdde880bSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
803fdde880bSBram Moolenaar     static void *
py_memsave(void * p,size_t len)804fdde880bSBram Moolenaar py_memsave(void *p, size_t len)
805fdde880bSBram Moolenaar {
806fdde880bSBram Moolenaar     void	*r;
807fdde880bSBram Moolenaar 
808fdde880bSBram Moolenaar     if (!(r = PyMem_Malloc(len)))
809fdde880bSBram Moolenaar 	return NULL;
810fdde880bSBram Moolenaar     mch_memmove(r, p, len);
811fdde880bSBram Moolenaar     return r;
812fdde880bSBram Moolenaar }
813fdde880bSBram Moolenaar 
814fdde880bSBram Moolenaar # define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1))
815fdde880bSBram Moolenaar #endif
816fdde880bSBram Moolenaar 
817922a4664SBram Moolenaar typedef PySliceObject PySliceObject_T;
818922a4664SBram Moolenaar 
819170bf1aeSBram Moolenaar /*
820170bf1aeSBram Moolenaar  * Include the code shared with if_python3.c
821170bf1aeSBram Moolenaar  */
822170bf1aeSBram Moolenaar #include "if_py_both.h"
823170bf1aeSBram Moolenaar 
824170bf1aeSBram Moolenaar 
8252ab2e860SBram Moolenaar ///////////////////////////////////////////////////////
8262ab2e860SBram Moolenaar // Internal function prototypes.
827071d4279SBram Moolenaar 
828071d4279SBram Moolenaar static int PythonMod_Init(void);
829071d4279SBram Moolenaar 
830071d4279SBram Moolenaar 
8312ab2e860SBram Moolenaar ///////////////////////////////////////////////////////
8322ab2e860SBram Moolenaar // 1. Python interpreter main program.
833071d4279SBram Moolenaar 
8342ab2e860SBram Moolenaar #if PYTHON_API_VERSION < 1007 // Python 1.4
835071d4279SBram Moolenaar typedef PyObject PyThreadState;
8369ba0eb85SBram Moolenaar #endif
837071d4279SBram Moolenaar 
83871700b89SBram Moolenaar #ifndef PY_CAN_RECURSE
839071d4279SBram Moolenaar static PyThreadState *saved_python_thread = NULL;
840071d4279SBram Moolenaar 
841071d4279SBram Moolenaar /*
842071d4279SBram Moolenaar  * Suspend a thread of the Python interpreter, other threads are allowed to
843071d4279SBram Moolenaar  * run.
844071d4279SBram Moolenaar  */
845293ee4d4SBram Moolenaar     static void
Python_SaveThread(void)846293ee4d4SBram Moolenaar Python_SaveThread(void)
847071d4279SBram Moolenaar {
848071d4279SBram Moolenaar     saved_python_thread = PyEval_SaveThread();
849071d4279SBram Moolenaar }
850071d4279SBram Moolenaar 
851071d4279SBram Moolenaar /*
852071d4279SBram Moolenaar  * Restore a thread of the Python interpreter, waits for other threads to
853071d4279SBram Moolenaar  * block.
854071d4279SBram Moolenaar  */
855293ee4d4SBram Moolenaar     static void
Python_RestoreThread(void)856293ee4d4SBram Moolenaar Python_RestoreThread(void)
857071d4279SBram Moolenaar {
858071d4279SBram Moolenaar     PyEval_RestoreThread(saved_python_thread);
859071d4279SBram Moolenaar     saved_python_thread = NULL;
8609ba0eb85SBram Moolenaar }
86171700b89SBram Moolenaar #endif
862071d4279SBram Moolenaar 
863071d4279SBram Moolenaar     void
python_end(void)86468c2f638SBram Moolenaar python_end(void)
865071d4279SBram Moolenaar {
866a5792f58SBram Moolenaar     static int recurse = 0;
867a5792f58SBram Moolenaar 
8682ab2e860SBram Moolenaar     // If a crash occurs while doing this, don't try again.
869a5792f58SBram Moolenaar     if (recurse != 0)
870a5792f58SBram Moolenaar 	return;
871a5792f58SBram Moolenaar 
872c4f83380SBram Moolenaar     python_end_called = TRUE;
873a5792f58SBram Moolenaar     ++recurse;
874a5792f58SBram Moolenaar 
875071d4279SBram Moolenaar #ifdef DYNAMIC_PYTHON
8760e21a3f6SBram Moolenaar     if (hinstPython && Py_IsInitialized())
8779ba0eb85SBram Moolenaar     {
87871700b89SBram Moolenaar # ifdef PY_CAN_RECURSE
87971700b89SBram Moolenaar 	PyGILState_Ensure();
88071700b89SBram Moolenaar # else
8812ab2e860SBram Moolenaar 	Python_RestoreThread();	    // enter python
88271700b89SBram Moolenaar # endif
8830e21a3f6SBram Moolenaar 	Py_Finalize();
8849ba0eb85SBram Moolenaar     }
8850e21a3f6SBram Moolenaar #else
8860e21a3f6SBram Moolenaar     if (Py_IsInitialized())
8879ba0eb85SBram Moolenaar     {
88871700b89SBram Moolenaar # ifdef PY_CAN_RECURSE
88971700b89SBram Moolenaar 	PyGILState_Ensure();
89071700b89SBram Moolenaar # else
8912ab2e860SBram Moolenaar 	Python_RestoreThread();	    // enter python
89271700b89SBram Moolenaar # endif
8930e21a3f6SBram Moolenaar 	Py_Finalize();
8949ba0eb85SBram Moolenaar     }
895071d4279SBram Moolenaar #endif
896a5792f58SBram Moolenaar 
897a5792f58SBram Moolenaar     --recurse;
898071d4279SBram Moolenaar }
899071d4279SBram Moolenaar 
9004c3a326cSBram Moolenaar #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
9014c3a326cSBram Moolenaar     int
python_loaded(void)90268c2f638SBram Moolenaar python_loaded(void)
9034c3a326cSBram Moolenaar {
9044c3a326cSBram Moolenaar     return (hinstPython != 0);
9054c3a326cSBram Moolenaar }
9064c3a326cSBram Moolenaar #endif
9074c3a326cSBram Moolenaar 
90894073167SBram Moolenaar static char *py_home_buf = NULL;
90994073167SBram Moolenaar 
910071d4279SBram Moolenaar     static int
Python_Init(void)911071d4279SBram Moolenaar Python_Init(void)
912071d4279SBram Moolenaar {
913071d4279SBram Moolenaar     if (!initialised)
914071d4279SBram Moolenaar     {
91512a28d4bSBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
91612a28d4bSBram Moolenaar 	PyObject *site;
91712a28d4bSBram Moolenaar #endif
91812a28d4bSBram Moolenaar 
919071d4279SBram Moolenaar #ifdef DYNAMIC_PYTHON
920071d4279SBram Moolenaar 	if (!python_enabled(TRUE))
921071d4279SBram Moolenaar 	{
922f9e3e09fSBram Moolenaar 	    emsg(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
923071d4279SBram Moolenaar 	    goto fail;
924071d4279SBram Moolenaar 	}
925071d4279SBram Moolenaar #endif
926071d4279SBram Moolenaar 
92794073167SBram Moolenaar 	if (*p_pyhome != NUL)
92894073167SBram Moolenaar 	{
9292ab2e860SBram Moolenaar 	    // The string must not change later, make a copy in static memory.
93094073167SBram Moolenaar 	    py_home_buf = (char *)vim_strsave(p_pyhome);
93194073167SBram Moolenaar 	    if (py_home_buf != NULL)
93294073167SBram Moolenaar 		Py_SetPythonHome(py_home_buf);
93394073167SBram Moolenaar 	}
934644d37b8SBram Moolenaar #ifdef PYTHON_HOME
93594073167SBram Moolenaar 	else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
936644d37b8SBram Moolenaar 	    Py_SetPythonHome(PYTHON_HOME);
937644d37b8SBram Moolenaar #endif
938644d37b8SBram Moolenaar 
939170bf1aeSBram Moolenaar 	init_structs();
940170bf1aeSBram Moolenaar 
94112a28d4bSBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
9422ab2e860SBram Moolenaar 	// Disable implicit 'import site', because it may cause Vim to exit
9432ab2e860SBram Moolenaar 	// when it can't be found.
94412a28d4bSBram Moolenaar 	Py_NoSiteFlag++;
94512a28d4bSBram Moolenaar #endif
94612a28d4bSBram Moolenaar 
947071d4279SBram Moolenaar 	Py_Initialize();
94812a28d4bSBram Moolenaar 
94912a28d4bSBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
9502ab2e860SBram Moolenaar 	// 'import site' explicitly.
95112a28d4bSBram Moolenaar 	site = PyImport_ImportModule("site");
95212a28d4bSBram Moolenaar 	if (site == NULL)
95312a28d4bSBram Moolenaar 	{
954f9e3e09fSBram Moolenaar 	    emsg(_("E887: Sorry, this command is disabled, the Python's site module could not be loaded."));
95512a28d4bSBram Moolenaar 	    goto fail;
95612a28d4bSBram Moolenaar 	}
95712a28d4bSBram Moolenaar 	Py_DECREF(site);
95812a28d4bSBram Moolenaar #endif
95912a28d4bSBram Moolenaar 
9602ab2e860SBram Moolenaar 	// Initialise threads, and below save the state using
9612ab2e860SBram Moolenaar 	// PyEval_SaveThread.  Without the call to PyEval_SaveThread, thread
9622ab2e860SBram Moolenaar 	// specific state (such as the system trace hook), will be lost
9632ab2e860SBram Moolenaar 	// between invocations of Python code.
964071d4279SBram Moolenaar 	PyEval_InitThreads();
965071d4279SBram Moolenaar #ifdef DYNAMIC_PYTHON
966071d4279SBram Moolenaar 	get_exceptions();
967071d4279SBram Moolenaar #endif
968071d4279SBram Moolenaar 
9691dc28783SBram Moolenaar 	if (PythonIO_Init_io())
970071d4279SBram Moolenaar 	    goto fail;
971071d4279SBram Moolenaar 
972071d4279SBram Moolenaar 	if (PythonMod_Init())
973071d4279SBram Moolenaar 	    goto fail;
974071d4279SBram Moolenaar 
975db913953SBram Moolenaar 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
976db913953SBram Moolenaar 
9772ab2e860SBram Moolenaar 	// Remove the element from sys.path that was added because of our
9782ab2e860SBram Moolenaar 	// argv[0] value in PythonMod_Init().  Previously we used an empty
9792ab2e860SBram Moolenaar 	// string, but depending on the OS we then get an empty entry or
9802ab2e860SBram Moolenaar 	// the current directory in sys.path.
9819774ecc8SBram Moolenaar 	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
9829774ecc8SBram Moolenaar 
9832ab2e860SBram Moolenaar 	// lock is created and acquired in PyEval_InitThreads() and thread
9842ab2e860SBram Moolenaar 	// state is created in Py_Initialize()
9852ab2e860SBram Moolenaar 	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
9862ab2e860SBram Moolenaar 	// (python must have threads enabled!)
9872ab2e860SBram Moolenaar 	// so the following does both: unlock GIL and save thread state in TLS
9882ab2e860SBram Moolenaar 	// without deleting thread state
98903db85b3SBram Moolenaar #ifndef PY_CAN_RECURSE
99003db85b3SBram Moolenaar 	saved_python_thread =
99103db85b3SBram Moolenaar #endif
99276d711c3SBram Moolenaar 	    PyEval_SaveThread();
993071d4279SBram Moolenaar 
994071d4279SBram Moolenaar 	initialised = 1;
995071d4279SBram Moolenaar     }
996071d4279SBram Moolenaar 
997071d4279SBram Moolenaar     return 0;
998071d4279SBram Moolenaar 
999071d4279SBram Moolenaar fail:
10002ab2e860SBram Moolenaar     // We call PythonIO_Flush() here to print any Python errors.
10012ab2e860SBram Moolenaar     // This is OK, as it is possible to call this function even
10022ab2e860SBram Moolenaar     // if PythonIO_Init_io() has not completed successfully (it will
10032ab2e860SBram Moolenaar     // not do anything in this case).
1004071d4279SBram Moolenaar     PythonIO_Flush();
1005071d4279SBram Moolenaar     return -1;
1006071d4279SBram Moolenaar }
1007071d4279SBram Moolenaar 
1008071d4279SBram Moolenaar /*
1009071d4279SBram Moolenaar  * External interface
1010071d4279SBram Moolenaar  */
1011071d4279SBram Moolenaar     static void
DoPyCommand(const char * cmd,rangeinitializer init_range,runner run,void * arg)1012b52f4c02SBram Moolenaar DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
1013071d4279SBram Moolenaar {
10149ba0eb85SBram Moolenaar #ifndef PY_CAN_RECURSE
1015071d4279SBram Moolenaar     static int		recursive = 0;
1016071d4279SBram Moolenaar #endif
1017071d4279SBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1018071d4279SBram Moolenaar     char		*saved_locale;
1019071d4279SBram Moolenaar #endif
102071700b89SBram Moolenaar #ifdef PY_CAN_RECURSE
102171700b89SBram Moolenaar     PyGILState_STATE	pygilstate;
102271700b89SBram Moolenaar #endif
1023071d4279SBram Moolenaar 
1024071d4279SBram Moolenaar #ifndef PY_CAN_RECURSE
1025071d4279SBram Moolenaar     if (recursive)
1026071d4279SBram Moolenaar     {
1027f9e3e09fSBram Moolenaar 	emsg(_("E659: Cannot invoke Python recursively"));
1028071d4279SBram Moolenaar 	return;
1029071d4279SBram Moolenaar     }
1030071d4279SBram Moolenaar     ++recursive;
1031071d4279SBram Moolenaar #endif
1032c4f83380SBram Moolenaar     if (python_end_called)
1033c4f83380SBram Moolenaar 	return;
1034071d4279SBram Moolenaar 
1035071d4279SBram Moolenaar     if (Python_Init())
1036071d4279SBram Moolenaar 	goto theend;
1037071d4279SBram Moolenaar 
1038b52f4c02SBram Moolenaar     init_range(arg);
1039b52f4c02SBram Moolenaar 
10402ab2e860SBram Moolenaar     Python_Release_Vim();	    // leave Vim
1041071d4279SBram Moolenaar 
1042071d4279SBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
10432ab2e860SBram Moolenaar     // Python only works properly when the LC_NUMERIC locale is "C".
1044071d4279SBram Moolenaar     saved_locale = setlocale(LC_NUMERIC, NULL);
1045071d4279SBram Moolenaar     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
1046071d4279SBram Moolenaar 	saved_locale = NULL;
1047071d4279SBram Moolenaar     else
1048071d4279SBram Moolenaar     {
10492ab2e860SBram Moolenaar 	// Need to make a copy, value may change when setting new locale.
1050e9ba516bSBram Moolenaar 	saved_locale = (char *) PY_STRSAVE(saved_locale);
1051071d4279SBram Moolenaar 	(void)setlocale(LC_NUMERIC, "C");
1052071d4279SBram Moolenaar     }
1053071d4279SBram Moolenaar #endif
1054071d4279SBram Moolenaar 
105571700b89SBram Moolenaar #ifdef PY_CAN_RECURSE
105671700b89SBram Moolenaar     pygilstate = PyGILState_Ensure();
105771700b89SBram Moolenaar #else
10582ab2e860SBram Moolenaar     Python_RestoreThread();	    // enter python
105971700b89SBram Moolenaar #endif
1060071d4279SBram Moolenaar 
10612a0f3d3fSBram Moolenaar     run((char *) cmd, arg
10622a0f3d3fSBram Moolenaar #ifdef PY_CAN_RECURSE
10632a0f3d3fSBram Moolenaar 	    , &pygilstate
10642a0f3d3fSBram Moolenaar #endif
10652a0f3d3fSBram Moolenaar 	    );
1066071d4279SBram Moolenaar 
106771700b89SBram Moolenaar #ifdef PY_CAN_RECURSE
106871700b89SBram Moolenaar     PyGILState_Release(pygilstate);
106971700b89SBram Moolenaar #else
10702ab2e860SBram Moolenaar     Python_SaveThread();	    // leave python
107171700b89SBram Moolenaar #endif
1072071d4279SBram Moolenaar 
1073071d4279SBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1074071d4279SBram Moolenaar     if (saved_locale != NULL)
1075071d4279SBram Moolenaar     {
1076071d4279SBram Moolenaar 	(void)setlocale(LC_NUMERIC, saved_locale);
1077e9ba516bSBram Moolenaar 	PyMem_Free(saved_locale);
1078071d4279SBram Moolenaar     }
1079071d4279SBram Moolenaar #endif
1080071d4279SBram Moolenaar 
10812ab2e860SBram Moolenaar     Python_Lock_Vim();		    // enter vim
1082071d4279SBram Moolenaar     PythonIO_Flush();
1083071d4279SBram Moolenaar 
1084071d4279SBram Moolenaar theend:
1085071d4279SBram Moolenaar #ifndef PY_CAN_RECURSE
1086071d4279SBram Moolenaar     --recursive;
1087071d4279SBram Moolenaar #endif
1088db913953SBram Moolenaar     return;
1089071d4279SBram Moolenaar }
1090071d4279SBram Moolenaar 
1091071d4279SBram Moolenaar /*
1092071d4279SBram Moolenaar  * ":python"
1093071d4279SBram Moolenaar  */
1094071d4279SBram Moolenaar     void
ex_python(exarg_T * eap)1095071d4279SBram Moolenaar ex_python(exarg_T *eap)
1096071d4279SBram Moolenaar {
1097071d4279SBram Moolenaar     char_u *script;
1098071d4279SBram Moolenaar 
1099071d4279SBram Moolenaar     script = script_get(eap, eap->arg);
1100071d4279SBram Moolenaar     if (!eap->skip)
1101071d4279SBram Moolenaar     {
110214816ad6SBram Moolenaar 	if (p_pyx == 0)
110314816ad6SBram Moolenaar 	    p_pyx = 2;
110414816ad6SBram Moolenaar 
1105b52f4c02SBram Moolenaar 	DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1106b52f4c02SBram Moolenaar 		(rangeinitializer) init_range_cmd,
1107b52f4c02SBram Moolenaar 		(runner) run_cmd,
1108b52f4c02SBram Moolenaar 		(void *) eap);
1109071d4279SBram Moolenaar     }
1110071d4279SBram Moolenaar     vim_free(script);
1111071d4279SBram Moolenaar }
1112071d4279SBram Moolenaar 
1113071d4279SBram Moolenaar #define BUFFER_SIZE 1024
1114071d4279SBram Moolenaar 
1115071d4279SBram Moolenaar /*
1116071d4279SBram Moolenaar  * ":pyfile"
1117071d4279SBram Moolenaar  */
1118071d4279SBram Moolenaar     void
ex_pyfile(exarg_T * eap)1119071d4279SBram Moolenaar ex_pyfile(exarg_T *eap)
1120071d4279SBram Moolenaar {
1121071d4279SBram Moolenaar     static char buffer[BUFFER_SIZE];
1122071d4279SBram Moolenaar     const char *file = (char *)eap->arg;
1123071d4279SBram Moolenaar     char *p;
1124071d4279SBram Moolenaar 
1125f42dd3c3SBram Moolenaar     if (p_pyx == 0)
1126f42dd3c3SBram Moolenaar 	p_pyx = 2;
1127f42dd3c3SBram Moolenaar 
11282ab2e860SBram Moolenaar     // Have to do it like this. PyRun_SimpleFile requires you to pass a
11292ab2e860SBram Moolenaar     // stdio file pointer, but Vim and the Python DLL are compiled with
11302ab2e860SBram Moolenaar     // different options under Windows, meaning that stdio pointers aren't
11312ab2e860SBram Moolenaar     // compatible between the two. Yuk.
11322ab2e860SBram Moolenaar     //
11332ab2e860SBram Moolenaar     // Put the string "execfile('file')" into buffer. But, we need to
11342ab2e860SBram Moolenaar     // escape any backslashes or single quotes in the file name, so that
11352ab2e860SBram Moolenaar     // Python won't mangle the file name.
1136071d4279SBram Moolenaar     strcpy(buffer, "execfile('");
11372ab2e860SBram Moolenaar     p = buffer + 10; // size of "execfile('"
1138071d4279SBram Moolenaar 
1139071d4279SBram Moolenaar     while (*file && p < buffer + (BUFFER_SIZE - 3))
1140071d4279SBram Moolenaar     {
1141071d4279SBram Moolenaar 	if (*file == '\\' || *file == '\'')
1142071d4279SBram Moolenaar 	    *p++ = '\\';
1143071d4279SBram Moolenaar 	*p++ = *file++;
1144071d4279SBram Moolenaar     }
1145071d4279SBram Moolenaar 
11462ab2e860SBram Moolenaar     // If we didn't finish the file name, we hit a buffer overflow
1147071d4279SBram Moolenaar     if (*file != '\0')
1148071d4279SBram Moolenaar 	return;
1149071d4279SBram Moolenaar 
11502ab2e860SBram Moolenaar     // Put in the terminating "')" and a null
1151071d4279SBram Moolenaar     *p++ = '\'';
1152071d4279SBram Moolenaar     *p++ = ')';
1153071d4279SBram Moolenaar     *p++ = '\0';
1154071d4279SBram Moolenaar 
11552ab2e860SBram Moolenaar     // Execute the file
1156b52f4c02SBram Moolenaar     DoPyCommand(buffer,
1157b52f4c02SBram Moolenaar 	    (rangeinitializer) init_range_cmd,
1158b52f4c02SBram Moolenaar 	    (runner) run_cmd,
1159b52f4c02SBram Moolenaar 	    (void *) eap);
1160071d4279SBram Moolenaar }
1161071d4279SBram Moolenaar 
1162d620aa9bSBram Moolenaar     void
ex_pydo(exarg_T * eap)1163d620aa9bSBram Moolenaar ex_pydo(exarg_T *eap)
1164d620aa9bSBram Moolenaar {
1165f42dd3c3SBram Moolenaar     if (p_pyx == 0)
1166f42dd3c3SBram Moolenaar 	p_pyx = 2;
1167f42dd3c3SBram Moolenaar 
1168b52f4c02SBram Moolenaar     DoPyCommand((char *)eap->arg,
1169b52f4c02SBram Moolenaar 	    (rangeinitializer) init_range_cmd,
1170b52f4c02SBram Moolenaar 	    (runner)run_do,
1171b52f4c02SBram Moolenaar 	    (void *)eap);
1172d620aa9bSBram Moolenaar }
1173d620aa9bSBram Moolenaar 
11742ab2e860SBram Moolenaar ///////////////////////////////////////////////////////
11752ab2e860SBram Moolenaar // 2. Python output stream: writes output via [e]msg().
1176071d4279SBram Moolenaar 
11772ab2e860SBram Moolenaar // Implementation functions
1178071d4279SBram Moolenaar 
1179071d4279SBram Moolenaar     static PyObject *
OutputGetattr(PyObject * self,char * name)1180071d4279SBram Moolenaar OutputGetattr(PyObject *self, char *name)
1181071d4279SBram Moolenaar {
1182071d4279SBram Moolenaar     if (strcmp(name, "softspace") == 0)
1183071d4279SBram Moolenaar 	return PyInt_FromLong(((OutputObject *)(self))->softspace);
1184dd8aca66SBram Moolenaar     else if (strcmp(name, "__members__") == 0)
1185dd8aca66SBram Moolenaar 	return ObjectDir(NULL, OutputAttrs);
11866d4431e7SBram Moolenaar     else if (strcmp(name, "errors") == 0)
11876d4431e7SBram Moolenaar 	return PyString_FromString("strict");
11886d4431e7SBram Moolenaar     else if (strcmp(name, "encoding") == 0)
11896d4431e7SBram Moolenaar 	return PyString_FromString(ENC_OPT);
1190071d4279SBram Moolenaar     return Py_FindMethod(OutputMethods, self, name);
1191071d4279SBram Moolenaar }
1192071d4279SBram Moolenaar 
11932ab2e860SBram Moolenaar ///////////////////////////////////////////////////////
11942ab2e860SBram Moolenaar // 3. Implementation of the Vim module for Python
1195071d4279SBram Moolenaar 
11962ab2e860SBram Moolenaar // Window type - Implementation functions
11972ab2e860SBram Moolenaar // --------------------------------------
1198071d4279SBram Moolenaar 
1199071d4279SBram Moolenaar #define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1200071d4279SBram Moolenaar 
12012ab2e860SBram Moolenaar // Buffer type - Implementation functions
12022ab2e860SBram Moolenaar // --------------------------------------
1203071d4279SBram Moolenaar 
1204071d4279SBram Moolenaar #define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1205071d4279SBram Moolenaar 
12064ce5fe4cSBram Moolenaar static int BufferAssItem(PyObject *, PyInt, PyObject *);
12074ce5fe4cSBram Moolenaar static int BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
1208071d4279SBram Moolenaar 
12092ab2e860SBram Moolenaar // Line range type - Implementation functions
12102ab2e860SBram Moolenaar // --------------------------------------
1211071d4279SBram Moolenaar 
1212071d4279SBram Moolenaar #define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1213071d4279SBram Moolenaar 
12144ce5fe4cSBram Moolenaar static int RangeAssItem(PyObject *, PyInt, PyObject *);
12154ce5fe4cSBram Moolenaar static int RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
1216071d4279SBram Moolenaar 
12172ab2e860SBram Moolenaar // Current objects type - Implementation functions
12182ab2e860SBram Moolenaar // -----------------------------------------------
1219071d4279SBram Moolenaar 
1220071d4279SBram Moolenaar static PySequenceMethods BufferAsSeq = {
12212ab2e860SBram Moolenaar     (PyInquiry)		BufferLength,	    // sq_length,    len(x)
12222ab2e860SBram Moolenaar     (binaryfunc)	0,		    // BufferConcat, sq_concat, x+y
12232ab2e860SBram Moolenaar     (PyIntArgFunc)	0,		    // BufferRepeat, sq_repeat, x*n
12242ab2e860SBram Moolenaar     (PyIntArgFunc)	BufferItem,	    // sq_item,      x[i]
12252ab2e860SBram Moolenaar     (PyIntIntArgFunc)	BufferSlice,	    // sq_slice,     x[i:j]
12262ab2e860SBram Moolenaar     (PyIntObjArgProc)	BufferAssItem,	    // sq_ass_item,  x[i]=v
12272ab2e860SBram Moolenaar     (PyIntIntObjArgProc) BufferAssSlice,    // sq_ass_slice, x[i:j]=v
12284d1da49cSBram Moolenaar     (objobjproc)	0,
12294d1da49cSBram Moolenaar     (binaryfunc)	0,
1230071d4279SBram Moolenaar     0,
1231071d4279SBram Moolenaar };
1232071d4279SBram Moolenaar 
12332ab2e860SBram Moolenaar // Buffer object - Implementation
1234071d4279SBram Moolenaar 
1235071d4279SBram Moolenaar     static PyObject *
BufferGetattr(PyObject * self,char * name)1236071d4279SBram Moolenaar BufferGetattr(PyObject *self, char *name)
1237071d4279SBram Moolenaar {
12384d1da49cSBram Moolenaar     PyObject *r;
1239071d4279SBram Moolenaar 
12409e822c00SBram Moolenaar     if ((r = BufferAttrValid((BufferObject *)(self), name)))
12419e822c00SBram Moolenaar 	return r;
12429e822c00SBram Moolenaar 
12434d1da49cSBram Moolenaar     if (CheckBuffer((BufferObject *)(self)))
1244071d4279SBram Moolenaar 	return NULL;
1245071d4279SBram Moolenaar 
12464d1da49cSBram Moolenaar     r = BufferAttr((BufferObject *)(self), name);
12474d1da49cSBram Moolenaar     if (r || PyErr_Occurred())
12484d1da49cSBram Moolenaar 	return r;
1249071d4279SBram Moolenaar     else
1250071d4279SBram Moolenaar 	return Py_FindMethod(BufferMethods, self, name);
1251071d4279SBram Moolenaar }
1252071d4279SBram Moolenaar 
12532ab2e860SBram Moolenaar //////////////////
1254071d4279SBram Moolenaar 
12554ce5fe4cSBram Moolenaar     static int
BufferAssItem(PyObject * self,PyInt n,PyObject * val)12562c45e945SBram Moolenaar BufferAssItem(PyObject *self, PyInt n, PyObject *val)
1257071d4279SBram Moolenaar {
12588f1723deSBram Moolenaar     return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
1259071d4279SBram Moolenaar }
1260071d4279SBram Moolenaar 
12614ce5fe4cSBram Moolenaar     static int
BufferAssSlice(PyObject * self,PyInt lo,PyInt hi,PyObject * val)12622c45e945SBram Moolenaar BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1263071d4279SBram Moolenaar {
12648f1723deSBram Moolenaar     return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
1265071d4279SBram Moolenaar }
1266071d4279SBram Moolenaar 
1267071d4279SBram Moolenaar static PySequenceMethods RangeAsSeq = {
12682ab2e860SBram Moolenaar     (PyInquiry)		RangeLength,	      // sq_length,    len(x)
12692ab2e860SBram Moolenaar     (binaryfunc)	0, /* RangeConcat, */ // sq_concat,    x+y
12702ab2e860SBram Moolenaar     (PyIntArgFunc)	0, /* RangeRepeat, */ // sq_repeat,    x*n
12712ab2e860SBram Moolenaar     (PyIntArgFunc)	RangeItem,	      // sq_item,      x[i]
12722ab2e860SBram Moolenaar     (PyIntIntArgFunc)	RangeSlice,	      // sq_slice,     x[i:j]
12732ab2e860SBram Moolenaar     (PyIntObjArgProc)	RangeAssItem,	      // sq_ass_item,  x[i]=v
12742ab2e860SBram Moolenaar     (PyIntIntObjArgProc) RangeAssSlice,	      // sq_ass_slice, x[i:j]=v
12754d1da49cSBram Moolenaar     (objobjproc)	0,
12764d1da49cSBram Moolenaar #if PY_MAJOR_VERSION >= 2
12774d1da49cSBram Moolenaar     (binaryfunc)	0,
12784d1da49cSBram Moolenaar     0,
12794d1da49cSBram Moolenaar #endif
1280071d4279SBram Moolenaar };
1281071d4279SBram Moolenaar 
12822ab2e860SBram Moolenaar // Line range object - Implementation
1283071d4279SBram Moolenaar 
1284071d4279SBram Moolenaar     static PyObject *
RangeGetattr(PyObject * self,char * name)1285071d4279SBram Moolenaar RangeGetattr(PyObject *self, char *name)
1286071d4279SBram Moolenaar {
1287071d4279SBram Moolenaar     if (strcmp(name, "start") == 0)
1288e7cb9cf6SBram Moolenaar 	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
1289071d4279SBram Moolenaar     else if (strcmp(name, "end") == 0)
1290e7cb9cf6SBram Moolenaar 	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
1291dd8aca66SBram Moolenaar     else if (strcmp(name, "__members__") == 0)
1292dd8aca66SBram Moolenaar 	return ObjectDir(NULL, RangeAttrs);
1293071d4279SBram Moolenaar     else
1294071d4279SBram Moolenaar 	return Py_FindMethod(RangeMethods, self, name);
1295071d4279SBram Moolenaar }
1296071d4279SBram Moolenaar 
12972ab2e860SBram Moolenaar ////////////////
1298071d4279SBram Moolenaar 
12994ce5fe4cSBram Moolenaar     static int
RangeAssItem(PyObject * self,PyInt n,PyObject * val)13002c45e945SBram Moolenaar RangeAssItem(PyObject *self, PyInt n, PyObject *val)
1301071d4279SBram Moolenaar {
1302ca8a4dfeSBram Moolenaar     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1303071d4279SBram Moolenaar 		     ((RangeObject *)(self))->start,
1304071d4279SBram Moolenaar 		     ((RangeObject *)(self))->end,
1305071d4279SBram Moolenaar 		     &((RangeObject *)(self))->end);
1306071d4279SBram Moolenaar }
1307071d4279SBram Moolenaar 
13084ce5fe4cSBram Moolenaar     static int
RangeAssSlice(PyObject * self,PyInt lo,PyInt hi,PyObject * val)13092c45e945SBram Moolenaar RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1310071d4279SBram Moolenaar {
131119e60943SBram Moolenaar     return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1312071d4279SBram Moolenaar 		      ((RangeObject *)(self))->start,
1313071d4279SBram Moolenaar 		      ((RangeObject *)(self))->end,
1314071d4279SBram Moolenaar 		      &((RangeObject *)(self))->end);
1315071d4279SBram Moolenaar }
1316071d4279SBram Moolenaar 
13172ab2e860SBram Moolenaar // TabPage object - Implementation
13185e538ecdSBram Moolenaar 
13195e538ecdSBram Moolenaar     static PyObject *
TabPageGetattr(PyObject * self,char * name)13205e538ecdSBram Moolenaar TabPageGetattr(PyObject *self, char *name)
13215e538ecdSBram Moolenaar {
13225e538ecdSBram Moolenaar     PyObject *r;
13235e538ecdSBram Moolenaar 
13249e822c00SBram Moolenaar     if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
13259e822c00SBram Moolenaar 	return r;
13269e822c00SBram Moolenaar 
13275e538ecdSBram Moolenaar     if (CheckTabPage((TabPageObject *)(self)))
13285e538ecdSBram Moolenaar 	return NULL;
13295e538ecdSBram Moolenaar 
13305e538ecdSBram Moolenaar     r = TabPageAttr((TabPageObject *)(self), name);
13315e538ecdSBram Moolenaar     if (r || PyErr_Occurred())
13325e538ecdSBram Moolenaar 	return r;
13335e538ecdSBram Moolenaar     else
13345e538ecdSBram Moolenaar 	return Py_FindMethod(TabPageMethods, self, name);
13355e538ecdSBram Moolenaar }
13365e538ecdSBram Moolenaar 
13372ab2e860SBram Moolenaar // Window object - Implementation
1338071d4279SBram Moolenaar 
1339071d4279SBram Moolenaar     static PyObject *
WindowGetattr(PyObject * self,char * name)1340071d4279SBram Moolenaar WindowGetattr(PyObject *self, char *name)
1341071d4279SBram Moolenaar {
13424d1da49cSBram Moolenaar     PyObject *r;
1343071d4279SBram Moolenaar 
13449e822c00SBram Moolenaar     if ((r = WindowAttrValid((WindowObject *)(self), name)))
13459e822c00SBram Moolenaar 	return r;
13469e822c00SBram Moolenaar 
13474d1da49cSBram Moolenaar     if (CheckWindow((WindowObject *)(self)))
1348071d4279SBram Moolenaar 	return NULL;
1349071d4279SBram Moolenaar 
13504d1da49cSBram Moolenaar     r = WindowAttr((WindowObject *)(self), name);
13514d1da49cSBram Moolenaar     if (r || PyErr_Occurred())
13524d1da49cSBram Moolenaar 	return r;
1353071d4279SBram Moolenaar     else
1354071d4279SBram Moolenaar 	return Py_FindMethod(WindowMethods, self, name);
1355071d4279SBram Moolenaar }
1356071d4279SBram Moolenaar 
13572ab2e860SBram Moolenaar // Tab page list object - Definitions
13585e538ecdSBram Moolenaar 
13595e538ecdSBram Moolenaar static PySequenceMethods TabListAsSeq = {
13602ab2e860SBram Moolenaar     (PyInquiry)		TabListLength,	    // sq_length,    len(x)
13612ab2e860SBram Moolenaar     (binaryfunc)	0,		    // sq_concat,    x+y
13622ab2e860SBram Moolenaar     (PyIntArgFunc)	0,		    // sq_repeat,    x*n
13632ab2e860SBram Moolenaar     (PyIntArgFunc)	TabListItem,	    // sq_item,      x[i]
13642ab2e860SBram Moolenaar     (PyIntIntArgFunc)	0,		    // sq_slice,     x[i:j]
13652ab2e860SBram Moolenaar     (PyIntObjArgProc)	0,		    // sq_ass_item,  x[i]=v
13662ab2e860SBram Moolenaar     (PyIntIntObjArgProc) 0,		    // sq_ass_slice, x[i:j]=v
13675e538ecdSBram Moolenaar     (objobjproc)	0,
13685e538ecdSBram Moolenaar #if PY_MAJOR_VERSION >= 2
13695e538ecdSBram Moolenaar     (binaryfunc)	0,
13705e538ecdSBram Moolenaar     0,
13715e538ecdSBram Moolenaar #endif
13725e538ecdSBram Moolenaar };
13735e538ecdSBram Moolenaar 
13742ab2e860SBram Moolenaar // Window list object - Definitions
1375071d4279SBram Moolenaar 
1376071d4279SBram Moolenaar static PySequenceMethods WinListAsSeq = {
13772ab2e860SBram Moolenaar     (PyInquiry)		WinListLength,	    // sq_length,    len(x)
13782ab2e860SBram Moolenaar     (binaryfunc)	0,		    // sq_concat,    x+y
13792ab2e860SBram Moolenaar     (PyIntArgFunc)	0,		    // sq_repeat,    x*n
13802ab2e860SBram Moolenaar     (PyIntArgFunc)	WinListItem,	    // sq_item,      x[i]
13812ab2e860SBram Moolenaar     (PyIntIntArgFunc)	0,		    // sq_slice,     x[i:j]
13822ab2e860SBram Moolenaar     (PyIntObjArgProc)	0,		    // sq_ass_item,  x[i]=v
13832ab2e860SBram Moolenaar     (PyIntIntObjArgProc) 0,		    // sq_ass_slice, x[i:j]=v
13844d1da49cSBram Moolenaar     (objobjproc)	0,
13854d1da49cSBram Moolenaar #if PY_MAJOR_VERSION >= 2
13864d1da49cSBram Moolenaar     (binaryfunc)	0,
13874d1da49cSBram Moolenaar     0,
13884d1da49cSBram Moolenaar #endif
1389071d4279SBram Moolenaar };
1390071d4279SBram Moolenaar 
13912ab2e860SBram Moolenaar // External interface
1392071d4279SBram Moolenaar 
1393071d4279SBram Moolenaar     void
python_buffer_free(buf_T * buf)1394071d4279SBram Moolenaar python_buffer_free(buf_T *buf)
1395071d4279SBram Moolenaar {
1396971db467SBram Moolenaar     if (BUF_PYTHON_REF(buf) != NULL)
1397071d4279SBram Moolenaar     {
1398971db467SBram Moolenaar 	BufferObject *bp = BUF_PYTHON_REF(buf);
1399071d4279SBram Moolenaar 	bp->buf = INVALID_BUFFER_VALUE;
1400971db467SBram Moolenaar 	BUF_PYTHON_REF(buf) = NULL;
1401071d4279SBram Moolenaar     }
1402071d4279SBram Moolenaar }
1403071d4279SBram Moolenaar 
1404071d4279SBram Moolenaar     void
python_window_free(win_T * win)1405071d4279SBram Moolenaar python_window_free(win_T *win)
1406071d4279SBram Moolenaar {
1407971db467SBram Moolenaar     if (WIN_PYTHON_REF(win) != NULL)
1408071d4279SBram Moolenaar     {
1409971db467SBram Moolenaar 	WindowObject *wp = WIN_PYTHON_REF(win);
1410071d4279SBram Moolenaar 	wp->win = INVALID_WINDOW_VALUE;
1411971db467SBram Moolenaar 	WIN_PYTHON_REF(win) = NULL;
1412071d4279SBram Moolenaar     }
1413071d4279SBram Moolenaar }
14145e538ecdSBram Moolenaar 
14155e538ecdSBram Moolenaar     void
python_tabpage_free(tabpage_T * tab)14165e538ecdSBram Moolenaar python_tabpage_free(tabpage_T *tab)
14175e538ecdSBram Moolenaar {
14185e538ecdSBram Moolenaar     if (TAB_PYTHON_REF(tab) != NULL)
14195e538ecdSBram Moolenaar     {
14205e538ecdSBram Moolenaar 	TabPageObject *tp = TAB_PYTHON_REF(tab);
14215e538ecdSBram Moolenaar 	tp->tab = INVALID_TABPAGE_VALUE;
14225e538ecdSBram Moolenaar 	TAB_PYTHON_REF(tab) = NULL;
14235e538ecdSBram Moolenaar     }
14245e538ecdSBram Moolenaar }
1425071d4279SBram Moolenaar 
14261dc28783SBram Moolenaar     static int
PythonMod_Init(void)1427071d4279SBram Moolenaar PythonMod_Init(void)
1428071d4279SBram Moolenaar {
14292ab2e860SBram Moolenaar     // The special value is removed from sys.path in Python_Init().
14309774ecc8SBram Moolenaar     static char	*(argv[2]) = {"/must>not&exist/foo", NULL};
1431071d4279SBram Moolenaar 
14321dc28783SBram Moolenaar     if (init_types())
14331dc28783SBram Moolenaar 	return -1;
1434071d4279SBram Moolenaar 
14352ab2e860SBram Moolenaar     // Set sys.argv[] to avoid a crash in warn().
1436071d4279SBram Moolenaar     PySys_SetArgv(1, argv);
1437071d4279SBram Moolenaar 
1438c09a6d6cSBram Moolenaar     vim_module = Py_InitModule4("vim", VimMethods, (char *)NULL,
1439c09a6d6cSBram Moolenaar 				(PyObject *)NULL, PYTHON_API_VERSION);
1440071d4279SBram Moolenaar 
1441dee2e315SBram Moolenaar     if (populate_module(vim_module))
1442c09a6d6cSBram Moolenaar 	return -1;
1443c09a6d6cSBram Moolenaar 
1444c09a6d6cSBram Moolenaar     if (init_sys_path())
1445c09a6d6cSBram Moolenaar 	return -1;
1446c09a6d6cSBram Moolenaar 
1447c09a6d6cSBram Moolenaar     return 0;
1448071d4279SBram Moolenaar }
1449071d4279SBram Moolenaar 
14502ab2e860SBram Moolenaar //////////////////////////////////////////////////////////////////////////
14512ab2e860SBram Moolenaar // 4. Utility functions for handling the interface between Vim and Python.
1452071d4279SBram Moolenaar 
14532ab2e860SBram Moolenaar // Convert a Vim line into a Python string.
14542ab2e860SBram Moolenaar // All internal newlines are replaced by null characters.
14552ab2e860SBram Moolenaar //
14562ab2e860SBram Moolenaar // On errors, the Python exception data is set, and NULL is returned.
1457071d4279SBram Moolenaar     static PyObject *
LineToString(const char * str)1458071d4279SBram Moolenaar LineToString(const char *str)
1459071d4279SBram Moolenaar {
1460071d4279SBram Moolenaar     PyObject *result;
14612c45e945SBram Moolenaar     PyInt len = strlen(str);
1462071d4279SBram Moolenaar     char *p;
1463071d4279SBram Moolenaar 
14642ab2e860SBram Moolenaar     // Allocate an Python string object, with uninitialised contents. We
14652ab2e860SBram Moolenaar     // must do it this way, so that we can modify the string in place
14662ab2e860SBram Moolenaar     // later. See the Python source, Objects/stringobject.c for details.
1467071d4279SBram Moolenaar     result = PyString_FromStringAndSize(NULL, len);
1468071d4279SBram Moolenaar     if (result == NULL)
1469071d4279SBram Moolenaar 	return NULL;
1470071d4279SBram Moolenaar 
1471071d4279SBram Moolenaar     p = PyString_AsString(result);
1472071d4279SBram Moolenaar 
1473071d4279SBram Moolenaar     while (*str)
1474071d4279SBram Moolenaar     {
1475071d4279SBram Moolenaar 	if (*str == '\n')
1476071d4279SBram Moolenaar 	    *p = '\0';
1477071d4279SBram Moolenaar 	else
1478071d4279SBram Moolenaar 	    *p = *str;
1479071d4279SBram Moolenaar 
1480071d4279SBram Moolenaar 	++p;
1481071d4279SBram Moolenaar 	++str;
1482071d4279SBram Moolenaar     }
1483071d4279SBram Moolenaar 
1484071d4279SBram Moolenaar     return result;
1485071d4279SBram Moolenaar }
1486071d4279SBram Moolenaar 
1487db913953SBram Moolenaar     static PyObject *
DictionaryGetattr(PyObject * self,char * name)1488db913953SBram Moolenaar DictionaryGetattr(PyObject *self, char *name)
1489db913953SBram Moolenaar {
149066b7985eSBram Moolenaar     DictionaryObject	*this = ((DictionaryObject *) (self));
149166b7985eSBram Moolenaar 
149266b7985eSBram Moolenaar     if (strcmp(name, "locked") == 0)
149366b7985eSBram Moolenaar 	return PyInt_FromLong(this->dict->dv_lock);
149466b7985eSBram Moolenaar     else if (strcmp(name, "scope") == 0)
149566b7985eSBram Moolenaar 	return PyInt_FromLong(this->dict->dv_scope);
1496dd8aca66SBram Moolenaar     else if (strcmp(name, "__members__") == 0)
1497dd8aca66SBram Moolenaar 	return ObjectDir(NULL, DictionaryAttrs);
149866b7985eSBram Moolenaar 
1499db913953SBram Moolenaar     return Py_FindMethod(DictionaryMethods, self, name);
1500db913953SBram Moolenaar }
1501db913953SBram Moolenaar 
1502db913953SBram Moolenaar     static PyObject *
ListGetattr(PyObject * self,char * name)1503db913953SBram Moolenaar ListGetattr(PyObject *self, char *name)
1504db913953SBram Moolenaar {
150566b7985eSBram Moolenaar     if (strcmp(name, "locked") == 0)
150666b7985eSBram Moolenaar 	return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1507dd8aca66SBram Moolenaar     else if (strcmp(name, "__members__") == 0)
1508dd8aca66SBram Moolenaar 	return ObjectDir(NULL, ListAttrs);
150966b7985eSBram Moolenaar 
1510db913953SBram Moolenaar     return Py_FindMethod(ListMethods, self, name);
1511db913953SBram Moolenaar }
1512db913953SBram Moolenaar 
1513db913953SBram Moolenaar     static PyObject *
FunctionGetattr(PyObject * self,char * name)1514db913953SBram Moolenaar FunctionGetattr(PyObject *self, char *name)
1515db913953SBram Moolenaar {
15168110a091SBram Moolenaar     PyObject	*r;
1517db913953SBram Moolenaar 
15188110a091SBram Moolenaar     r = FunctionAttr((FunctionObject *)(self), name);
15198110a091SBram Moolenaar 
15208110a091SBram Moolenaar     if (r || PyErr_Occurred())
15218110a091SBram Moolenaar 	return r;
1522db913953SBram Moolenaar     else
1523db913953SBram Moolenaar 	return Py_FindMethod(FunctionMethods, self, name);
1524db913953SBram Moolenaar }
1525db913953SBram Moolenaar 
1526db913953SBram Moolenaar     void
do_pyeval(char_u * str,typval_T * rettv)1527db913953SBram Moolenaar do_pyeval(char_u *str, typval_T *rettv)
1528db913953SBram Moolenaar {
1529b52f4c02SBram Moolenaar     DoPyCommand((char *) str,
1530b52f4c02SBram Moolenaar 	    (rangeinitializer) init_range_eval,
1531b52f4c02SBram Moolenaar 	    (runner) run_eval,
1532b52f4c02SBram Moolenaar 	    (void *) rettv);
15338e9a24a1SBram Moolenaar     if (rettv->v_type == VAR_UNKNOWN)
1534db913953SBram Moolenaar     {
153577fceb89SBram Moolenaar 	rettv->v_type = VAR_NUMBER;
153677fceb89SBram Moolenaar 	rettv->vval.v_number = 0;
1537db913953SBram Moolenaar     }
1538db913953SBram Moolenaar }
1539071d4279SBram Moolenaar 
15402ab2e860SBram Moolenaar // Don't generate a prototype for the next function, it generates an error on
15412ab2e860SBram Moolenaar // newer Python versions.
1542071d4279SBram Moolenaar #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1543071d4279SBram Moolenaar 
1544071d4279SBram Moolenaar     char *
Py_GetProgramName(void)1545071d4279SBram Moolenaar Py_GetProgramName(void)
1546071d4279SBram Moolenaar {
1547071d4279SBram Moolenaar     return "vim";
1548071d4279SBram Moolenaar }
15492ab2e860SBram Moolenaar #endif // Python 1.4
1550170bf1aeSBram Moolenaar 
15512459a5ecSBram Moolenaar     int
set_ref_in_python(int copyID)1552db913953SBram Moolenaar set_ref_in_python(int copyID)
1553db913953SBram Moolenaar {
15542459a5ecSBram Moolenaar     return set_ref_in_py(copyID);
1555db913953SBram Moolenaar }
1556