xref: /vim-8.2.3635/src/if_python3.c (revision 9c9cbf13)
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)
94170bf1aeSBram Moolenaar 
950c1f3f4dSBram Moolenaar #if defined(DYNAMIC_PYTHON3) || defined(PROTO)
96bd5e15fdSBram Moolenaar 
97fa5d1e63SBram Moolenaar # ifndef WIN3264
98bd5e15fdSBram Moolenaar #  include <dlfcn.h>
99bd5e15fdSBram Moolenaar #  define FARPROC void*
100bd5e15fdSBram Moolenaar #  define HINSTANCE void*
101644d37b8SBram Moolenaar #  if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
102b61f95c3SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY)
103b61f95c3SBram Moolenaar #  else
104fa5d1e63SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
105b61f95c3SBram Moolenaar #  endif
106bd5e15fdSBram Moolenaar #  define close_dll dlclose
107bd5e15fdSBram Moolenaar #  define symbol_from_dll dlsym
108bd5e15fdSBram Moolenaar # else
109ebbcb824SBram Moolenaar #  define load_dll vimLoadLib
110bd5e15fdSBram Moolenaar #  define close_dll FreeLibrary
111bd5e15fdSBram Moolenaar #  define symbol_from_dll GetProcAddress
112bd5e15fdSBram Moolenaar # endif
113bd5e15fdSBram Moolenaar /*
114bd5e15fdSBram Moolenaar  * Wrapper defines
115bd5e15fdSBram Moolenaar  */
116bd5e15fdSBram Moolenaar # undef PyArg_Parse
117bd5e15fdSBram Moolenaar # define PyArg_Parse py3_PyArg_Parse
118bd5e15fdSBram Moolenaar # undef PyArg_ParseTuple
119bd5e15fdSBram Moolenaar # define PyArg_ParseTuple py3_PyArg_ParseTuple
12019e60943SBram Moolenaar # define PyMem_Free py3_PyMem_Free
121db913953SBram Moolenaar # define PyMem_Malloc py3_PyMem_Malloc
122bd5e15fdSBram Moolenaar # define PyDict_SetItemString py3_PyDict_SetItemString
123bd5e15fdSBram Moolenaar # define PyErr_BadArgument py3_PyErr_BadArgument
124bd5e15fdSBram Moolenaar # define PyErr_Clear py3_PyErr_Clear
125bd5e15fdSBram Moolenaar # define PyErr_NoMemory py3_PyErr_NoMemory
126bd5e15fdSBram Moolenaar # define PyErr_Occurred py3_PyErr_Occurred
127bd5e15fdSBram Moolenaar # define PyErr_SetNone py3_PyErr_SetNone
128bd5e15fdSBram Moolenaar # define PyErr_SetString py3_PyErr_SetString
129bd5e15fdSBram Moolenaar # define PyEval_InitThreads py3_PyEval_InitThreads
130bd5e15fdSBram Moolenaar # define PyEval_RestoreThread py3_PyEval_RestoreThread
131bd5e15fdSBram Moolenaar # define PyEval_SaveThread py3_PyEval_SaveThread
132bd5e15fdSBram Moolenaar # define PyGILState_Ensure py3_PyGILState_Ensure
133bd5e15fdSBram Moolenaar # define PyGILState_Release py3_PyGILState_Release
134bd5e15fdSBram Moolenaar # define PyLong_AsLong py3_PyLong_AsLong
135bd5e15fdSBram Moolenaar # define PyLong_FromLong py3_PyLong_FromLong
136bd5e15fdSBram Moolenaar # define PyList_GetItem py3_PyList_GetItem
137bd5e15fdSBram Moolenaar # define PyList_Append py3_PyList_Append
138bd5e15fdSBram Moolenaar # define PyList_New py3_PyList_New
139bd5e15fdSBram Moolenaar # define PyList_SetItem py3_PyList_SetItem
140bd5e15fdSBram Moolenaar # define PyList_Size py3_PyList_Size
141db913953SBram Moolenaar # define PySequence_Check py3_PySequence_Check
142db913953SBram Moolenaar # define PySequence_Size py3_PySequence_Size
143db913953SBram Moolenaar # define PySequence_GetItem py3_PySequence_GetItem
144db913953SBram Moolenaar # define PyTuple_Size py3_PyTuple_Size
145db913953SBram Moolenaar # define PyTuple_GetItem py3_PyTuple_GetItem
146bd5e15fdSBram Moolenaar # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
147bd5e15fdSBram Moolenaar # define PyImport_ImportModule py3_PyImport_ImportModule
148db913953SBram Moolenaar # define PyImport_AddModule py3_PyImport_AddModule
149bd5e15fdSBram Moolenaar # define PyObject_Init py3__PyObject_Init
150bd5e15fdSBram Moolenaar # define PyDict_New py3_PyDict_New
151bd5e15fdSBram Moolenaar # define PyDict_GetItemString py3_PyDict_GetItemString
152db913953SBram Moolenaar # define PyDict_Next py3_PyDict_Next
153db913953SBram Moolenaar # define PyMapping_Check py3_PyMapping_Check
154db913953SBram Moolenaar # define PyMapping_Items py3_PyMapping_Items
155db913953SBram Moolenaar # define PyIter_Next py3_PyIter_Next
156db913953SBram Moolenaar # define PyObject_GetIter py3_PyObject_GetIter
157bd5e15fdSBram Moolenaar # define PyModule_GetDict py3_PyModule_GetDict
158bd5e15fdSBram Moolenaar #undef PyRun_SimpleString
159bd5e15fdSBram Moolenaar # define PyRun_SimpleString py3_PyRun_SimpleString
160db913953SBram Moolenaar #undef PyRun_String
161db913953SBram Moolenaar # define PyRun_String py3_PyRun_String
162bd5e15fdSBram Moolenaar # define PySys_SetObject py3_PySys_SetObject
163bd5e15fdSBram Moolenaar # define PySys_SetArgv py3_PySys_SetArgv
164bd5e15fdSBram Moolenaar # define PyType_Ready py3_PyType_Ready
165bd5e15fdSBram Moolenaar #undef Py_BuildValue
166bd5e15fdSBram Moolenaar # define Py_BuildValue py3_Py_BuildValue
167644d37b8SBram Moolenaar # define Py_SetPythonHome py3_Py_SetPythonHome
168bd5e15fdSBram Moolenaar # define Py_Initialize py3_Py_Initialize
169bd5e15fdSBram Moolenaar # define Py_Finalize py3_Py_Finalize
170bd5e15fdSBram Moolenaar # define Py_IsInitialized py3_Py_IsInitialized
171bd5e15fdSBram Moolenaar # define _Py_NoneStruct (*py3__Py_NoneStruct)
17266b7985eSBram Moolenaar # define _Py_FalseStruct (*py3__Py_FalseStruct)
17366b7985eSBram Moolenaar # define _Py_TrueStruct (*py3__Py_TrueStruct)
174db913953SBram Moolenaar # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
175bd5e15fdSBram Moolenaar # define PyModule_AddObject py3_PyModule_AddObject
176bd5e15fdSBram Moolenaar # define PyImport_AppendInittab py3_PyImport_AppendInittab
1777bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
1787bc4f93cSBram Moolenaar #  undef _PyUnicode_AsString
179*9c9cbf13SBram Moolenaar #  define _PyUnicode_AsString py3_PyUnicode_AsUTF8
1807bc4f93cSBram Moolenaar # else
181bd5e15fdSBram Moolenaar #  define _PyUnicode_AsString py3__PyUnicode_AsString
1827bc4f93cSBram Moolenaar # endif
18319e60943SBram Moolenaar # undef PyUnicode_AsEncodedString
18419e60943SBram Moolenaar # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
18519e60943SBram Moolenaar # undef PyBytes_AsString
18619e60943SBram Moolenaar # define PyBytes_AsString py3_PyBytes_AsString
187cdab9051SBram Moolenaar # define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
188db913953SBram Moolenaar # undef PyBytes_FromString
189db913953SBram Moolenaar # define PyBytes_FromString py3_PyBytes_FromString
190db913953SBram Moolenaar # define PyFloat_FromDouble py3_PyFloat_FromDouble
191db913953SBram Moolenaar # define PyFloat_AsDouble py3_PyFloat_AsDouble
192bd5e15fdSBram Moolenaar # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
19366b7985eSBram Moolenaar # define PyType_Type (*py3_PyType_Type)
194bd5e15fdSBram Moolenaar # define PySlice_Type (*py3_PySlice_Type)
195db913953SBram Moolenaar # define PyFloat_Type (*py3_PyFloat_Type)
19666b7985eSBram Moolenaar # define PyBool_Type (*py3_PyBool_Type)
19719e60943SBram Moolenaar # define PyErr_NewException py3_PyErr_NewException
198bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
199bd5e15fdSBram Moolenaar #  define _Py_NegativeRefcount py3__Py_NegativeRefcount
200bd5e15fdSBram Moolenaar #  define _Py_RefTotal (*py3__Py_RefTotal)
201bd5e15fdSBram Moolenaar #  define _Py_Dealloc py3__Py_Dealloc
202bd5e15fdSBram Moolenaar #  define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
203bd5e15fdSBram Moolenaar #  define _PyObject_DebugFree py3__PyObject_DebugFree
204bd5e15fdSBram Moolenaar # else
205bd5e15fdSBram Moolenaar #  define PyObject_Malloc py3_PyObject_Malloc
206bd5e15fdSBram Moolenaar #  define PyObject_Free py3_PyObject_Free
207bd5e15fdSBram Moolenaar # endif
208bd5e15fdSBram Moolenaar # define PyType_GenericAlloc py3_PyType_GenericAlloc
209bd5e15fdSBram Moolenaar # define PyType_GenericNew py3_PyType_GenericNew
210bd5e15fdSBram Moolenaar # define PyModule_Create2 py3_PyModule_Create2
211bd5e15fdSBram Moolenaar # undef PyUnicode_FromString
212bd5e15fdSBram Moolenaar # define PyUnicode_FromString py3_PyUnicode_FromString
21319e60943SBram Moolenaar # undef PyUnicode_Decode
21419e60943SBram Moolenaar # define PyUnicode_Decode py3_PyUnicode_Decode
215db913953SBram Moolenaar # define PyType_IsSubtype py3_PyType_IsSubtype
216db913953SBram Moolenaar # define PyCapsule_New py3_PyCapsule_New
217db913953SBram Moolenaar # define PyCapsule_GetPointer py3_PyCapsule_GetPointer
218bd5e15fdSBram Moolenaar 
219bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
220bd5e15fdSBram Moolenaar #  undef PyObject_NEW
221bd5e15fdSBram Moolenaar #  define PyObject_NEW(type, typeobj) \
222bd5e15fdSBram Moolenaar ( (type *) PyObject_Init( \
223bd5e15fdSBram Moolenaar 	(PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
224bd5e15fdSBram Moolenaar # endif
225b61f95c3SBram Moolenaar 
226bd5e15fdSBram Moolenaar /*
227bd5e15fdSBram Moolenaar  * Pointers for dynamic link
228bd5e15fdSBram Moolenaar  */
229bd5e15fdSBram Moolenaar static int (*py3_PySys_SetArgv)(int, wchar_t **);
230644d37b8SBram Moolenaar static void (*py3_Py_SetPythonHome)(wchar_t *home);
231bd5e15fdSBram Moolenaar static void (*py3_Py_Initialize)(void);
232bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_New)(Py_ssize_t size);
233bd5e15fdSBram Moolenaar static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
234bd5e15fdSBram Moolenaar static void (*py3_PyGILState_Release)(PyGILState_STATE);
235bd5e15fdSBram Moolenaar static int (*py3_PySys_SetObject)(char *, PyObject *);
236bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
237bd5e15fdSBram Moolenaar static Py_ssize_t (*py3_PyList_Size)(PyObject *);
238db913953SBram Moolenaar static int (*py3_PySequence_Check)(PyObject *);
239db913953SBram Moolenaar static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
240db913953SBram Moolenaar static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
241db913953SBram Moolenaar static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
242db913953SBram Moolenaar static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
243db913953SBram Moolenaar static int (*py3_PyMapping_Check)(PyObject *);
244db913953SBram Moolenaar static PyObject* (*py3_PyMapping_Items)(PyObject *);
245314ed4b2SBram Moolenaar static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
246bd5e15fdSBram Moolenaar 		     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
247bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_NoMemory)(void);
248bd5e15fdSBram Moolenaar static void (*py3_Py_Finalize)(void);
249bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetString)(PyObject *, const char *);
250bd5e15fdSBram Moolenaar static int (*py3_PyRun_SimpleString)(char *);
251db913953SBram Moolenaar static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
252bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
253bd5e15fdSBram Moolenaar static PyObject* (*py3_PyImport_ImportModule)(const char *);
254db913953SBram Moolenaar static PyObject* (*py3_PyImport_AddModule)(const char *);
255bd5e15fdSBram Moolenaar static int (*py3_PyErr_BadArgument)(void);
256bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_Occurred)(void);
257bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_GetDict)(PyObject *);
258bd5e15fdSBram Moolenaar static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
259bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
260db913953SBram Moolenaar static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
261bd5e15fdSBram Moolenaar static PyObject* (*py3_PyLong_FromLong)(long);
262bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_New)(void);
263db913953SBram Moolenaar static PyObject* (*py3_PyIter_Next)(PyObject *);
264db913953SBram Moolenaar static PyObject* (*py3_PyObject_GetIter)(PyObject *);
265bd5e15fdSBram Moolenaar static PyObject* (*py3_Py_BuildValue)(char *, ...);
266bd5e15fdSBram Moolenaar static int (*py3_PyType_Ready)(PyTypeObject *type);
267bd5e15fdSBram Moolenaar static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
268bd5e15fdSBram Moolenaar static PyObject* (*py3_PyUnicode_FromString)(const char *u);
26919e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
27019e60943SBram Moolenaar 	const char *encoding, const char *errors);
271bd5e15fdSBram Moolenaar static long (*py3_PyLong_AsLong)(PyObject *);
272bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetNone)(PyObject *);
273bd5e15fdSBram Moolenaar static void (*py3_PyEval_InitThreads)(void);
274bd5e15fdSBram Moolenaar static void(*py3_PyEval_RestoreThread)(PyThreadState *);
275bd5e15fdSBram Moolenaar static PyThreadState*(*py3_PyEval_SaveThread)(void);
276bd5e15fdSBram Moolenaar static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
277bd5e15fdSBram Moolenaar static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
27819e60943SBram Moolenaar static int (*py3_PyMem_Free)(void *);
279db913953SBram Moolenaar static void* (*py3_PyMem_Malloc)(size_t);
280bd5e15fdSBram Moolenaar static int (*py3_Py_IsInitialized)(void);
281bd5e15fdSBram Moolenaar static void (*py3_PyErr_Clear)(void);
282bd5e15fdSBram Moolenaar static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
283db913953SBram Moolenaar static iternextfunc py3__PyObject_NextNotImplemented;
284bd5e15fdSBram Moolenaar static PyObject* py3__Py_NoneStruct;
28566b7985eSBram Moolenaar static PyObject* py3__Py_FalseStruct;
28666b7985eSBram Moolenaar static PyObject* py3__Py_TrueStruct;
287bd5e15fdSBram Moolenaar static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
288bd5e15fdSBram Moolenaar static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
2897bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
290*9c9cbf13SBram Moolenaar static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
2917bc4f93cSBram Moolenaar # else
292bd5e15fdSBram Moolenaar static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
2937bc4f93cSBram Moolenaar # endif
29419e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
29519e60943SBram Moolenaar static char* (*py3_PyBytes_AsString)(PyObject *bytes);
296cdab9051SBram Moolenaar static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
297db913953SBram Moolenaar static PyObject* (*py3_PyBytes_FromString)(char *str);
298db913953SBram Moolenaar static PyObject* (*py3_PyFloat_FromDouble)(double num);
299db913953SBram Moolenaar static double (*py3_PyFloat_AsDouble)(PyObject *);
300bd5e15fdSBram Moolenaar static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
301bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
302bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
303bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
30466b7985eSBram Moolenaar static PyTypeObject* py3_PyType_Type;
305bd5e15fdSBram Moolenaar static PyTypeObject* py3_PySlice_Type;
306db913953SBram Moolenaar static PyTypeObject* py3_PyFloat_Type;
30766b7985eSBram Moolenaar static PyTypeObject* py3_PyBool_Type;
30819e60943SBram Moolenaar static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
309db913953SBram Moolenaar static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
310db913953SBram Moolenaar static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
311bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
312bd5e15fdSBram Moolenaar     static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
313bd5e15fdSBram Moolenaar     static Py_ssize_t* py3__Py_RefTotal;
314bd5e15fdSBram Moolenaar     static void (*py3__Py_Dealloc)(PyObject *obj);
315bd5e15fdSBram Moolenaar     static void (*py3__PyObject_DebugFree)(void*);
316bd5e15fdSBram Moolenaar     static void* (*py3__PyObject_DebugMalloc)(size_t);
317bd5e15fdSBram Moolenaar # else
318bd5e15fdSBram Moolenaar     static void (*py3_PyObject_Free)(void*);
319bd5e15fdSBram Moolenaar     static void* (*py3_PyObject_Malloc)(size_t);
320bd5e15fdSBram Moolenaar # endif
321db913953SBram Moolenaar static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
322bd5e15fdSBram Moolenaar 
323bd5e15fdSBram Moolenaar static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
324bd5e15fdSBram Moolenaar 
325bd5e15fdSBram Moolenaar /* Imported exception objects */
326bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_AttributeError;
327bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_IndexError;
328bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_KeyboardInterrupt;
329bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_TypeError;
330bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_ValueError;
331bd5e15fdSBram Moolenaar 
332bd5e15fdSBram Moolenaar # define PyExc_AttributeError p3imp_PyExc_AttributeError
333bd5e15fdSBram Moolenaar # define PyExc_IndexError p3imp_PyExc_IndexError
334bd5e15fdSBram Moolenaar # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
335bd5e15fdSBram Moolenaar # define PyExc_TypeError p3imp_PyExc_TypeError
336bd5e15fdSBram Moolenaar # define PyExc_ValueError p3imp_PyExc_ValueError
337bd5e15fdSBram Moolenaar 
338bd5e15fdSBram Moolenaar /*
339bd5e15fdSBram Moolenaar  * Table of name to function pointer of python.
340bd5e15fdSBram Moolenaar  */
341bd5e15fdSBram Moolenaar # define PYTHON_PROC FARPROC
342bd5e15fdSBram Moolenaar static struct
343bd5e15fdSBram Moolenaar {
344bd5e15fdSBram Moolenaar     char *name;
345bd5e15fdSBram Moolenaar     PYTHON_PROC *ptr;
346bd5e15fdSBram Moolenaar } py3_funcname_table[] =
347bd5e15fdSBram Moolenaar {
348bd5e15fdSBram Moolenaar     {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
349644d37b8SBram Moolenaar     {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
350bd5e15fdSBram Moolenaar     {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
351e8cdcef8SBram Moolenaar # ifndef PY_SSIZE_T_CLEAN
352bd5e15fdSBram Moolenaar     {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
353e8cdcef8SBram Moolenaar     {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
354e8cdcef8SBram Moolenaar # else
355e8cdcef8SBram Moolenaar     {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
356e8cdcef8SBram Moolenaar     {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
357e8cdcef8SBram Moolenaar # endif
35819e60943SBram Moolenaar     {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
359db913953SBram Moolenaar     {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
360bd5e15fdSBram Moolenaar     {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
361bd5e15fdSBram Moolenaar     {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
362bd5e15fdSBram Moolenaar     {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
363bd5e15fdSBram Moolenaar     {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
364bd5e15fdSBram Moolenaar     {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
365bd5e15fdSBram Moolenaar     {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
366db913953SBram Moolenaar     {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
367db913953SBram Moolenaar     {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
368db913953SBram Moolenaar     {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
369db913953SBram Moolenaar     {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
370db913953SBram Moolenaar     {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
371bd5e15fdSBram Moolenaar     {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
372bd5e15fdSBram Moolenaar     {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
373bd5e15fdSBram Moolenaar     {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
374bd5e15fdSBram Moolenaar     {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
375bd5e15fdSBram Moolenaar     {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
376db913953SBram Moolenaar     {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
377bd5e15fdSBram Moolenaar     {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
378bd5e15fdSBram Moolenaar     {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
379db913953SBram Moolenaar     {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
380bd5e15fdSBram Moolenaar     {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
381bd5e15fdSBram Moolenaar     {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
382bd5e15fdSBram Moolenaar     {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
383bd5e15fdSBram Moolenaar     {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
384bd5e15fdSBram Moolenaar     {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
385db913953SBram Moolenaar     {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
386db913953SBram Moolenaar     {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
387db913953SBram Moolenaar     {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
388db913953SBram Moolenaar     {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
389db913953SBram Moolenaar     {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
390bd5e15fdSBram Moolenaar     {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
391bd5e15fdSBram Moolenaar     {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
392bd5e15fdSBram Moolenaar     {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
393bd5e15fdSBram Moolenaar     {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
394bd5e15fdSBram Moolenaar     {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
395bd5e15fdSBram Moolenaar     {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
396bd5e15fdSBram Moolenaar     {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
397bd5e15fdSBram Moolenaar     {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
398bd5e15fdSBram Moolenaar     {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
399bd5e15fdSBram Moolenaar     {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
400bd5e15fdSBram Moolenaar     {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
401db913953SBram Moolenaar     {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
402bd5e15fdSBram Moolenaar     {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
40366b7985eSBram Moolenaar     {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
40466b7985eSBram Moolenaar     {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
405bd5e15fdSBram Moolenaar     {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
406bd5e15fdSBram Moolenaar     {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
407bd5e15fdSBram Moolenaar     {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
408bd5e15fdSBram Moolenaar     {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
4097bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
410*9c9cbf13SBram Moolenaar     {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
4117bc4f93cSBram Moolenaar # else
412bd5e15fdSBram Moolenaar     {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
4137bc4f93cSBram Moolenaar # endif
41419e60943SBram Moolenaar     {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
415cdab9051SBram Moolenaar     {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
416db913953SBram Moolenaar     {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
417db913953SBram Moolenaar     {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
418db913953SBram Moolenaar     {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
419bd5e15fdSBram Moolenaar     {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
420bd5e15fdSBram Moolenaar     {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
421bd5e15fdSBram Moolenaar     {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
422bd5e15fdSBram Moolenaar     {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
42366b7985eSBram Moolenaar     {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
424bd5e15fdSBram Moolenaar     {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
425db913953SBram Moolenaar     {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
42666b7985eSBram Moolenaar     {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
42719e60943SBram Moolenaar     {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
428bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
429bd5e15fdSBram Moolenaar     {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
430bd5e15fdSBram Moolenaar     {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
431bd5e15fdSBram Moolenaar     {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
432bd5e15fdSBram Moolenaar     {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
433bd5e15fdSBram Moolenaar     {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
434bd5e15fdSBram Moolenaar # else
435bd5e15fdSBram Moolenaar     {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
436bd5e15fdSBram Moolenaar     {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
437bd5e15fdSBram Moolenaar # endif
438db913953SBram Moolenaar     {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
439db913953SBram Moolenaar     {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
440db913953SBram Moolenaar     {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
441bd5e15fdSBram Moolenaar     {"", NULL},
442bd5e15fdSBram Moolenaar };
443bd5e15fdSBram Moolenaar 
444bd5e15fdSBram Moolenaar /*
445bd5e15fdSBram Moolenaar  * Free python.dll
446bd5e15fdSBram Moolenaar  */
447170bf1aeSBram Moolenaar     static void
448170bf1aeSBram Moolenaar end_dynamic_python3(void)
449bd5e15fdSBram Moolenaar {
4504c3a326cSBram Moolenaar     if (hinstPy3 != 0)
451bd5e15fdSBram Moolenaar     {
452bd5e15fdSBram Moolenaar 	close_dll(hinstPy3);
453bd5e15fdSBram Moolenaar 	hinstPy3 = 0;
454bd5e15fdSBram Moolenaar     }
455bd5e15fdSBram Moolenaar }
456bd5e15fdSBram Moolenaar 
457bd5e15fdSBram Moolenaar /*
458bd5e15fdSBram Moolenaar  * Load library and get all pointers.
459bd5e15fdSBram Moolenaar  * Parameter 'libname' provides name of DLL.
460bd5e15fdSBram Moolenaar  * Return OK or FAIL.
461bd5e15fdSBram Moolenaar  */
462170bf1aeSBram Moolenaar     static int
463170bf1aeSBram Moolenaar py3_runtime_link_init(char *libname, int verbose)
464bd5e15fdSBram Moolenaar {
465bd5e15fdSBram Moolenaar     int i;
46619e60943SBram Moolenaar     void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
467bd5e15fdSBram Moolenaar 
468644d37b8SBram Moolenaar # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
469b744b2faSBram Moolenaar     /* Can't have Python and Python3 loaded at the same time.
470b744b2faSBram Moolenaar      * It cause a crash, because RTLD_GLOBAL is needed for
471b744b2faSBram Moolenaar      * standard C extension libraries of one or both python versions. */
4724c3a326cSBram Moolenaar     if (python_loaded())
4734c3a326cSBram Moolenaar     {
4749dc93ae4SBram Moolenaar 	if (verbose)
475b744b2faSBram Moolenaar 	    EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
4764c3a326cSBram Moolenaar 	return FAIL;
4774c3a326cSBram Moolenaar     }
4784c3a326cSBram Moolenaar # endif
4794c3a326cSBram Moolenaar 
4804c3a326cSBram Moolenaar     if (hinstPy3 != 0)
481bd5e15fdSBram Moolenaar 	return OK;
482bd5e15fdSBram Moolenaar     hinstPy3 = load_dll(libname);
483bd5e15fdSBram Moolenaar 
484bd5e15fdSBram Moolenaar     if (!hinstPy3)
485bd5e15fdSBram Moolenaar     {
486bd5e15fdSBram Moolenaar 	if (verbose)
487bd5e15fdSBram Moolenaar 	    EMSG2(_(e_loadlib), libname);
488bd5e15fdSBram Moolenaar 	return FAIL;
489bd5e15fdSBram Moolenaar     }
490bd5e15fdSBram Moolenaar 
491bd5e15fdSBram Moolenaar     for (i = 0; py3_funcname_table[i].ptr; ++i)
492bd5e15fdSBram Moolenaar     {
493bd5e15fdSBram Moolenaar 	if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
494bd5e15fdSBram Moolenaar 			py3_funcname_table[i].name)) == NULL)
495bd5e15fdSBram Moolenaar 	{
496bd5e15fdSBram Moolenaar 	    close_dll(hinstPy3);
497bd5e15fdSBram Moolenaar 	    hinstPy3 = 0;
498bd5e15fdSBram Moolenaar 	    if (verbose)
499bd5e15fdSBram Moolenaar 		EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
500bd5e15fdSBram Moolenaar 	    return FAIL;
501bd5e15fdSBram Moolenaar 	}
502bd5e15fdSBram Moolenaar     }
503bd5e15fdSBram Moolenaar 
50469154f22SBram Moolenaar     /* Load unicode functions separately as only the ucs2 or the ucs4 functions
50569154f22SBram Moolenaar      * will be present in the library. */
5067bc4f93cSBram Moolenaar # if PY_VERSION_HEX >= 0x030300f0
5077bc4f93cSBram Moolenaar     ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
5087bc4f93cSBram Moolenaar     ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
5097bc4f93cSBram Moolenaar     ucs_as_encoded_string = symbol_from_dll(hinstPy3,
5107bc4f93cSBram Moolenaar 	    "PyUnicode_AsEncodedString");
5117bc4f93cSBram Moolenaar # else
512bd5e15fdSBram Moolenaar     ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
51319e60943SBram Moolenaar     ucs_decode = symbol_from_dll(hinstPy3,
51419e60943SBram Moolenaar 	    "PyUnicodeUCS2_Decode");
51519e60943SBram Moolenaar     ucs_as_encoded_string = symbol_from_dll(hinstPy3,
51619e60943SBram Moolenaar 	    "PyUnicodeUCS2_AsEncodedString");
51719e60943SBram Moolenaar     if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
518bd5e15fdSBram Moolenaar     {
519bd5e15fdSBram Moolenaar 	ucs_from_string = symbol_from_dll(hinstPy3,
520bd5e15fdSBram Moolenaar 		"PyUnicodeUCS4_FromString");
52119e60943SBram Moolenaar 	ucs_decode = symbol_from_dll(hinstPy3,
52219e60943SBram Moolenaar 		"PyUnicodeUCS4_Decode");
52319e60943SBram Moolenaar 	ucs_as_encoded_string = symbol_from_dll(hinstPy3,
52419e60943SBram Moolenaar 		"PyUnicodeUCS4_AsEncodedString");
525bd5e15fdSBram Moolenaar     }
5267bc4f93cSBram Moolenaar # endif
52719e60943SBram Moolenaar     if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
528bd5e15fdSBram Moolenaar     {
529bd5e15fdSBram Moolenaar 	py3_PyUnicode_FromString = ucs_from_string;
53019e60943SBram Moolenaar 	py3_PyUnicode_Decode = ucs_decode;
53119e60943SBram Moolenaar 	py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
532bd5e15fdSBram Moolenaar     }
533bd5e15fdSBram Moolenaar     else
534bd5e15fdSBram Moolenaar     {
535bd5e15fdSBram Moolenaar 	close_dll(hinstPy3);
536bd5e15fdSBram Moolenaar 	hinstPy3 = 0;
537bd5e15fdSBram Moolenaar 	if (verbose)
538bd5e15fdSBram Moolenaar 	    EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
539bd5e15fdSBram Moolenaar 	return FAIL;
540bd5e15fdSBram Moolenaar     }
541bd5e15fdSBram Moolenaar 
542bd5e15fdSBram Moolenaar     return OK;
543bd5e15fdSBram Moolenaar }
544bd5e15fdSBram Moolenaar 
545bd5e15fdSBram Moolenaar /*
546bd5e15fdSBram Moolenaar  * If python is enabled (there is installed python on Windows system) return
547bd5e15fdSBram Moolenaar  * TRUE, else FALSE.
548bd5e15fdSBram Moolenaar  */
549170bf1aeSBram Moolenaar     int
550170bf1aeSBram Moolenaar python3_enabled(int verbose)
551bd5e15fdSBram Moolenaar {
552bd5e15fdSBram Moolenaar     return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
553bd5e15fdSBram Moolenaar }
554bd5e15fdSBram Moolenaar 
555bd5e15fdSBram Moolenaar /* Load the standard Python exceptions - don't import the symbols from the
556bd5e15fdSBram Moolenaar  * DLL, as this can cause errors (importing data symbols is not reliable).
557bd5e15fdSBram Moolenaar  */
558bd5e15fdSBram Moolenaar static void get_py3_exceptions __ARGS((void));
559bd5e15fdSBram Moolenaar 
560170bf1aeSBram Moolenaar     static void
561170bf1aeSBram Moolenaar get_py3_exceptions()
562bd5e15fdSBram Moolenaar {
563bd5e15fdSBram Moolenaar     PyObject *exmod = PyImport_ImportModule("builtins");
564bd5e15fdSBram Moolenaar     PyObject *exdict = PyModule_GetDict(exmod);
565bd5e15fdSBram Moolenaar     p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
566bd5e15fdSBram Moolenaar     p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
567bd5e15fdSBram Moolenaar     p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
568bd5e15fdSBram Moolenaar     p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
569bd5e15fdSBram Moolenaar     p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
570bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_AttributeError);
571bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_IndexError);
572bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
573bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_TypeError);
574bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_ValueError);
575bd5e15fdSBram Moolenaar     Py_XDECREF(exmod);
576bd5e15fdSBram Moolenaar }
577bd5e15fdSBram Moolenaar #endif /* DYNAMIC_PYTHON3 */
578bd5e15fdSBram Moolenaar 
579ca8a4dfeSBram Moolenaar static PyObject *BufferNew (buf_T *);
580ca8a4dfeSBram Moolenaar static PyObject *WindowNew(win_T *);
581ca8a4dfeSBram Moolenaar static PyObject *LineToString(const char *);
5827f85d297SBram Moolenaar static PyObject *BufferDir(PyObject *, PyObject *);
583ca8a4dfeSBram Moolenaar 
584ca8a4dfeSBram Moolenaar static PyTypeObject RangeType;
585ca8a4dfeSBram Moolenaar 
586db913953SBram Moolenaar static int py3initialised = 0;
587db913953SBram Moolenaar 
588db913953SBram Moolenaar #define PYINITIALISED py3initialised
589db913953SBram Moolenaar 
590cdab9051SBram Moolenaar #define DICTKEY_DECL PyObject *bytes = NULL;
591cdab9051SBram Moolenaar 
592db913953SBram Moolenaar #define DICTKEY_GET(err) \
593db913953SBram Moolenaar     if (PyBytes_Check(keyObject)) \
594cdab9051SBram Moolenaar     { \
595afa6b9afSBram Moolenaar 	if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
596cdab9051SBram Moolenaar 	    return err; \
597cdab9051SBram Moolenaar     } \
598db913953SBram Moolenaar     else if (PyUnicode_Check(keyObject)) \
599db913953SBram Moolenaar     { \
600db913953SBram Moolenaar 	bytes = PyString_AsBytes(keyObject); \
601db913953SBram Moolenaar 	if (bytes == NULL) \
602db913953SBram Moolenaar 	    return err; \
603afa6b9afSBram Moolenaar 	if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
604db913953SBram Moolenaar 	    return err; \
605db913953SBram Moolenaar     } \
606db913953SBram Moolenaar     else \
607db913953SBram Moolenaar     { \
608db913953SBram Moolenaar 	PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
609db913953SBram Moolenaar 	return err; \
610db913953SBram Moolenaar     }
611cdab9051SBram Moolenaar 
612db913953SBram Moolenaar #define DICTKEY_UNREF \
613db913953SBram Moolenaar     if (bytes != NULL) \
614db913953SBram Moolenaar 	Py_XDECREF(bytes);
615db913953SBram Moolenaar 
616170bf1aeSBram Moolenaar /*
617170bf1aeSBram Moolenaar  * Include the code shared with if_python.c
618170bf1aeSBram Moolenaar  */
619170bf1aeSBram Moolenaar #include "if_py_both.h"
620170bf1aeSBram Moolenaar 
62177045658SBram Moolenaar #define GET_ATTR_STRING(name, nameobj) \
62277045658SBram Moolenaar     char	*name = ""; \
62377045658SBram Moolenaar     if (PyUnicode_Check(nameobj)) \
62477045658SBram Moolenaar 	name = _PyUnicode_AsString(nameobj)
62577045658SBram Moolenaar 
626db913953SBram Moolenaar #define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
627db913953SBram Moolenaar 
628170bf1aeSBram Moolenaar     static void
629170bf1aeSBram Moolenaar call_PyObject_Free(void *p)
630bd5e15fdSBram Moolenaar {
631bd5e15fdSBram Moolenaar #ifdef Py_DEBUG
632bd5e15fdSBram Moolenaar     _PyObject_DebugFree(p);
633bd5e15fdSBram Moolenaar #else
634bd5e15fdSBram Moolenaar     PyObject_Free(p);
635bd5e15fdSBram Moolenaar #endif
636bd5e15fdSBram Moolenaar }
637170bf1aeSBram Moolenaar 
638170bf1aeSBram Moolenaar     static PyObject *
639170bf1aeSBram Moolenaar call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
640bd5e15fdSBram Moolenaar {
641bd5e15fdSBram Moolenaar     return PyType_GenericNew(type,args,kwds);
642bd5e15fdSBram Moolenaar }
643170bf1aeSBram Moolenaar 
644170bf1aeSBram Moolenaar     static PyObject *
645170bf1aeSBram Moolenaar call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
646bd5e15fdSBram Moolenaar {
647bd5e15fdSBram Moolenaar     return PyType_GenericAlloc(type,nitems);
648bd5e15fdSBram Moolenaar }
649bd5e15fdSBram Moolenaar 
650bd5e15fdSBram Moolenaar /******************************************************
651bd5e15fdSBram Moolenaar  * Internal function prototypes.
652bd5e15fdSBram Moolenaar  */
653bd5e15fdSBram Moolenaar 
654bd5e15fdSBram Moolenaar static Py_ssize_t RangeStart;
655bd5e15fdSBram Moolenaar static Py_ssize_t RangeEnd;
656bd5e15fdSBram Moolenaar 
657db913953SBram Moolenaar static PyObject *globals;
658db913953SBram Moolenaar 
659bd5e15fdSBram Moolenaar static int PythonIO_Init(void);
660bd5e15fdSBram Moolenaar static void PythonIO_Fini(void);
66169154f22SBram Moolenaar PyMODINIT_FUNC Py3Init_vim(void);
662bd5e15fdSBram Moolenaar 
663bd5e15fdSBram Moolenaar /******************************************************
664bd5e15fdSBram Moolenaar  * 1. Python interpreter main program.
665bd5e15fdSBram Moolenaar  */
666bd5e15fdSBram Moolenaar 
667bd5e15fdSBram Moolenaar static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
668bd5e15fdSBram Moolenaar 
669170bf1aeSBram Moolenaar     void
670170bf1aeSBram Moolenaar python3_end()
671bd5e15fdSBram Moolenaar {
672bd5e15fdSBram Moolenaar     static int recurse = 0;
673bd5e15fdSBram Moolenaar 
674bd5e15fdSBram Moolenaar     /* If a crash occurs while doing this, don't try again. */
675bd5e15fdSBram Moolenaar     if (recurse != 0)
676bd5e15fdSBram Moolenaar 	return;
677bd5e15fdSBram Moolenaar 
678bd5e15fdSBram Moolenaar     ++recurse;
679bd5e15fdSBram Moolenaar 
680bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
681bd5e15fdSBram Moolenaar     if (hinstPy3)
682bd5e15fdSBram Moolenaar #endif
683bd5e15fdSBram Moolenaar     if (Py_IsInitialized())
684bd5e15fdSBram Moolenaar     {
685bd5e15fdSBram Moolenaar 	// acquire lock before finalizing
686bd5e15fdSBram Moolenaar 	pygilstate = PyGILState_Ensure();
687bd5e15fdSBram Moolenaar 
688bd5e15fdSBram Moolenaar 	PythonIO_Fini();
689bd5e15fdSBram Moolenaar 	Py_Finalize();
690bd5e15fdSBram Moolenaar     }
691bd5e15fdSBram Moolenaar 
692bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
693bd5e15fdSBram Moolenaar     end_dynamic_python3();
694bd5e15fdSBram Moolenaar #endif
695bd5e15fdSBram Moolenaar 
696bd5e15fdSBram Moolenaar     --recurse;
697bd5e15fdSBram Moolenaar }
698bd5e15fdSBram Moolenaar 
6994c3a326cSBram Moolenaar #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
7004c3a326cSBram Moolenaar     int
7014c3a326cSBram Moolenaar python3_loaded()
7024c3a326cSBram Moolenaar {
7034c3a326cSBram Moolenaar     return (hinstPy3 != 0);
7044c3a326cSBram Moolenaar }
7054c3a326cSBram Moolenaar #endif
7064c3a326cSBram Moolenaar 
707170bf1aeSBram Moolenaar     static int
708170bf1aeSBram Moolenaar Python3_Init(void)
709bd5e15fdSBram Moolenaar {
710bd5e15fdSBram Moolenaar     if (!py3initialised)
711bd5e15fdSBram Moolenaar     {
712bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
713bd5e15fdSBram Moolenaar 	if (!python3_enabled(TRUE))
714bd5e15fdSBram Moolenaar 	{
715bd5e15fdSBram Moolenaar 	    EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
716bd5e15fdSBram Moolenaar 	    goto fail;
717bd5e15fdSBram Moolenaar 	}
718bd5e15fdSBram Moolenaar #endif
719bd5e15fdSBram Moolenaar 
720bd5e15fdSBram Moolenaar 	init_structs();
721bd5e15fdSBram Moolenaar 
722644d37b8SBram Moolenaar 
723644d37b8SBram Moolenaar #ifdef PYTHON3_HOME
724644d37b8SBram Moolenaar 	Py_SetPythonHome(PYTHON3_HOME);
725644d37b8SBram Moolenaar #endif
726644d37b8SBram Moolenaar 
7277bc4f93cSBram Moolenaar 	PyImport_AppendInittab("vim", Py3Init_vim);
7287bc4f93cSBram Moolenaar 
729bd5e15fdSBram Moolenaar #if !defined(MACOS) || defined(MACOS_X_UNIX)
730bd5e15fdSBram Moolenaar 	Py_Initialize();
731bd5e15fdSBram Moolenaar #else
732bd5e15fdSBram Moolenaar 	PyMac_Initialize();
733bd5e15fdSBram Moolenaar #endif
734003d14a2SBram Moolenaar 	/* Initialise threads, and save the state using PyGILState_Ensure.
735003d14a2SBram Moolenaar 	 * Without the call to PyGILState_Ensure, thread specific state (such
736003d14a2SBram Moolenaar 	 * as the system trace hook), will be lost between invocations of
737003d14a2SBram Moolenaar 	 * Python code. */
738456f2bb2SBram Moolenaar 	PyEval_InitThreads();
739003d14a2SBram Moolenaar 	pygilstate = PyGILState_Ensure();
740bd5e15fdSBram Moolenaar 
741bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
742bd5e15fdSBram Moolenaar 	get_py3_exceptions();
743bd5e15fdSBram Moolenaar #endif
744bd5e15fdSBram Moolenaar 
745bd5e15fdSBram Moolenaar 	if (PythonIO_Init())
746bd5e15fdSBram Moolenaar 	    goto fail;
747bd5e15fdSBram Moolenaar 
748db913953SBram Moolenaar 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
749db913953SBram Moolenaar 
750bd5e15fdSBram Moolenaar 	/* Remove the element from sys.path that was added because of our
751bd5e15fdSBram Moolenaar 	 * argv[0] value in Py3Init_vim().  Previously we used an empty
752bd5e15fdSBram Moolenaar 	 * string, but dependinding on the OS we then get an empty entry or
75319e60943SBram Moolenaar 	 * the current directory in sys.path.
75419e60943SBram Moolenaar 	 * Only after vim has been imported, the element does exist in
75519e60943SBram Moolenaar 	 * sys.path.
75619e60943SBram Moolenaar 	 */
75719e60943SBram Moolenaar 	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
758bd5e15fdSBram Moolenaar 
759bd5e15fdSBram Moolenaar 	// lock is created and acquired in PyEval_InitThreads() and thread
760bd5e15fdSBram Moolenaar 	// state is created in Py_Initialize()
761bd5e15fdSBram Moolenaar 	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
762bd5e15fdSBram Moolenaar 	// (python must have threads enabled!)
763bd5e15fdSBram Moolenaar 	// so the following does both: unlock GIL and save thread state in TLS
764bd5e15fdSBram Moolenaar 	// without deleting thread state
765bd5e15fdSBram Moolenaar 	PyGILState_Release(pygilstate);
766bd5e15fdSBram Moolenaar 
767bd5e15fdSBram Moolenaar 	py3initialised = 1;
768bd5e15fdSBram Moolenaar     }
769bd5e15fdSBram Moolenaar 
770bd5e15fdSBram Moolenaar     return 0;
771bd5e15fdSBram Moolenaar 
772bd5e15fdSBram Moolenaar fail:
773bd5e15fdSBram Moolenaar     /* We call PythonIO_Flush() here to print any Python errors.
774bd5e15fdSBram Moolenaar      * This is OK, as it is possible to call this function even
775bd5e15fdSBram Moolenaar      * if PythonIO_Init() has not completed successfully (it will
776bd5e15fdSBram Moolenaar      * not do anything in this case).
777bd5e15fdSBram Moolenaar      */
778bd5e15fdSBram Moolenaar     PythonIO_Flush();
779bd5e15fdSBram Moolenaar     return -1;
780bd5e15fdSBram Moolenaar }
781bd5e15fdSBram Moolenaar 
782bd5e15fdSBram Moolenaar /*
783bd5e15fdSBram Moolenaar  * External interface
784bd5e15fdSBram Moolenaar  */
785170bf1aeSBram Moolenaar     static void
786db913953SBram Moolenaar DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
787bd5e15fdSBram Moolenaar {
788bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
789bd5e15fdSBram Moolenaar     GrafPtr		oldPort;
790bd5e15fdSBram Moolenaar #endif
791bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
792bd5e15fdSBram Moolenaar     char		*saved_locale;
793bd5e15fdSBram Moolenaar #endif
79419e60943SBram Moolenaar     PyObject		*cmdstr;
79519e60943SBram Moolenaar     PyObject		*cmdbytes;
796bd5e15fdSBram Moolenaar 
797bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
798bd5e15fdSBram Moolenaar     GetPort(&oldPort);
799bd5e15fdSBram Moolenaar     /* Check if the Python library is available */
800bd5e15fdSBram Moolenaar     if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
801bd5e15fdSBram Moolenaar 	goto theend;
802bd5e15fdSBram Moolenaar #endif
803bd5e15fdSBram Moolenaar     if (Python3_Init())
804bd5e15fdSBram Moolenaar 	goto theend;
805bd5e15fdSBram Moolenaar 
806db913953SBram Moolenaar     if (rettv == NULL)
807db913953SBram Moolenaar     {
808bd5e15fdSBram Moolenaar 	RangeStart = eap->line1;
809bd5e15fdSBram Moolenaar 	RangeEnd = eap->line2;
810db913953SBram Moolenaar     }
811db913953SBram Moolenaar     else
812db913953SBram Moolenaar     {
813db913953SBram Moolenaar 	RangeStart = (PyInt) curwin->w_cursor.lnum;
814db913953SBram Moolenaar 	RangeEnd = RangeStart;
815db913953SBram Moolenaar     }
816bd5e15fdSBram Moolenaar     Python_Release_Vim();	    /* leave vim */
817bd5e15fdSBram Moolenaar 
818bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
819bd5e15fdSBram Moolenaar     /* Python only works properly when the LC_NUMERIC locale is "C". */
820bd5e15fdSBram Moolenaar     saved_locale = setlocale(LC_NUMERIC, NULL);
821bd5e15fdSBram Moolenaar     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
822bd5e15fdSBram Moolenaar 	saved_locale = NULL;
823bd5e15fdSBram Moolenaar     else
824bd5e15fdSBram Moolenaar     {
825bd5e15fdSBram Moolenaar 	/* Need to make a copy, value may change when setting new locale. */
826bd5e15fdSBram Moolenaar 	saved_locale = (char *)vim_strsave((char_u *)saved_locale);
827bd5e15fdSBram Moolenaar 	(void)setlocale(LC_NUMERIC, "C");
828bd5e15fdSBram Moolenaar     }
829bd5e15fdSBram Moolenaar #endif
830bd5e15fdSBram Moolenaar 
831bd5e15fdSBram Moolenaar     pygilstate = PyGILState_Ensure();
832bd5e15fdSBram Moolenaar 
83319e60943SBram Moolenaar     /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
83419e60943SBram Moolenaar      * SyntaxError (unicode error). */
8353d64a317SBram Moolenaar     cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
8363d64a317SBram Moolenaar 					(char *)ENC_OPT, CODEC_ERROR_HANDLER);
8373d64a317SBram Moolenaar     cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
83819e60943SBram Moolenaar     Py_XDECREF(cmdstr);
839db913953SBram Moolenaar     if (rettv == NULL)
84019e60943SBram Moolenaar 	PyRun_SimpleString(PyBytes_AsString(cmdbytes));
841db913953SBram Moolenaar     else
842db913953SBram Moolenaar     {
843db913953SBram Moolenaar 	PyObject	*r;
844db913953SBram Moolenaar 
845db913953SBram Moolenaar 	r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
846db913953SBram Moolenaar 			 globals, globals);
847db913953SBram Moolenaar 	if (r == NULL)
848db913953SBram Moolenaar 	    EMSG(_("E860: Eval did not return a valid python 3 object"));
849db913953SBram Moolenaar 	else
850db913953SBram Moolenaar 	{
851db913953SBram Moolenaar 	    if (ConvertFromPyObject(r, rettv) == -1)
852db913953SBram Moolenaar 		EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
853db913953SBram Moolenaar 	    Py_DECREF(r);
854db913953SBram Moolenaar 	}
855db913953SBram Moolenaar 	PyErr_Clear();
856db913953SBram Moolenaar     }
85719e60943SBram Moolenaar     Py_XDECREF(cmdbytes);
858bd5e15fdSBram Moolenaar 
859bd5e15fdSBram Moolenaar     PyGILState_Release(pygilstate);
860bd5e15fdSBram Moolenaar 
861bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
862bd5e15fdSBram Moolenaar     if (saved_locale != NULL)
863bd5e15fdSBram Moolenaar     {
864bd5e15fdSBram Moolenaar 	(void)setlocale(LC_NUMERIC, saved_locale);
865bd5e15fdSBram Moolenaar 	vim_free(saved_locale);
866bd5e15fdSBram Moolenaar     }
867bd5e15fdSBram Moolenaar #endif
868bd5e15fdSBram Moolenaar 
869bd5e15fdSBram Moolenaar     Python_Lock_Vim();		    /* enter vim */
870bd5e15fdSBram Moolenaar     PythonIO_Flush();
871bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
872bd5e15fdSBram Moolenaar     SetPort(oldPort);
873bd5e15fdSBram Moolenaar #endif
874bd5e15fdSBram Moolenaar 
875bd5e15fdSBram Moolenaar theend:
876bd5e15fdSBram Moolenaar     return;	    /* keeps lint happy */
877bd5e15fdSBram Moolenaar }
878bd5e15fdSBram Moolenaar 
879bd5e15fdSBram Moolenaar /*
880368373e9SBram Moolenaar  * ":py3"
881bd5e15fdSBram Moolenaar  */
882170bf1aeSBram Moolenaar     void
883170bf1aeSBram Moolenaar ex_py3(exarg_T *eap)
884bd5e15fdSBram Moolenaar {
885bd5e15fdSBram Moolenaar     char_u *script;
886bd5e15fdSBram Moolenaar 
887bd5e15fdSBram Moolenaar     script = script_get(eap, eap->arg);
888bd5e15fdSBram Moolenaar     if (!eap->skip)
889bd5e15fdSBram Moolenaar     {
890bd5e15fdSBram Moolenaar 	if (script == NULL)
891db913953SBram Moolenaar 	    DoPy3Command(eap, (char *)eap->arg, NULL);
892bd5e15fdSBram Moolenaar 	else
893db913953SBram Moolenaar 	    DoPy3Command(eap, (char *)script, NULL);
894bd5e15fdSBram Moolenaar     }
895bd5e15fdSBram Moolenaar     vim_free(script);
896bd5e15fdSBram Moolenaar }
897bd5e15fdSBram Moolenaar 
898bd5e15fdSBram Moolenaar #define BUFFER_SIZE 2048
899bd5e15fdSBram Moolenaar 
900bd5e15fdSBram Moolenaar /*
9016df6f47dSBram Moolenaar  * ":py3file"
902bd5e15fdSBram Moolenaar  */
903bd5e15fdSBram Moolenaar     void
904bd5e15fdSBram Moolenaar ex_py3file(exarg_T *eap)
905bd5e15fdSBram Moolenaar {
906bd5e15fdSBram Moolenaar     static char buffer[BUFFER_SIZE];
907bd5e15fdSBram Moolenaar     const char *file;
908bd5e15fdSBram Moolenaar     char *p;
909bd5e15fdSBram Moolenaar     int i;
910bd5e15fdSBram Moolenaar 
911bd5e15fdSBram Moolenaar     /* Have to do it like this. PyRun_SimpleFile requires you to pass a
912bd5e15fdSBram Moolenaar      * stdio file pointer, but Vim and the Python DLL are compiled with
913bd5e15fdSBram Moolenaar      * different options under Windows, meaning that stdio pointers aren't
914bd5e15fdSBram Moolenaar      * compatible between the two. Yuk.
915bd5e15fdSBram Moolenaar      *
91619e60943SBram Moolenaar      * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
91719e60943SBram Moolenaar      *
91819e60943SBram Moolenaar      * Using bytes so that Python can detect the source encoding as it normally
91919e60943SBram Moolenaar      * does. The doc does not say "compile" accept bytes, though.
920bd5e15fdSBram Moolenaar      *
921bd5e15fdSBram Moolenaar      * We need to escape any backslashes or single quotes in the file name, so that
922bd5e15fdSBram Moolenaar      * Python won't mangle the file name.
923bd5e15fdSBram Moolenaar      */
924bd5e15fdSBram Moolenaar 
925bd5e15fdSBram Moolenaar     strcpy(buffer, "exec(compile(open('");
926bd5e15fdSBram Moolenaar     p = buffer + 19; /* size of "exec(compile(open('" */
927bd5e15fdSBram Moolenaar 
928bd5e15fdSBram Moolenaar     for (i=0; i<2; ++i)
929bd5e15fdSBram Moolenaar     {
930bd5e15fdSBram Moolenaar 	file = (char *)eap->arg;
931bd5e15fdSBram Moolenaar 	while (*file && p < buffer + (BUFFER_SIZE - 3))
932bd5e15fdSBram Moolenaar 	{
933bd5e15fdSBram Moolenaar 	    if (*file == '\\' || *file == '\'')
934bd5e15fdSBram Moolenaar 		*p++ = '\\';
935bd5e15fdSBram Moolenaar 	    *p++ = *file++;
936bd5e15fdSBram Moolenaar 	}
937bd5e15fdSBram Moolenaar 	/* If we didn't finish the file name, we hit a buffer overflow */
938bd5e15fdSBram Moolenaar 	if (*file != '\0')
939bd5e15fdSBram Moolenaar 	    return;
940bd5e15fdSBram Moolenaar 	if (i==0)
941bd5e15fdSBram Moolenaar 	{
94219e60943SBram Moolenaar 	    strcpy(p,"','rb').read(),'");
94319e60943SBram Moolenaar 	    p += 16;
944bd5e15fdSBram Moolenaar 	}
945bd5e15fdSBram Moolenaar 	else
946bd5e15fdSBram Moolenaar 	{
947bd5e15fdSBram Moolenaar 	    strcpy(p,"','exec'))");
948bd5e15fdSBram Moolenaar 	    p += 10;
949bd5e15fdSBram Moolenaar 	}
950bd5e15fdSBram Moolenaar     }
951bd5e15fdSBram Moolenaar 
952bd5e15fdSBram Moolenaar 
953bd5e15fdSBram Moolenaar     /* Execute the file */
954db913953SBram Moolenaar     DoPy3Command(eap, buffer, NULL);
955bd5e15fdSBram Moolenaar }
956bd5e15fdSBram Moolenaar 
957bd5e15fdSBram Moolenaar /******************************************************
958bd5e15fdSBram Moolenaar  * 2. Python output stream: writes output via [e]msg().
959bd5e15fdSBram Moolenaar  */
960bd5e15fdSBram Moolenaar 
961bd5e15fdSBram Moolenaar /* Implementation functions
962bd5e15fdSBram Moolenaar  */
963bd5e15fdSBram Moolenaar 
964170bf1aeSBram Moolenaar     static PyObject *
965170bf1aeSBram Moolenaar OutputGetattro(PyObject *self, PyObject *nameobj)
966bd5e15fdSBram Moolenaar {
96777045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
968bd5e15fdSBram Moolenaar 
969bd5e15fdSBram Moolenaar     if (strcmp(name, "softspace") == 0)
970bd5e15fdSBram Moolenaar 	return PyLong_FromLong(((OutputObject *)(self))->softspace);
971bd5e15fdSBram Moolenaar 
972bd5e15fdSBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
973bd5e15fdSBram Moolenaar }
974bd5e15fdSBram Moolenaar 
975170bf1aeSBram Moolenaar     static int
976170bf1aeSBram Moolenaar OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
977bd5e15fdSBram Moolenaar {
97877045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
979bd5e15fdSBram Moolenaar 
98077045658SBram Moolenaar     return OutputSetattr(self, name, val);
981bd5e15fdSBram Moolenaar }
982bd5e15fdSBram Moolenaar 
983bd5e15fdSBram Moolenaar /***************/
984bd5e15fdSBram Moolenaar 
985170bf1aeSBram Moolenaar     static int
986170bf1aeSBram Moolenaar PythonIO_Init(void)
987bd5e15fdSBram Moolenaar {
988bd5e15fdSBram Moolenaar     PyType_Ready(&OutputType);
989170bf1aeSBram Moolenaar     return PythonIO_Init_io();
990bd5e15fdSBram Moolenaar }
991bd5e15fdSBram Moolenaar 
992170bf1aeSBram Moolenaar     static void
993170bf1aeSBram Moolenaar PythonIO_Fini(void)
994bd5e15fdSBram Moolenaar {
995bd5e15fdSBram Moolenaar     PySys_SetObject("stdout", NULL);
996bd5e15fdSBram Moolenaar     PySys_SetObject("stderr", NULL);
997bd5e15fdSBram Moolenaar }
998bd5e15fdSBram Moolenaar 
999bd5e15fdSBram Moolenaar /******************************************************
1000bd5e15fdSBram Moolenaar  * 3. Implementation of the Vim module for Python
1001bd5e15fdSBram Moolenaar  */
1002bd5e15fdSBram Moolenaar 
1003bd5e15fdSBram Moolenaar /* Window type - Implementation functions
1004bd5e15fdSBram Moolenaar  * --------------------------------------
1005bd5e15fdSBram Moolenaar  */
1006bd5e15fdSBram Moolenaar 
1007bd5e15fdSBram Moolenaar #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1008bd5e15fdSBram Moolenaar 
1009bd5e15fdSBram Moolenaar /* Buffer type - Implementation functions
1010bd5e15fdSBram Moolenaar  * --------------------------------------
1011bd5e15fdSBram Moolenaar  */
1012bd5e15fdSBram Moolenaar 
1013bd5e15fdSBram Moolenaar #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1014bd5e15fdSBram Moolenaar 
1015bd5e15fdSBram Moolenaar static Py_ssize_t BufferLength(PyObject *);
1016bd5e15fdSBram Moolenaar static PyObject *BufferItem(PyObject *, Py_ssize_t);
1017bd5e15fdSBram Moolenaar static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
101819e60943SBram Moolenaar static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
1019bd5e15fdSBram Moolenaar 
1020bd5e15fdSBram Moolenaar 
1021bd5e15fdSBram Moolenaar /* Line range type - Implementation functions
1022bd5e15fdSBram Moolenaar  * --------------------------------------
1023bd5e15fdSBram Moolenaar  */
1024bd5e15fdSBram Moolenaar 
1025bd5e15fdSBram Moolenaar #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1026bd5e15fdSBram Moolenaar 
1027bd5e15fdSBram Moolenaar static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
1028bd5e15fdSBram Moolenaar static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1029ba4897e6SBram Moolenaar static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
1030bd5e15fdSBram Moolenaar 
1031bd5e15fdSBram Moolenaar /* Current objects type - Implementation functions
1032bd5e15fdSBram Moolenaar  * -----------------------------------------------
1033bd5e15fdSBram Moolenaar  */
1034bd5e15fdSBram Moolenaar 
1035bd5e15fdSBram Moolenaar static PySequenceMethods BufferAsSeq = {
1036bd5e15fdSBram Moolenaar     (lenfunc)		BufferLength,	    /* sq_length,    len(x)   */
1037bd5e15fdSBram Moolenaar     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1038bd5e15fdSBram Moolenaar     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1039bd5e15fdSBram Moolenaar     (ssizeargfunc)	BufferItem,	    /* sq_item,      x[i]     */
1040bd5e15fdSBram Moolenaar     0,					    /* was_sq_slice,	 x[i:j]   */
104119e60943SBram Moolenaar     0,					    /* sq_ass_item,  x[i]=v   */
1042bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1043bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1044bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1045bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1046bd5e15fdSBram Moolenaar };
1047bd5e15fdSBram Moolenaar 
1048bd5e15fdSBram Moolenaar PyMappingMethods BufferAsMapping = {
1049bd5e15fdSBram Moolenaar     /* mp_length	*/ (lenfunc)BufferLength,
1050bd5e15fdSBram Moolenaar     /* mp_subscript     */ (binaryfunc)BufferSubscript,
105119e60943SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
1052bd5e15fdSBram Moolenaar };
1053bd5e15fdSBram Moolenaar 
1054bd5e15fdSBram Moolenaar 
1055bd5e15fdSBram Moolenaar /* Buffer object - Definitions
1056bd5e15fdSBram Moolenaar  */
1057bd5e15fdSBram Moolenaar 
1058bd5e15fdSBram Moolenaar static PyTypeObject BufferType;
1059bd5e15fdSBram Moolenaar 
1060170bf1aeSBram Moolenaar     static PyObject *
1061170bf1aeSBram Moolenaar BufferNew(buf_T *buf)
1062bd5e15fdSBram Moolenaar {
1063bd5e15fdSBram Moolenaar     /* We need to handle deletion of buffers underneath us.
1064bd5e15fdSBram Moolenaar      * If we add a "b_python3_ref" field to the buf_T structure,
1065bd5e15fdSBram Moolenaar      * then we can get at it in buf_freeall() in vim. We then
1066bd5e15fdSBram Moolenaar      * need to create only ONE Python object per buffer - if
1067bd5e15fdSBram Moolenaar      * we try to create a second, just INCREF the existing one
1068bd5e15fdSBram Moolenaar      * and return it. The (single) Python object referring to
1069bd5e15fdSBram Moolenaar      * the buffer is stored in "b_python3_ref".
1070bd5e15fdSBram Moolenaar      * Question: what to do on a buf_freeall(). We'll probably
1071bd5e15fdSBram Moolenaar      * have to either delete the Python object (DECREF it to
1072bd5e15fdSBram Moolenaar      * zero - a bad idea, as it leaves dangling refs!) or
1073bd5e15fdSBram Moolenaar      * set the buf_T * value to an invalid value (-1?), which
1074bd5e15fdSBram Moolenaar      * means we need checks in all access functions... Bah.
1075bd5e15fdSBram Moolenaar      */
1076bd5e15fdSBram Moolenaar 
1077bd5e15fdSBram Moolenaar     BufferObject *self;
1078bd5e15fdSBram Moolenaar 
1079bd5e15fdSBram Moolenaar     if (buf->b_python3_ref != NULL)
1080bd5e15fdSBram Moolenaar     {
1081bd5e15fdSBram Moolenaar 	self = buf->b_python3_ref;
1082bd5e15fdSBram Moolenaar 	Py_INCREF(self);
1083bd5e15fdSBram Moolenaar     }
1084bd5e15fdSBram Moolenaar     else
1085bd5e15fdSBram Moolenaar     {
1086bd5e15fdSBram Moolenaar 	self = PyObject_NEW(BufferObject, &BufferType);
1087bd5e15fdSBram Moolenaar 	buf->b_python3_ref = self;
1088bd5e15fdSBram Moolenaar 	if (self == NULL)
1089bd5e15fdSBram Moolenaar 	    return NULL;
1090bd5e15fdSBram Moolenaar 	self->buf = buf;
1091bd5e15fdSBram Moolenaar     }
1092bd5e15fdSBram Moolenaar 
1093bd5e15fdSBram Moolenaar     return (PyObject *)(self);
1094bd5e15fdSBram Moolenaar }
1095bd5e15fdSBram Moolenaar 
1096170bf1aeSBram Moolenaar     static void
1097170bf1aeSBram Moolenaar BufferDestructor(PyObject *self)
1098bd5e15fdSBram Moolenaar {
1099bd5e15fdSBram Moolenaar     BufferObject *this = (BufferObject *)(self);
1100bd5e15fdSBram Moolenaar 
1101bd5e15fdSBram Moolenaar     if (this->buf && this->buf != INVALID_BUFFER_VALUE)
1102bd5e15fdSBram Moolenaar 	this->buf->b_python3_ref = NULL;
110319e60943SBram Moolenaar 
110419e60943SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1105bd5e15fdSBram Moolenaar }
1106bd5e15fdSBram Moolenaar 
1107170bf1aeSBram Moolenaar     static PyObject *
1108170bf1aeSBram Moolenaar BufferGetattro(PyObject *self, PyObject*nameobj)
1109bd5e15fdSBram Moolenaar {
1110bd5e15fdSBram Moolenaar     BufferObject *this = (BufferObject *)(self);
1111bd5e15fdSBram Moolenaar 
111277045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1113bd5e15fdSBram Moolenaar 
1114bd5e15fdSBram Moolenaar     if (CheckBuffer(this))
1115bd5e15fdSBram Moolenaar 	return NULL;
1116bd5e15fdSBram Moolenaar 
1117bd5e15fdSBram Moolenaar     if (strcmp(name, "name") == 0)
1118bd5e15fdSBram Moolenaar 	return Py_BuildValue("s", this->buf->b_ffname);
1119bd5e15fdSBram Moolenaar     else if (strcmp(name, "number") == 0)
1120bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", this->buf->b_fnum);
1121bd5e15fdSBram Moolenaar     else
1122bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1123bd5e15fdSBram Moolenaar }
1124bd5e15fdSBram Moolenaar 
1125170bf1aeSBram Moolenaar     static PyObject *
11267f85d297SBram Moolenaar BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
11277f85d297SBram Moolenaar {
11287f85d297SBram Moolenaar     return Py_BuildValue("[sssss]", "name", "number",
11297f85d297SBram Moolenaar 						   "append", "mark", "range");
11307f85d297SBram Moolenaar }
11317f85d297SBram Moolenaar 
11327f85d297SBram Moolenaar     static PyObject *
1133170bf1aeSBram Moolenaar BufferRepr(PyObject *self)
1134bd5e15fdSBram Moolenaar {
1135bd5e15fdSBram Moolenaar     static char repr[100];
1136bd5e15fdSBram Moolenaar     BufferObject *this = (BufferObject *)(self);
1137bd5e15fdSBram Moolenaar 
1138bd5e15fdSBram Moolenaar     if (this->buf == INVALID_BUFFER_VALUE)
1139bd5e15fdSBram Moolenaar     {
1140bd5e15fdSBram Moolenaar 	vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1141bd5e15fdSBram Moolenaar 	return PyUnicode_FromString(repr);
1142bd5e15fdSBram Moolenaar     }
1143bd5e15fdSBram Moolenaar     else
1144bd5e15fdSBram Moolenaar     {
1145bd5e15fdSBram Moolenaar 	char *name = (char *)this->buf->b_fname;
1146bd5e15fdSBram Moolenaar 	Py_ssize_t len;
1147bd5e15fdSBram Moolenaar 
1148bd5e15fdSBram Moolenaar 	if (name == NULL)
1149bd5e15fdSBram Moolenaar 	    name = "";
1150bd5e15fdSBram Moolenaar 	len = strlen(name);
1151bd5e15fdSBram Moolenaar 
1152bd5e15fdSBram Moolenaar 	if (len > 35)
1153bd5e15fdSBram Moolenaar 	    name = name + (35 - len);
1154bd5e15fdSBram Moolenaar 
1155bd5e15fdSBram Moolenaar 	vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
1156bd5e15fdSBram Moolenaar 
1157bd5e15fdSBram Moolenaar 	return PyUnicode_FromString(repr);
1158bd5e15fdSBram Moolenaar     }
1159bd5e15fdSBram Moolenaar }
1160bd5e15fdSBram Moolenaar 
1161bd5e15fdSBram Moolenaar /******************/
1162bd5e15fdSBram Moolenaar 
1163170bf1aeSBram Moolenaar     static Py_ssize_t
1164170bf1aeSBram Moolenaar BufferLength(PyObject *self)
1165bd5e15fdSBram Moolenaar {
1166bd5e15fdSBram Moolenaar     if (CheckBuffer((BufferObject *)(self)))
1167bd5e15fdSBram Moolenaar 	return -1;
1168bd5e15fdSBram Moolenaar 
1169bd5e15fdSBram Moolenaar     return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1170bd5e15fdSBram Moolenaar }
1171bd5e15fdSBram Moolenaar 
1172170bf1aeSBram Moolenaar     static PyObject *
1173170bf1aeSBram Moolenaar BufferItem(PyObject *self, Py_ssize_t n)
1174bd5e15fdSBram Moolenaar {
1175bd5e15fdSBram Moolenaar     return RBItem((BufferObject *)(self), n, 1,
1176bd5e15fdSBram Moolenaar 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1177bd5e15fdSBram Moolenaar }
1178bd5e15fdSBram Moolenaar 
1179170bf1aeSBram Moolenaar     static PyObject *
1180170bf1aeSBram Moolenaar BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1181170bf1aeSBram Moolenaar {
1182170bf1aeSBram Moolenaar     return RBSlice((BufferObject *)(self), lo, hi, 1,
1183170bf1aeSBram Moolenaar 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1184170bf1aeSBram Moolenaar }
1185170bf1aeSBram Moolenaar 
1186170bf1aeSBram Moolenaar     static PyObject *
1187170bf1aeSBram Moolenaar BufferSubscript(PyObject *self, PyObject* idx)
1188bd5e15fdSBram Moolenaar {
1189db913953SBram Moolenaar     if (PyLong_Check(idx))
1190db913953SBram Moolenaar     {
1191bd5e15fdSBram Moolenaar 	long _idx = PyLong_AsLong(idx);
1192bd5e15fdSBram Moolenaar 	return BufferItem(self,_idx);
1193db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1194db913953SBram Moolenaar     {
1195bd5e15fdSBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1196bd5e15fdSBram Moolenaar 
11979e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1198bd5e15fdSBram Moolenaar 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1199bd5e15fdSBram Moolenaar 	      &start, &stop,
1200db913953SBram Moolenaar 	      &step, &slicelen) < 0)
1201db913953SBram Moolenaar 	{
1202bd5e15fdSBram Moolenaar 	    return NULL;
1203bd5e15fdSBram Moolenaar 	}
120419e60943SBram Moolenaar 	return BufferSlice(self, start, stop);
1205db913953SBram Moolenaar     }
1206db913953SBram Moolenaar     else
1207db913953SBram Moolenaar     {
1208bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1209bd5e15fdSBram Moolenaar 	return NULL;
1210bd5e15fdSBram Moolenaar     }
1211bd5e15fdSBram Moolenaar }
1212bd5e15fdSBram Moolenaar 
121319e60943SBram Moolenaar     static Py_ssize_t
121419e60943SBram Moolenaar BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
121519e60943SBram Moolenaar {
1216db913953SBram Moolenaar     if (PyLong_Check(idx))
1217db913953SBram Moolenaar     {
121819e60943SBram Moolenaar 	long n = PyLong_AsLong(idx);
121919e60943SBram Moolenaar 	return RBAsItem((BufferObject *)(self), n, val, 1,
122019e60943SBram Moolenaar 		    (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
122119e60943SBram Moolenaar 		    NULL);
1222db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1223db913953SBram Moolenaar     {
122419e60943SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
122519e60943SBram Moolenaar 
12269e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
122719e60943SBram Moolenaar 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
122819e60943SBram Moolenaar 	      &start, &stop,
1229db913953SBram Moolenaar 	      &step, &slicelen) < 0)
1230db913953SBram Moolenaar 	{
123119e60943SBram Moolenaar 	    return -1;
123219e60943SBram Moolenaar 	}
123319e60943SBram Moolenaar 	return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
123419e60943SBram Moolenaar 			  (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
123519e60943SBram Moolenaar 			  NULL);
1236db913953SBram Moolenaar     }
1237db913953SBram Moolenaar     else
1238db913953SBram Moolenaar     {
123919e60943SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
124019e60943SBram Moolenaar 	return -1;
124119e60943SBram Moolenaar     }
124219e60943SBram Moolenaar }
124319e60943SBram Moolenaar 
1244bd5e15fdSBram Moolenaar static PySequenceMethods RangeAsSeq = {
1245bd5e15fdSBram Moolenaar     (lenfunc)		RangeLength,	 /* sq_length,	  len(x)   */
124655d5c034SBram Moolenaar     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
124755d5c034SBram Moolenaar     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1248bd5e15fdSBram Moolenaar     (ssizeargfunc)	RangeItem,	 /* sq_item,	  x[i]	   */
1249bd5e15fdSBram Moolenaar     0,					 /* was_sq_slice,     x[i:j]   */
1250bd5e15fdSBram Moolenaar     (ssizeobjargproc)	RangeAsItem,	 /* sq_as_item,  x[i]=v   */
1251bd5e15fdSBram Moolenaar     0,					 /* sq_ass_slice, x[i:j]=v */
1252bd5e15fdSBram Moolenaar     0,					 /* sq_contains */
1253bd5e15fdSBram Moolenaar     0,					 /* sq_inplace_concat */
1254bd5e15fdSBram Moolenaar     0,					 /* sq_inplace_repeat */
1255bd5e15fdSBram Moolenaar };
1256bd5e15fdSBram Moolenaar 
1257bd5e15fdSBram Moolenaar PyMappingMethods RangeAsMapping = {
1258bd5e15fdSBram Moolenaar     /* mp_length	*/ (lenfunc)RangeLength,
1259bd5e15fdSBram Moolenaar     /* mp_subscript     */ (binaryfunc)RangeSubscript,
1260ba4897e6SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
1261bd5e15fdSBram Moolenaar };
1262bd5e15fdSBram Moolenaar 
1263bd5e15fdSBram Moolenaar /* Line range object - Implementation
1264bd5e15fdSBram Moolenaar  */
1265bd5e15fdSBram Moolenaar 
1266170bf1aeSBram Moolenaar     static void
1267170bf1aeSBram Moolenaar RangeDestructor(PyObject *self)
1268bd5e15fdSBram Moolenaar {
1269bd5e15fdSBram Moolenaar     Py_DECREF(((RangeObject *)(self))->buf);
127019e60943SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1271bd5e15fdSBram Moolenaar }
1272bd5e15fdSBram Moolenaar 
1273170bf1aeSBram Moolenaar     static PyObject *
1274170bf1aeSBram Moolenaar RangeGetattro(PyObject *self, PyObject *nameobj)
1275bd5e15fdSBram Moolenaar {
127677045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1277bd5e15fdSBram Moolenaar 
1278bd5e15fdSBram Moolenaar     if (strcmp(name, "start") == 0)
1279bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
1280bd5e15fdSBram Moolenaar     else if (strcmp(name, "end") == 0)
1281bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
1282bd5e15fdSBram Moolenaar     else
1283bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1284bd5e15fdSBram Moolenaar }
1285bd5e15fdSBram Moolenaar 
1286bd5e15fdSBram Moolenaar /****************/
1287bd5e15fdSBram Moolenaar 
1288170bf1aeSBram Moolenaar     static Py_ssize_t
1289170bf1aeSBram Moolenaar RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
1290bd5e15fdSBram Moolenaar {
1291bd5e15fdSBram Moolenaar     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1292bd5e15fdSBram Moolenaar 		    ((RangeObject *)(self))->start,
1293bd5e15fdSBram Moolenaar 		    ((RangeObject *)(self))->end,
1294bd5e15fdSBram Moolenaar 		    &((RangeObject *)(self))->end);
1295bd5e15fdSBram Moolenaar }
1296bd5e15fdSBram Moolenaar 
1297ba4897e6SBram Moolenaar     static Py_ssize_t
1298ba4897e6SBram Moolenaar RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1299ba4897e6SBram Moolenaar {
1300ba4897e6SBram Moolenaar     return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1301ba4897e6SBram Moolenaar 		    ((RangeObject *)(self))->start,
1302ba4897e6SBram Moolenaar 		    ((RangeObject *)(self))->end,
1303ba4897e6SBram Moolenaar 		    &((RangeObject *)(self))->end);
1304ba4897e6SBram Moolenaar }
1305ba4897e6SBram Moolenaar 
1306170bf1aeSBram Moolenaar     static PyObject *
1307170bf1aeSBram Moolenaar RangeSubscript(PyObject *self, PyObject* idx)
1308bd5e15fdSBram Moolenaar {
1309db913953SBram Moolenaar     if (PyLong_Check(idx))
1310db913953SBram Moolenaar     {
1311bd5e15fdSBram Moolenaar 	long _idx = PyLong_AsLong(idx);
1312bd5e15fdSBram Moolenaar 	return RangeItem(self,_idx);
1313db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1314db913953SBram Moolenaar     {
1315bd5e15fdSBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1316bd5e15fdSBram Moolenaar 
13179e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1318bd5e15fdSBram Moolenaar 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1319bd5e15fdSBram Moolenaar 		&start, &stop,
1320db913953SBram Moolenaar 		&step, &slicelen) < 0)
1321db913953SBram Moolenaar 	{
1322bd5e15fdSBram Moolenaar 	    return NULL;
1323bd5e15fdSBram Moolenaar 	}
1324ba4897e6SBram Moolenaar 	return RangeSlice(self, start, stop);
1325db913953SBram Moolenaar     }
1326db913953SBram Moolenaar     else
1327db913953SBram Moolenaar     {
1328bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1329bd5e15fdSBram Moolenaar 	return NULL;
1330bd5e15fdSBram Moolenaar     }
1331bd5e15fdSBram Moolenaar }
1332bd5e15fdSBram Moolenaar 
1333ba4897e6SBram Moolenaar     static Py_ssize_t
1334ba4897e6SBram Moolenaar RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1335ba4897e6SBram Moolenaar {
1336db913953SBram Moolenaar     if (PyLong_Check(idx))
1337db913953SBram Moolenaar     {
1338ba4897e6SBram Moolenaar 	long n = PyLong_AsLong(idx);
1339ba4897e6SBram Moolenaar 	return RangeAsItem(self, n, val);
1340db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1341db913953SBram Moolenaar     {
1342ba4897e6SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1343ba4897e6SBram Moolenaar 
13449e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1345ba4897e6SBram Moolenaar 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1346ba4897e6SBram Moolenaar 		&start, &stop,
1347db913953SBram Moolenaar 		&step, &slicelen) < 0)
1348db913953SBram Moolenaar 	{
1349ba4897e6SBram Moolenaar 	    return -1;
1350ba4897e6SBram Moolenaar 	}
1351ba4897e6SBram Moolenaar 	return RangeAsSlice(self, start, stop, val);
1352db913953SBram Moolenaar     }
1353db913953SBram Moolenaar     else
1354db913953SBram Moolenaar     {
1355ba4897e6SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1356ba4897e6SBram Moolenaar 	return -1;
1357ba4897e6SBram Moolenaar     }
1358ba4897e6SBram Moolenaar }
1359ba4897e6SBram Moolenaar 
1360ba4897e6SBram Moolenaar 
1361bd5e15fdSBram Moolenaar /* Buffer list object - Definitions
1362bd5e15fdSBram Moolenaar  */
1363bd5e15fdSBram Moolenaar 
1364bd5e15fdSBram Moolenaar typedef struct
1365bd5e15fdSBram Moolenaar {
1366bd5e15fdSBram Moolenaar     PyObject_HEAD
1367ca8a4dfeSBram Moolenaar } BufListObject;
1368bd5e15fdSBram Moolenaar 
1369bd5e15fdSBram Moolenaar static PySequenceMethods BufListAsSeq = {
1370bd5e15fdSBram Moolenaar     (lenfunc)		BufListLength,	    /* sq_length,    len(x)   */
1371bd5e15fdSBram Moolenaar     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1372bd5e15fdSBram Moolenaar     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1373bd5e15fdSBram Moolenaar     (ssizeargfunc)	BufListItem,	    /* sq_item,      x[i]     */
1374bd5e15fdSBram Moolenaar     0,					    /* was_sq_slice,	 x[i:j]   */
1375bd5e15fdSBram Moolenaar     (ssizeobjargproc)	0,		    /* sq_as_item,  x[i]=v   */
1376bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1377bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1378bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1379bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1380bd5e15fdSBram Moolenaar };
1381bd5e15fdSBram Moolenaar 
1382bd5e15fdSBram Moolenaar static PyTypeObject BufListType;
1383bd5e15fdSBram Moolenaar 
1384bd5e15fdSBram Moolenaar /* Window object - Definitions
1385bd5e15fdSBram Moolenaar  */
1386bd5e15fdSBram Moolenaar 
1387bd5e15fdSBram Moolenaar static struct PyMethodDef WindowMethods[] = {
1388bd5e15fdSBram Moolenaar     /* name,	    function,		calling,    documentation */
1389bd5e15fdSBram Moolenaar     { NULL,	    NULL,		0,	    NULL }
1390bd5e15fdSBram Moolenaar };
1391bd5e15fdSBram Moolenaar 
139255d5c034SBram Moolenaar static PyTypeObject WindowType;
1393bd5e15fdSBram Moolenaar 
1394bd5e15fdSBram Moolenaar /* Window object - Implementation
1395bd5e15fdSBram Moolenaar  */
1396bd5e15fdSBram Moolenaar 
1397170bf1aeSBram Moolenaar     static PyObject *
1398170bf1aeSBram Moolenaar WindowNew(win_T *win)
1399bd5e15fdSBram Moolenaar {
1400bd5e15fdSBram Moolenaar     /* We need to handle deletion of windows underneath us.
1401bd5e15fdSBram Moolenaar      * If we add a "w_python3_ref" field to the win_T structure,
1402bd5e15fdSBram Moolenaar      * then we can get at it in win_free() in vim. We then
1403bd5e15fdSBram Moolenaar      * need to create only ONE Python object per window - if
1404bd5e15fdSBram Moolenaar      * we try to create a second, just INCREF the existing one
1405bd5e15fdSBram Moolenaar      * and return it. The (single) Python object referring to
1406bd5e15fdSBram Moolenaar      * the window is stored in "w_python3_ref".
1407bd5e15fdSBram Moolenaar      * On a win_free() we set the Python object's win_T* field
1408bd5e15fdSBram Moolenaar      * to an invalid value. We trap all uses of a window
1409bd5e15fdSBram Moolenaar      * object, and reject them if the win_T* field is invalid.
1410bd5e15fdSBram Moolenaar      */
1411bd5e15fdSBram Moolenaar 
1412bd5e15fdSBram Moolenaar     WindowObject *self;
1413bd5e15fdSBram Moolenaar 
1414bd5e15fdSBram Moolenaar     if (win->w_python3_ref)
1415bd5e15fdSBram Moolenaar     {
1416bd5e15fdSBram Moolenaar 	self = win->w_python3_ref;
1417bd5e15fdSBram Moolenaar 	Py_INCREF(self);
1418bd5e15fdSBram Moolenaar     }
1419bd5e15fdSBram Moolenaar     else
1420bd5e15fdSBram Moolenaar     {
1421bd5e15fdSBram Moolenaar 	self = PyObject_NEW(WindowObject, &WindowType);
1422bd5e15fdSBram Moolenaar 	if (self == NULL)
1423bd5e15fdSBram Moolenaar 	    return NULL;
1424bd5e15fdSBram Moolenaar 	self->win = win;
1425bd5e15fdSBram Moolenaar 	win->w_python3_ref = self;
1426bd5e15fdSBram Moolenaar     }
1427bd5e15fdSBram Moolenaar 
1428bd5e15fdSBram Moolenaar     return (PyObject *)(self);
1429bd5e15fdSBram Moolenaar }
1430bd5e15fdSBram Moolenaar 
1431170bf1aeSBram Moolenaar     static void
1432170bf1aeSBram Moolenaar WindowDestructor(PyObject *self)
1433bd5e15fdSBram Moolenaar {
1434bd5e15fdSBram Moolenaar     WindowObject *this = (WindowObject *)(self);
1435bd5e15fdSBram Moolenaar 
1436bd5e15fdSBram Moolenaar     if (this->win && this->win != INVALID_WINDOW_VALUE)
1437bd5e15fdSBram Moolenaar 	this->win->w_python3_ref = NULL;
143819e60943SBram Moolenaar 
143919e60943SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1440bd5e15fdSBram Moolenaar }
1441bd5e15fdSBram Moolenaar 
1442170bf1aeSBram Moolenaar     static PyObject *
1443170bf1aeSBram Moolenaar WindowGetattro(PyObject *self, PyObject *nameobj)
1444bd5e15fdSBram Moolenaar {
1445bd5e15fdSBram Moolenaar     WindowObject *this = (WindowObject *)(self);
1446bd5e15fdSBram Moolenaar 
144777045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1448bd5e15fdSBram Moolenaar 
1449bd5e15fdSBram Moolenaar     if (CheckWindow(this))
1450bd5e15fdSBram Moolenaar 	return NULL;
1451bd5e15fdSBram Moolenaar 
1452bd5e15fdSBram Moolenaar     if (strcmp(name, "buffer") == 0)
1453bd5e15fdSBram Moolenaar 	return (PyObject *)BufferNew(this->win->w_buffer);
1454bd5e15fdSBram Moolenaar     else if (strcmp(name, "cursor") == 0)
1455bd5e15fdSBram Moolenaar     {
1456bd5e15fdSBram Moolenaar 	pos_T *pos = &this->win->w_cursor;
1457bd5e15fdSBram Moolenaar 
1458bd5e15fdSBram Moolenaar 	return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1459bd5e15fdSBram Moolenaar     }
1460bd5e15fdSBram Moolenaar     else if (strcmp(name, "height") == 0)
1461bd5e15fdSBram Moolenaar 	return Py_BuildValue("l", (long)(this->win->w_height));
1462bd5e15fdSBram Moolenaar #ifdef FEAT_VERTSPLIT
1463bd5e15fdSBram Moolenaar     else if (strcmp(name, "width") == 0)
1464bd5e15fdSBram Moolenaar 	return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1465bd5e15fdSBram Moolenaar #endif
1466bd5e15fdSBram Moolenaar     else if (strcmp(name,"__members__") == 0)
1467bd5e15fdSBram Moolenaar 	return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1468bd5e15fdSBram Moolenaar     else
1469bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1470bd5e15fdSBram Moolenaar }
1471bd5e15fdSBram Moolenaar 
1472170bf1aeSBram Moolenaar     static int
1473170bf1aeSBram Moolenaar WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1474bd5e15fdSBram Moolenaar {
147577045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1476bd5e15fdSBram Moolenaar 
1477ca8a4dfeSBram Moolenaar     return WindowSetattr(self, name, val);
1478bd5e15fdSBram Moolenaar }
1479bd5e15fdSBram Moolenaar 
1480bd5e15fdSBram Moolenaar /* Window list object - Definitions
1481bd5e15fdSBram Moolenaar  */
1482bd5e15fdSBram Moolenaar 
1483bd5e15fdSBram Moolenaar typedef struct
1484bd5e15fdSBram Moolenaar {
1485bd5e15fdSBram Moolenaar     PyObject_HEAD
1486bd5e15fdSBram Moolenaar }
1487bd5e15fdSBram Moolenaar WinListObject;
1488bd5e15fdSBram Moolenaar 
1489bd5e15fdSBram Moolenaar static PySequenceMethods WinListAsSeq = {
1490bd5e15fdSBram Moolenaar     (lenfunc)	     WinListLength,	    /* sq_length,    len(x)   */
1491bd5e15fdSBram Moolenaar     (binaryfunc)     0,			    /* sq_concat,    x+y      */
1492bd5e15fdSBram Moolenaar     (ssizeargfunc)   0,			    /* sq_repeat,    x*n      */
1493bd5e15fdSBram Moolenaar     (ssizeargfunc)   WinListItem,	    /* sq_item,      x[i]     */
1494bd5e15fdSBram Moolenaar     0,					    /* sq_slice,     x[i:j]   */
1495bd5e15fdSBram Moolenaar     (ssizeobjargproc)0,			    /* sq_as_item,  x[i]=v   */
1496bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1497bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1498bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1499bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1500bd5e15fdSBram Moolenaar };
1501bd5e15fdSBram Moolenaar 
1502bd5e15fdSBram Moolenaar static PyTypeObject WinListType;
1503bd5e15fdSBram Moolenaar 
1504bd5e15fdSBram Moolenaar /* Current items object - Definitions
1505bd5e15fdSBram Moolenaar  */
1506bd5e15fdSBram Moolenaar 
1507bd5e15fdSBram Moolenaar typedef struct
1508bd5e15fdSBram Moolenaar {
1509bd5e15fdSBram Moolenaar     PyObject_HEAD
1510ca8a4dfeSBram Moolenaar } CurrentObject;
1511bd5e15fdSBram Moolenaar 
1512bd5e15fdSBram Moolenaar static PyTypeObject CurrentType;
1513bd5e15fdSBram Moolenaar 
1514bd5e15fdSBram Moolenaar /* Current items object - Implementation
1515bd5e15fdSBram Moolenaar  */
1516170bf1aeSBram Moolenaar     static PyObject *
1517170bf1aeSBram Moolenaar CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
1518bd5e15fdSBram Moolenaar {
151977045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1520bd5e15fdSBram Moolenaar 
1521bd5e15fdSBram Moolenaar     if (strcmp(name, "buffer") == 0)
1522bd5e15fdSBram Moolenaar 	return (PyObject *)BufferNew(curbuf);
1523bd5e15fdSBram Moolenaar     else if (strcmp(name, "window") == 0)
1524bd5e15fdSBram Moolenaar 	return (PyObject *)WindowNew(curwin);
1525bd5e15fdSBram Moolenaar     else if (strcmp(name, "line") == 0)
1526bd5e15fdSBram Moolenaar 	return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
1527bd5e15fdSBram Moolenaar     else if (strcmp(name, "range") == 0)
1528bd5e15fdSBram Moolenaar 	return RangeNew(curbuf, RangeStart, RangeEnd);
1529bd5e15fdSBram Moolenaar     else if (strcmp(name,"__members__") == 0)
1530bd5e15fdSBram Moolenaar 	return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1531bd5e15fdSBram Moolenaar     else
1532bd5e15fdSBram Moolenaar     {
1533bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_AttributeError, name);
1534bd5e15fdSBram Moolenaar 	return NULL;
1535bd5e15fdSBram Moolenaar     }
1536bd5e15fdSBram Moolenaar }
1537bd5e15fdSBram Moolenaar 
1538170bf1aeSBram Moolenaar     static int
1539170bf1aeSBram Moolenaar CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
1540bd5e15fdSBram Moolenaar {
1541bd5e15fdSBram Moolenaar     char *name = "";
1542bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
1543bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1544bd5e15fdSBram Moolenaar 
1545bd5e15fdSBram Moolenaar     if (strcmp(name, "line") == 0)
1546bd5e15fdSBram Moolenaar     {
1547bd5e15fdSBram Moolenaar 	if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1548bd5e15fdSBram Moolenaar 	    return -1;
1549bd5e15fdSBram Moolenaar 
1550bd5e15fdSBram Moolenaar 	return 0;
1551bd5e15fdSBram Moolenaar     }
1552bd5e15fdSBram Moolenaar     else
1553bd5e15fdSBram Moolenaar     {
1554bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_AttributeError, name);
1555bd5e15fdSBram Moolenaar 	return -1;
1556bd5e15fdSBram Moolenaar     }
1557bd5e15fdSBram Moolenaar }
1558bd5e15fdSBram Moolenaar 
1559db913953SBram Moolenaar /* Dictionary object - Definitions
1560db913953SBram Moolenaar  */
1561db913953SBram Moolenaar 
1562db913953SBram Moolenaar static PyInt DictionaryLength(PyObject *);
1563db913953SBram Moolenaar 
1564db913953SBram Moolenaar static PyMappingMethods DictionaryAsMapping = {
1565db913953SBram Moolenaar     /* mp_length	*/ (lenfunc) DictionaryLength,
1566db913953SBram Moolenaar     /* mp_subscript     */ (binaryfunc) DictionaryItem,
1567db913953SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1568db913953SBram Moolenaar };
1569db913953SBram Moolenaar 
157066b7985eSBram Moolenaar     static PyObject *
157166b7985eSBram Moolenaar DictionaryGetattro(PyObject *self, PyObject *nameobj)
157266b7985eSBram Moolenaar {
157366b7985eSBram Moolenaar     DictionaryObject	*this = ((DictionaryObject *) (self));
157466b7985eSBram Moolenaar 
157566b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
157666b7985eSBram Moolenaar 
157766b7985eSBram Moolenaar     if (strcmp(name, "locked") == 0)
157866b7985eSBram Moolenaar 	return PyLong_FromLong(this->dict->dv_lock);
157966b7985eSBram Moolenaar     else if (strcmp(name, "scope") == 0)
158066b7985eSBram Moolenaar 	return PyLong_FromLong(this->dict->dv_scope);
158166b7985eSBram Moolenaar 
158266b7985eSBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
158366b7985eSBram Moolenaar }
158466b7985eSBram Moolenaar 
158566b7985eSBram Moolenaar     static int
158666b7985eSBram Moolenaar DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
158766b7985eSBram Moolenaar {
158866b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
158966b7985eSBram Moolenaar     return DictionarySetattr((DictionaryObject *) self, name, val);
159066b7985eSBram Moolenaar }
159166b7985eSBram Moolenaar 
1592db913953SBram Moolenaar static PyTypeObject DictionaryType;
1593db913953SBram Moolenaar 
1594db913953SBram Moolenaar     static void
1595db913953SBram Moolenaar DictionaryDestructor(PyObject *self)
1596db913953SBram Moolenaar {
1597db913953SBram Moolenaar     DictionaryObject *this = (DictionaryObject *)(self);
1598db913953SBram Moolenaar 
1599db913953SBram Moolenaar     pyll_remove(&this->ref, &lastdict);
1600db913953SBram Moolenaar     dict_unref(this->dict);
1601db913953SBram Moolenaar 
1602db913953SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1603db913953SBram Moolenaar }
1604db913953SBram Moolenaar 
1605db913953SBram Moolenaar /* List object - Definitions
1606db913953SBram Moolenaar  */
1607db913953SBram Moolenaar 
1608db913953SBram Moolenaar static PyInt ListLength(PyObject *);
1609db913953SBram Moolenaar static PyObject *ListItem(PyObject *, Py_ssize_t);
1610db913953SBram Moolenaar 
1611db913953SBram Moolenaar static PySequenceMethods ListAsSeq = {
1612db913953SBram Moolenaar     (lenfunc)		ListLength,	 /* sq_length,	  len(x)   */
1613db913953SBram Moolenaar     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
1614db913953SBram Moolenaar     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1615db913953SBram Moolenaar     (ssizeargfunc)	ListItem,	 /* sq_item,	  x[i]	   */
1616db913953SBram Moolenaar     (void *)		0,		 /* was_sq_slice,     x[i:j]   */
1617db913953SBram Moolenaar     (ssizeobjargproc)	ListAssItem,	 /* sq_as_item,  x[i]=v   */
1618db913953SBram Moolenaar     (void *)		0,		 /* was_sq_ass_slice, x[i:j]=v */
1619db913953SBram Moolenaar     0,					 /* sq_contains */
1620db913953SBram Moolenaar     (binaryfunc)	ListConcatInPlace,/* sq_inplace_concat */
1621db913953SBram Moolenaar     0,					 /* sq_inplace_repeat */
1622db913953SBram Moolenaar };
1623db913953SBram Moolenaar 
1624db913953SBram Moolenaar static PyObject *ListSubscript(PyObject *, PyObject *);
1625db913953SBram Moolenaar static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1626db913953SBram Moolenaar 
1627db913953SBram Moolenaar static PyMappingMethods ListAsMapping = {
1628db913953SBram Moolenaar     /* mp_length	*/ (lenfunc) ListLength,
1629db913953SBram Moolenaar     /* mp_subscript     */ (binaryfunc) ListSubscript,
1630db913953SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1631db913953SBram Moolenaar };
1632db913953SBram Moolenaar 
1633db913953SBram Moolenaar static PyTypeObject ListType;
1634db913953SBram Moolenaar 
1635db913953SBram Moolenaar     static PyObject *
1636db913953SBram Moolenaar ListSubscript(PyObject *self, PyObject* idxObject)
1637db913953SBram Moolenaar {
1638db913953SBram Moolenaar     if (PyLong_Check(idxObject))
1639db913953SBram Moolenaar     {
1640db913953SBram Moolenaar 	long idx = PyLong_AsLong(idxObject);
1641db913953SBram Moolenaar 	return ListItem(self, idx);
1642db913953SBram Moolenaar     }
1643db913953SBram Moolenaar     else if (PySlice_Check(idxObject))
1644db913953SBram Moolenaar     {
1645db913953SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1646db913953SBram Moolenaar 
1647db913953SBram Moolenaar 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1648db913953SBram Moolenaar 				 &step, &slicelen) < 0)
1649db913953SBram Moolenaar 	    return NULL;
1650db913953SBram Moolenaar 	return ListSlice(self, start, stop);
1651db913953SBram Moolenaar     }
1652db913953SBram Moolenaar     else
1653db913953SBram Moolenaar     {
1654db913953SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1655db913953SBram Moolenaar 	return NULL;
1656db913953SBram Moolenaar     }
1657db913953SBram Moolenaar }
1658db913953SBram Moolenaar 
1659db913953SBram Moolenaar     static Py_ssize_t
1660db913953SBram Moolenaar ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1661db913953SBram Moolenaar {
1662db913953SBram Moolenaar     if (PyLong_Check(idxObject))
1663db913953SBram Moolenaar     {
1664db913953SBram Moolenaar 	long idx = PyLong_AsLong(idxObject);
1665db913953SBram Moolenaar 	return ListAssItem(self, idx, obj);
1666db913953SBram Moolenaar     }
1667db913953SBram Moolenaar     else if (PySlice_Check(idxObject))
1668db913953SBram Moolenaar     {
1669db913953SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1670db913953SBram Moolenaar 
1671db913953SBram Moolenaar 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1672db913953SBram Moolenaar 				 &step, &slicelen) < 0)
1673db913953SBram Moolenaar 	    return -1;
1674db913953SBram Moolenaar 	return ListAssSlice(self, start, stop, obj);
1675db913953SBram Moolenaar     }
1676db913953SBram Moolenaar     else
1677db913953SBram Moolenaar     {
1678db913953SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1679db913953SBram Moolenaar 	return -1;
1680db913953SBram Moolenaar     }
1681db913953SBram Moolenaar }
1682db913953SBram Moolenaar 
168366b7985eSBram Moolenaar     static PyObject *
168466b7985eSBram Moolenaar ListGetattro(PyObject *self, PyObject *nameobj)
168566b7985eSBram Moolenaar {
168666b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
168766b7985eSBram Moolenaar 
168866b7985eSBram Moolenaar     if (strcmp(name, "locked") == 0)
168966b7985eSBram Moolenaar 	return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
169066b7985eSBram Moolenaar 
169166b7985eSBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
169266b7985eSBram Moolenaar }
169366b7985eSBram Moolenaar 
169466b7985eSBram Moolenaar     static int
169566b7985eSBram Moolenaar ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
169666b7985eSBram Moolenaar {
169766b7985eSBram Moolenaar     GET_ATTR_STRING(name, nameobj);
169866b7985eSBram Moolenaar     return ListSetattr((ListObject *) self, name, val);
169966b7985eSBram Moolenaar }
170066b7985eSBram Moolenaar 
1701db913953SBram Moolenaar     static void
1702db913953SBram Moolenaar ListDestructor(PyObject *self)
1703db913953SBram Moolenaar {
1704db913953SBram Moolenaar     ListObject *this = (ListObject *)(self);
1705db913953SBram Moolenaar 
1706db913953SBram Moolenaar     pyll_remove(&this->ref, &lastlist);
1707db913953SBram Moolenaar     list_unref(this->list);
1708db913953SBram Moolenaar 
1709db913953SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1710db913953SBram Moolenaar }
1711db913953SBram Moolenaar 
1712db913953SBram Moolenaar /* Function object - Definitions
1713db913953SBram Moolenaar  */
1714db913953SBram Moolenaar 
1715db913953SBram Moolenaar     static void
1716db913953SBram Moolenaar FunctionDestructor(PyObject *self)
1717db913953SBram Moolenaar {
1718db913953SBram Moolenaar     FunctionObject	*this = (FunctionObject *) (self);
1719db913953SBram Moolenaar 
1720db913953SBram Moolenaar     func_unref(this->name);
1721db913953SBram Moolenaar     PyMem_Del(this->name);
1722db913953SBram Moolenaar 
1723db913953SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1724db913953SBram Moolenaar }
1725db913953SBram Moolenaar 
1726db913953SBram Moolenaar     static PyObject *
1727db913953SBram Moolenaar FunctionGetattro(PyObject *self, PyObject *nameobj)
1728db913953SBram Moolenaar {
1729db913953SBram Moolenaar     FunctionObject	*this = (FunctionObject *)(self);
173077045658SBram Moolenaar 
173177045658SBram Moolenaar     GET_ATTR_STRING(name, nameobj);
1732db913953SBram Moolenaar 
1733db913953SBram Moolenaar     if (strcmp(name, "name") == 0)
1734db913953SBram Moolenaar 	return PyUnicode_FromString((char *)(this->name));
1735db913953SBram Moolenaar 
1736db913953SBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
1737db913953SBram Moolenaar }
1738db913953SBram Moolenaar 
1739bd5e15fdSBram Moolenaar /* External interface
1740bd5e15fdSBram Moolenaar  */
1741bd5e15fdSBram Moolenaar 
1742bd5e15fdSBram Moolenaar     void
1743bd5e15fdSBram Moolenaar python3_buffer_free(buf_T *buf)
1744bd5e15fdSBram Moolenaar {
1745bd5e15fdSBram Moolenaar     if (buf->b_python3_ref != NULL)
1746bd5e15fdSBram Moolenaar     {
1747bd5e15fdSBram Moolenaar 	BufferObject *bp = buf->b_python3_ref;
1748bd5e15fdSBram Moolenaar 	bp->buf = INVALID_BUFFER_VALUE;
1749bd5e15fdSBram Moolenaar 	buf->b_python3_ref = NULL;
1750bd5e15fdSBram Moolenaar     }
1751bd5e15fdSBram Moolenaar }
1752bd5e15fdSBram Moolenaar 
1753bd5e15fdSBram Moolenaar #if defined(FEAT_WINDOWS) || defined(PROTO)
1754bd5e15fdSBram Moolenaar     void
1755bd5e15fdSBram Moolenaar python3_window_free(win_T *win)
1756bd5e15fdSBram Moolenaar {
1757bd5e15fdSBram Moolenaar     if (win->w_python3_ref != NULL)
1758bd5e15fdSBram Moolenaar     {
1759bd5e15fdSBram Moolenaar 	WindowObject *wp = win->w_python3_ref;
1760bd5e15fdSBram Moolenaar 	wp->win = INVALID_WINDOW_VALUE;
1761bd5e15fdSBram Moolenaar 	win->w_python3_ref = NULL;
1762bd5e15fdSBram Moolenaar     }
1763bd5e15fdSBram Moolenaar }
1764bd5e15fdSBram Moolenaar #endif
1765bd5e15fdSBram Moolenaar 
1766bd5e15fdSBram Moolenaar static BufListObject TheBufferList =
1767bd5e15fdSBram Moolenaar {
1768bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&BufListType)
1769bd5e15fdSBram Moolenaar };
1770bd5e15fdSBram Moolenaar 
1771bd5e15fdSBram Moolenaar static WinListObject TheWindowList =
1772bd5e15fdSBram Moolenaar {
1773bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&WinListType)
1774bd5e15fdSBram Moolenaar };
1775bd5e15fdSBram Moolenaar 
1776bd5e15fdSBram Moolenaar static CurrentObject TheCurrent =
1777bd5e15fdSBram Moolenaar {
1778bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&CurrentType)
1779bd5e15fdSBram Moolenaar };
1780bd5e15fdSBram Moolenaar 
1781bd5e15fdSBram Moolenaar PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1782bd5e15fdSBram Moolenaar 
1783bd5e15fdSBram Moolenaar static struct PyModuleDef vimmodule;
1784bd5e15fdSBram Moolenaar 
178569154f22SBram Moolenaar #ifndef PROTO
178669154f22SBram Moolenaar PyMODINIT_FUNC Py3Init_vim(void)
1787bd5e15fdSBram Moolenaar {
1788bd5e15fdSBram Moolenaar     PyObject *mod;
178966b7985eSBram Moolenaar     PyObject *tmp;
1790bd5e15fdSBram Moolenaar     /* The special value is removed from sys.path in Python3_Init(). */
1791bd5e15fdSBram Moolenaar     static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1792bd5e15fdSBram Moolenaar 
1793bd5e15fdSBram Moolenaar     PyType_Ready(&BufferType);
1794bd5e15fdSBram Moolenaar     PyType_Ready(&RangeType);
1795bd5e15fdSBram Moolenaar     PyType_Ready(&WindowType);
1796bd5e15fdSBram Moolenaar     PyType_Ready(&BufListType);
1797bd5e15fdSBram Moolenaar     PyType_Ready(&WinListType);
1798bd5e15fdSBram Moolenaar     PyType_Ready(&CurrentType);
1799db913953SBram Moolenaar     PyType_Ready(&DictionaryType);
1800db913953SBram Moolenaar     PyType_Ready(&ListType);
1801db913953SBram Moolenaar     PyType_Ready(&FunctionType);
1802bd5e15fdSBram Moolenaar 
1803bd5e15fdSBram Moolenaar     /* Set sys.argv[] to avoid a crash in warn(). */
1804bd5e15fdSBram Moolenaar     PySys_SetArgv(1, argv);
1805bd5e15fdSBram Moolenaar 
1806bd5e15fdSBram Moolenaar     mod = PyModule_Create(&vimmodule);
180719e60943SBram Moolenaar     if (mod == NULL)
180819e60943SBram Moolenaar 	return NULL;
1809bd5e15fdSBram Moolenaar 
181019e60943SBram Moolenaar     VimError = PyErr_NewException("vim.error", NULL, NULL);
181119e60943SBram Moolenaar     Py_INCREF(VimError);
1812bd5e15fdSBram Moolenaar 
1813bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "error", VimError);
1814bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheBufferList);
1815bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1816bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheCurrent);
1817bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1818bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheWindowList);
1819bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1820bd5e15fdSBram Moolenaar 
182166b7985eSBram Moolenaar #define ADD_INT_CONSTANT(name, value) \
182266b7985eSBram Moolenaar     tmp = PyLong_FromLong(value); \
182366b7985eSBram Moolenaar     Py_INCREF(tmp); \
182466b7985eSBram Moolenaar     PyModule_AddObject(mod, name, tmp)
182566b7985eSBram Moolenaar 
182666b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_LOCKED",     VAR_LOCKED);
182766b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_FIXED",      VAR_FIXED);
182866b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_SCOPE",      VAR_SCOPE);
182966b7985eSBram Moolenaar     ADD_INT_CONSTANT("VAR_DEF_SCOPE",  VAR_DEF_SCOPE);
183066b7985eSBram Moolenaar 
1831bd5e15fdSBram Moolenaar     if (PyErr_Occurred())
1832bd5e15fdSBram Moolenaar 	return NULL;
1833bd5e15fdSBram Moolenaar 
1834bd5e15fdSBram Moolenaar     return mod;
1835bd5e15fdSBram Moolenaar }
183669154f22SBram Moolenaar #endif
1837bd5e15fdSBram Moolenaar 
1838bd5e15fdSBram Moolenaar /*************************************************************************
1839bd5e15fdSBram Moolenaar  * 4. Utility functions for handling the interface between Vim and Python.
1840bd5e15fdSBram Moolenaar  */
1841bd5e15fdSBram Moolenaar 
1842bd5e15fdSBram Moolenaar /* Convert a Vim line into a Python string.
1843bd5e15fdSBram Moolenaar  * All internal newlines are replaced by null characters.
1844bd5e15fdSBram Moolenaar  *
1845bd5e15fdSBram Moolenaar  * On errors, the Python exception data is set, and NULL is returned.
1846bd5e15fdSBram Moolenaar  */
1847170bf1aeSBram Moolenaar     static PyObject *
1848170bf1aeSBram Moolenaar LineToString(const char *str)
1849bd5e15fdSBram Moolenaar {
1850bd5e15fdSBram Moolenaar     PyObject *result;
1851bd5e15fdSBram Moolenaar     Py_ssize_t len = strlen(str);
1852bd5e15fdSBram Moolenaar     char *tmp,*p;
1853bd5e15fdSBram Moolenaar 
1854bd5e15fdSBram Moolenaar     tmp = (char *)alloc((unsigned)(len+1));
1855bd5e15fdSBram Moolenaar     p = tmp;
1856bd5e15fdSBram Moolenaar     if (p == NULL)
1857bd5e15fdSBram Moolenaar     {
1858bd5e15fdSBram Moolenaar 	PyErr_NoMemory();
1859bd5e15fdSBram Moolenaar 	return NULL;
1860bd5e15fdSBram Moolenaar     }
1861bd5e15fdSBram Moolenaar 
1862bd5e15fdSBram Moolenaar     while (*str)
1863bd5e15fdSBram Moolenaar     {
1864bd5e15fdSBram Moolenaar 	if (*str == '\n')
1865bd5e15fdSBram Moolenaar 	    *p = '\0';
1866bd5e15fdSBram Moolenaar 	else
1867bd5e15fdSBram Moolenaar 	    *p = *str;
1868bd5e15fdSBram Moolenaar 
1869bd5e15fdSBram Moolenaar 	++p;
1870bd5e15fdSBram Moolenaar 	++str;
1871bd5e15fdSBram Moolenaar     }
1872bd5e15fdSBram Moolenaar     *p = '\0';
1873bd5e15fdSBram Moolenaar 
18743d64a317SBram Moolenaar     result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1875bd5e15fdSBram Moolenaar 
1876bd5e15fdSBram Moolenaar     vim_free(tmp);
1877bd5e15fdSBram Moolenaar     return result;
1878bd5e15fdSBram Moolenaar }
1879bd5e15fdSBram Moolenaar 
1880db913953SBram Moolenaar     void
1881db913953SBram Moolenaar do_py3eval (char_u *str, typval_T *rettv)
1882db913953SBram Moolenaar {
1883db913953SBram Moolenaar     DoPy3Command(NULL, (char *) str, rettv);
1884db913953SBram Moolenaar     switch(rettv->v_type)
1885db913953SBram Moolenaar     {
1886db913953SBram Moolenaar 	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1887db913953SBram Moolenaar 	case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1888db913953SBram Moolenaar 	case VAR_FUNC: func_ref(rettv->vval.v_string);    break;
188977fceb89SBram Moolenaar 	case VAR_UNKNOWN:
189077fceb89SBram Moolenaar 	    rettv->v_type = VAR_NUMBER;
189177fceb89SBram Moolenaar 	    rettv->vval.v_number = 0;
189277fceb89SBram Moolenaar 	    break;
1893db913953SBram Moolenaar     }
1894db913953SBram Moolenaar }
1895db913953SBram Moolenaar 
1896db913953SBram Moolenaar     void
1897db913953SBram Moolenaar set_ref_in_python3 (int copyID)
1898db913953SBram Moolenaar {
1899db913953SBram Moolenaar     set_ref_in_py(copyID);
1900db913953SBram Moolenaar }
1901db913953SBram Moolenaar 
1902170bf1aeSBram Moolenaar     static void
1903170bf1aeSBram Moolenaar init_structs(void)
1904bd5e15fdSBram Moolenaar {
1905bd5e15fdSBram Moolenaar     vim_memset(&OutputType, 0, sizeof(OutputType));
1906bd5e15fdSBram Moolenaar     OutputType.tp_name = "vim.message";
1907bd5e15fdSBram Moolenaar     OutputType.tp_basicsize = sizeof(OutputObject);
1908bd5e15fdSBram Moolenaar     OutputType.tp_getattro = OutputGetattro;
1909bd5e15fdSBram Moolenaar     OutputType.tp_setattro = OutputSetattro;
1910bd5e15fdSBram Moolenaar     OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1911bd5e15fdSBram Moolenaar     OutputType.tp_doc = "vim message object";
1912bd5e15fdSBram Moolenaar     OutputType.tp_methods = OutputMethods;
1913bd5e15fdSBram Moolenaar     OutputType.tp_alloc = call_PyType_GenericAlloc;
1914bd5e15fdSBram Moolenaar     OutputType.tp_new = call_PyType_GenericNew;
1915bd5e15fdSBram Moolenaar     OutputType.tp_free = call_PyObject_Free;
1916bd5e15fdSBram Moolenaar 
1917bd5e15fdSBram Moolenaar     vim_memset(&BufferType, 0, sizeof(BufferType));
1918bd5e15fdSBram Moolenaar     BufferType.tp_name = "vim.buffer";
1919bd5e15fdSBram Moolenaar     BufferType.tp_basicsize = sizeof(BufferType);
1920bd5e15fdSBram Moolenaar     BufferType.tp_dealloc = BufferDestructor;
1921bd5e15fdSBram Moolenaar     BufferType.tp_repr = BufferRepr;
1922bd5e15fdSBram Moolenaar     BufferType.tp_as_sequence = &BufferAsSeq;
1923bd5e15fdSBram Moolenaar     BufferType.tp_as_mapping = &BufferAsMapping;
1924bd5e15fdSBram Moolenaar     BufferType.tp_getattro = BufferGetattro;
1925bd5e15fdSBram Moolenaar     BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1926bd5e15fdSBram Moolenaar     BufferType.tp_doc = "vim buffer object";
1927bd5e15fdSBram Moolenaar     BufferType.tp_methods = BufferMethods;
1928bd5e15fdSBram Moolenaar     BufferType.tp_alloc = call_PyType_GenericAlloc;
1929bd5e15fdSBram Moolenaar     BufferType.tp_new = call_PyType_GenericNew;
1930bd5e15fdSBram Moolenaar     BufferType.tp_free = call_PyObject_Free;
1931bd5e15fdSBram Moolenaar 
193255d5c034SBram Moolenaar     vim_memset(&WindowType, 0, sizeof(WindowType));
193355d5c034SBram Moolenaar     WindowType.tp_name = "vim.window";
193455d5c034SBram Moolenaar     WindowType.tp_basicsize = sizeof(WindowObject);
193555d5c034SBram Moolenaar     WindowType.tp_dealloc = WindowDestructor;
193655d5c034SBram Moolenaar     WindowType.tp_repr = WindowRepr;
193755d5c034SBram Moolenaar     WindowType.tp_getattro = WindowGetattro;
193855d5c034SBram Moolenaar     WindowType.tp_setattro = WindowSetattro;
193955d5c034SBram Moolenaar     WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
194055d5c034SBram Moolenaar     WindowType.tp_doc = "vim Window object";
194155d5c034SBram Moolenaar     WindowType.tp_methods = WindowMethods;
194255d5c034SBram Moolenaar     WindowType.tp_alloc = call_PyType_GenericAlloc;
194355d5c034SBram Moolenaar     WindowType.tp_new = call_PyType_GenericNew;
194455d5c034SBram Moolenaar     WindowType.tp_free = call_PyObject_Free;
194555d5c034SBram Moolenaar 
1946bd5e15fdSBram Moolenaar     vim_memset(&BufListType, 0, sizeof(BufListType));
1947bd5e15fdSBram Moolenaar     BufListType.tp_name = "vim.bufferlist";
1948bd5e15fdSBram Moolenaar     BufListType.tp_basicsize = sizeof(BufListObject);
1949bd5e15fdSBram Moolenaar     BufListType.tp_as_sequence = &BufListAsSeq;
1950bd5e15fdSBram Moolenaar     BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1951bd5e15fdSBram Moolenaar     BufferType.tp_doc = "vim buffer list";
1952bd5e15fdSBram Moolenaar 
1953bd5e15fdSBram Moolenaar     vim_memset(&WinListType, 0, sizeof(WinListType));
1954bd5e15fdSBram Moolenaar     WinListType.tp_name = "vim.windowlist";
1955bd5e15fdSBram Moolenaar     WinListType.tp_basicsize = sizeof(WinListType);
1956bd5e15fdSBram Moolenaar     WinListType.tp_as_sequence = &WinListAsSeq;
1957bd5e15fdSBram Moolenaar     WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1958bd5e15fdSBram Moolenaar     WinListType.tp_doc = "vim window list";
1959bd5e15fdSBram Moolenaar 
1960bd5e15fdSBram Moolenaar     vim_memset(&RangeType, 0, sizeof(RangeType));
1961bd5e15fdSBram Moolenaar     RangeType.tp_name = "vim.range";
1962bd5e15fdSBram Moolenaar     RangeType.tp_basicsize = sizeof(RangeObject);
1963bd5e15fdSBram Moolenaar     RangeType.tp_dealloc = RangeDestructor;
1964bd5e15fdSBram Moolenaar     RangeType.tp_repr = RangeRepr;
1965bd5e15fdSBram Moolenaar     RangeType.tp_as_sequence = &RangeAsSeq;
1966bd5e15fdSBram Moolenaar     RangeType.tp_as_mapping = &RangeAsMapping;
1967bd5e15fdSBram Moolenaar     RangeType.tp_getattro = RangeGetattro;
1968bd5e15fdSBram Moolenaar     RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1969bd5e15fdSBram Moolenaar     RangeType.tp_doc = "vim Range object";
1970bd5e15fdSBram Moolenaar     RangeType.tp_methods = RangeMethods;
1971bd5e15fdSBram Moolenaar     RangeType.tp_alloc = call_PyType_GenericAlloc;
1972bd5e15fdSBram Moolenaar     RangeType.tp_new = call_PyType_GenericNew;
1973bd5e15fdSBram Moolenaar     RangeType.tp_free = call_PyObject_Free;
1974bd5e15fdSBram Moolenaar 
1975bd5e15fdSBram Moolenaar     vim_memset(&CurrentType, 0, sizeof(CurrentType));
1976bd5e15fdSBram Moolenaar     CurrentType.tp_name = "vim.currentdata";
1977bd5e15fdSBram Moolenaar     CurrentType.tp_basicsize = sizeof(CurrentObject);
1978bd5e15fdSBram Moolenaar     CurrentType.tp_getattro = CurrentGetattro;
1979bd5e15fdSBram Moolenaar     CurrentType.tp_setattro = CurrentSetattro;
1980bd5e15fdSBram Moolenaar     CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1981bd5e15fdSBram Moolenaar     CurrentType.tp_doc = "vim current object";
1982bd5e15fdSBram Moolenaar 
1983db913953SBram Moolenaar     vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1984db913953SBram Moolenaar     DictionaryType.tp_name = "vim.dictionary";
1985db913953SBram Moolenaar     DictionaryType.tp_basicsize = sizeof(DictionaryObject);
198666b7985eSBram Moolenaar     DictionaryType.tp_getattro = DictionaryGetattro;
198766b7985eSBram Moolenaar     DictionaryType.tp_setattro = DictionarySetattro;
1988db913953SBram Moolenaar     DictionaryType.tp_dealloc = DictionaryDestructor;
1989db913953SBram Moolenaar     DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1990db913953SBram Moolenaar     DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1991db913953SBram Moolenaar     DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1992db913953SBram Moolenaar     DictionaryType.tp_methods = DictionaryMethods;
1993db913953SBram Moolenaar 
1994db913953SBram Moolenaar     vim_memset(&ListType, 0, sizeof(ListType));
1995db913953SBram Moolenaar     ListType.tp_name = "vim.list";
1996db913953SBram Moolenaar     ListType.tp_dealloc = ListDestructor;
1997db913953SBram Moolenaar     ListType.tp_basicsize = sizeof(ListObject);
199866b7985eSBram Moolenaar     ListType.tp_getattro = ListGetattro;
199966b7985eSBram Moolenaar     ListType.tp_setattro = ListSetattro;
2000db913953SBram Moolenaar     ListType.tp_as_sequence = &ListAsSeq;
2001db913953SBram Moolenaar     ListType.tp_as_mapping = &ListAsMapping;
2002db913953SBram Moolenaar     ListType.tp_flags = Py_TPFLAGS_DEFAULT;
2003db913953SBram Moolenaar     ListType.tp_doc = "list pushing modifications to vim structure";
2004db913953SBram Moolenaar     ListType.tp_methods = ListMethods;
2005db913953SBram Moolenaar 
2006db913953SBram Moolenaar     vim_memset(&FunctionType, 0, sizeof(FunctionType));
2007db913953SBram Moolenaar     FunctionType.tp_name = "vim.list";
2008db913953SBram Moolenaar     FunctionType.tp_basicsize = sizeof(FunctionObject);
2009db913953SBram Moolenaar     FunctionType.tp_getattro = FunctionGetattro;
2010db913953SBram Moolenaar     FunctionType.tp_dealloc = FunctionDestructor;
2011db913953SBram Moolenaar     FunctionType.tp_call = FunctionCall;
2012db913953SBram Moolenaar     FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
2013db913953SBram Moolenaar     FunctionType.tp_doc = "object that calls vim function";
2014db913953SBram Moolenaar     FunctionType.tp_methods = FunctionMethods;
2015db913953SBram Moolenaar 
2016bd5e15fdSBram Moolenaar     vim_memset(&vimmodule, 0, sizeof(vimmodule));
2017bd5e15fdSBram Moolenaar     vimmodule.m_name = "vim";
2018bd5e15fdSBram Moolenaar     vimmodule.m_doc = vim_module_doc;
2019bd5e15fdSBram Moolenaar     vimmodule.m_size = -1;
2020bd5e15fdSBram Moolenaar     vimmodule.m_methods = VimMethods;
2021bd5e15fdSBram Moolenaar }
2022