xref: /vim-8.2.3635/src/if_python3.c (revision 84e0f6ca)
1bd5e15fdSBram Moolenaar /* vi:set ts=8 sts=4 sw=4:
2bd5e15fdSBram Moolenaar  *
3bd5e15fdSBram Moolenaar  * VIM - Vi IMproved    by Bram Moolenaar
4bd5e15fdSBram Moolenaar  *
5bd5e15fdSBram Moolenaar  * Do ":help uganda"  in Vim to read copying and usage conditions.
6bd5e15fdSBram Moolenaar  * Do ":help credits" in Vim to see a list of people who contributed.
7bd5e15fdSBram Moolenaar  * See README.txt for an overview of the Vim source code.
8bd5e15fdSBram Moolenaar  */
9bd5e15fdSBram Moolenaar /*
10bd5e15fdSBram Moolenaar  * Python extensions by Paul Moore.
11bd5e15fdSBram Moolenaar  * Changes for Unix by David Leonard.
12bd5e15fdSBram Moolenaar  *
13bd5e15fdSBram Moolenaar  * This consists of four parts:
14bd5e15fdSBram Moolenaar  * 1. Python interpreter main program
15bd5e15fdSBram Moolenaar  * 2. Python output stream: writes output via [e]msg().
16bd5e15fdSBram Moolenaar  * 3. Implementation of the Vim module for Python
17bd5e15fdSBram Moolenaar  * 4. Utility functions for handling the interface between Vim and Python.
18bd5e15fdSBram Moolenaar  */
19bd5e15fdSBram Moolenaar 
20bd5e15fdSBram Moolenaar /*
21bd5e15fdSBram Moolenaar  * Roland Puntaier 2009/sept/16:
22bd5e15fdSBram Moolenaar  * Adaptations to support both python3.x and python2.x
23bd5e15fdSBram Moolenaar  */
24bd5e15fdSBram Moolenaar 
250c1f3f4dSBram Moolenaar /* uncomment this if used with the debug version of python */
260c1f3f4dSBram Moolenaar /* #define Py_DEBUG */
27bd5e15fdSBram Moolenaar 
28bd5e15fdSBram Moolenaar #include "vim.h"
29bd5e15fdSBram Moolenaar 
30bd5e15fdSBram Moolenaar #include <limits.h>
31bd5e15fdSBram Moolenaar 
32bd5e15fdSBram Moolenaar /* Python.h defines _POSIX_THREADS itself (if needed) */
33bd5e15fdSBram Moolenaar #ifdef _POSIX_THREADS
34bd5e15fdSBram Moolenaar # undef _POSIX_THREADS
35bd5e15fdSBram Moolenaar #endif
36bd5e15fdSBram Moolenaar 
37bd5e15fdSBram Moolenaar #if defined(_WIN32) && defined(HAVE_FCNTL_H)
38bd5e15fdSBram Moolenaar # undef HAVE_FCNTL_H
39bd5e15fdSBram Moolenaar #endif
40bd5e15fdSBram Moolenaar 
41bd5e15fdSBram Moolenaar #ifdef _DEBUG
42bd5e15fdSBram Moolenaar # undef _DEBUG
43bd5e15fdSBram Moolenaar #endif
44bd5e15fdSBram Moolenaar 
45bd5e15fdSBram Moolenaar #ifdef F_BLANK
46bd5e15fdSBram Moolenaar # undef F_BLANK
47bd5e15fdSBram Moolenaar #endif
48bd5e15fdSBram Moolenaar 
496df6f47dSBram Moolenaar #ifdef HAVE_STDARG_H
506df6f47dSBram Moolenaar # undef HAVE_STDARG_H   /* Python's config.h defines it as well. */
516df6f47dSBram Moolenaar #endif
52bd5e15fdSBram Moolenaar #ifdef _POSIX_C_SOURCE  /* defined in feature.h */
53bd5e15fdSBram Moolenaar # undef _POSIX_C_SOURCE
54bd5e15fdSBram Moolenaar #endif
556df6f47dSBram Moolenaar #ifdef _XOPEN_SOURCE
566df6f47dSBram Moolenaar # undef _XOPEN_SOURCE	/* pyconfig.h defines it as well. */
576df6f47dSBram Moolenaar #endif
58bd5e15fdSBram Moolenaar 
59bd5e15fdSBram Moolenaar #include <Python.h>
60bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
61bd5e15fdSBram Moolenaar # include "macglue.h"
62bd5e15fdSBram Moolenaar # include <CodeFragments.h>
63bd5e15fdSBram Moolenaar #endif
64bd5e15fdSBram Moolenaar #undef main /* Defined in python.h - aargh */
65bd5e15fdSBram Moolenaar #undef HAVE_FCNTL_H /* Clash with os_win32.h */
66bd5e15fdSBram Moolenaar 
67e8cdcef8SBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
68e8cdcef8SBram Moolenaar # define PY_SSIZE_T_CLEAN
69e8cdcef8SBram Moolenaar #endif
70e8cdcef8SBram Moolenaar 
71bd5e15fdSBram Moolenaar static void init_structs(void);
72bd5e15fdSBram Moolenaar 
733d64a317SBram Moolenaar /* The "surrogateescape" error handler is new in Python 3.1 */
743d64a317SBram Moolenaar #if PY_VERSION_HEX >= 0x030100f0
753d64a317SBram Moolenaar # define CODEC_ERROR_HANDLER "surrogateescape"
763d64a317SBram Moolenaar #else
773d64a317SBram Moolenaar # define CODEC_ERROR_HANDLER NULL
783d64a317SBram Moolenaar #endif
793d64a317SBram Moolenaar 
802afa3238SBram Moolenaar /* Python 3 does not support CObjects, always use Capsules */
812afa3238SBram Moolenaar #define PY_USE_CAPSULE
822afa3238SBram Moolenaar 
83170bf1aeSBram Moolenaar #define PyInt Py_ssize_t
84ca8a4dfeSBram Moolenaar #define PyString_Check(obj) PyUnicode_Check(obj)
85db913953SBram Moolenaar #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
8619e60943SBram Moolenaar #define PyString_FreeBytes(obj) Py_XDECREF(bytes)
8719e60943SBram Moolenaar #define PyString_AsString(obj) PyBytes_AsString(obj)
8819e60943SBram Moolenaar #define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
89ca8a4dfeSBram Moolenaar #define PyString_FromString(repr) PyUnicode_FromString(repr)
90afa6b9afSBram Moolenaar #define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len)
9177045658SBram Moolenaar #define PyInt_Check(obj) PyLong_Check(obj)
9277045658SBram Moolenaar #define PyInt_FromLong(i) PyLong_FromLong(i)
9377045658SBram Moolenaar #define PyInt_AsLong(obj) PyLong_AsLong(obj)
944d1da49cSBram Moolenaar #define Py_ssize_t_fmt "n"
95170bf1aeSBram Moolenaar 
960c1f3f4dSBram Moolenaar #if defined(DYNAMIC_PYTHON3) || defined(PROTO)
97bd5e15fdSBram Moolenaar 
98fa5d1e63SBram Moolenaar # ifndef WIN3264
99bd5e15fdSBram Moolenaar #  include <dlfcn.h>
100bd5e15fdSBram Moolenaar #  define FARPROC void*
101bd5e15fdSBram Moolenaar #  define HINSTANCE void*
102644d37b8SBram Moolenaar #  if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
103b61f95c3SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY)
104b61f95c3SBram Moolenaar #  else
105fa5d1e63SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
106b61f95c3SBram Moolenaar #  endif
107bd5e15fdSBram Moolenaar #  define close_dll dlclose
108bd5e15fdSBram Moolenaar #  define symbol_from_dll dlsym
109bd5e15fdSBram Moolenaar # else
110ebbcb824SBram Moolenaar #  define load_dll vimLoadLib
111bd5e15fdSBram Moolenaar #  define close_dll FreeLibrary
112bd5e15fdSBram Moolenaar #  define symbol_from_dll GetProcAddress
113bd5e15fdSBram Moolenaar # endif
114bd5e15fdSBram Moolenaar /*
115bd5e15fdSBram Moolenaar  * Wrapper defines
116bd5e15fdSBram Moolenaar  */
117bd5e15fdSBram Moolenaar # undef PyArg_Parse
118bd5e15fdSBram Moolenaar # define PyArg_Parse py3_PyArg_Parse
119bd5e15fdSBram Moolenaar # undef PyArg_ParseTuple
120bd5e15fdSBram Moolenaar # define PyArg_ParseTuple py3_PyArg_ParseTuple
12119e60943SBram Moolenaar # define PyMem_Free py3_PyMem_Free
122db913953SBram Moolenaar # define PyMem_Malloc py3_PyMem_Malloc
123bd5e15fdSBram Moolenaar # define PyDict_SetItemString py3_PyDict_SetItemString
124bd5e15fdSBram Moolenaar # define PyErr_BadArgument py3_PyErr_BadArgument
125bd5e15fdSBram Moolenaar # define PyErr_Clear py3_PyErr_Clear
1264d36987cSBram Moolenaar # define PyErr_PrintEx py3_PyErr_PrintEx
127bd5e15fdSBram Moolenaar # define PyErr_NoMemory py3_PyErr_NoMemory
128bd5e15fdSBram Moolenaar # define PyErr_Occurred py3_PyErr_Occurred
129bd5e15fdSBram Moolenaar # define PyErr_SetNone py3_PyErr_SetNone
130bd5e15fdSBram Moolenaar # define PyErr_SetString py3_PyErr_SetString
131bd5e15fdSBram Moolenaar # define PyEval_InitThreads py3_PyEval_InitThreads
132bd5e15fdSBram Moolenaar # define PyEval_RestoreThread py3_PyEval_RestoreThread
133bd5e15fdSBram Moolenaar # define PyEval_SaveThread py3_PyEval_SaveThread
134bd5e15fdSBram Moolenaar # define PyGILState_Ensure py3_PyGILState_Ensure
135bd5e15fdSBram Moolenaar # define PyGILState_Release py3_PyGILState_Release
136bd5e15fdSBram Moolenaar # define PyLong_AsLong py3_PyLong_AsLong
137bd5e15fdSBram Moolenaar # define PyLong_FromLong py3_PyLong_FromLong
138bd5e15fdSBram Moolenaar # define PyList_GetItem py3_PyList_GetItem
139bd5e15fdSBram Moolenaar # define PyList_Append py3_PyList_Append
140bd5e15fdSBram Moolenaar # define PyList_New py3_PyList_New
141bd5e15fdSBram Moolenaar # define PyList_SetItem py3_PyList_SetItem
142bd5e15fdSBram Moolenaar # define PyList_Size py3_PyList_Size
143db913953SBram Moolenaar # define PySequence_Check py3_PySequence_Check
144db913953SBram Moolenaar # define PySequence_Size py3_PySequence_Size
145db913953SBram Moolenaar # define PySequence_GetItem py3_PySequence_GetItem
146db913953SBram Moolenaar # define PyTuple_Size py3_PyTuple_Size
147db913953SBram Moolenaar # define PyTuple_GetItem py3_PyTuple_GetItem
148bd5e15fdSBram Moolenaar # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
149bd5e15fdSBram Moolenaar # define PyImport_ImportModule py3_PyImport_ImportModule
150db913953SBram Moolenaar # define PyImport_AddModule py3_PyImport_AddModule
151bd5e15fdSBram Moolenaar # define PyObject_Init py3__PyObject_Init
152bd5e15fdSBram Moolenaar # define PyDict_New py3_PyDict_New
153bd5e15fdSBram Moolenaar # define PyDict_GetItemString py3_PyDict_GetItemString
154db913953SBram Moolenaar # define PyDict_Next py3_PyDict_Next
155db913953SBram Moolenaar # define PyMapping_Check py3_PyMapping_Check
156db913953SBram Moolenaar # define PyMapping_Items py3_PyMapping_Items
157db913953SBram Moolenaar # define PyIter_Next py3_PyIter_Next
158db913953SBram Moolenaar # define PyObject_GetIter py3_PyObject_GetIter
159bd5e15fdSBram Moolenaar # define PyModule_GetDict py3_PyModule_GetDict
160bd5e15fdSBram Moolenaar #undef PyRun_SimpleString
161bd5e15fdSBram Moolenaar # define PyRun_SimpleString py3_PyRun_SimpleString
162db913953SBram Moolenaar #undef PyRun_String
163db913953SBram Moolenaar # define PyRun_String py3_PyRun_String
164bd5e15fdSBram Moolenaar # define PySys_SetObject py3_PySys_SetObject
165bd5e15fdSBram Moolenaar # define PySys_SetArgv py3_PySys_SetArgv
166bd5e15fdSBram Moolenaar # define PyType_Ready py3_PyType_Ready
167bd5e15fdSBram Moolenaar #undef Py_BuildValue
168bd5e15fdSBram Moolenaar # define Py_BuildValue py3_Py_BuildValue
169644d37b8SBram Moolenaar # define Py_SetPythonHome py3_Py_SetPythonHome
170bd5e15fdSBram Moolenaar # define Py_Initialize py3_Py_Initialize
171bd5e15fdSBram Moolenaar # define Py_Finalize py3_Py_Finalize
172bd5e15fdSBram Moolenaar # define Py_IsInitialized py3_Py_IsInitialized
173bd5e15fdSBram Moolenaar # define _Py_NoneStruct (*py3__Py_NoneStruct)
17466b7985eSBram Moolenaar # define _Py_FalseStruct (*py3__Py_FalseStruct)
17566b7985eSBram Moolenaar # define _Py_TrueStruct (*py3__Py_TrueStruct)
176db913953SBram Moolenaar # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
177bd5e15fdSBram Moolenaar # define PyModule_AddObject py3_PyModule_AddObject
178bd5e15fdSBram Moolenaar # define PyImport_AppendInittab py3_PyImport_AppendInittab
1797bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
1807bc4f93cSBram Moolenaar #  undef _PyUnicode_AsString
1819c9cbf13SBram Moolenaar #  define _PyUnicode_AsString py3_PyUnicode_AsUTF8
1827bc4f93cSBram Moolenaar # else
183bd5e15fdSBram Moolenaar #  define _PyUnicode_AsString py3__PyUnicode_AsString
1847bc4f93cSBram Moolenaar # endif
18519e60943SBram Moolenaar # undef PyUnicode_AsEncodedString
18619e60943SBram Moolenaar # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
18719e60943SBram Moolenaar # undef PyBytes_AsString
18819e60943SBram Moolenaar # define PyBytes_AsString py3_PyBytes_AsString
189cdab9051SBram Moolenaar # define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
190db913953SBram Moolenaar # undef PyBytes_FromString
191db913953SBram Moolenaar # define PyBytes_FromString py3_PyBytes_FromString
192db913953SBram Moolenaar # define PyFloat_FromDouble py3_PyFloat_FromDouble
193db913953SBram Moolenaar # define PyFloat_AsDouble py3_PyFloat_AsDouble
194bd5e15fdSBram Moolenaar # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
19566b7985eSBram Moolenaar # define PyType_Type (*py3_PyType_Type)
196bd5e15fdSBram Moolenaar # define PySlice_Type (*py3_PySlice_Type)
197db913953SBram Moolenaar # define PyFloat_Type (*py3_PyFloat_Type)
19866b7985eSBram Moolenaar # define PyBool_Type (*py3_PyBool_Type)
19919e60943SBram Moolenaar # define PyErr_NewException py3_PyErr_NewException
200bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
201bd5e15fdSBram Moolenaar #  define _Py_NegativeRefcount py3__Py_NegativeRefcount
202bd5e15fdSBram Moolenaar #  define _Py_RefTotal (*py3__Py_RefTotal)
203bd5e15fdSBram Moolenaar #  define _Py_Dealloc py3__Py_Dealloc
204bd5e15fdSBram Moolenaar #  define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
205bd5e15fdSBram Moolenaar #  define _PyObject_DebugFree py3__PyObject_DebugFree
206bd5e15fdSBram Moolenaar # else
207bd5e15fdSBram Moolenaar #  define PyObject_Malloc py3_PyObject_Malloc
208bd5e15fdSBram Moolenaar #  define PyObject_Free py3_PyObject_Free
209bd5e15fdSBram Moolenaar # endif
210bd5e15fdSBram Moolenaar # define PyType_GenericAlloc py3_PyType_GenericAlloc
211bd5e15fdSBram Moolenaar # define PyType_GenericNew py3_PyType_GenericNew
212bd5e15fdSBram Moolenaar # define PyModule_Create2 py3_PyModule_Create2
213bd5e15fdSBram Moolenaar # undef PyUnicode_FromString
214bd5e15fdSBram Moolenaar # define PyUnicode_FromString py3_PyUnicode_FromString
21519e60943SBram Moolenaar # undef PyUnicode_Decode
21619e60943SBram Moolenaar # define PyUnicode_Decode py3_PyUnicode_Decode
217db913953SBram Moolenaar # define PyType_IsSubtype py3_PyType_IsSubtype
218db913953SBram Moolenaar # define PyCapsule_New py3_PyCapsule_New
219db913953SBram Moolenaar # define PyCapsule_GetPointer py3_PyCapsule_GetPointer
220bd5e15fdSBram Moolenaar 
221bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
222bd5e15fdSBram Moolenaar #  undef PyObject_NEW
223bd5e15fdSBram Moolenaar #  define PyObject_NEW(type, typeobj) \
224bd5e15fdSBram Moolenaar ( (type *) PyObject_Init( \
225bd5e15fdSBram Moolenaar 	(PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
226bd5e15fdSBram Moolenaar # endif
227b61f95c3SBram Moolenaar 
228bd5e15fdSBram Moolenaar /*
229bd5e15fdSBram Moolenaar  * Pointers for dynamic link
230bd5e15fdSBram Moolenaar  */
231bd5e15fdSBram Moolenaar static int (*py3_PySys_SetArgv)(int, wchar_t **);
232644d37b8SBram Moolenaar static void (*py3_Py_SetPythonHome)(wchar_t *home);
233bd5e15fdSBram Moolenaar static void (*py3_Py_Initialize)(void);
234bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_New)(Py_ssize_t size);
235bd5e15fdSBram Moolenaar static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
236bd5e15fdSBram Moolenaar static void (*py3_PyGILState_Release)(PyGILState_STATE);
237bd5e15fdSBram Moolenaar static int (*py3_PySys_SetObject)(char *, PyObject *);
238bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
239bd5e15fdSBram Moolenaar static Py_ssize_t (*py3_PyList_Size)(PyObject *);
240db913953SBram Moolenaar static int (*py3_PySequence_Check)(PyObject *);
241db913953SBram Moolenaar static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
242db913953SBram Moolenaar static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
243db913953SBram Moolenaar static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
244db913953SBram Moolenaar static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
245db913953SBram Moolenaar static int (*py3_PyMapping_Check)(PyObject *);
246db913953SBram Moolenaar static PyObject* (*py3_PyMapping_Items)(PyObject *);
247314ed4b2SBram Moolenaar static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
248bd5e15fdSBram Moolenaar 		     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
249bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_NoMemory)(void);
250bd5e15fdSBram Moolenaar static void (*py3_Py_Finalize)(void);
251bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetString)(PyObject *, const char *);
252bd5e15fdSBram Moolenaar static int (*py3_PyRun_SimpleString)(char *);
253db913953SBram Moolenaar static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
254bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
255bd5e15fdSBram Moolenaar static PyObject* (*py3_PyImport_ImportModule)(const char *);
256db913953SBram Moolenaar static PyObject* (*py3_PyImport_AddModule)(const char *);
257bd5e15fdSBram Moolenaar static int (*py3_PyErr_BadArgument)(void);
258bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_Occurred)(void);
259bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_GetDict)(PyObject *);
260bd5e15fdSBram Moolenaar static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
261bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
262db913953SBram Moolenaar static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
263bd5e15fdSBram Moolenaar static PyObject* (*py3_PyLong_FromLong)(long);
264bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_New)(void);
265db913953SBram Moolenaar static PyObject* (*py3_PyIter_Next)(PyObject *);
266db913953SBram Moolenaar static PyObject* (*py3_PyObject_GetIter)(PyObject *);
267bd5e15fdSBram Moolenaar static PyObject* (*py3_Py_BuildValue)(char *, ...);
268bd5e15fdSBram Moolenaar static int (*py3_PyType_Ready)(PyTypeObject *type);
269bd5e15fdSBram Moolenaar static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
270bd5e15fdSBram Moolenaar static PyObject* (*py3_PyUnicode_FromString)(const char *u);
27119e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
27219e60943SBram Moolenaar 	const char *encoding, const char *errors);
273bd5e15fdSBram Moolenaar static long (*py3_PyLong_AsLong)(PyObject *);
274bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetNone)(PyObject *);
275bd5e15fdSBram Moolenaar static void (*py3_PyEval_InitThreads)(void);
276bd5e15fdSBram Moolenaar static void(*py3_PyEval_RestoreThread)(PyThreadState *);
277bd5e15fdSBram Moolenaar static PyThreadState*(*py3_PyEval_SaveThread)(void);
278bd5e15fdSBram Moolenaar static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
279bd5e15fdSBram Moolenaar static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
28019e60943SBram Moolenaar static int (*py3_PyMem_Free)(void *);
281db913953SBram Moolenaar static void* (*py3_PyMem_Malloc)(size_t);
282bd5e15fdSBram Moolenaar static int (*py3_Py_IsInitialized)(void);
283bd5e15fdSBram Moolenaar static void (*py3_PyErr_Clear)(void);
2844d36987cSBram Moolenaar static void (*py3_PyErr_PrintEx)(int);
285bd5e15fdSBram Moolenaar static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
286db913953SBram Moolenaar static iternextfunc py3__PyObject_NextNotImplemented;
287bd5e15fdSBram Moolenaar static PyObject* py3__Py_NoneStruct;
28866b7985eSBram Moolenaar static PyObject* py3__Py_FalseStruct;
28966b7985eSBram Moolenaar static PyObject* py3__Py_TrueStruct;
290bd5e15fdSBram Moolenaar static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
291bd5e15fdSBram Moolenaar static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
2927bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
2939c9cbf13SBram Moolenaar static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
2947bc4f93cSBram Moolenaar # else
295bd5e15fdSBram Moolenaar static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
2967bc4f93cSBram Moolenaar # endif
29719e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
29819e60943SBram Moolenaar static char* (*py3_PyBytes_AsString)(PyObject *bytes);
299cdab9051SBram Moolenaar static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
300db913953SBram Moolenaar static PyObject* (*py3_PyBytes_FromString)(char *str);
301db913953SBram Moolenaar static PyObject* (*py3_PyFloat_FromDouble)(double num);
302db913953SBram Moolenaar static double (*py3_PyFloat_AsDouble)(PyObject *);
303bd5e15fdSBram Moolenaar static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
304bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
305bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
306bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
30766b7985eSBram Moolenaar static PyTypeObject* py3_PyType_Type;
308bd5e15fdSBram Moolenaar static PyTypeObject* py3_PySlice_Type;
309db913953SBram Moolenaar static PyTypeObject* py3_PyFloat_Type;
31066b7985eSBram Moolenaar static PyTypeObject* py3_PyBool_Type;
31119e60943SBram Moolenaar static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
312db913953SBram Moolenaar static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
313db913953SBram Moolenaar static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
314bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
315bd5e15fdSBram Moolenaar     static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
316bd5e15fdSBram Moolenaar     static Py_ssize_t* py3__Py_RefTotal;
317bd5e15fdSBram Moolenaar     static void (*py3__Py_Dealloc)(PyObject *obj);
318bd5e15fdSBram Moolenaar     static void (*py3__PyObject_DebugFree)(void*);
319bd5e15fdSBram Moolenaar     static void* (*py3__PyObject_DebugMalloc)(size_t);
320bd5e15fdSBram Moolenaar # else
321bd5e15fdSBram Moolenaar     static void (*py3_PyObject_Free)(void*);
322bd5e15fdSBram Moolenaar     static void* (*py3_PyObject_Malloc)(size_t);
323bd5e15fdSBram Moolenaar # endif
324db913953SBram Moolenaar static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
325bd5e15fdSBram Moolenaar 
326bd5e15fdSBram Moolenaar static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
327bd5e15fdSBram Moolenaar 
328bd5e15fdSBram Moolenaar /* Imported exception objects */
329bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_AttributeError;
330bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_IndexError;
331af6abb9dSBram Moolenaar static PyObject *p3imp_PyExc_KeyError;
332bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_KeyboardInterrupt;
333bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_TypeError;
334bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_ValueError;
335bd5e15fdSBram Moolenaar 
336bd5e15fdSBram Moolenaar # define PyExc_AttributeError p3imp_PyExc_AttributeError
337bd5e15fdSBram Moolenaar # define PyExc_IndexError p3imp_PyExc_IndexError
338af6abb9dSBram Moolenaar # define PyExc_KeyError p3imp_PyExc_KeyError
339bd5e15fdSBram Moolenaar # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
340bd5e15fdSBram Moolenaar # define PyExc_TypeError p3imp_PyExc_TypeError
341bd5e15fdSBram Moolenaar # define PyExc_ValueError p3imp_PyExc_ValueError
342bd5e15fdSBram Moolenaar 
343bd5e15fdSBram Moolenaar /*
344bd5e15fdSBram Moolenaar  * Table of name to function pointer of python.
345bd5e15fdSBram Moolenaar  */
346bd5e15fdSBram Moolenaar # define PYTHON_PROC FARPROC
347bd5e15fdSBram Moolenaar static struct
348bd5e15fdSBram Moolenaar {
349bd5e15fdSBram Moolenaar     char *name;
350bd5e15fdSBram Moolenaar     PYTHON_PROC *ptr;
351bd5e15fdSBram Moolenaar } py3_funcname_table[] =
352bd5e15fdSBram Moolenaar {
353bd5e15fdSBram Moolenaar     {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
354644d37b8SBram Moolenaar     {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
355bd5e15fdSBram Moolenaar     {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
356e8cdcef8SBram Moolenaar # ifndef PY_SSIZE_T_CLEAN
357bd5e15fdSBram Moolenaar     {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
358e8cdcef8SBram Moolenaar     {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
359e8cdcef8SBram Moolenaar # else
360e8cdcef8SBram Moolenaar     {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
361e8cdcef8SBram Moolenaar     {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
362e8cdcef8SBram Moolenaar # endif
36319e60943SBram Moolenaar     {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
364db913953SBram Moolenaar     {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
365bd5e15fdSBram Moolenaar     {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
366bd5e15fdSBram Moolenaar     {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
367bd5e15fdSBram Moolenaar     {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
368bd5e15fdSBram Moolenaar     {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
369bd5e15fdSBram Moolenaar     {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
370bd5e15fdSBram Moolenaar     {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
371db913953SBram Moolenaar     {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
372db913953SBram Moolenaar     {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
373db913953SBram Moolenaar     {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
374db913953SBram Moolenaar     {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
375db913953SBram Moolenaar     {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
376bd5e15fdSBram Moolenaar     {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
377bd5e15fdSBram Moolenaar     {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
378bd5e15fdSBram Moolenaar     {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
379bd5e15fdSBram Moolenaar     {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
380bd5e15fdSBram Moolenaar     {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
381db913953SBram Moolenaar     {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
382bd5e15fdSBram Moolenaar     {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
383bd5e15fdSBram Moolenaar     {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
384db913953SBram Moolenaar     {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
385bd5e15fdSBram Moolenaar     {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
386bd5e15fdSBram Moolenaar     {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
387bd5e15fdSBram Moolenaar     {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
388bd5e15fdSBram Moolenaar     {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
389bd5e15fdSBram Moolenaar     {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
390db913953SBram Moolenaar     {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
391db913953SBram Moolenaar     {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
392db913953SBram Moolenaar     {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
393db913953SBram Moolenaar     {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
394db913953SBram Moolenaar     {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
395bd5e15fdSBram Moolenaar     {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
396bd5e15fdSBram Moolenaar     {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
397bd5e15fdSBram Moolenaar     {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
398bd5e15fdSBram Moolenaar     {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
399bd5e15fdSBram Moolenaar     {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
400bd5e15fdSBram Moolenaar     {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
401bd5e15fdSBram Moolenaar     {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
402bd5e15fdSBram Moolenaar     {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
403bd5e15fdSBram Moolenaar     {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
404bd5e15fdSBram Moolenaar     {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
405bd5e15fdSBram Moolenaar     {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
406db913953SBram Moolenaar     {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
407bd5e15fdSBram Moolenaar     {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
40866b7985eSBram Moolenaar     {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
40966b7985eSBram Moolenaar     {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
410bd5e15fdSBram Moolenaar     {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
4114d36987cSBram Moolenaar     {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
412bd5e15fdSBram Moolenaar     {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
413bd5e15fdSBram Moolenaar     {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
414bd5e15fdSBram Moolenaar     {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
4157bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
4169c9cbf13SBram Moolenaar     {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
4177bc4f93cSBram Moolenaar # else
418bd5e15fdSBram Moolenaar     {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
4197bc4f93cSBram Moolenaar # endif
42019e60943SBram Moolenaar     {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
421cdab9051SBram Moolenaar     {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
422db913953SBram Moolenaar     {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
423db913953SBram Moolenaar     {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
424db913953SBram Moolenaar     {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
425bd5e15fdSBram Moolenaar     {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
426bd5e15fdSBram Moolenaar     {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
427bd5e15fdSBram Moolenaar     {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
428bd5e15fdSBram Moolenaar     {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
42966b7985eSBram Moolenaar     {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
430bd5e15fdSBram Moolenaar     {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
431db913953SBram Moolenaar     {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
43266b7985eSBram Moolenaar     {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
43319e60943SBram Moolenaar     {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
434bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
435bd5e15fdSBram Moolenaar     {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
436bd5e15fdSBram Moolenaar     {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
437bd5e15fdSBram Moolenaar     {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
438bd5e15fdSBram Moolenaar     {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
439bd5e15fdSBram Moolenaar     {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
440bd5e15fdSBram Moolenaar # else
441bd5e15fdSBram Moolenaar     {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
442bd5e15fdSBram Moolenaar     {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
443bd5e15fdSBram Moolenaar # endif
444db913953SBram Moolenaar     {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
445db913953SBram Moolenaar     {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
446db913953SBram Moolenaar     {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
447bd5e15fdSBram Moolenaar     {"", NULL},
448bd5e15fdSBram Moolenaar };
449bd5e15fdSBram Moolenaar 
450bd5e15fdSBram Moolenaar /*
451bd5e15fdSBram Moolenaar  * Free python.dll
452bd5e15fdSBram Moolenaar  */
453170bf1aeSBram Moolenaar     static void
454170bf1aeSBram Moolenaar end_dynamic_python3(void)
455bd5e15fdSBram Moolenaar {
4564c3a326cSBram Moolenaar     if (hinstPy3 != 0)
457bd5e15fdSBram Moolenaar     {
458bd5e15fdSBram Moolenaar 	close_dll(hinstPy3);
459bd5e15fdSBram Moolenaar 	hinstPy3 = 0;
460bd5e15fdSBram Moolenaar     }
461bd5e15fdSBram Moolenaar }
462bd5e15fdSBram Moolenaar 
463bd5e15fdSBram Moolenaar /*
464bd5e15fdSBram Moolenaar  * Load library and get all pointers.
465bd5e15fdSBram Moolenaar  * Parameter 'libname' provides name of DLL.
466bd5e15fdSBram Moolenaar  * Return OK or FAIL.
467bd5e15fdSBram Moolenaar  */
468170bf1aeSBram Moolenaar     static int
469170bf1aeSBram Moolenaar py3_runtime_link_init(char *libname, int verbose)
470bd5e15fdSBram Moolenaar {
471bd5e15fdSBram Moolenaar     int i;
47219e60943SBram Moolenaar     void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
473bd5e15fdSBram Moolenaar 
474644d37b8SBram Moolenaar # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
475b744b2faSBram Moolenaar     /* Can't have Python and Python3 loaded at the same time.
476b744b2faSBram Moolenaar      * It cause a crash, because RTLD_GLOBAL is needed for
477b744b2faSBram Moolenaar      * standard C extension libraries of one or both python versions. */
4784c3a326cSBram Moolenaar     if (python_loaded())
4794c3a326cSBram Moolenaar     {
4809dc93ae4SBram Moolenaar 	if (verbose)
481b744b2faSBram Moolenaar 	    EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
4824c3a326cSBram Moolenaar 	return FAIL;
4834c3a326cSBram Moolenaar     }
4844c3a326cSBram Moolenaar # endif
4854c3a326cSBram Moolenaar 
4864c3a326cSBram Moolenaar     if (hinstPy3 != 0)
487bd5e15fdSBram Moolenaar 	return OK;
488bd5e15fdSBram Moolenaar     hinstPy3 = load_dll(libname);
489bd5e15fdSBram Moolenaar 
490bd5e15fdSBram Moolenaar     if (!hinstPy3)
491bd5e15fdSBram Moolenaar     {
492bd5e15fdSBram Moolenaar 	if (verbose)
493bd5e15fdSBram Moolenaar 	    EMSG2(_(e_loadlib), libname);
494bd5e15fdSBram Moolenaar 	return FAIL;
495bd5e15fdSBram Moolenaar     }
496bd5e15fdSBram Moolenaar 
497bd5e15fdSBram Moolenaar     for (i = 0; py3_funcname_table[i].ptr; ++i)
498bd5e15fdSBram Moolenaar     {
499bd5e15fdSBram Moolenaar 	if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
500bd5e15fdSBram Moolenaar 			py3_funcname_table[i].name)) == NULL)
501bd5e15fdSBram Moolenaar 	{
502bd5e15fdSBram Moolenaar 	    close_dll(hinstPy3);
503bd5e15fdSBram Moolenaar 	    hinstPy3 = 0;
504bd5e15fdSBram Moolenaar 	    if (verbose)
505bd5e15fdSBram Moolenaar 		EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
506bd5e15fdSBram Moolenaar 	    return FAIL;
507bd5e15fdSBram Moolenaar 	}
508bd5e15fdSBram Moolenaar     }
509bd5e15fdSBram Moolenaar 
51069154f22SBram Moolenaar     /* Load unicode functions separately as only the ucs2 or the ucs4 functions
51169154f22SBram Moolenaar      * will be present in the library. */
5127bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
5137bc4f93cSBram Moolenaar     ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
5147bc4f93cSBram Moolenaar     ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
5157bc4f93cSBram Moolenaar     ucs_as_encoded_string = symbol_from_dll(hinstPy3,
5167bc4f93cSBram Moolenaar 	    "PyUnicode_AsEncodedString");
5177bc4f93cSBram Moolenaar # else
518bd5e15fdSBram Moolenaar     ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
51919e60943SBram Moolenaar     ucs_decode = symbol_from_dll(hinstPy3,
52019e60943SBram Moolenaar 	    "PyUnicodeUCS2_Decode");
52119e60943SBram Moolenaar     ucs_as_encoded_string = symbol_from_dll(hinstPy3,
52219e60943SBram Moolenaar 	    "PyUnicodeUCS2_AsEncodedString");
52319e60943SBram Moolenaar     if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
524bd5e15fdSBram Moolenaar     {
525bd5e15fdSBram Moolenaar 	ucs_from_string = symbol_from_dll(hinstPy3,
526bd5e15fdSBram Moolenaar 		"PyUnicodeUCS4_FromString");
52719e60943SBram Moolenaar 	ucs_decode = symbol_from_dll(hinstPy3,
52819e60943SBram Moolenaar 		"PyUnicodeUCS4_Decode");
52919e60943SBram Moolenaar 	ucs_as_encoded_string = symbol_from_dll(hinstPy3,
53019e60943SBram Moolenaar 		"PyUnicodeUCS4_AsEncodedString");
531bd5e15fdSBram Moolenaar     }
5327bc4f93cSBram Moolenaar # endif
53319e60943SBram Moolenaar     if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
534bd5e15fdSBram Moolenaar     {
535bd5e15fdSBram Moolenaar 	py3_PyUnicode_FromString = ucs_from_string;
53619e60943SBram Moolenaar 	py3_PyUnicode_Decode = ucs_decode;
53719e60943SBram Moolenaar 	py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
538bd5e15fdSBram Moolenaar     }
539bd5e15fdSBram Moolenaar     else
540bd5e15fdSBram Moolenaar     {
541bd5e15fdSBram Moolenaar 	close_dll(hinstPy3);
542bd5e15fdSBram Moolenaar 	hinstPy3 = 0;
543bd5e15fdSBram Moolenaar 	if (verbose)
544bd5e15fdSBram Moolenaar 	    EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
545bd5e15fdSBram Moolenaar 	return FAIL;
546bd5e15fdSBram Moolenaar     }
547bd5e15fdSBram Moolenaar 
548bd5e15fdSBram Moolenaar     return OK;
549bd5e15fdSBram Moolenaar }
550bd5e15fdSBram Moolenaar 
551bd5e15fdSBram Moolenaar /*
552bd5e15fdSBram Moolenaar  * If python is enabled (there is installed python on Windows system) return
553bd5e15fdSBram Moolenaar  * TRUE, else FALSE.
554bd5e15fdSBram Moolenaar  */
555170bf1aeSBram Moolenaar     int
556170bf1aeSBram Moolenaar python3_enabled(int verbose)
557bd5e15fdSBram Moolenaar {
558bd5e15fdSBram Moolenaar     return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
559bd5e15fdSBram Moolenaar }
560bd5e15fdSBram Moolenaar 
561bd5e15fdSBram Moolenaar /* Load the standard Python exceptions - don't import the symbols from the
562bd5e15fdSBram Moolenaar  * DLL, as this can cause errors (importing data symbols is not reliable).
563bd5e15fdSBram Moolenaar  */
564bd5e15fdSBram Moolenaar static void get_py3_exceptions __ARGS((void));
565bd5e15fdSBram Moolenaar 
566170bf1aeSBram Moolenaar     static void
567170bf1aeSBram Moolenaar get_py3_exceptions()
568bd5e15fdSBram Moolenaar {
569bd5e15fdSBram Moolenaar     PyObject *exmod = PyImport_ImportModule("builtins");
570bd5e15fdSBram Moolenaar     PyObject *exdict = PyModule_GetDict(exmod);
571bd5e15fdSBram Moolenaar     p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
572bd5e15fdSBram Moolenaar     p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
573af6abb9dSBram Moolenaar     p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
574bd5e15fdSBram Moolenaar     p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
575bd5e15fdSBram Moolenaar     p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
576bd5e15fdSBram Moolenaar     p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
577bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_AttributeError);
578bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_IndexError);
579af6abb9dSBram Moolenaar     Py_XINCREF(p3imp_PyExc_KeyError);
580bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
581bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_TypeError);
582bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_ValueError);
583bd5e15fdSBram Moolenaar     Py_XDECREF(exmod);
584bd5e15fdSBram Moolenaar }
585bd5e15fdSBram Moolenaar #endif /* DYNAMIC_PYTHON3 */
586bd5e15fdSBram Moolenaar 
587ca8a4dfeSBram Moolenaar static PyObject *BufferNew (buf_T *);
588ca8a4dfeSBram Moolenaar static PyObject *WindowNew(win_T *);
589ca8a4dfeSBram Moolenaar static PyObject *LineToString(const char *);
5907f85d297SBram Moolenaar static PyObject *BufferDir(PyObject *, PyObject *);
591ca8a4dfeSBram Moolenaar 
592db913953SBram Moolenaar static int py3initialised = 0;
593db913953SBram Moolenaar 
594db913953SBram Moolenaar #define PYINITIALISED py3initialised
595db913953SBram Moolenaar 
596cdab9051SBram Moolenaar #define DICTKEY_DECL PyObject *bytes = NULL;
597cdab9051SBram Moolenaar 
598db913953SBram Moolenaar #define DICTKEY_GET(err) \
599db913953SBram Moolenaar     if (PyBytes_Check(keyObject)) \
600cdab9051SBram Moolenaar     { \
601afa6b9afSBram Moolenaar 	if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
602cdab9051SBram Moolenaar 	    return err; \
603cdab9051SBram Moolenaar     } \
604db913953SBram Moolenaar     else if (PyUnicode_Check(keyObject)) \
605db913953SBram Moolenaar     { \
606db913953SBram Moolenaar 	bytes = PyString_AsBytes(keyObject); \
607db913953SBram Moolenaar 	if (bytes == NULL) \
608db913953SBram Moolenaar 	    return err; \
609afa6b9afSBram Moolenaar 	if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
610db913953SBram Moolenaar 	    return err; \
611db913953SBram Moolenaar     } \
612db913953SBram Moolenaar     else \
613db913953SBram Moolenaar     { \
614db913953SBram Moolenaar 	PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
615db913953SBram Moolenaar 	return err; \
616db913953SBram Moolenaar     }
617cdab9051SBram Moolenaar 
618db913953SBram Moolenaar #define DICTKEY_UNREF \
619db913953SBram Moolenaar     if (bytes != NULL) \
620db913953SBram Moolenaar 	Py_XDECREF(bytes);
621db913953SBram Moolenaar 
6224d1da49cSBram Moolenaar #define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self);
623db913953SBram Moolenaar 
624170bf1aeSBram Moolenaar     static void
625170bf1aeSBram Moolenaar call_PyObject_Free(void *p)
626bd5e15fdSBram Moolenaar {
627bd5e15fdSBram Moolenaar #ifdef Py_DEBUG
628bd5e15fdSBram Moolenaar     _PyObject_DebugFree(p);
629bd5e15fdSBram Moolenaar #else
630bd5e15fdSBram Moolenaar     PyObject_Free(p);
631bd5e15fdSBram Moolenaar #endif
632bd5e15fdSBram Moolenaar }
633170bf1aeSBram Moolenaar 
634170bf1aeSBram Moolenaar     static PyObject *
635170bf1aeSBram Moolenaar call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
636bd5e15fdSBram Moolenaar {
637bd5e15fdSBram Moolenaar     return PyType_GenericNew(type,args,kwds);
638bd5e15fdSBram Moolenaar }
639170bf1aeSBram Moolenaar 
640170bf1aeSBram Moolenaar     static PyObject *
641170bf1aeSBram Moolenaar call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
642bd5e15fdSBram Moolenaar {
643bd5e15fdSBram Moolenaar     return PyType_GenericAlloc(type,nitems);
644bd5e15fdSBram Moolenaar }
645bd5e15fdSBram Moolenaar 
6464d1da49cSBram Moolenaar static PyObject *OutputGetattro(PyObject *, PyObject *);
6474d1da49cSBram Moolenaar static int OutputSetattro(PyObject *, PyObject *, PyObject *);
6484d1da49cSBram Moolenaar static PyObject *BufferGetattro(PyObject *, PyObject *);
6494d1da49cSBram Moolenaar static PyObject *WindowGetattro(PyObject *, PyObject *);
6504d1da49cSBram Moolenaar static int WindowSetattro(PyObject *, PyObject *, PyObject *);
6514d1da49cSBram Moolenaar static PyObject *RangeGetattro(PyObject *, PyObject *);
6524d1da49cSBram Moolenaar static PyObject *CurrentGetattro(PyObject *, PyObject *);
6534d1da49cSBram Moolenaar static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
6544d1da49cSBram Moolenaar static PyObject *DictionaryGetattro(PyObject *, PyObject *);
6554d1da49cSBram Moolenaar static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
6564d1da49cSBram Moolenaar static PyObject *ListGetattro(PyObject *, PyObject *);
6574d1da49cSBram Moolenaar static int ListSetattro(PyObject *, PyObject *, PyObject *);
6584d1da49cSBram Moolenaar static PyObject *FunctionGetattro(PyObject *, PyObject *);
6594d1da49cSBram Moolenaar 
6604d1da49cSBram Moolenaar static struct PyModuleDef vimmodule;
6614d1da49cSBram Moolenaar 
6624d1da49cSBram Moolenaar /*
6634d1da49cSBram Moolenaar  * Include the code shared with if_python.c
6644d1da49cSBram Moolenaar  */
6654d1da49cSBram Moolenaar #include "if_py_both.h"
6664d1da49cSBram Moolenaar 
6674d1da49cSBram Moolenaar #define GET_ATTR_STRING(name, nameobj) \
6684d1da49cSBram Moolenaar     char	*name = ""; \
6694d1da49cSBram Moolenaar     if (PyUnicode_Check(nameobj)) \
6704d1da49cSBram Moolenaar 	name = _PyUnicode_AsString(nameobj)
6714d1da49cSBram Moolenaar 
6724d1da49cSBram Moolenaar #define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
6734d1da49cSBram Moolenaar 
674bd5e15fdSBram Moolenaar /******************************************************
675bd5e15fdSBram Moolenaar  * Internal function prototypes.
676bd5e15fdSBram Moolenaar  */
677bd5e15fdSBram Moolenaar 
678db913953SBram Moolenaar static PyObject *globals;
679db913953SBram Moolenaar 
680bd5e15fdSBram Moolenaar static int PythonIO_Init(void);
6817854e3abSBram Moolenaar static PyObject *Py3Init_vim(void);
682bd5e15fdSBram Moolenaar 
683bd5e15fdSBram Moolenaar /******************************************************
684bd5e15fdSBram Moolenaar  * 1. Python interpreter main program.
685bd5e15fdSBram Moolenaar  */
686bd5e15fdSBram Moolenaar 
687bd5e15fdSBram Moolenaar static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
688bd5e15fdSBram Moolenaar 
689170bf1aeSBram Moolenaar     void
690170bf1aeSBram Moolenaar python3_end()
691bd5e15fdSBram Moolenaar {
692bd5e15fdSBram Moolenaar     static int recurse = 0;
693bd5e15fdSBram Moolenaar 
694bd5e15fdSBram Moolenaar     /* If a crash occurs while doing this, don't try again. */
695bd5e15fdSBram Moolenaar     if (recurse != 0)
696bd5e15fdSBram Moolenaar 	return;
697bd5e15fdSBram Moolenaar 
698bd5e15fdSBram Moolenaar     ++recurse;
699bd5e15fdSBram Moolenaar 
700bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
701bd5e15fdSBram Moolenaar     if (hinstPy3)
702bd5e15fdSBram Moolenaar #endif
703bd5e15fdSBram Moolenaar     if (Py_IsInitialized())
704bd5e15fdSBram Moolenaar     {
705bd5e15fdSBram Moolenaar 	// acquire lock before finalizing
706bd5e15fdSBram Moolenaar 	pygilstate = PyGILState_Ensure();
707bd5e15fdSBram Moolenaar 
708bd5e15fdSBram Moolenaar 	Py_Finalize();
709bd5e15fdSBram Moolenaar     }
710bd5e15fdSBram Moolenaar 
711bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
712bd5e15fdSBram Moolenaar     end_dynamic_python3();
713bd5e15fdSBram Moolenaar #endif
714bd5e15fdSBram Moolenaar 
715bd5e15fdSBram Moolenaar     --recurse;
716bd5e15fdSBram Moolenaar }
717bd5e15fdSBram Moolenaar 
7184c3a326cSBram Moolenaar #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
7194c3a326cSBram Moolenaar     int
7204c3a326cSBram Moolenaar python3_loaded()
7214c3a326cSBram Moolenaar {
7224c3a326cSBram Moolenaar     return (hinstPy3 != 0);
7234c3a326cSBram Moolenaar }
7244c3a326cSBram Moolenaar #endif
7254c3a326cSBram Moolenaar 
726170bf1aeSBram Moolenaar     static int
727170bf1aeSBram Moolenaar Python3_Init(void)
728bd5e15fdSBram Moolenaar {
729bd5e15fdSBram Moolenaar     if (!py3initialised)
730bd5e15fdSBram Moolenaar     {
731bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
732bd5e15fdSBram Moolenaar 	if (!python3_enabled(TRUE))
733bd5e15fdSBram Moolenaar 	{
734bd5e15fdSBram Moolenaar 	    EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
735bd5e15fdSBram Moolenaar 	    goto fail;
736bd5e15fdSBram Moolenaar 	}
737bd5e15fdSBram Moolenaar #endif
738bd5e15fdSBram Moolenaar 
739bd5e15fdSBram Moolenaar 	init_structs();
740bd5e15fdSBram Moolenaar 
741644d37b8SBram Moolenaar 
742644d37b8SBram Moolenaar #ifdef PYTHON3_HOME
743644d37b8SBram Moolenaar 	Py_SetPythonHome(PYTHON3_HOME);
744644d37b8SBram Moolenaar #endif
745644d37b8SBram Moolenaar 
7467bc4f93cSBram Moolenaar 	PyImport_AppendInittab("vim", Py3Init_vim);
7477bc4f93cSBram Moolenaar 
748bd5e15fdSBram Moolenaar #if !defined(MACOS) || defined(MACOS_X_UNIX)
749bd5e15fdSBram Moolenaar 	Py_Initialize();
750bd5e15fdSBram Moolenaar #else
751bd5e15fdSBram Moolenaar 	PyMac_Initialize();
752bd5e15fdSBram Moolenaar #endif
75376d711c3SBram Moolenaar 	/* Initialise threads, and below save the state using
75476d711c3SBram Moolenaar 	 * PyEval_SaveThread.  Without the call to PyEval_SaveThread, thread
75576d711c3SBram Moolenaar 	 * specific state (such as the system trace hook), will be lost
75676d711c3SBram Moolenaar 	 * between invocations of Python code. */
757456f2bb2SBram Moolenaar 	PyEval_InitThreads();
758bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
759bd5e15fdSBram Moolenaar 	get_py3_exceptions();
760bd5e15fdSBram Moolenaar #endif
761bd5e15fdSBram Moolenaar 
762bd5e15fdSBram Moolenaar 	if (PythonIO_Init())
763bd5e15fdSBram Moolenaar 	    goto fail;
764bd5e15fdSBram Moolenaar 
765db913953SBram Moolenaar 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
766db913953SBram Moolenaar 
767bd5e15fdSBram Moolenaar 	/* Remove the element from sys.path that was added because of our
768bd5e15fdSBram Moolenaar 	 * argv[0] value in Py3Init_vim().  Previously we used an empty
769bd5e15fdSBram Moolenaar 	 * string, but dependinding on the OS we then get an empty entry or
77019e60943SBram Moolenaar 	 * the current directory in sys.path.
77119e60943SBram Moolenaar 	 * Only after vim has been imported, the element does exist in
77219e60943SBram Moolenaar 	 * sys.path.
77319e60943SBram Moolenaar 	 */
77419e60943SBram Moolenaar 	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
775bd5e15fdSBram Moolenaar 
77676d711c3SBram Moolenaar 	/* lock is created and acquired in PyEval_InitThreads() and thread
77776d711c3SBram Moolenaar 	 * state is created in Py_Initialize()
77876d711c3SBram Moolenaar 	 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
77976d711c3SBram Moolenaar 	 * (python must have threads enabled!)
78076d711c3SBram Moolenaar 	 * so the following does both: unlock GIL and save thread state in TLS
78176d711c3SBram Moolenaar 	 * without deleting thread state
78276d711c3SBram Moolenaar 	 */
78376d711c3SBram Moolenaar 	PyEval_SaveThread();
784bd5e15fdSBram Moolenaar 
785bd5e15fdSBram Moolenaar 	py3initialised = 1;
786bd5e15fdSBram Moolenaar     }
787bd5e15fdSBram Moolenaar 
788bd5e15fdSBram Moolenaar     return 0;
789bd5e15fdSBram Moolenaar 
790bd5e15fdSBram Moolenaar fail:
791bd5e15fdSBram Moolenaar     /* We call PythonIO_Flush() here to print any Python errors.
792bd5e15fdSBram Moolenaar      * This is OK, as it is possible to call this function even
793bd5e15fdSBram Moolenaar      * if PythonIO_Init() has not completed successfully (it will
794bd5e15fdSBram Moolenaar      * not do anything in this case).
795bd5e15fdSBram Moolenaar      */
796bd5e15fdSBram Moolenaar     PythonIO_Flush();
797bd5e15fdSBram Moolenaar     return -1;
798bd5e15fdSBram Moolenaar }
799bd5e15fdSBram Moolenaar 
800bd5e15fdSBram Moolenaar /*
801bd5e15fdSBram Moolenaar  * External interface
802bd5e15fdSBram Moolenaar  */
803170bf1aeSBram Moolenaar     static void
804db913953SBram Moolenaar DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
805bd5e15fdSBram Moolenaar {
806bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
807bd5e15fdSBram Moolenaar     GrafPtr		oldPort;
808bd5e15fdSBram Moolenaar #endif
809bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
810bd5e15fdSBram Moolenaar     char		*saved_locale;
811bd5e15fdSBram Moolenaar #endif
81219e60943SBram Moolenaar     PyObject		*cmdstr;
81319e60943SBram Moolenaar     PyObject		*cmdbytes;
814bd5e15fdSBram Moolenaar 
815bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
816bd5e15fdSBram Moolenaar     GetPort(&oldPort);
817bd5e15fdSBram Moolenaar     /* Check if the Python library is available */
818bd5e15fdSBram Moolenaar     if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
819bd5e15fdSBram Moolenaar 	goto theend;
820bd5e15fdSBram Moolenaar #endif
821bd5e15fdSBram Moolenaar     if (Python3_Init())
822bd5e15fdSBram Moolenaar 	goto theend;
823bd5e15fdSBram Moolenaar 
824db913953SBram Moolenaar     if (rettv == NULL)
825db913953SBram Moolenaar     {
826bd5e15fdSBram Moolenaar 	RangeStart = eap->line1;
827bd5e15fdSBram Moolenaar 	RangeEnd = eap->line2;
828db913953SBram Moolenaar     }
829db913953SBram Moolenaar     else
830db913953SBram Moolenaar     {
831db913953SBram Moolenaar 	RangeStart = (PyInt) curwin->w_cursor.lnum;
832db913953SBram Moolenaar 	RangeEnd = RangeStart;
833db913953SBram Moolenaar     }
834bd5e15fdSBram Moolenaar     Python_Release_Vim();	    /* leave vim */
835bd5e15fdSBram Moolenaar 
836bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
837bd5e15fdSBram Moolenaar     /* Python only works properly when the LC_NUMERIC locale is "C". */
838bd5e15fdSBram Moolenaar     saved_locale = setlocale(LC_NUMERIC, NULL);
839bd5e15fdSBram Moolenaar     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
840bd5e15fdSBram Moolenaar 	saved_locale = NULL;
841bd5e15fdSBram Moolenaar     else
842bd5e15fdSBram Moolenaar     {
843bd5e15fdSBram Moolenaar 	/* Need to make a copy, value may change when setting new locale. */
844bd5e15fdSBram Moolenaar 	saved_locale = (char *)vim_strsave((char_u *)saved_locale);
845bd5e15fdSBram Moolenaar 	(void)setlocale(LC_NUMERIC, "C");
846bd5e15fdSBram Moolenaar     }
847bd5e15fdSBram Moolenaar #endif
848bd5e15fdSBram Moolenaar 
849bd5e15fdSBram Moolenaar     pygilstate = PyGILState_Ensure();
850bd5e15fdSBram Moolenaar 
85119e60943SBram Moolenaar     /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
85219e60943SBram Moolenaar      * SyntaxError (unicode error). */
8533d64a317SBram Moolenaar     cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
8543d64a317SBram Moolenaar 					(char *)ENC_OPT, CODEC_ERROR_HANDLER);
8553d64a317SBram Moolenaar     cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
85619e60943SBram Moolenaar     Py_XDECREF(cmdstr);
857db913953SBram Moolenaar     if (rettv == NULL)
85819e60943SBram Moolenaar 	PyRun_SimpleString(PyBytes_AsString(cmdbytes));
859db913953SBram Moolenaar     else
860db913953SBram Moolenaar     {
861db913953SBram Moolenaar 	PyObject	*r;
862db913953SBram Moolenaar 
863db913953SBram Moolenaar 	r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
864db913953SBram Moolenaar 			 globals, globals);
865db913953SBram Moolenaar 	if (r == NULL)
8664d36987cSBram Moolenaar 	{
8674d36987cSBram Moolenaar 	    if (PyErr_Occurred() && !msg_silent)
8684d36987cSBram Moolenaar 		PyErr_PrintEx(0);
869db913953SBram Moolenaar 	    EMSG(_("E860: Eval did not return a valid python 3 object"));
8704d36987cSBram Moolenaar 	}
871db913953SBram Moolenaar 	else
872db913953SBram Moolenaar 	{
873db913953SBram Moolenaar 	    if (ConvertFromPyObject(r, rettv) == -1)
874db913953SBram Moolenaar 		EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
875db913953SBram Moolenaar 	    Py_DECREF(r);
876db913953SBram Moolenaar 	}
877db913953SBram Moolenaar 	PyErr_Clear();
878db913953SBram Moolenaar     }
87919e60943SBram Moolenaar     Py_XDECREF(cmdbytes);
880bd5e15fdSBram Moolenaar 
881bd5e15fdSBram Moolenaar     PyGILState_Release(pygilstate);
882bd5e15fdSBram Moolenaar 
883bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
884bd5e15fdSBram Moolenaar     if (saved_locale != NULL)
885bd5e15fdSBram Moolenaar     {
886bd5e15fdSBram Moolenaar 	(void)setlocale(LC_NUMERIC, saved_locale);
887bd5e15fdSBram Moolenaar 	vim_free(saved_locale);
888bd5e15fdSBram Moolenaar     }
889bd5e15fdSBram Moolenaar #endif
890bd5e15fdSBram Moolenaar 
891bd5e15fdSBram Moolenaar     Python_Lock_Vim();		    /* enter vim */
892bd5e15fdSBram Moolenaar     PythonIO_Flush();
893bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
894bd5e15fdSBram Moolenaar     SetPort(oldPort);
895bd5e15fdSBram Moolenaar #endif
896bd5e15fdSBram Moolenaar 
897bd5e15fdSBram Moolenaar theend:
898bd5e15fdSBram Moolenaar     return;	    /* keeps lint happy */
899bd5e15fdSBram Moolenaar }
900bd5e15fdSBram Moolenaar 
901bd5e15fdSBram Moolenaar /*
902368373e9SBram Moolenaar  * ":py3"
903bd5e15fdSBram Moolenaar  */
904170bf1aeSBram Moolenaar     void
905170bf1aeSBram Moolenaar ex_py3(exarg_T *eap)
906bd5e15fdSBram Moolenaar {
907bd5e15fdSBram Moolenaar     char_u *script;
908bd5e15fdSBram Moolenaar 
909bd5e15fdSBram Moolenaar     script = script_get(eap, eap->arg);
910bd5e15fdSBram Moolenaar     if (!eap->skip)
911bd5e15fdSBram Moolenaar     {
912bd5e15fdSBram Moolenaar 	if (script == NULL)
913db913953SBram Moolenaar 	    DoPy3Command(eap, (char *)eap->arg, NULL);
914bd5e15fdSBram Moolenaar 	else
915db913953SBram Moolenaar 	    DoPy3Command(eap, (char *)script, NULL);
916bd5e15fdSBram Moolenaar     }
917bd5e15fdSBram Moolenaar     vim_free(script);
918bd5e15fdSBram Moolenaar }
919bd5e15fdSBram Moolenaar 
920bd5e15fdSBram Moolenaar #define BUFFER_SIZE 2048
921bd5e15fdSBram Moolenaar 
922bd5e15fdSBram Moolenaar /*
9236df6f47dSBram Moolenaar  * ":py3file"
924bd5e15fdSBram Moolenaar  */
925bd5e15fdSBram Moolenaar     void
926bd5e15fdSBram Moolenaar ex_py3file(exarg_T *eap)
927bd5e15fdSBram Moolenaar {
928bd5e15fdSBram Moolenaar     static char buffer[BUFFER_SIZE];
929bd5e15fdSBram Moolenaar     const char *file;
930bd5e15fdSBram Moolenaar     char *p;
931bd5e15fdSBram Moolenaar     int i;
932bd5e15fdSBram Moolenaar 
933bd5e15fdSBram Moolenaar     /* Have to do it like this. PyRun_SimpleFile requires you to pass a
934bd5e15fdSBram Moolenaar      * stdio file pointer, but Vim and the Python DLL are compiled with
935bd5e15fdSBram Moolenaar      * different options under Windows, meaning that stdio pointers aren't
936bd5e15fdSBram Moolenaar      * compatible between the two. Yuk.
937bd5e15fdSBram Moolenaar      *
93819e60943SBram Moolenaar      * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
93919e60943SBram Moolenaar      *
94019e60943SBram Moolenaar      * Using bytes so that Python can detect the source encoding as it normally
94119e60943SBram Moolenaar      * does. The doc does not say "compile" accept bytes, though.
942bd5e15fdSBram Moolenaar      *
943bd5e15fdSBram Moolenaar      * We need to escape any backslashes or single quotes in the file name, so that
944bd5e15fdSBram Moolenaar      * Python won't mangle the file name.
945bd5e15fdSBram Moolenaar      */
946bd5e15fdSBram Moolenaar 
947bd5e15fdSBram Moolenaar     strcpy(buffer, "exec(compile(open('");
948bd5e15fdSBram Moolenaar     p = buffer + 19; /* size of "exec(compile(open('" */
949bd5e15fdSBram Moolenaar 
950bd5e15fdSBram Moolenaar     for (i=0; i<2; ++i)
951bd5e15fdSBram Moolenaar     {
952bd5e15fdSBram Moolenaar 	file = (char *)eap->arg;
953bd5e15fdSBram Moolenaar 	while (*file && p < buffer + (BUFFER_SIZE - 3))
954bd5e15fdSBram Moolenaar 	{
955bd5e15fdSBram Moolenaar 	    if (*file == '\\' || *file == '\'')
956bd5e15fdSBram Moolenaar 		*p++ = '\\';
957bd5e15fdSBram Moolenaar 	    *p++ = *file++;
958bd5e15fdSBram Moolenaar 	}
959bd5e15fdSBram Moolenaar 	/* If we didn't finish the file name, we hit a buffer overflow */
960bd5e15fdSBram Moolenaar 	if (*file != '\0')
961bd5e15fdSBram Moolenaar 	    return;
962bd5e15fdSBram Moolenaar 	if (i==0)
963bd5e15fdSBram Moolenaar 	{
96419e60943SBram Moolenaar 	    strcpy(p,"','rb').read(),'");
96519e60943SBram Moolenaar 	    p += 16;
966bd5e15fdSBram Moolenaar 	}
967bd5e15fdSBram Moolenaar 	else
968bd5e15fdSBram Moolenaar 	{
969bd5e15fdSBram Moolenaar 	    strcpy(p,"','exec'))");
970bd5e15fdSBram Moolenaar 	    p += 10;
971bd5e15fdSBram Moolenaar 	}
972bd5e15fdSBram Moolenaar     }
973bd5e15fdSBram Moolenaar 
974bd5e15fdSBram Moolenaar 
975bd5e15fdSBram Moolenaar     /* Execute the file */
976db913953SBram Moolenaar     DoPy3Command(eap, buffer, NULL);
977bd5e15fdSBram Moolenaar }
978bd5e15fdSBram Moolenaar 
979bd5e15fdSBram Moolenaar /******************************************************
980bd5e15fdSBram Moolenaar  * 2. Python output stream: writes output via [e]msg().
981bd5e15fdSBram Moolenaar  */
982bd5e15fdSBram Moolenaar 
983bd5e15fdSBram Moolenaar /* Implementation functions
984bd5e15fdSBram Moolenaar  */
985bd5e15fdSBram Moolenaar 
986170bf1aeSBram Moolenaar     static PyObject *
987170bf1aeSBram Moolenaar OutputGetattro(PyObject *self, PyObject *nameobj)
988bd5e15fdSBram Moolenaar {
98977045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
990bd5e15fdSBram Moolenaar 
991bd5e15fdSBram Moolenaar     if (strcmp(name, "softspace") == 0)
992bd5e15fdSBram Moolenaar 	return PyLong_FromLong(((OutputObject *)(self))->softspace);
993bd5e15fdSBram Moolenaar 
994bd5e15fdSBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
995bd5e15fdSBram Moolenaar }
996bd5e15fdSBram Moolenaar 
997170bf1aeSBram Moolenaar     static int
998170bf1aeSBram Moolenaar OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
999bd5e15fdSBram Moolenaar {
100077045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1001bd5e15fdSBram Moolenaar 
100277045658SBram Moolenaar     return OutputSetattr(self, name, val);
1003bd5e15fdSBram Moolenaar }
1004bd5e15fdSBram Moolenaar 
1005bd5e15fdSBram Moolenaar /***************/
1006bd5e15fdSBram Moolenaar 
1007170bf1aeSBram Moolenaar     static int
1008170bf1aeSBram Moolenaar PythonIO_Init(void)
1009bd5e15fdSBram Moolenaar {
1010bd5e15fdSBram Moolenaar     PyType_Ready(&OutputType);
1011170bf1aeSBram Moolenaar     return PythonIO_Init_io();
1012bd5e15fdSBram Moolenaar }
1013bd5e15fdSBram Moolenaar 
1014bd5e15fdSBram Moolenaar /******************************************************
1015bd5e15fdSBram Moolenaar  * 3. Implementation of the Vim module for Python
1016bd5e15fdSBram Moolenaar  */
1017bd5e15fdSBram Moolenaar 
1018bd5e15fdSBram Moolenaar /* Window type - Implementation functions
1019bd5e15fdSBram Moolenaar  * --------------------------------------
1020bd5e15fdSBram Moolenaar  */
1021bd5e15fdSBram Moolenaar 
1022bd5e15fdSBram Moolenaar #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1023bd5e15fdSBram Moolenaar 
1024bd5e15fdSBram Moolenaar /* Buffer type - Implementation functions
1025bd5e15fdSBram Moolenaar  * --------------------------------------
1026bd5e15fdSBram Moolenaar  */
1027bd5e15fdSBram Moolenaar 
1028bd5e15fdSBram Moolenaar #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1029bd5e15fdSBram Moolenaar 
1030bd5e15fdSBram Moolenaar static Py_ssize_t BufferLength(PyObject *);
1031bd5e15fdSBram Moolenaar static PyObject *BufferItem(PyObject *, Py_ssize_t);
1032bd5e15fdSBram Moolenaar static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
103319e60943SBram Moolenaar static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
1034bd5e15fdSBram Moolenaar 
1035bd5e15fdSBram Moolenaar 
1036bd5e15fdSBram Moolenaar /* Line range type - Implementation functions
1037bd5e15fdSBram Moolenaar  * --------------------------------------
1038bd5e15fdSBram Moolenaar  */
1039bd5e15fdSBram Moolenaar 
1040bd5e15fdSBram Moolenaar #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1041bd5e15fdSBram Moolenaar 
1042bd5e15fdSBram Moolenaar static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
1043bd5e15fdSBram Moolenaar static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1044ba4897e6SBram Moolenaar static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
1045bd5e15fdSBram Moolenaar 
1046bd5e15fdSBram Moolenaar /* Current objects type - Implementation functions
1047bd5e15fdSBram Moolenaar  * -----------------------------------------------
1048bd5e15fdSBram Moolenaar  */
1049bd5e15fdSBram Moolenaar 
1050bd5e15fdSBram Moolenaar static PySequenceMethods BufferAsSeq = {
1051bd5e15fdSBram Moolenaar     (lenfunc)		BufferLength,	    /* sq_length,    len(x)   */
1052bd5e15fdSBram Moolenaar     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1053bd5e15fdSBram Moolenaar     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1054bd5e15fdSBram Moolenaar     (ssizeargfunc)	BufferItem,	    /* sq_item,      x[i]     */
1055bd5e15fdSBram Moolenaar     0,					    /* was_sq_slice,	 x[i:j]   */
105619e60943SBram Moolenaar     0,					    /* sq_ass_item,  x[i]=v   */
1057bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1058bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1059bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1060bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1061bd5e15fdSBram Moolenaar };
1062bd5e15fdSBram Moolenaar 
10634d1da49cSBram Moolenaar static PyMappingMethods BufferAsMapping = {
1064bd5e15fdSBram Moolenaar     /* mp_length	*/ (lenfunc)BufferLength,
1065bd5e15fdSBram Moolenaar     /* mp_subscript     */ (binaryfunc)BufferSubscript,
106619e60943SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
1067bd5e15fdSBram Moolenaar };
1068bd5e15fdSBram Moolenaar 
1069bd5e15fdSBram Moolenaar 
1070bd5e15fdSBram Moolenaar /* Buffer object - Definitions
1071bd5e15fdSBram Moolenaar  */
1072bd5e15fdSBram Moolenaar 
1073170bf1aeSBram Moolenaar     static PyObject *
1074170bf1aeSBram Moolenaar BufferNew(buf_T *buf)
1075bd5e15fdSBram Moolenaar {
1076bd5e15fdSBram Moolenaar     /* We need to handle deletion of buffers underneath us.
1077bd5e15fdSBram Moolenaar      * If we add a "b_python3_ref" field to the buf_T structure,
1078bd5e15fdSBram Moolenaar      * then we can get at it in buf_freeall() in vim. We then
1079bd5e15fdSBram Moolenaar      * need to create only ONE Python object per buffer - if
1080bd5e15fdSBram Moolenaar      * we try to create a second, just INCREF the existing one
1081bd5e15fdSBram Moolenaar      * and return it. The (single) Python object referring to
1082bd5e15fdSBram Moolenaar      * the buffer is stored in "b_python3_ref".
1083bd5e15fdSBram Moolenaar      * Question: what to do on a buf_freeall(). We'll probably
1084bd5e15fdSBram Moolenaar      * have to either delete the Python object (DECREF it to
1085bd5e15fdSBram Moolenaar      * zero - a bad idea, as it leaves dangling refs!) or
1086bd5e15fdSBram Moolenaar      * set the buf_T * value to an invalid value (-1?), which
1087bd5e15fdSBram Moolenaar      * means we need checks in all access functions... Bah.
1088bd5e15fdSBram Moolenaar      */
1089bd5e15fdSBram Moolenaar 
1090bd5e15fdSBram Moolenaar     BufferObject *self;
1091bd5e15fdSBram Moolenaar 
1092bd5e15fdSBram Moolenaar     if (buf->b_python3_ref != NULL)
1093bd5e15fdSBram Moolenaar     {
1094bd5e15fdSBram Moolenaar 	self = buf->b_python3_ref;
1095bd5e15fdSBram Moolenaar 	Py_INCREF(self);
1096bd5e15fdSBram Moolenaar     }
1097bd5e15fdSBram Moolenaar     else
1098bd5e15fdSBram Moolenaar     {
1099bd5e15fdSBram Moolenaar 	self = PyObject_NEW(BufferObject, &BufferType);
1100bd5e15fdSBram Moolenaar 	buf->b_python3_ref = self;
1101bd5e15fdSBram Moolenaar 	if (self == NULL)
1102bd5e15fdSBram Moolenaar 	    return NULL;
1103bd5e15fdSBram Moolenaar 	self->buf = buf;
1104bd5e15fdSBram Moolenaar     }
1105bd5e15fdSBram Moolenaar 
1106bd5e15fdSBram Moolenaar     return (PyObject *)(self);
1107bd5e15fdSBram Moolenaar }
1108bd5e15fdSBram Moolenaar 
1109170bf1aeSBram Moolenaar     static PyObject *
1110170bf1aeSBram Moolenaar BufferGetattro(PyObject *self, PyObject*nameobj)
1111bd5e15fdSBram Moolenaar {
11124d1da49cSBram Moolenaar     PyObject *r;
1113bd5e15fdSBram Moolenaar 
111477045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1115bd5e15fdSBram Moolenaar 
11164d1da49cSBram Moolenaar     if (CheckBuffer((BufferObject *)(self)))
1117bd5e15fdSBram Moolenaar 	return NULL;
1118bd5e15fdSBram Moolenaar 
11194d1da49cSBram Moolenaar     r = BufferAttr((BufferObject *)(self), name);
11204d1da49cSBram Moolenaar     if (r || PyErr_Occurred())
11214d1da49cSBram Moolenaar 	return r;
1122bd5e15fdSBram Moolenaar     else
1123bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1124bd5e15fdSBram Moolenaar }
1125bd5e15fdSBram Moolenaar 
1126170bf1aeSBram Moolenaar     static PyObject *
11277f85d297SBram Moolenaar BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
11287f85d297SBram Moolenaar {
11297f85d297SBram Moolenaar     return Py_BuildValue("[sssss]", "name", "number",
11307f85d297SBram Moolenaar 						   "append", "mark", "range");
11317f85d297SBram Moolenaar }
11327f85d297SBram Moolenaar 
1133bd5e15fdSBram Moolenaar /******************/
1134bd5e15fdSBram Moolenaar 
1135170bf1aeSBram Moolenaar     static Py_ssize_t
1136170bf1aeSBram Moolenaar BufferLength(PyObject *self)
1137bd5e15fdSBram Moolenaar {
1138bd5e15fdSBram Moolenaar     if (CheckBuffer((BufferObject *)(self)))
1139bd5e15fdSBram Moolenaar 	return -1;
1140bd5e15fdSBram Moolenaar 
1141bd5e15fdSBram Moolenaar     return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1142bd5e15fdSBram Moolenaar }
1143bd5e15fdSBram Moolenaar 
1144170bf1aeSBram Moolenaar     static PyObject *
1145170bf1aeSBram Moolenaar BufferItem(PyObject *self, Py_ssize_t n)
1146bd5e15fdSBram Moolenaar {
1147bd5e15fdSBram Moolenaar     return RBItem((BufferObject *)(self), n, 1,
1148bd5e15fdSBram Moolenaar 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1149bd5e15fdSBram Moolenaar }
1150bd5e15fdSBram Moolenaar 
1151170bf1aeSBram Moolenaar     static PyObject *
1152170bf1aeSBram Moolenaar BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1153170bf1aeSBram Moolenaar {
1154170bf1aeSBram Moolenaar     return RBSlice((BufferObject *)(self), lo, hi, 1,
1155170bf1aeSBram Moolenaar 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1156170bf1aeSBram Moolenaar }
1157170bf1aeSBram Moolenaar 
1158170bf1aeSBram Moolenaar     static PyObject *
1159170bf1aeSBram Moolenaar BufferSubscript(PyObject *self, PyObject* idx)
1160bd5e15fdSBram Moolenaar {
1161db913953SBram Moolenaar     if (PyLong_Check(idx))
1162db913953SBram Moolenaar     {
1163bd5e15fdSBram Moolenaar 	long _idx = PyLong_AsLong(idx);
1164bd5e15fdSBram Moolenaar 	return BufferItem(self,_idx);
1165db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1166db913953SBram Moolenaar     {
1167bd5e15fdSBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1168bd5e15fdSBram Moolenaar 
11699e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1170bd5e15fdSBram Moolenaar 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1171bd5e15fdSBram Moolenaar 	      &start, &stop,
1172db913953SBram Moolenaar 	      &step, &slicelen) < 0)
1173db913953SBram Moolenaar 	{
1174bd5e15fdSBram Moolenaar 	    return NULL;
1175bd5e15fdSBram Moolenaar 	}
117619e60943SBram Moolenaar 	return BufferSlice(self, start, stop);
1177db913953SBram Moolenaar     }
1178db913953SBram Moolenaar     else
1179db913953SBram Moolenaar     {
1180bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1181bd5e15fdSBram Moolenaar 	return NULL;
1182bd5e15fdSBram Moolenaar     }
1183bd5e15fdSBram Moolenaar }
1184bd5e15fdSBram Moolenaar 
118519e60943SBram Moolenaar     static Py_ssize_t
118619e60943SBram Moolenaar BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
118719e60943SBram Moolenaar {
1188db913953SBram Moolenaar     if (PyLong_Check(idx))
1189db913953SBram Moolenaar     {
119019e60943SBram Moolenaar 	long n = PyLong_AsLong(idx);
119119e60943SBram Moolenaar 	return RBAsItem((BufferObject *)(self), n, val, 1,
119219e60943SBram Moolenaar 		    (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
119319e60943SBram Moolenaar 		    NULL);
1194db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1195db913953SBram Moolenaar     {
119619e60943SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
119719e60943SBram Moolenaar 
11989e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
119919e60943SBram Moolenaar 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
120019e60943SBram Moolenaar 	      &start, &stop,
1201db913953SBram Moolenaar 	      &step, &slicelen) < 0)
1202db913953SBram Moolenaar 	{
120319e60943SBram Moolenaar 	    return -1;
120419e60943SBram Moolenaar 	}
120519e60943SBram Moolenaar 	return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
120619e60943SBram Moolenaar 			  (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
120719e60943SBram Moolenaar 			  NULL);
1208db913953SBram Moolenaar     }
1209db913953SBram Moolenaar     else
1210db913953SBram Moolenaar     {
121119e60943SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
121219e60943SBram Moolenaar 	return -1;
121319e60943SBram Moolenaar     }
121419e60943SBram Moolenaar }
121519e60943SBram Moolenaar 
1216bd5e15fdSBram Moolenaar static PySequenceMethods RangeAsSeq = {
1217bd5e15fdSBram Moolenaar     (lenfunc)		RangeLength,	 /* sq_length,	  len(x)   */
121855d5c034SBram Moolenaar     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
121955d5c034SBram Moolenaar     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1220bd5e15fdSBram Moolenaar     (ssizeargfunc)	RangeItem,	 /* sq_item,	  x[i]	   */
1221bd5e15fdSBram Moolenaar     0,					 /* was_sq_slice,     x[i:j]   */
1222bd5e15fdSBram Moolenaar     (ssizeobjargproc)	RangeAsItem,	 /* sq_as_item,  x[i]=v   */
1223bd5e15fdSBram Moolenaar     0,					 /* sq_ass_slice, x[i:j]=v */
1224bd5e15fdSBram Moolenaar     0,					 /* sq_contains */
1225bd5e15fdSBram Moolenaar     0,					 /* sq_inplace_concat */
1226bd5e15fdSBram Moolenaar     0,					 /* sq_inplace_repeat */
1227bd5e15fdSBram Moolenaar };
1228bd5e15fdSBram Moolenaar 
12294d1da49cSBram Moolenaar static PyMappingMethods RangeAsMapping = {
1230bd5e15fdSBram Moolenaar     /* mp_length	*/ (lenfunc)RangeLength,
1231bd5e15fdSBram Moolenaar     /* mp_subscript     */ (binaryfunc)RangeSubscript,
1232ba4897e6SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
1233bd5e15fdSBram Moolenaar };
1234bd5e15fdSBram Moolenaar 
1235bd5e15fdSBram Moolenaar /* Line range object - Implementation
1236bd5e15fdSBram Moolenaar  */
1237bd5e15fdSBram Moolenaar 
1238170bf1aeSBram Moolenaar     static PyObject *
1239170bf1aeSBram Moolenaar RangeGetattro(PyObject *self, PyObject *nameobj)
1240bd5e15fdSBram Moolenaar {
124177045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1242bd5e15fdSBram Moolenaar 
1243bd5e15fdSBram Moolenaar     if (strcmp(name, "start") == 0)
1244bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
1245bd5e15fdSBram Moolenaar     else if (strcmp(name, "end") == 0)
1246bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
1247bd5e15fdSBram Moolenaar     else
1248bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1249bd5e15fdSBram Moolenaar }
1250bd5e15fdSBram Moolenaar 
1251bd5e15fdSBram Moolenaar /****************/
1252bd5e15fdSBram Moolenaar 
1253170bf1aeSBram Moolenaar     static Py_ssize_t
1254170bf1aeSBram Moolenaar RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
1255bd5e15fdSBram Moolenaar {
1256bd5e15fdSBram Moolenaar     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1257bd5e15fdSBram Moolenaar 		    ((RangeObject *)(self))->start,
1258bd5e15fdSBram Moolenaar 		    ((RangeObject *)(self))->end,
1259bd5e15fdSBram Moolenaar 		    &((RangeObject *)(self))->end);
1260bd5e15fdSBram Moolenaar }
1261bd5e15fdSBram Moolenaar 
1262ba4897e6SBram Moolenaar     static Py_ssize_t
1263ba4897e6SBram Moolenaar RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1264ba4897e6SBram Moolenaar {
1265ba4897e6SBram Moolenaar     return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1266ba4897e6SBram Moolenaar 		    ((RangeObject *)(self))->start,
1267ba4897e6SBram Moolenaar 		    ((RangeObject *)(self))->end,
1268ba4897e6SBram Moolenaar 		    &((RangeObject *)(self))->end);
1269ba4897e6SBram Moolenaar }
1270ba4897e6SBram Moolenaar 
1271170bf1aeSBram Moolenaar     static PyObject *
1272170bf1aeSBram Moolenaar RangeSubscript(PyObject *self, PyObject* idx)
1273bd5e15fdSBram Moolenaar {
1274db913953SBram Moolenaar     if (PyLong_Check(idx))
1275db913953SBram Moolenaar     {
1276bd5e15fdSBram Moolenaar 	long _idx = PyLong_AsLong(idx);
1277bd5e15fdSBram Moolenaar 	return RangeItem(self,_idx);
1278db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1279db913953SBram Moolenaar     {
1280bd5e15fdSBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1281bd5e15fdSBram Moolenaar 
12829e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1283bd5e15fdSBram Moolenaar 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1284bd5e15fdSBram Moolenaar 		&start, &stop,
1285db913953SBram Moolenaar 		&step, &slicelen) < 0)
1286db913953SBram Moolenaar 	{
1287bd5e15fdSBram Moolenaar 	    return NULL;
1288bd5e15fdSBram Moolenaar 	}
1289ba4897e6SBram Moolenaar 	return RangeSlice(self, start, stop);
1290db913953SBram Moolenaar     }
1291db913953SBram Moolenaar     else
1292db913953SBram Moolenaar     {
1293bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1294bd5e15fdSBram Moolenaar 	return NULL;
1295bd5e15fdSBram Moolenaar     }
1296bd5e15fdSBram Moolenaar }
1297bd5e15fdSBram Moolenaar 
1298ba4897e6SBram Moolenaar     static Py_ssize_t
1299ba4897e6SBram Moolenaar RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1300ba4897e6SBram Moolenaar {
1301db913953SBram Moolenaar     if (PyLong_Check(idx))
1302db913953SBram Moolenaar     {
1303ba4897e6SBram Moolenaar 	long n = PyLong_AsLong(idx);
1304ba4897e6SBram Moolenaar 	return RangeAsItem(self, n, val);
1305db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1306db913953SBram Moolenaar     {
1307ba4897e6SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1308ba4897e6SBram Moolenaar 
13099e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1310ba4897e6SBram Moolenaar 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1311ba4897e6SBram Moolenaar 		&start, &stop,
1312db913953SBram Moolenaar 		&step, &slicelen) < 0)
1313db913953SBram Moolenaar 	{
1314ba4897e6SBram Moolenaar 	    return -1;
1315ba4897e6SBram Moolenaar 	}
1316ba4897e6SBram Moolenaar 	return RangeAsSlice(self, start, stop, val);
1317db913953SBram Moolenaar     }
1318db913953SBram Moolenaar     else
1319db913953SBram Moolenaar     {
1320ba4897e6SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1321ba4897e6SBram Moolenaar 	return -1;
1322ba4897e6SBram Moolenaar     }
1323ba4897e6SBram Moolenaar }
1324ba4897e6SBram Moolenaar 
1325bd5e15fdSBram Moolenaar /* Buffer list object - Definitions
1326bd5e15fdSBram Moolenaar  */
1327bd5e15fdSBram Moolenaar 
1328bd5e15fdSBram Moolenaar static PySequenceMethods BufListAsSeq = {
1329bd5e15fdSBram Moolenaar     (lenfunc)		BufListLength,	    /* sq_length,    len(x)   */
1330bd5e15fdSBram Moolenaar     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1331bd5e15fdSBram Moolenaar     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1332bd5e15fdSBram Moolenaar     (ssizeargfunc)	BufListItem,	    /* sq_item,      x[i]     */
1333bd5e15fdSBram Moolenaar     0,					    /* was_sq_slice,	 x[i:j]   */
1334bd5e15fdSBram Moolenaar     (ssizeobjargproc)	0,		    /* sq_as_item,  x[i]=v   */
1335bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1336bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1337bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1338bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1339bd5e15fdSBram Moolenaar };
1340bd5e15fdSBram Moolenaar 
1341bd5e15fdSBram Moolenaar /* Window object - Implementation
1342bd5e15fdSBram Moolenaar  */
1343bd5e15fdSBram Moolenaar 
1344170bf1aeSBram Moolenaar     static PyObject *
1345170bf1aeSBram Moolenaar WindowNew(win_T *win)
1346bd5e15fdSBram Moolenaar {
1347bd5e15fdSBram Moolenaar     /* We need to handle deletion of windows underneath us.
1348bd5e15fdSBram Moolenaar      * If we add a "w_python3_ref" field to the win_T structure,
1349bd5e15fdSBram Moolenaar      * then we can get at it in win_free() in vim. We then
1350bd5e15fdSBram Moolenaar      * need to create only ONE Python object per window - if
1351bd5e15fdSBram Moolenaar      * we try to create a second, just INCREF the existing one
1352bd5e15fdSBram Moolenaar      * and return it. The (single) Python object referring to
1353bd5e15fdSBram Moolenaar      * the window is stored in "w_python3_ref".
1354bd5e15fdSBram Moolenaar      * On a win_free() we set the Python object's win_T* field
1355bd5e15fdSBram Moolenaar      * to an invalid value. We trap all uses of a window
1356bd5e15fdSBram Moolenaar      * object, and reject them if the win_T* field is invalid.
1357bd5e15fdSBram Moolenaar      */
1358bd5e15fdSBram Moolenaar 
1359bd5e15fdSBram Moolenaar     WindowObject *self;
1360bd5e15fdSBram Moolenaar 
1361bd5e15fdSBram Moolenaar     if (win->w_python3_ref)
1362bd5e15fdSBram Moolenaar     {
1363bd5e15fdSBram Moolenaar 	self = win->w_python3_ref;
1364bd5e15fdSBram Moolenaar 	Py_INCREF(self);
1365bd5e15fdSBram Moolenaar     }
1366bd5e15fdSBram Moolenaar     else
1367bd5e15fdSBram Moolenaar     {
1368bd5e15fdSBram Moolenaar 	self = PyObject_NEW(WindowObject, &WindowType);
1369bd5e15fdSBram Moolenaar 	if (self == NULL)
1370bd5e15fdSBram Moolenaar 	    return NULL;
1371bd5e15fdSBram Moolenaar 	self->win = win;
1372bd5e15fdSBram Moolenaar 	win->w_python3_ref = self;
1373bd5e15fdSBram Moolenaar     }
1374bd5e15fdSBram Moolenaar 
1375bd5e15fdSBram Moolenaar     return (PyObject *)(self);
1376bd5e15fdSBram Moolenaar }
1377bd5e15fdSBram Moolenaar 
1378170bf1aeSBram Moolenaar     static PyObject *
1379170bf1aeSBram Moolenaar WindowGetattro(PyObject *self, PyObject *nameobj)
1380bd5e15fdSBram Moolenaar {
13814d1da49cSBram Moolenaar     PyObject *r;
1382bd5e15fdSBram Moolenaar 
138377045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1384bd5e15fdSBram Moolenaar 
13854d1da49cSBram Moolenaar     if (CheckWindow((WindowObject *)(self)))
1386bd5e15fdSBram Moolenaar 	return NULL;
1387bd5e15fdSBram Moolenaar 
13884d1da49cSBram Moolenaar     r = WindowAttr((WindowObject *)(self), name);
13894d1da49cSBram Moolenaar     if (r || PyErr_Occurred())
13904d1da49cSBram Moolenaar 	return r;
1391bd5e15fdSBram Moolenaar     else
1392bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1393bd5e15fdSBram Moolenaar }
1394bd5e15fdSBram Moolenaar 
1395170bf1aeSBram Moolenaar     static int
1396170bf1aeSBram Moolenaar WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1397bd5e15fdSBram Moolenaar {
139877045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1399bd5e15fdSBram Moolenaar 
1400ca8a4dfeSBram Moolenaar     return WindowSetattr(self, name, val);
1401bd5e15fdSBram Moolenaar }
1402bd5e15fdSBram Moolenaar 
1403bd5e15fdSBram Moolenaar /* Window list object - Definitions
1404bd5e15fdSBram Moolenaar  */
1405bd5e15fdSBram Moolenaar 
1406bd5e15fdSBram Moolenaar static PySequenceMethods WinListAsSeq = {
1407bd5e15fdSBram Moolenaar     (lenfunc)	     WinListLength,	    /* sq_length,    len(x)   */
1408bd5e15fdSBram Moolenaar     (binaryfunc)     0,			    /* sq_concat,    x+y      */
1409bd5e15fdSBram Moolenaar     (ssizeargfunc)   0,			    /* sq_repeat,    x*n      */
1410bd5e15fdSBram Moolenaar     (ssizeargfunc)   WinListItem,	    /* sq_item,      x[i]     */
1411bd5e15fdSBram Moolenaar     0,					    /* sq_slice,     x[i:j]   */
1412bd5e15fdSBram Moolenaar     (ssizeobjargproc)0,			    /* sq_as_item,  x[i]=v   */
1413bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1414bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1415bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1416bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1417bd5e15fdSBram Moolenaar };
1418bd5e15fdSBram Moolenaar 
1419bd5e15fdSBram Moolenaar /* Current items object - Implementation
1420bd5e15fdSBram Moolenaar  */
1421170bf1aeSBram Moolenaar     static PyObject *
14224d1da49cSBram Moolenaar CurrentGetattro(PyObject *self, PyObject *nameobj)
1423bd5e15fdSBram Moolenaar {
142477045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
14254d1da49cSBram Moolenaar     return CurrentGetattr(self, name);
1426bd5e15fdSBram Moolenaar }
1427bd5e15fdSBram Moolenaar 
1428170bf1aeSBram Moolenaar     static int
14294d1da49cSBram Moolenaar CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
1430bd5e15fdSBram Moolenaar {
14314d1da49cSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
14324d1da49cSBram Moolenaar     return CurrentSetattr(self, name, value);
1433bd5e15fdSBram Moolenaar }
1434bd5e15fdSBram Moolenaar 
1435db913953SBram Moolenaar /* Dictionary object - Definitions
1436db913953SBram Moolenaar  */
1437db913953SBram Moolenaar 
1438db913953SBram Moolenaar static PyInt DictionaryLength(PyObject *);
1439db913953SBram Moolenaar 
144066b7985eSBram Moolenaar     static PyObject *
144166b7985eSBram Moolenaar DictionaryGetattro(PyObject *self, PyObject *nameobj)
144266b7985eSBram Moolenaar {
144366b7985eSBram Moolenaar     DictionaryObject	*this = ((DictionaryObject *) (self));
144466b7985eSBram Moolenaar 
144566b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
144666b7985eSBram Moolenaar 
144766b7985eSBram Moolenaar     if (strcmp(name, "locked") == 0)
144866b7985eSBram Moolenaar 	return PyLong_FromLong(this->dict->dv_lock);
144966b7985eSBram Moolenaar     else if (strcmp(name, "scope") == 0)
145066b7985eSBram Moolenaar 	return PyLong_FromLong(this->dict->dv_scope);
145166b7985eSBram Moolenaar 
145266b7985eSBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
145366b7985eSBram Moolenaar }
145466b7985eSBram Moolenaar 
145566b7985eSBram Moolenaar     static int
145666b7985eSBram Moolenaar DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
145766b7985eSBram Moolenaar {
145866b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
14594d1da49cSBram Moolenaar     return DictionarySetattr(self, name, val);
1460db913953SBram Moolenaar }
1461db913953SBram Moolenaar 
1462db913953SBram Moolenaar /* List object - Definitions
1463db913953SBram Moolenaar  */
1464db913953SBram Moolenaar 
1465db913953SBram Moolenaar static PyInt ListLength(PyObject *);
1466db913953SBram Moolenaar static PyObject *ListItem(PyObject *, Py_ssize_t);
1467db913953SBram Moolenaar 
1468db913953SBram Moolenaar static PySequenceMethods ListAsSeq = {
1469db913953SBram Moolenaar     (lenfunc)		ListLength,	 /* sq_length,	  len(x)   */
1470db913953SBram Moolenaar     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
1471db913953SBram Moolenaar     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1472db913953SBram Moolenaar     (ssizeargfunc)	ListItem,	 /* sq_item,	  x[i]	   */
1473db913953SBram Moolenaar     (void *)		0,		 /* was_sq_slice,     x[i:j]   */
1474db913953SBram Moolenaar     (ssizeobjargproc)	ListAssItem,	 /* sq_as_item,  x[i]=v   */
1475db913953SBram Moolenaar     (void *)		0,		 /* was_sq_ass_slice, x[i:j]=v */
1476db913953SBram Moolenaar     0,					 /* sq_contains */
1477db913953SBram Moolenaar     (binaryfunc)	ListConcatInPlace,/* sq_inplace_concat */
1478db913953SBram Moolenaar     0,					 /* sq_inplace_repeat */
1479db913953SBram Moolenaar };
1480db913953SBram Moolenaar 
1481db913953SBram Moolenaar static PyObject *ListSubscript(PyObject *, PyObject *);
1482db913953SBram Moolenaar static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1483db913953SBram Moolenaar 
1484db913953SBram Moolenaar static PyMappingMethods ListAsMapping = {
1485db913953SBram Moolenaar     /* mp_length	*/ (lenfunc) ListLength,
1486db913953SBram Moolenaar     /* mp_subscript     */ (binaryfunc) ListSubscript,
1487db913953SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1488db913953SBram Moolenaar };
1489db913953SBram Moolenaar 
1490db913953SBram Moolenaar     static PyObject *
1491db913953SBram Moolenaar ListSubscript(PyObject *self, PyObject* idxObject)
1492db913953SBram Moolenaar {
1493db913953SBram Moolenaar     if (PyLong_Check(idxObject))
1494db913953SBram Moolenaar     {
1495db913953SBram Moolenaar 	long idx = PyLong_AsLong(idxObject);
1496db913953SBram Moolenaar 	return ListItem(self, idx);
1497db913953SBram Moolenaar     }
1498db913953SBram Moolenaar     else if (PySlice_Check(idxObject))
1499db913953SBram Moolenaar     {
1500db913953SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1501db913953SBram Moolenaar 
1502db913953SBram Moolenaar 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1503db913953SBram Moolenaar 				 &step, &slicelen) < 0)
1504db913953SBram Moolenaar 	    return NULL;
1505db913953SBram Moolenaar 	return ListSlice(self, start, stop);
1506db913953SBram Moolenaar     }
1507db913953SBram Moolenaar     else
1508db913953SBram Moolenaar     {
1509db913953SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1510db913953SBram Moolenaar 	return NULL;
1511db913953SBram Moolenaar     }
1512db913953SBram Moolenaar }
1513db913953SBram Moolenaar 
1514db913953SBram Moolenaar     static Py_ssize_t
1515db913953SBram Moolenaar ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1516db913953SBram Moolenaar {
1517db913953SBram Moolenaar     if (PyLong_Check(idxObject))
1518db913953SBram Moolenaar     {
1519db913953SBram Moolenaar 	long idx = PyLong_AsLong(idxObject);
1520db913953SBram Moolenaar 	return ListAssItem(self, idx, obj);
1521db913953SBram Moolenaar     }
1522db913953SBram Moolenaar     else if (PySlice_Check(idxObject))
1523db913953SBram Moolenaar     {
1524db913953SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1525db913953SBram Moolenaar 
1526db913953SBram Moolenaar 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1527db913953SBram Moolenaar 				 &step, &slicelen) < 0)
1528db913953SBram Moolenaar 	    return -1;
1529db913953SBram Moolenaar 	return ListAssSlice(self, start, stop, obj);
1530db913953SBram Moolenaar     }
1531db913953SBram Moolenaar     else
1532db913953SBram Moolenaar     {
1533db913953SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1534db913953SBram Moolenaar 	return -1;
1535db913953SBram Moolenaar     }
1536db913953SBram Moolenaar }
1537db913953SBram Moolenaar 
153866b7985eSBram Moolenaar     static PyObject *
153966b7985eSBram Moolenaar ListGetattro(PyObject *self, PyObject *nameobj)
154066b7985eSBram Moolenaar {
154166b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
154266b7985eSBram Moolenaar 
154366b7985eSBram Moolenaar     if (strcmp(name, "locked") == 0)
154466b7985eSBram Moolenaar 	return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
154566b7985eSBram Moolenaar 
154666b7985eSBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
154766b7985eSBram Moolenaar }
154866b7985eSBram Moolenaar 
154966b7985eSBram Moolenaar     static int
155066b7985eSBram Moolenaar ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
155166b7985eSBram Moolenaar {
155266b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
15534d1da49cSBram Moolenaar     return ListSetattr(self, name, val);
1554db913953SBram Moolenaar }
1555db913953SBram Moolenaar 
1556db913953SBram Moolenaar /* Function object - Definitions
1557db913953SBram Moolenaar  */
1558db913953SBram Moolenaar 
1559db913953SBram Moolenaar     static PyObject *
1560db913953SBram Moolenaar FunctionGetattro(PyObject *self, PyObject *nameobj)
1561db913953SBram Moolenaar {
1562db913953SBram Moolenaar     FunctionObject	*this = (FunctionObject *)(self);
156377045658SBram Moolenaar 
156477045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1565db913953SBram Moolenaar 
1566db913953SBram Moolenaar     if (strcmp(name, "name") == 0)
1567db913953SBram Moolenaar 	return PyUnicode_FromString((char *)(this->name));
1568db913953SBram Moolenaar 
1569db913953SBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
1570db913953SBram Moolenaar }
1571db913953SBram Moolenaar 
1572bd5e15fdSBram Moolenaar /* External interface
1573bd5e15fdSBram Moolenaar  */
1574bd5e15fdSBram Moolenaar 
1575bd5e15fdSBram Moolenaar     void
1576bd5e15fdSBram Moolenaar python3_buffer_free(buf_T *buf)
1577bd5e15fdSBram Moolenaar {
1578bd5e15fdSBram Moolenaar     if (buf->b_python3_ref != NULL)
1579bd5e15fdSBram Moolenaar     {
1580bd5e15fdSBram Moolenaar 	BufferObject *bp = buf->b_python3_ref;
1581bd5e15fdSBram Moolenaar 	bp->buf = INVALID_BUFFER_VALUE;
1582bd5e15fdSBram Moolenaar 	buf->b_python3_ref = NULL;
1583bd5e15fdSBram Moolenaar     }
1584bd5e15fdSBram Moolenaar }
1585bd5e15fdSBram Moolenaar 
1586bd5e15fdSBram Moolenaar #if defined(FEAT_WINDOWS) || defined(PROTO)
1587bd5e15fdSBram Moolenaar     void
1588bd5e15fdSBram Moolenaar python3_window_free(win_T *win)
1589bd5e15fdSBram Moolenaar {
1590bd5e15fdSBram Moolenaar     if (win->w_python3_ref != NULL)
1591bd5e15fdSBram Moolenaar     {
1592bd5e15fdSBram Moolenaar 	WindowObject *wp = win->w_python3_ref;
1593bd5e15fdSBram Moolenaar 	wp->win = INVALID_WINDOW_VALUE;
1594bd5e15fdSBram Moolenaar 	win->w_python3_ref = NULL;
1595bd5e15fdSBram Moolenaar     }
1596bd5e15fdSBram Moolenaar }
1597bd5e15fdSBram Moolenaar #endif
1598bd5e15fdSBram Moolenaar 
1599bd5e15fdSBram Moolenaar static BufListObject TheBufferList =
1600bd5e15fdSBram Moolenaar {
1601bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&BufListType)
1602bd5e15fdSBram Moolenaar };
1603bd5e15fdSBram Moolenaar 
1604bd5e15fdSBram Moolenaar static WinListObject TheWindowList =
1605bd5e15fdSBram Moolenaar {
1606bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&WinListType)
1607bd5e15fdSBram Moolenaar };
1608bd5e15fdSBram Moolenaar 
1609bd5e15fdSBram Moolenaar static CurrentObject TheCurrent =
1610bd5e15fdSBram Moolenaar {
1611bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&CurrentType)
1612bd5e15fdSBram Moolenaar };
1613bd5e15fdSBram Moolenaar 
16147854e3abSBram Moolenaar     static PyObject *
16157854e3abSBram Moolenaar Py3Init_vim(void)
1616bd5e15fdSBram Moolenaar {
1617bd5e15fdSBram Moolenaar     PyObject *mod;
161866b7985eSBram Moolenaar     PyObject *tmp;
1619bd5e15fdSBram Moolenaar     /* The special value is removed from sys.path in Python3_Init(). */
1620bd5e15fdSBram Moolenaar     static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1621bd5e15fdSBram Moolenaar 
1622bd5e15fdSBram Moolenaar     PyType_Ready(&BufferType);
1623bd5e15fdSBram Moolenaar     PyType_Ready(&RangeType);
1624bd5e15fdSBram Moolenaar     PyType_Ready(&WindowType);
1625bd5e15fdSBram Moolenaar     PyType_Ready(&BufListType);
1626bd5e15fdSBram Moolenaar     PyType_Ready(&WinListType);
1627bd5e15fdSBram Moolenaar     PyType_Ready(&CurrentType);
1628db913953SBram Moolenaar     PyType_Ready(&DictionaryType);
1629db913953SBram Moolenaar     PyType_Ready(&ListType);
1630db913953SBram Moolenaar     PyType_Ready(&FunctionType);
1631*84e0f6caSBram Moolenaar     PyType_Ready(&OptionsType);
1632bd5e15fdSBram Moolenaar 
1633bd5e15fdSBram Moolenaar     /* Set sys.argv[] to avoid a crash in warn(). */
1634bd5e15fdSBram Moolenaar     PySys_SetArgv(1, argv);
1635bd5e15fdSBram Moolenaar 
1636bd5e15fdSBram Moolenaar     mod = PyModule_Create(&vimmodule);
163719e60943SBram Moolenaar     if (mod == NULL)
163819e60943SBram Moolenaar 	return NULL;
1639bd5e15fdSBram Moolenaar 
164019e60943SBram Moolenaar     VimError = PyErr_NewException("vim.error", NULL, NULL);
164119e60943SBram Moolenaar     Py_INCREF(VimError);
1642bd5e15fdSBram Moolenaar 
1643bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "error", VimError);
1644bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheBufferList);
1645bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1646bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheCurrent);
1647bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1648bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheWindowList);
1649bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1650bd5e15fdSBram Moolenaar 
1651230bb3f0SBram Moolenaar     PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
1652230bb3f0SBram Moolenaar     PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
1653*84e0f6caSBram Moolenaar     PyModule_AddObject(mod, "options",
1654*84e0f6caSBram Moolenaar 	    OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
1655230bb3f0SBram Moolenaar 
165666b7985eSBram Moolenaar #define ADD_INT_CONSTANT(name, value) \
165766b7985eSBram Moolenaar     tmp = PyLong_FromLong(value); \
165866b7985eSBram Moolenaar     Py_INCREF(tmp); \
165966b7985eSBram Moolenaar     PyModule_AddObject(mod, name, tmp)
166066b7985eSBram Moolenaar 
166166b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_LOCKED",     VAR_LOCKED);
166266b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_FIXED",      VAR_FIXED);
166366b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_SCOPE",      VAR_SCOPE);
166466b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_DEF_SCOPE",  VAR_DEF_SCOPE);
166566b7985eSBram Moolenaar 
1666bd5e15fdSBram Moolenaar     if (PyErr_Occurred())
1667bd5e15fdSBram Moolenaar 	return NULL;
1668bd5e15fdSBram Moolenaar 
1669bd5e15fdSBram Moolenaar     return mod;
1670bd5e15fdSBram Moolenaar }
1671bd5e15fdSBram Moolenaar 
1672bd5e15fdSBram Moolenaar /*************************************************************************
1673bd5e15fdSBram Moolenaar  * 4. Utility functions for handling the interface between Vim and Python.
1674bd5e15fdSBram Moolenaar  */
1675bd5e15fdSBram Moolenaar 
1676bd5e15fdSBram Moolenaar /* Convert a Vim line into a Python string.
1677bd5e15fdSBram Moolenaar  * All internal newlines are replaced by null characters.
1678bd5e15fdSBram Moolenaar  *
1679bd5e15fdSBram Moolenaar  * On errors, the Python exception data is set, and NULL is returned.
1680bd5e15fdSBram Moolenaar  */
1681170bf1aeSBram Moolenaar     static PyObject *
1682170bf1aeSBram Moolenaar LineToString(const char *str)
1683bd5e15fdSBram Moolenaar {
1684bd5e15fdSBram Moolenaar     PyObject *result;
1685bd5e15fdSBram Moolenaar     Py_ssize_t len = strlen(str);
1686bd5e15fdSBram Moolenaar     char *tmp,*p;
1687bd5e15fdSBram Moolenaar 
1688bd5e15fdSBram Moolenaar     tmp = (char *)alloc((unsigned)(len+1));
1689bd5e15fdSBram Moolenaar     p = tmp;
1690bd5e15fdSBram Moolenaar     if (p == NULL)
1691bd5e15fdSBram Moolenaar     {
1692bd5e15fdSBram Moolenaar 	PyErr_NoMemory();
1693bd5e15fdSBram Moolenaar 	return NULL;
1694bd5e15fdSBram Moolenaar     }
1695bd5e15fdSBram Moolenaar 
1696bd5e15fdSBram Moolenaar     while (*str)
1697bd5e15fdSBram Moolenaar     {
1698bd5e15fdSBram Moolenaar 	if (*str == '\n')
1699bd5e15fdSBram Moolenaar 	    *p = '\0';
1700bd5e15fdSBram Moolenaar 	else
1701bd5e15fdSBram Moolenaar 	    *p = *str;
1702bd5e15fdSBram Moolenaar 
1703bd5e15fdSBram Moolenaar 	++p;
1704bd5e15fdSBram Moolenaar 	++str;
1705bd5e15fdSBram Moolenaar     }
1706bd5e15fdSBram Moolenaar     *p = '\0';
1707bd5e15fdSBram Moolenaar 
17083d64a317SBram Moolenaar     result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1709bd5e15fdSBram Moolenaar 
1710bd5e15fdSBram Moolenaar     vim_free(tmp);
1711bd5e15fdSBram Moolenaar     return result;
1712bd5e15fdSBram Moolenaar }
1713bd5e15fdSBram Moolenaar 
1714db913953SBram Moolenaar     void
1715db913953SBram Moolenaar do_py3eval (char_u *str, typval_T *rettv)
1716db913953SBram Moolenaar {
1717db913953SBram Moolenaar     DoPy3Command(NULL, (char *) str, rettv);
1718db913953SBram Moolenaar     switch(rettv->v_type)
1719db913953SBram Moolenaar     {
1720db913953SBram Moolenaar 	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1721db913953SBram Moolenaar 	case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1722db913953SBram Moolenaar 	case VAR_FUNC: func_ref(rettv->vval.v_string);    break;
172377fceb89SBram Moolenaar 	case VAR_UNKNOWN:
172477fceb89SBram Moolenaar 	    rettv->v_type = VAR_NUMBER;
172577fceb89SBram Moolenaar 	    rettv->vval.v_number = 0;
172677fceb89SBram Moolenaar 	    break;
1727db913953SBram Moolenaar     }
1728db913953SBram Moolenaar }
1729db913953SBram Moolenaar 
1730db913953SBram Moolenaar     void
1731db913953SBram Moolenaar set_ref_in_python3 (int copyID)
1732db913953SBram Moolenaar {
1733db913953SBram Moolenaar     set_ref_in_py(copyID);
1734db913953SBram Moolenaar }
1735