xref: /vim-8.2.3635/src/if_python3.c (revision e8cdcef8)
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 
67*e8cdcef8SBram Moolenaar #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
68*e8cdcef8SBram Moolenaar # define PY_SSIZE_T_CLEAN
69*e8cdcef8SBram Moolenaar #endif
70*e8cdcef8SBram 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)
91170bf1aeSBram Moolenaar 
920c1f3f4dSBram Moolenaar #if defined(DYNAMIC_PYTHON3) || defined(PROTO)
93bd5e15fdSBram Moolenaar 
94fa5d1e63SBram Moolenaar # ifndef WIN3264
95bd5e15fdSBram Moolenaar #  include <dlfcn.h>
96bd5e15fdSBram Moolenaar #  define FARPROC void*
97bd5e15fdSBram Moolenaar #  define HINSTANCE void*
98644d37b8SBram Moolenaar #  if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
99b61f95c3SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY)
100b61f95c3SBram Moolenaar #  else
101fa5d1e63SBram Moolenaar #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
102b61f95c3SBram Moolenaar #  endif
103bd5e15fdSBram Moolenaar #  define close_dll dlclose
104bd5e15fdSBram Moolenaar #  define symbol_from_dll dlsym
105bd5e15fdSBram Moolenaar # else
106ebbcb824SBram Moolenaar #  define load_dll vimLoadLib
107bd5e15fdSBram Moolenaar #  define close_dll FreeLibrary
108bd5e15fdSBram Moolenaar #  define symbol_from_dll GetProcAddress
109bd5e15fdSBram Moolenaar # endif
110bd5e15fdSBram Moolenaar /*
111bd5e15fdSBram Moolenaar  * Wrapper defines
112bd5e15fdSBram Moolenaar  */
113bd5e15fdSBram Moolenaar # undef PyArg_Parse
114bd5e15fdSBram Moolenaar # define PyArg_Parse py3_PyArg_Parse
115bd5e15fdSBram Moolenaar # undef PyArg_ParseTuple
116bd5e15fdSBram Moolenaar # define PyArg_ParseTuple py3_PyArg_ParseTuple
11719e60943SBram Moolenaar # define PyMem_Free py3_PyMem_Free
118db913953SBram Moolenaar # define PyMem_Malloc py3_PyMem_Malloc
119bd5e15fdSBram Moolenaar # define PyDict_SetItemString py3_PyDict_SetItemString
120bd5e15fdSBram Moolenaar # define PyErr_BadArgument py3_PyErr_BadArgument
121bd5e15fdSBram Moolenaar # define PyErr_Clear py3_PyErr_Clear
122bd5e15fdSBram Moolenaar # define PyErr_NoMemory py3_PyErr_NoMemory
123bd5e15fdSBram Moolenaar # define PyErr_Occurred py3_PyErr_Occurred
124bd5e15fdSBram Moolenaar # define PyErr_SetNone py3_PyErr_SetNone
125bd5e15fdSBram Moolenaar # define PyErr_SetString py3_PyErr_SetString
126bd5e15fdSBram Moolenaar # define PyEval_InitThreads py3_PyEval_InitThreads
127bd5e15fdSBram Moolenaar # define PyEval_RestoreThread py3_PyEval_RestoreThread
128bd5e15fdSBram Moolenaar # define PyEval_SaveThread py3_PyEval_SaveThread
129bd5e15fdSBram Moolenaar # define PyGILState_Ensure py3_PyGILState_Ensure
130bd5e15fdSBram Moolenaar # define PyGILState_Release py3_PyGILState_Release
131bd5e15fdSBram Moolenaar # define PyLong_AsLong py3_PyLong_AsLong
132bd5e15fdSBram Moolenaar # define PyLong_FromLong py3_PyLong_FromLong
133bd5e15fdSBram Moolenaar # define PyList_GetItem py3_PyList_GetItem
134bd5e15fdSBram Moolenaar # define PyList_Append py3_PyList_Append
135bd5e15fdSBram Moolenaar # define PyList_New py3_PyList_New
136bd5e15fdSBram Moolenaar # define PyList_SetItem py3_PyList_SetItem
137bd5e15fdSBram Moolenaar # define PyList_Size py3_PyList_Size
138db913953SBram Moolenaar # define PySequence_Check py3_PySequence_Check
139db913953SBram Moolenaar # define PySequence_Size py3_PySequence_Size
140db913953SBram Moolenaar # define PySequence_GetItem py3_PySequence_GetItem
141db913953SBram Moolenaar # define PyTuple_Size py3_PyTuple_Size
142db913953SBram Moolenaar # define PyTuple_GetItem py3_PyTuple_GetItem
143bd5e15fdSBram Moolenaar # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
144bd5e15fdSBram Moolenaar # define PyImport_ImportModule py3_PyImport_ImportModule
145db913953SBram Moolenaar # define PyImport_AddModule py3_PyImport_AddModule
146bd5e15fdSBram Moolenaar # define PyObject_Init py3__PyObject_Init
147bd5e15fdSBram Moolenaar # define PyDict_New py3_PyDict_New
148bd5e15fdSBram Moolenaar # define PyDict_GetItemString py3_PyDict_GetItemString
149db913953SBram Moolenaar # define PyDict_Next py3_PyDict_Next
150db913953SBram Moolenaar # define PyMapping_Check py3_PyMapping_Check
151db913953SBram Moolenaar # define PyMapping_Items py3_PyMapping_Items
152db913953SBram Moolenaar # define PyIter_Next py3_PyIter_Next
153db913953SBram Moolenaar # define PyObject_GetIter py3_PyObject_GetIter
154bd5e15fdSBram Moolenaar # define PyModule_GetDict py3_PyModule_GetDict
155bd5e15fdSBram Moolenaar #undef PyRun_SimpleString
156bd5e15fdSBram Moolenaar # define PyRun_SimpleString py3_PyRun_SimpleString
157db913953SBram Moolenaar #undef PyRun_String
158db913953SBram Moolenaar # define PyRun_String py3_PyRun_String
159bd5e15fdSBram Moolenaar # define PySys_SetObject py3_PySys_SetObject
160bd5e15fdSBram Moolenaar # define PySys_SetArgv py3_PySys_SetArgv
161bd5e15fdSBram Moolenaar # define PyType_Type (*py3_PyType_Type)
162bd5e15fdSBram Moolenaar # define PyType_Ready py3_PyType_Ready
163bd5e15fdSBram Moolenaar #undef Py_BuildValue
164bd5e15fdSBram Moolenaar # define Py_BuildValue py3_Py_BuildValue
165644d37b8SBram Moolenaar # define Py_SetPythonHome py3_Py_SetPythonHome
166bd5e15fdSBram Moolenaar # define Py_Initialize py3_Py_Initialize
167bd5e15fdSBram Moolenaar # define Py_Finalize py3_Py_Finalize
168bd5e15fdSBram Moolenaar # define Py_IsInitialized py3_Py_IsInitialized
169bd5e15fdSBram Moolenaar # define _Py_NoneStruct (*py3__Py_NoneStruct)
170db913953SBram Moolenaar # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
171bd5e15fdSBram Moolenaar # define PyModule_AddObject py3_PyModule_AddObject
172bd5e15fdSBram Moolenaar # define PyImport_AppendInittab py3_PyImport_AppendInittab
173bd5e15fdSBram Moolenaar # define _PyUnicode_AsString py3__PyUnicode_AsString
17419e60943SBram Moolenaar # undef PyUnicode_AsEncodedString
17519e60943SBram Moolenaar # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
17619e60943SBram Moolenaar # undef PyBytes_AsString
17719e60943SBram Moolenaar # define PyBytes_AsString py3_PyBytes_AsString
178cdab9051SBram Moolenaar # define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
179db913953SBram Moolenaar # undef PyBytes_FromString
180db913953SBram Moolenaar # define PyBytes_FromString py3_PyBytes_FromString
181db913953SBram Moolenaar # define PyFloat_FromDouble py3_PyFloat_FromDouble
182db913953SBram Moolenaar # define PyFloat_AsDouble py3_PyFloat_AsDouble
183bd5e15fdSBram Moolenaar # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
184bd5e15fdSBram Moolenaar # define PySlice_Type (*py3_PySlice_Type)
185db913953SBram Moolenaar # define PyFloat_Type (*py3_PyFloat_Type)
18619e60943SBram Moolenaar # define PyErr_NewException py3_PyErr_NewException
187bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
188bd5e15fdSBram Moolenaar #  define _Py_NegativeRefcount py3__Py_NegativeRefcount
189bd5e15fdSBram Moolenaar #  define _Py_RefTotal (*py3__Py_RefTotal)
190bd5e15fdSBram Moolenaar #  define _Py_Dealloc py3__Py_Dealloc
191bd5e15fdSBram Moolenaar #  define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
192bd5e15fdSBram Moolenaar #  define _PyObject_DebugFree py3__PyObject_DebugFree
193bd5e15fdSBram Moolenaar # else
194bd5e15fdSBram Moolenaar #  define PyObject_Malloc py3_PyObject_Malloc
195bd5e15fdSBram Moolenaar #  define PyObject_Free py3_PyObject_Free
196bd5e15fdSBram Moolenaar # endif
197bd5e15fdSBram Moolenaar # define PyType_GenericAlloc py3_PyType_GenericAlloc
198bd5e15fdSBram Moolenaar # define PyType_GenericNew py3_PyType_GenericNew
199bd5e15fdSBram Moolenaar # define PyModule_Create2 py3_PyModule_Create2
200bd5e15fdSBram Moolenaar # undef PyUnicode_FromString
201bd5e15fdSBram Moolenaar # define PyUnicode_FromString py3_PyUnicode_FromString
20219e60943SBram Moolenaar # undef PyUnicode_Decode
20319e60943SBram Moolenaar # define PyUnicode_Decode py3_PyUnicode_Decode
204db913953SBram Moolenaar # define PyType_IsSubtype py3_PyType_IsSubtype
205db913953SBram Moolenaar # define PyCapsule_New py3_PyCapsule_New
206db913953SBram Moolenaar # define PyCapsule_GetPointer py3_PyCapsule_GetPointer
207bd5e15fdSBram Moolenaar 
208bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
209bd5e15fdSBram Moolenaar #  undef PyObject_NEW
210bd5e15fdSBram Moolenaar #  define PyObject_NEW(type, typeobj) \
211bd5e15fdSBram Moolenaar ( (type *) PyObject_Init( \
212bd5e15fdSBram Moolenaar 	(PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
213bd5e15fdSBram Moolenaar # endif
214b61f95c3SBram Moolenaar 
215bd5e15fdSBram Moolenaar /*
216bd5e15fdSBram Moolenaar  * Pointers for dynamic link
217bd5e15fdSBram Moolenaar  */
218bd5e15fdSBram Moolenaar static int (*py3_PySys_SetArgv)(int, wchar_t **);
219644d37b8SBram Moolenaar static void (*py3_Py_SetPythonHome)(wchar_t *home);
220bd5e15fdSBram Moolenaar static void (*py3_Py_Initialize)(void);
221bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_New)(Py_ssize_t size);
222bd5e15fdSBram Moolenaar static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
223bd5e15fdSBram Moolenaar static void (*py3_PyGILState_Release)(PyGILState_STATE);
224bd5e15fdSBram Moolenaar static int (*py3_PySys_SetObject)(char *, PyObject *);
225bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
226bd5e15fdSBram Moolenaar static Py_ssize_t (*py3_PyList_Size)(PyObject *);
227db913953SBram Moolenaar static int (*py3_PySequence_Check)(PyObject *);
228db913953SBram Moolenaar static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
229db913953SBram Moolenaar static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
230db913953SBram Moolenaar static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
231db913953SBram Moolenaar static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
232db913953SBram Moolenaar static int (*py3_PyMapping_Check)(PyObject *);
233db913953SBram Moolenaar static PyObject* (*py3_PyMapping_Items)(PyObject *);
234314ed4b2SBram Moolenaar static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
235bd5e15fdSBram Moolenaar 		     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
236bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_NoMemory)(void);
237bd5e15fdSBram Moolenaar static void (*py3_Py_Finalize)(void);
238bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetString)(PyObject *, const char *);
239bd5e15fdSBram Moolenaar static int (*py3_PyRun_SimpleString)(char *);
240db913953SBram Moolenaar static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
241bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
242bd5e15fdSBram Moolenaar static PyObject* (*py3_PyImport_ImportModule)(const char *);
243db913953SBram Moolenaar static PyObject* (*py3_PyImport_AddModule)(const char *);
244bd5e15fdSBram Moolenaar static int (*py3_PyErr_BadArgument)(void);
245bd5e15fdSBram Moolenaar static PyTypeObject* py3_PyType_Type;
246bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_Occurred)(void);
247bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_GetDict)(PyObject *);
248bd5e15fdSBram Moolenaar static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
249bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
250db913953SBram Moolenaar static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
251bd5e15fdSBram Moolenaar static PyObject* (*py3_PyLong_FromLong)(long);
252bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_New)(void);
253db913953SBram Moolenaar static PyObject* (*py3_PyIter_Next)(PyObject *);
254db913953SBram Moolenaar static PyObject* (*py3_PyObject_GetIter)(PyObject *);
255bd5e15fdSBram Moolenaar static PyObject* (*py3_Py_BuildValue)(char *, ...);
256bd5e15fdSBram Moolenaar static int (*py3_PyType_Ready)(PyTypeObject *type);
257bd5e15fdSBram Moolenaar static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
258bd5e15fdSBram Moolenaar static PyObject* (*py3_PyUnicode_FromString)(const char *u);
25919e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
26019e60943SBram Moolenaar 	const char *encoding, const char *errors);
261bd5e15fdSBram Moolenaar static long (*py3_PyLong_AsLong)(PyObject *);
262bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetNone)(PyObject *);
263bd5e15fdSBram Moolenaar static void (*py3_PyEval_InitThreads)(void);
264bd5e15fdSBram Moolenaar static void(*py3_PyEval_RestoreThread)(PyThreadState *);
265bd5e15fdSBram Moolenaar static PyThreadState*(*py3_PyEval_SaveThread)(void);
266bd5e15fdSBram Moolenaar static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
267bd5e15fdSBram Moolenaar static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
26819e60943SBram Moolenaar static int (*py3_PyMem_Free)(void *);
269db913953SBram Moolenaar static void* (*py3_PyMem_Malloc)(size_t);
270bd5e15fdSBram Moolenaar static int (*py3_Py_IsInitialized)(void);
271bd5e15fdSBram Moolenaar static void (*py3_PyErr_Clear)(void);
272bd5e15fdSBram Moolenaar static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
273db913953SBram Moolenaar static iternextfunc py3__PyObject_NextNotImplemented;
274bd5e15fdSBram Moolenaar static PyObject* py3__Py_NoneStruct;
275bd5e15fdSBram Moolenaar static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
276bd5e15fdSBram Moolenaar static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
277bd5e15fdSBram Moolenaar static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
27819e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
27919e60943SBram Moolenaar static char* (*py3_PyBytes_AsString)(PyObject *bytes);
280cdab9051SBram Moolenaar static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
281db913953SBram Moolenaar static PyObject* (*py3_PyBytes_FromString)(char *str);
282db913953SBram Moolenaar static PyObject* (*py3_PyFloat_FromDouble)(double num);
283db913953SBram Moolenaar static double (*py3_PyFloat_AsDouble)(PyObject *);
284bd5e15fdSBram Moolenaar static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
285bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
286bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
287bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
288bd5e15fdSBram Moolenaar static PyTypeObject* py3_PySlice_Type;
289db913953SBram Moolenaar static PyTypeObject* py3_PyFloat_Type;
29019e60943SBram Moolenaar static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
291db913953SBram Moolenaar static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
292db913953SBram Moolenaar static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
293bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
294bd5e15fdSBram Moolenaar     static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
295bd5e15fdSBram Moolenaar     static Py_ssize_t* py3__Py_RefTotal;
296bd5e15fdSBram Moolenaar     static void (*py3__Py_Dealloc)(PyObject *obj);
297bd5e15fdSBram Moolenaar     static void (*py3__PyObject_DebugFree)(void*);
298bd5e15fdSBram Moolenaar     static void* (*py3__PyObject_DebugMalloc)(size_t);
299bd5e15fdSBram Moolenaar # else
300bd5e15fdSBram Moolenaar     static void (*py3_PyObject_Free)(void*);
301bd5e15fdSBram Moolenaar     static void* (*py3_PyObject_Malloc)(size_t);
302bd5e15fdSBram Moolenaar # endif
303db913953SBram Moolenaar static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
304bd5e15fdSBram Moolenaar 
305bd5e15fdSBram Moolenaar static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
306bd5e15fdSBram Moolenaar 
307bd5e15fdSBram Moolenaar /* Imported exception objects */
308bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_AttributeError;
309bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_IndexError;
310bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_KeyboardInterrupt;
311bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_TypeError;
312bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_ValueError;
313bd5e15fdSBram Moolenaar 
314bd5e15fdSBram Moolenaar # define PyExc_AttributeError p3imp_PyExc_AttributeError
315bd5e15fdSBram Moolenaar # define PyExc_IndexError p3imp_PyExc_IndexError
316bd5e15fdSBram Moolenaar # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
317bd5e15fdSBram Moolenaar # define PyExc_TypeError p3imp_PyExc_TypeError
318bd5e15fdSBram Moolenaar # define PyExc_ValueError p3imp_PyExc_ValueError
319bd5e15fdSBram Moolenaar 
320bd5e15fdSBram Moolenaar /*
321bd5e15fdSBram Moolenaar  * Table of name to function pointer of python.
322bd5e15fdSBram Moolenaar  */
323bd5e15fdSBram Moolenaar # define PYTHON_PROC FARPROC
324bd5e15fdSBram Moolenaar static struct
325bd5e15fdSBram Moolenaar {
326bd5e15fdSBram Moolenaar     char *name;
327bd5e15fdSBram Moolenaar     PYTHON_PROC *ptr;
328bd5e15fdSBram Moolenaar } py3_funcname_table[] =
329bd5e15fdSBram Moolenaar {
330bd5e15fdSBram Moolenaar     {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
331644d37b8SBram Moolenaar     {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
332bd5e15fdSBram Moolenaar     {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
333*e8cdcef8SBram Moolenaar #ifndef PY_SSIZE_T_CLEAN
334bd5e15fdSBram Moolenaar     {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
335*e8cdcef8SBram Moolenaar     {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
336*e8cdcef8SBram Moolenaar #else
337*e8cdcef8SBram Moolenaar     {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
338*e8cdcef8SBram Moolenaar     {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
339*e8cdcef8SBram Moolenaar #endif
34019e60943SBram Moolenaar     {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
341db913953SBram Moolenaar     {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
342bd5e15fdSBram Moolenaar     {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
343bd5e15fdSBram Moolenaar     {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
344bd5e15fdSBram Moolenaar     {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
345bd5e15fdSBram Moolenaar     {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
346bd5e15fdSBram Moolenaar     {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
347bd5e15fdSBram Moolenaar     {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
348db913953SBram Moolenaar     {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
349db913953SBram Moolenaar     {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
350db913953SBram Moolenaar     {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
351db913953SBram Moolenaar     {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
352db913953SBram Moolenaar     {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
353bd5e15fdSBram Moolenaar     {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
354bd5e15fdSBram Moolenaar     {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
355bd5e15fdSBram Moolenaar     {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
356bd5e15fdSBram Moolenaar     {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
357bd5e15fdSBram Moolenaar     {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
358db913953SBram Moolenaar     {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
359bd5e15fdSBram Moolenaar     {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
360bd5e15fdSBram Moolenaar     {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
361db913953SBram Moolenaar     {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
362bd5e15fdSBram Moolenaar     {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
363bd5e15fdSBram Moolenaar     {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
364bd5e15fdSBram Moolenaar     {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
365bd5e15fdSBram Moolenaar     {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
366bd5e15fdSBram Moolenaar     {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
367bd5e15fdSBram Moolenaar     {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
368db913953SBram Moolenaar     {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
369db913953SBram Moolenaar     {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
370db913953SBram Moolenaar     {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
371db913953SBram Moolenaar     {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
372db913953SBram Moolenaar     {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
373bd5e15fdSBram Moolenaar     {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
374bd5e15fdSBram Moolenaar     {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
375bd5e15fdSBram Moolenaar     {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
376bd5e15fdSBram Moolenaar     {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
377bd5e15fdSBram Moolenaar     {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
378bd5e15fdSBram Moolenaar     {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
379bd5e15fdSBram Moolenaar     {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
380bd5e15fdSBram Moolenaar     {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
381bd5e15fdSBram Moolenaar     {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
382bd5e15fdSBram Moolenaar     {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
383bd5e15fdSBram Moolenaar     {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
384db913953SBram Moolenaar     {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
385bd5e15fdSBram Moolenaar     {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
386bd5e15fdSBram Moolenaar     {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
387bd5e15fdSBram Moolenaar     {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
388bd5e15fdSBram Moolenaar     {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
389bd5e15fdSBram Moolenaar     {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
390bd5e15fdSBram Moolenaar     {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
39119e60943SBram Moolenaar     {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
392cdab9051SBram Moolenaar     {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
393db913953SBram Moolenaar     {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
394db913953SBram Moolenaar     {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
395db913953SBram Moolenaar     {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
396bd5e15fdSBram Moolenaar     {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
397bd5e15fdSBram Moolenaar     {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
398bd5e15fdSBram Moolenaar     {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
399bd5e15fdSBram Moolenaar     {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
400bd5e15fdSBram Moolenaar     {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
401db913953SBram Moolenaar     {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
40219e60943SBram Moolenaar     {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
403bd5e15fdSBram Moolenaar # ifdef Py_DEBUG
404bd5e15fdSBram Moolenaar     {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
405bd5e15fdSBram Moolenaar     {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
406bd5e15fdSBram Moolenaar     {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
407bd5e15fdSBram Moolenaar     {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
408bd5e15fdSBram Moolenaar     {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
409bd5e15fdSBram Moolenaar # else
410bd5e15fdSBram Moolenaar     {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
411bd5e15fdSBram Moolenaar     {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
412bd5e15fdSBram Moolenaar # endif
413db913953SBram Moolenaar     {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
414db913953SBram Moolenaar     {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
415db913953SBram Moolenaar     {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
416bd5e15fdSBram Moolenaar     {"", NULL},
417bd5e15fdSBram Moolenaar };
418bd5e15fdSBram Moolenaar 
419bd5e15fdSBram Moolenaar /*
420bd5e15fdSBram Moolenaar  * Free python.dll
421bd5e15fdSBram Moolenaar  */
422170bf1aeSBram Moolenaar     static void
423170bf1aeSBram Moolenaar end_dynamic_python3(void)
424bd5e15fdSBram Moolenaar {
4254c3a326cSBram Moolenaar     if (hinstPy3 != 0)
426bd5e15fdSBram Moolenaar     {
427bd5e15fdSBram Moolenaar 	close_dll(hinstPy3);
428bd5e15fdSBram Moolenaar 	hinstPy3 = 0;
429bd5e15fdSBram Moolenaar     }
430bd5e15fdSBram Moolenaar }
431bd5e15fdSBram Moolenaar 
432bd5e15fdSBram Moolenaar /*
433bd5e15fdSBram Moolenaar  * Load library and get all pointers.
434bd5e15fdSBram Moolenaar  * Parameter 'libname' provides name of DLL.
435bd5e15fdSBram Moolenaar  * Return OK or FAIL.
436bd5e15fdSBram Moolenaar  */
437170bf1aeSBram Moolenaar     static int
438170bf1aeSBram Moolenaar py3_runtime_link_init(char *libname, int verbose)
439bd5e15fdSBram Moolenaar {
440bd5e15fdSBram Moolenaar     int i;
44119e60943SBram Moolenaar     void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
442bd5e15fdSBram Moolenaar 
443644d37b8SBram Moolenaar # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
444b744b2faSBram Moolenaar     /* Can't have Python and Python3 loaded at the same time.
445b744b2faSBram Moolenaar      * It cause a crash, because RTLD_GLOBAL is needed for
446b744b2faSBram Moolenaar      * standard C extension libraries of one or both python versions. */
4474c3a326cSBram Moolenaar     if (python_loaded())
4484c3a326cSBram Moolenaar     {
4499dc93ae4SBram Moolenaar 	if (verbose)
450b744b2faSBram Moolenaar 	    EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
4514c3a326cSBram Moolenaar 	return FAIL;
4524c3a326cSBram Moolenaar     }
4534c3a326cSBram Moolenaar # endif
4544c3a326cSBram Moolenaar 
4554c3a326cSBram Moolenaar     if (hinstPy3 != 0)
456bd5e15fdSBram Moolenaar 	return OK;
457bd5e15fdSBram Moolenaar     hinstPy3 = load_dll(libname);
458bd5e15fdSBram Moolenaar 
459bd5e15fdSBram Moolenaar     if (!hinstPy3)
460bd5e15fdSBram Moolenaar     {
461bd5e15fdSBram Moolenaar 	if (verbose)
462bd5e15fdSBram Moolenaar 	    EMSG2(_(e_loadlib), libname);
463bd5e15fdSBram Moolenaar 	return FAIL;
464bd5e15fdSBram Moolenaar     }
465bd5e15fdSBram Moolenaar 
466bd5e15fdSBram Moolenaar     for (i = 0; py3_funcname_table[i].ptr; ++i)
467bd5e15fdSBram Moolenaar     {
468bd5e15fdSBram Moolenaar 	if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
469bd5e15fdSBram Moolenaar 			py3_funcname_table[i].name)) == NULL)
470bd5e15fdSBram Moolenaar 	{
471bd5e15fdSBram Moolenaar 	    close_dll(hinstPy3);
472bd5e15fdSBram Moolenaar 	    hinstPy3 = 0;
473bd5e15fdSBram Moolenaar 	    if (verbose)
474bd5e15fdSBram Moolenaar 		EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
475bd5e15fdSBram Moolenaar 	    return FAIL;
476bd5e15fdSBram Moolenaar 	}
477bd5e15fdSBram Moolenaar     }
478bd5e15fdSBram Moolenaar 
47969154f22SBram Moolenaar     /* Load unicode functions separately as only the ucs2 or the ucs4 functions
48069154f22SBram Moolenaar      * will be present in the library. */
481bd5e15fdSBram Moolenaar     ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
48219e60943SBram Moolenaar     ucs_decode = symbol_from_dll(hinstPy3,
48319e60943SBram Moolenaar 	    "PyUnicodeUCS2_Decode");
48419e60943SBram Moolenaar     ucs_as_encoded_string = symbol_from_dll(hinstPy3,
48519e60943SBram Moolenaar 	    "PyUnicodeUCS2_AsEncodedString");
48619e60943SBram Moolenaar     if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
487bd5e15fdSBram Moolenaar     {
488bd5e15fdSBram Moolenaar 	ucs_from_string = symbol_from_dll(hinstPy3,
489bd5e15fdSBram Moolenaar 		"PyUnicodeUCS4_FromString");
49019e60943SBram Moolenaar 	ucs_decode = symbol_from_dll(hinstPy3,
49119e60943SBram Moolenaar 		"PyUnicodeUCS4_Decode");
49219e60943SBram Moolenaar 	ucs_as_encoded_string = symbol_from_dll(hinstPy3,
49319e60943SBram Moolenaar 		"PyUnicodeUCS4_AsEncodedString");
494bd5e15fdSBram Moolenaar     }
49519e60943SBram Moolenaar     if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
496bd5e15fdSBram Moolenaar     {
497bd5e15fdSBram Moolenaar 	py3_PyUnicode_FromString = ucs_from_string;
49819e60943SBram Moolenaar 	py3_PyUnicode_Decode = ucs_decode;
49919e60943SBram Moolenaar 	py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
500bd5e15fdSBram Moolenaar     }
501bd5e15fdSBram Moolenaar     else
502bd5e15fdSBram Moolenaar     {
503bd5e15fdSBram Moolenaar 	close_dll(hinstPy3);
504bd5e15fdSBram Moolenaar 	hinstPy3 = 0;
505bd5e15fdSBram Moolenaar 	if (verbose)
506bd5e15fdSBram Moolenaar 	    EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
507bd5e15fdSBram Moolenaar 	return FAIL;
508bd5e15fdSBram Moolenaar     }
509bd5e15fdSBram Moolenaar 
510bd5e15fdSBram Moolenaar     return OK;
511bd5e15fdSBram Moolenaar }
512bd5e15fdSBram Moolenaar 
513bd5e15fdSBram Moolenaar /*
514bd5e15fdSBram Moolenaar  * If python is enabled (there is installed python on Windows system) return
515bd5e15fdSBram Moolenaar  * TRUE, else FALSE.
516bd5e15fdSBram Moolenaar  */
517170bf1aeSBram Moolenaar     int
518170bf1aeSBram Moolenaar python3_enabled(int verbose)
519bd5e15fdSBram Moolenaar {
520bd5e15fdSBram Moolenaar     return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
521bd5e15fdSBram Moolenaar }
522bd5e15fdSBram Moolenaar 
523bd5e15fdSBram Moolenaar /* Load the standard Python exceptions - don't import the symbols from the
524bd5e15fdSBram Moolenaar  * DLL, as this can cause errors (importing data symbols is not reliable).
525bd5e15fdSBram Moolenaar  */
526bd5e15fdSBram Moolenaar static void get_py3_exceptions __ARGS((void));
527bd5e15fdSBram Moolenaar 
528170bf1aeSBram Moolenaar     static void
529170bf1aeSBram Moolenaar get_py3_exceptions()
530bd5e15fdSBram Moolenaar {
531bd5e15fdSBram Moolenaar     PyObject *exmod = PyImport_ImportModule("builtins");
532bd5e15fdSBram Moolenaar     PyObject *exdict = PyModule_GetDict(exmod);
533bd5e15fdSBram Moolenaar     p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
534bd5e15fdSBram Moolenaar     p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
535bd5e15fdSBram Moolenaar     p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
536bd5e15fdSBram Moolenaar     p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
537bd5e15fdSBram Moolenaar     p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
538bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_AttributeError);
539bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_IndexError);
540bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
541bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_TypeError);
542bd5e15fdSBram Moolenaar     Py_XINCREF(p3imp_PyExc_ValueError);
543bd5e15fdSBram Moolenaar     Py_XDECREF(exmod);
544bd5e15fdSBram Moolenaar }
545bd5e15fdSBram Moolenaar #endif /* DYNAMIC_PYTHON3 */
546bd5e15fdSBram Moolenaar 
547ca8a4dfeSBram Moolenaar static PyObject *BufferNew (buf_T *);
548ca8a4dfeSBram Moolenaar static PyObject *WindowNew(win_T *);
549ca8a4dfeSBram Moolenaar static PyObject *LineToString(const char *);
5507f85d297SBram Moolenaar static PyObject *BufferDir(PyObject *, PyObject *);
551ca8a4dfeSBram Moolenaar 
552ca8a4dfeSBram Moolenaar static PyTypeObject RangeType;
553ca8a4dfeSBram Moolenaar 
554db913953SBram Moolenaar static int py3initialised = 0;
555db913953SBram Moolenaar 
556db913953SBram Moolenaar #define PYINITIALISED py3initialised
557db913953SBram Moolenaar 
558cdab9051SBram Moolenaar #define DICTKEY_DECL PyObject *bytes = NULL;
559cdab9051SBram Moolenaar 
560db913953SBram Moolenaar #define DICTKEY_GET(err) \
561db913953SBram Moolenaar     if (PyBytes_Check(keyObject)) \
562cdab9051SBram Moolenaar     { \
563afa6b9afSBram Moolenaar 	if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
564cdab9051SBram Moolenaar 	    return err; \
565cdab9051SBram Moolenaar     } \
566db913953SBram Moolenaar     else if (PyUnicode_Check(keyObject)) \
567db913953SBram Moolenaar     { \
568db913953SBram Moolenaar 	bytes = PyString_AsBytes(keyObject); \
569db913953SBram Moolenaar 	if (bytes == NULL) \
570db913953SBram Moolenaar 	    return err; \
571afa6b9afSBram Moolenaar 	if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
572db913953SBram Moolenaar 	    return err; \
573db913953SBram Moolenaar     } \
574db913953SBram Moolenaar     else \
575db913953SBram Moolenaar     { \
576db913953SBram Moolenaar 	PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
577db913953SBram Moolenaar 	return err; \
578db913953SBram Moolenaar     }
579cdab9051SBram Moolenaar 
580db913953SBram Moolenaar #define DICTKEY_UNREF \
581db913953SBram Moolenaar     if (bytes != NULL) \
582db913953SBram Moolenaar 	Py_XDECREF(bytes);
583db913953SBram Moolenaar 
584170bf1aeSBram Moolenaar /*
585170bf1aeSBram Moolenaar  * Include the code shared with if_python.c
586170bf1aeSBram Moolenaar  */
587170bf1aeSBram Moolenaar #include "if_py_both.h"
588170bf1aeSBram Moolenaar 
589db913953SBram Moolenaar #define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
590db913953SBram Moolenaar 
591170bf1aeSBram Moolenaar     static void
592170bf1aeSBram Moolenaar call_PyObject_Free(void *p)
593bd5e15fdSBram Moolenaar {
594bd5e15fdSBram Moolenaar #ifdef Py_DEBUG
595bd5e15fdSBram Moolenaar     _PyObject_DebugFree(p);
596bd5e15fdSBram Moolenaar #else
597bd5e15fdSBram Moolenaar     PyObject_Free(p);
598bd5e15fdSBram Moolenaar #endif
599bd5e15fdSBram Moolenaar }
600170bf1aeSBram Moolenaar 
601170bf1aeSBram Moolenaar     static PyObject *
602170bf1aeSBram Moolenaar call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
603bd5e15fdSBram Moolenaar {
604bd5e15fdSBram Moolenaar     return PyType_GenericNew(type,args,kwds);
605bd5e15fdSBram Moolenaar }
606170bf1aeSBram Moolenaar 
607170bf1aeSBram Moolenaar     static PyObject *
608170bf1aeSBram Moolenaar call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
609bd5e15fdSBram Moolenaar {
610bd5e15fdSBram Moolenaar     return PyType_GenericAlloc(type,nitems);
611bd5e15fdSBram Moolenaar }
612bd5e15fdSBram Moolenaar 
613bd5e15fdSBram Moolenaar /******************************************************
614bd5e15fdSBram Moolenaar  * Internal function prototypes.
615bd5e15fdSBram Moolenaar  */
616bd5e15fdSBram Moolenaar 
617bd5e15fdSBram Moolenaar static Py_ssize_t RangeStart;
618bd5e15fdSBram Moolenaar static Py_ssize_t RangeEnd;
619bd5e15fdSBram Moolenaar 
620db913953SBram Moolenaar static PyObject *globals;
621db913953SBram Moolenaar 
622bd5e15fdSBram Moolenaar static int PythonIO_Init(void);
623bd5e15fdSBram Moolenaar static void PythonIO_Fini(void);
62469154f22SBram Moolenaar PyMODINIT_FUNC Py3Init_vim(void);
625bd5e15fdSBram Moolenaar 
626bd5e15fdSBram Moolenaar /******************************************************
627bd5e15fdSBram Moolenaar  * 1. Python interpreter main program.
628bd5e15fdSBram Moolenaar  */
629bd5e15fdSBram Moolenaar 
630bd5e15fdSBram Moolenaar static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
631bd5e15fdSBram Moolenaar 
632170bf1aeSBram Moolenaar     void
633170bf1aeSBram Moolenaar python3_end()
634bd5e15fdSBram Moolenaar {
635bd5e15fdSBram Moolenaar     static int recurse = 0;
636bd5e15fdSBram Moolenaar 
637bd5e15fdSBram Moolenaar     /* If a crash occurs while doing this, don't try again. */
638bd5e15fdSBram Moolenaar     if (recurse != 0)
639bd5e15fdSBram Moolenaar 	return;
640bd5e15fdSBram Moolenaar 
641bd5e15fdSBram Moolenaar     ++recurse;
642bd5e15fdSBram Moolenaar 
643bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
644bd5e15fdSBram Moolenaar     if (hinstPy3)
645bd5e15fdSBram Moolenaar #endif
646bd5e15fdSBram Moolenaar     if (Py_IsInitialized())
647bd5e15fdSBram Moolenaar     {
648bd5e15fdSBram Moolenaar 	// acquire lock before finalizing
649bd5e15fdSBram Moolenaar 	pygilstate = PyGILState_Ensure();
650bd5e15fdSBram Moolenaar 
651bd5e15fdSBram Moolenaar 	PythonIO_Fini();
652bd5e15fdSBram Moolenaar 	Py_Finalize();
653bd5e15fdSBram Moolenaar     }
654bd5e15fdSBram Moolenaar 
655bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
656bd5e15fdSBram Moolenaar     end_dynamic_python3();
657bd5e15fdSBram Moolenaar #endif
658bd5e15fdSBram Moolenaar 
659bd5e15fdSBram Moolenaar     --recurse;
660bd5e15fdSBram Moolenaar }
661bd5e15fdSBram Moolenaar 
6624c3a326cSBram Moolenaar #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
6634c3a326cSBram Moolenaar     int
6644c3a326cSBram Moolenaar python3_loaded()
6654c3a326cSBram Moolenaar {
6664c3a326cSBram Moolenaar     return (hinstPy3 != 0);
6674c3a326cSBram Moolenaar }
6684c3a326cSBram Moolenaar #endif
6694c3a326cSBram Moolenaar 
670170bf1aeSBram Moolenaar     static int
671170bf1aeSBram Moolenaar Python3_Init(void)
672bd5e15fdSBram Moolenaar {
673bd5e15fdSBram Moolenaar     if (!py3initialised)
674bd5e15fdSBram Moolenaar     {
675bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
676bd5e15fdSBram Moolenaar 	if (!python3_enabled(TRUE))
677bd5e15fdSBram Moolenaar 	{
678bd5e15fdSBram Moolenaar 	    EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
679bd5e15fdSBram Moolenaar 	    goto fail;
680bd5e15fdSBram Moolenaar 	}
681bd5e15fdSBram Moolenaar #endif
682bd5e15fdSBram Moolenaar 
683bd5e15fdSBram Moolenaar 	init_structs();
684bd5e15fdSBram Moolenaar 
685644d37b8SBram Moolenaar 
686644d37b8SBram Moolenaar #ifdef PYTHON3_HOME
687644d37b8SBram Moolenaar 	Py_SetPythonHome(PYTHON3_HOME);
688644d37b8SBram Moolenaar #endif
689644d37b8SBram Moolenaar 
690bd5e15fdSBram Moolenaar #if !defined(MACOS) || defined(MACOS_X_UNIX)
691bd5e15fdSBram Moolenaar 	Py_Initialize();
692bd5e15fdSBram Moolenaar #else
693bd5e15fdSBram Moolenaar 	PyMac_Initialize();
694bd5e15fdSBram Moolenaar #endif
695456f2bb2SBram Moolenaar 	/* initialise threads, must be after Py_Initialize() */
696456f2bb2SBram Moolenaar 	PyEval_InitThreads();
697bd5e15fdSBram Moolenaar 
698bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3
699bd5e15fdSBram Moolenaar 	get_py3_exceptions();
700bd5e15fdSBram Moolenaar #endif
701bd5e15fdSBram Moolenaar 
702bd5e15fdSBram Moolenaar 	if (PythonIO_Init())
703bd5e15fdSBram Moolenaar 	    goto fail;
704bd5e15fdSBram Moolenaar 
705bd5e15fdSBram Moolenaar 	PyImport_AppendInittab("vim", Py3Init_vim);
706bd5e15fdSBram Moolenaar 
707db913953SBram Moolenaar 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
708db913953SBram Moolenaar 
709bd5e15fdSBram Moolenaar 	/* Remove the element from sys.path that was added because of our
710bd5e15fdSBram Moolenaar 	 * argv[0] value in Py3Init_vim().  Previously we used an empty
711bd5e15fdSBram Moolenaar 	 * string, but dependinding on the OS we then get an empty entry or
71219e60943SBram Moolenaar 	 * the current directory in sys.path.
71319e60943SBram Moolenaar 	 * Only after vim has been imported, the element does exist in
71419e60943SBram Moolenaar 	 * sys.path.
71519e60943SBram Moolenaar 	 */
71619e60943SBram Moolenaar 	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
717bd5e15fdSBram Moolenaar 
718bd5e15fdSBram Moolenaar 	// lock is created and acquired in PyEval_InitThreads() and thread
719bd5e15fdSBram Moolenaar 	// state is created in Py_Initialize()
720bd5e15fdSBram Moolenaar 	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
721bd5e15fdSBram Moolenaar 	// (python must have threads enabled!)
722bd5e15fdSBram Moolenaar 	// so the following does both: unlock GIL and save thread state in TLS
723bd5e15fdSBram Moolenaar 	// without deleting thread state
724bd5e15fdSBram Moolenaar 	PyGILState_Release(pygilstate);
725bd5e15fdSBram Moolenaar 
726bd5e15fdSBram Moolenaar 	py3initialised = 1;
727bd5e15fdSBram Moolenaar     }
728bd5e15fdSBram Moolenaar 
729bd5e15fdSBram Moolenaar     return 0;
730bd5e15fdSBram Moolenaar 
731bd5e15fdSBram Moolenaar fail:
732bd5e15fdSBram Moolenaar     /* We call PythonIO_Flush() here to print any Python errors.
733bd5e15fdSBram Moolenaar      * This is OK, as it is possible to call this function even
734bd5e15fdSBram Moolenaar      * if PythonIO_Init() has not completed successfully (it will
735bd5e15fdSBram Moolenaar      * not do anything in this case).
736bd5e15fdSBram Moolenaar      */
737bd5e15fdSBram Moolenaar     PythonIO_Flush();
738bd5e15fdSBram Moolenaar     return -1;
739bd5e15fdSBram Moolenaar }
740bd5e15fdSBram Moolenaar 
741bd5e15fdSBram Moolenaar /*
742bd5e15fdSBram Moolenaar  * External interface
743bd5e15fdSBram Moolenaar  */
744170bf1aeSBram Moolenaar     static void
745db913953SBram Moolenaar DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
746bd5e15fdSBram Moolenaar {
747bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
748bd5e15fdSBram Moolenaar     GrafPtr		oldPort;
749bd5e15fdSBram Moolenaar #endif
750bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
751bd5e15fdSBram Moolenaar     char		*saved_locale;
752bd5e15fdSBram Moolenaar #endif
75319e60943SBram Moolenaar     PyObject		*cmdstr;
75419e60943SBram Moolenaar     PyObject		*cmdbytes;
755bd5e15fdSBram Moolenaar 
756bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
757bd5e15fdSBram Moolenaar     GetPort(&oldPort);
758bd5e15fdSBram Moolenaar     /* Check if the Python library is available */
759bd5e15fdSBram Moolenaar     if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
760bd5e15fdSBram Moolenaar 	goto theend;
761bd5e15fdSBram Moolenaar #endif
762bd5e15fdSBram Moolenaar     if (Python3_Init())
763bd5e15fdSBram Moolenaar 	goto theend;
764bd5e15fdSBram Moolenaar 
765db913953SBram Moolenaar     if (rettv == NULL)
766db913953SBram Moolenaar     {
767bd5e15fdSBram Moolenaar 	RangeStart = eap->line1;
768bd5e15fdSBram Moolenaar 	RangeEnd = eap->line2;
769db913953SBram Moolenaar     }
770db913953SBram Moolenaar     else
771db913953SBram Moolenaar     {
772db913953SBram Moolenaar 	RangeStart = (PyInt) curwin->w_cursor.lnum;
773db913953SBram Moolenaar 	RangeEnd = RangeStart;
774db913953SBram Moolenaar     }
775bd5e15fdSBram Moolenaar     Python_Release_Vim();	    /* leave vim */
776bd5e15fdSBram Moolenaar 
777bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
778bd5e15fdSBram Moolenaar     /* Python only works properly when the LC_NUMERIC locale is "C". */
779bd5e15fdSBram Moolenaar     saved_locale = setlocale(LC_NUMERIC, NULL);
780bd5e15fdSBram Moolenaar     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
781bd5e15fdSBram Moolenaar 	saved_locale = NULL;
782bd5e15fdSBram Moolenaar     else
783bd5e15fdSBram Moolenaar     {
784bd5e15fdSBram Moolenaar 	/* Need to make a copy, value may change when setting new locale. */
785bd5e15fdSBram Moolenaar 	saved_locale = (char *)vim_strsave((char_u *)saved_locale);
786bd5e15fdSBram Moolenaar 	(void)setlocale(LC_NUMERIC, "C");
787bd5e15fdSBram Moolenaar     }
788bd5e15fdSBram Moolenaar #endif
789bd5e15fdSBram Moolenaar 
790bd5e15fdSBram Moolenaar     pygilstate = PyGILState_Ensure();
791bd5e15fdSBram Moolenaar 
79219e60943SBram Moolenaar     /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
79319e60943SBram Moolenaar      * SyntaxError (unicode error). */
7943d64a317SBram Moolenaar     cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
7953d64a317SBram Moolenaar 					(char *)ENC_OPT, CODEC_ERROR_HANDLER);
7963d64a317SBram Moolenaar     cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
79719e60943SBram Moolenaar     Py_XDECREF(cmdstr);
798db913953SBram Moolenaar     if (rettv == NULL)
79919e60943SBram Moolenaar 	PyRun_SimpleString(PyBytes_AsString(cmdbytes));
800db913953SBram Moolenaar     else
801db913953SBram Moolenaar     {
802db913953SBram Moolenaar 	PyObject	*r;
803db913953SBram Moolenaar 
804db913953SBram Moolenaar 	r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
805db913953SBram Moolenaar 			 globals, globals);
806db913953SBram Moolenaar 	if (r == NULL)
807db913953SBram Moolenaar 	    EMSG(_("E860: Eval did not return a valid python 3 object"));
808db913953SBram Moolenaar 	else
809db913953SBram Moolenaar 	{
810db913953SBram Moolenaar 	    if (ConvertFromPyObject(r, rettv) == -1)
811db913953SBram Moolenaar 		EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
812db913953SBram Moolenaar 	    Py_DECREF(r);
813db913953SBram Moolenaar 	}
814db913953SBram Moolenaar 	PyErr_Clear();
815db913953SBram Moolenaar     }
81619e60943SBram Moolenaar     Py_XDECREF(cmdbytes);
817bd5e15fdSBram Moolenaar 
818bd5e15fdSBram Moolenaar     PyGILState_Release(pygilstate);
819bd5e15fdSBram Moolenaar 
820bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
821bd5e15fdSBram Moolenaar     if (saved_locale != NULL)
822bd5e15fdSBram Moolenaar     {
823bd5e15fdSBram Moolenaar 	(void)setlocale(LC_NUMERIC, saved_locale);
824bd5e15fdSBram Moolenaar 	vim_free(saved_locale);
825bd5e15fdSBram Moolenaar     }
826bd5e15fdSBram Moolenaar #endif
827bd5e15fdSBram Moolenaar 
828bd5e15fdSBram Moolenaar     Python_Lock_Vim();		    /* enter vim */
829bd5e15fdSBram Moolenaar     PythonIO_Flush();
830bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX)
831bd5e15fdSBram Moolenaar     SetPort(oldPort);
832bd5e15fdSBram Moolenaar #endif
833bd5e15fdSBram Moolenaar 
834bd5e15fdSBram Moolenaar theend:
835bd5e15fdSBram Moolenaar     return;	    /* keeps lint happy */
836bd5e15fdSBram Moolenaar }
837bd5e15fdSBram Moolenaar 
838bd5e15fdSBram Moolenaar /*
839368373e9SBram Moolenaar  * ":py3"
840bd5e15fdSBram Moolenaar  */
841170bf1aeSBram Moolenaar     void
842170bf1aeSBram Moolenaar ex_py3(exarg_T *eap)
843bd5e15fdSBram Moolenaar {
844bd5e15fdSBram Moolenaar     char_u *script;
845bd5e15fdSBram Moolenaar 
846bd5e15fdSBram Moolenaar     script = script_get(eap, eap->arg);
847bd5e15fdSBram Moolenaar     if (!eap->skip)
848bd5e15fdSBram Moolenaar     {
849bd5e15fdSBram Moolenaar 	if (script == NULL)
850db913953SBram Moolenaar 	    DoPy3Command(eap, (char *)eap->arg, NULL);
851bd5e15fdSBram Moolenaar 	else
852db913953SBram Moolenaar 	    DoPy3Command(eap, (char *)script, NULL);
853bd5e15fdSBram Moolenaar     }
854bd5e15fdSBram Moolenaar     vim_free(script);
855bd5e15fdSBram Moolenaar }
856bd5e15fdSBram Moolenaar 
857bd5e15fdSBram Moolenaar #define BUFFER_SIZE 2048
858bd5e15fdSBram Moolenaar 
859bd5e15fdSBram Moolenaar /*
8606df6f47dSBram Moolenaar  * ":py3file"
861bd5e15fdSBram Moolenaar  */
862bd5e15fdSBram Moolenaar     void
863bd5e15fdSBram Moolenaar ex_py3file(exarg_T *eap)
864bd5e15fdSBram Moolenaar {
865bd5e15fdSBram Moolenaar     static char buffer[BUFFER_SIZE];
866bd5e15fdSBram Moolenaar     const char *file;
867bd5e15fdSBram Moolenaar     char *p;
868bd5e15fdSBram Moolenaar     int i;
869bd5e15fdSBram Moolenaar 
870bd5e15fdSBram Moolenaar     /* Have to do it like this. PyRun_SimpleFile requires you to pass a
871bd5e15fdSBram Moolenaar      * stdio file pointer, but Vim and the Python DLL are compiled with
872bd5e15fdSBram Moolenaar      * different options under Windows, meaning that stdio pointers aren't
873bd5e15fdSBram Moolenaar      * compatible between the two. Yuk.
874bd5e15fdSBram Moolenaar      *
87519e60943SBram Moolenaar      * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
87619e60943SBram Moolenaar      *
87719e60943SBram Moolenaar      * Using bytes so that Python can detect the source encoding as it normally
87819e60943SBram Moolenaar      * does. The doc does not say "compile" accept bytes, though.
879bd5e15fdSBram Moolenaar      *
880bd5e15fdSBram Moolenaar      * We need to escape any backslashes or single quotes in the file name, so that
881bd5e15fdSBram Moolenaar      * Python won't mangle the file name.
882bd5e15fdSBram Moolenaar      */
883bd5e15fdSBram Moolenaar 
884bd5e15fdSBram Moolenaar     strcpy(buffer, "exec(compile(open('");
885bd5e15fdSBram Moolenaar     p = buffer + 19; /* size of "exec(compile(open('" */
886bd5e15fdSBram Moolenaar 
887bd5e15fdSBram Moolenaar     for (i=0; i<2; ++i)
888bd5e15fdSBram Moolenaar     {
889bd5e15fdSBram Moolenaar 	file = (char *)eap->arg;
890bd5e15fdSBram Moolenaar 	while (*file && p < buffer + (BUFFER_SIZE - 3))
891bd5e15fdSBram Moolenaar 	{
892bd5e15fdSBram Moolenaar 	    if (*file == '\\' || *file == '\'')
893bd5e15fdSBram Moolenaar 		*p++ = '\\';
894bd5e15fdSBram Moolenaar 	    *p++ = *file++;
895bd5e15fdSBram Moolenaar 	}
896bd5e15fdSBram Moolenaar 	/* If we didn't finish the file name, we hit a buffer overflow */
897bd5e15fdSBram Moolenaar 	if (*file != '\0')
898bd5e15fdSBram Moolenaar 	    return;
899bd5e15fdSBram Moolenaar 	if (i==0)
900bd5e15fdSBram Moolenaar 	{
90119e60943SBram Moolenaar 	    strcpy(p,"','rb').read(),'");
90219e60943SBram Moolenaar 	    p += 16;
903bd5e15fdSBram Moolenaar 	}
904bd5e15fdSBram Moolenaar 	else
905bd5e15fdSBram Moolenaar 	{
906bd5e15fdSBram Moolenaar 	    strcpy(p,"','exec'))");
907bd5e15fdSBram Moolenaar 	    p += 10;
908bd5e15fdSBram Moolenaar 	}
909bd5e15fdSBram Moolenaar     }
910bd5e15fdSBram Moolenaar 
911bd5e15fdSBram Moolenaar 
912bd5e15fdSBram Moolenaar     /* Execute the file */
913db913953SBram Moolenaar     DoPy3Command(eap, buffer, NULL);
914bd5e15fdSBram Moolenaar }
915bd5e15fdSBram Moolenaar 
916bd5e15fdSBram Moolenaar /******************************************************
917bd5e15fdSBram Moolenaar  * 2. Python output stream: writes output via [e]msg().
918bd5e15fdSBram Moolenaar  */
919bd5e15fdSBram Moolenaar 
920bd5e15fdSBram Moolenaar /* Implementation functions
921bd5e15fdSBram Moolenaar  */
922bd5e15fdSBram Moolenaar 
923170bf1aeSBram Moolenaar     static PyObject *
924170bf1aeSBram Moolenaar OutputGetattro(PyObject *self, PyObject *nameobj)
925bd5e15fdSBram Moolenaar {
926bd5e15fdSBram Moolenaar     char *name = "";
927bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
928bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
929bd5e15fdSBram Moolenaar 
930bd5e15fdSBram Moolenaar     if (strcmp(name, "softspace") == 0)
931bd5e15fdSBram Moolenaar 	return PyLong_FromLong(((OutputObject *)(self))->softspace);
932bd5e15fdSBram Moolenaar 
933bd5e15fdSBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
934bd5e15fdSBram Moolenaar }
935bd5e15fdSBram Moolenaar 
936170bf1aeSBram Moolenaar     static int
937170bf1aeSBram Moolenaar OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
938bd5e15fdSBram Moolenaar {
939bd5e15fdSBram Moolenaar     char *name = "";
940bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
941bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
942bd5e15fdSBram Moolenaar 
943db913953SBram Moolenaar     if (val == NULL)
944db913953SBram Moolenaar     {
945bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
946bd5e15fdSBram Moolenaar 	return -1;
947bd5e15fdSBram Moolenaar     }
948bd5e15fdSBram Moolenaar 
949bd5e15fdSBram Moolenaar     if (strcmp(name, "softspace") == 0)
950bd5e15fdSBram Moolenaar     {
951db913953SBram Moolenaar 	if (!PyLong_Check(val))
952db913953SBram Moolenaar 	{
953bd5e15fdSBram Moolenaar 	    PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
954bd5e15fdSBram Moolenaar 	    return -1;
955bd5e15fdSBram Moolenaar 	}
956bd5e15fdSBram Moolenaar 
957bd5e15fdSBram Moolenaar 	((OutputObject *)(self))->softspace = PyLong_AsLong(val);
958bd5e15fdSBram Moolenaar 	return 0;
959bd5e15fdSBram Moolenaar     }
960bd5e15fdSBram Moolenaar 
961bd5e15fdSBram Moolenaar     PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
962bd5e15fdSBram Moolenaar     return -1;
963bd5e15fdSBram Moolenaar }
964bd5e15fdSBram Moolenaar 
965bd5e15fdSBram Moolenaar /***************/
966bd5e15fdSBram Moolenaar 
967170bf1aeSBram Moolenaar     static int
968170bf1aeSBram Moolenaar PythonIO_Init(void)
969bd5e15fdSBram Moolenaar {
970bd5e15fdSBram Moolenaar     PyType_Ready(&OutputType);
971170bf1aeSBram Moolenaar     return PythonIO_Init_io();
972bd5e15fdSBram Moolenaar }
973bd5e15fdSBram Moolenaar 
974170bf1aeSBram Moolenaar     static void
975170bf1aeSBram Moolenaar PythonIO_Fini(void)
976bd5e15fdSBram Moolenaar {
977bd5e15fdSBram Moolenaar     PySys_SetObject("stdout", NULL);
978bd5e15fdSBram Moolenaar     PySys_SetObject("stderr", NULL);
979bd5e15fdSBram Moolenaar }
980bd5e15fdSBram Moolenaar 
981bd5e15fdSBram Moolenaar /******************************************************
982bd5e15fdSBram Moolenaar  * 3. Implementation of the Vim module for Python
983bd5e15fdSBram Moolenaar  */
984bd5e15fdSBram Moolenaar 
985bd5e15fdSBram Moolenaar /* Window type - Implementation functions
986bd5e15fdSBram Moolenaar  * --------------------------------------
987bd5e15fdSBram Moolenaar  */
988bd5e15fdSBram Moolenaar 
989bd5e15fdSBram Moolenaar #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
990bd5e15fdSBram Moolenaar 
991bd5e15fdSBram Moolenaar /* Buffer type - Implementation functions
992bd5e15fdSBram Moolenaar  * --------------------------------------
993bd5e15fdSBram Moolenaar  */
994bd5e15fdSBram Moolenaar 
995bd5e15fdSBram Moolenaar #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
996bd5e15fdSBram Moolenaar 
997bd5e15fdSBram Moolenaar static Py_ssize_t BufferLength(PyObject *);
998bd5e15fdSBram Moolenaar static PyObject *BufferItem(PyObject *, Py_ssize_t);
999bd5e15fdSBram Moolenaar static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
100019e60943SBram Moolenaar static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
1001bd5e15fdSBram Moolenaar 
1002bd5e15fdSBram Moolenaar 
1003bd5e15fdSBram Moolenaar /* Line range type - Implementation functions
1004bd5e15fdSBram Moolenaar  * --------------------------------------
1005bd5e15fdSBram Moolenaar  */
1006bd5e15fdSBram Moolenaar 
1007bd5e15fdSBram Moolenaar #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1008bd5e15fdSBram Moolenaar 
1009bd5e15fdSBram Moolenaar static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
1010bd5e15fdSBram Moolenaar static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1011ba4897e6SBram Moolenaar static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
1012bd5e15fdSBram Moolenaar 
1013bd5e15fdSBram Moolenaar /* Current objects type - Implementation functions
1014bd5e15fdSBram Moolenaar  * -----------------------------------------------
1015bd5e15fdSBram Moolenaar  */
1016bd5e15fdSBram Moolenaar 
1017bd5e15fdSBram Moolenaar static PySequenceMethods BufferAsSeq = {
1018bd5e15fdSBram Moolenaar     (lenfunc)		BufferLength,	    /* sq_length,    len(x)   */
1019bd5e15fdSBram Moolenaar     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1020bd5e15fdSBram Moolenaar     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1021bd5e15fdSBram Moolenaar     (ssizeargfunc)	BufferItem,	    /* sq_item,      x[i]     */
1022bd5e15fdSBram Moolenaar     0,					    /* was_sq_slice,	 x[i:j]   */
102319e60943SBram Moolenaar     0,					    /* sq_ass_item,  x[i]=v   */
1024bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1025bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1026bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1027bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1028bd5e15fdSBram Moolenaar };
1029bd5e15fdSBram Moolenaar 
1030bd5e15fdSBram Moolenaar PyMappingMethods BufferAsMapping = {
1031bd5e15fdSBram Moolenaar     /* mp_length	*/ (lenfunc)BufferLength,
1032bd5e15fdSBram Moolenaar     /* mp_subscript     */ (binaryfunc)BufferSubscript,
103319e60943SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
1034bd5e15fdSBram Moolenaar };
1035bd5e15fdSBram Moolenaar 
1036bd5e15fdSBram Moolenaar 
1037bd5e15fdSBram Moolenaar /* Buffer object - Definitions
1038bd5e15fdSBram Moolenaar  */
1039bd5e15fdSBram Moolenaar 
1040bd5e15fdSBram Moolenaar static PyTypeObject BufferType;
1041bd5e15fdSBram Moolenaar 
1042170bf1aeSBram Moolenaar     static PyObject *
1043170bf1aeSBram Moolenaar BufferNew(buf_T *buf)
1044bd5e15fdSBram Moolenaar {
1045bd5e15fdSBram Moolenaar     /* We need to handle deletion of buffers underneath us.
1046bd5e15fdSBram Moolenaar      * If we add a "b_python3_ref" field to the buf_T structure,
1047bd5e15fdSBram Moolenaar      * then we can get at it in buf_freeall() in vim. We then
1048bd5e15fdSBram Moolenaar      * need to create only ONE Python object per buffer - if
1049bd5e15fdSBram Moolenaar      * we try to create a second, just INCREF the existing one
1050bd5e15fdSBram Moolenaar      * and return it. The (single) Python object referring to
1051bd5e15fdSBram Moolenaar      * the buffer is stored in "b_python3_ref".
1052bd5e15fdSBram Moolenaar      * Question: what to do on a buf_freeall(). We'll probably
1053bd5e15fdSBram Moolenaar      * have to either delete the Python object (DECREF it to
1054bd5e15fdSBram Moolenaar      * zero - a bad idea, as it leaves dangling refs!) or
1055bd5e15fdSBram Moolenaar      * set the buf_T * value to an invalid value (-1?), which
1056bd5e15fdSBram Moolenaar      * means we need checks in all access functions... Bah.
1057bd5e15fdSBram Moolenaar      */
1058bd5e15fdSBram Moolenaar 
1059bd5e15fdSBram Moolenaar     BufferObject *self;
1060bd5e15fdSBram Moolenaar 
1061bd5e15fdSBram Moolenaar     if (buf->b_python3_ref != NULL)
1062bd5e15fdSBram Moolenaar     {
1063bd5e15fdSBram Moolenaar 	self = buf->b_python3_ref;
1064bd5e15fdSBram Moolenaar 	Py_INCREF(self);
1065bd5e15fdSBram Moolenaar     }
1066bd5e15fdSBram Moolenaar     else
1067bd5e15fdSBram Moolenaar     {
1068bd5e15fdSBram Moolenaar 	self = PyObject_NEW(BufferObject, &BufferType);
1069bd5e15fdSBram Moolenaar 	buf->b_python3_ref = self;
1070bd5e15fdSBram Moolenaar 	if (self == NULL)
1071bd5e15fdSBram Moolenaar 	    return NULL;
1072bd5e15fdSBram Moolenaar 	self->buf = buf;
1073bd5e15fdSBram Moolenaar     }
1074bd5e15fdSBram Moolenaar 
1075bd5e15fdSBram Moolenaar     return (PyObject *)(self);
1076bd5e15fdSBram Moolenaar }
1077bd5e15fdSBram Moolenaar 
1078170bf1aeSBram Moolenaar     static void
1079170bf1aeSBram Moolenaar BufferDestructor(PyObject *self)
1080bd5e15fdSBram Moolenaar {
1081bd5e15fdSBram Moolenaar     BufferObject *this = (BufferObject *)(self);
1082bd5e15fdSBram Moolenaar 
1083bd5e15fdSBram Moolenaar     if (this->buf && this->buf != INVALID_BUFFER_VALUE)
1084bd5e15fdSBram Moolenaar 	this->buf->b_python3_ref = NULL;
108519e60943SBram Moolenaar 
108619e60943SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1087bd5e15fdSBram Moolenaar }
1088bd5e15fdSBram Moolenaar 
1089170bf1aeSBram Moolenaar     static PyObject *
1090170bf1aeSBram Moolenaar BufferGetattro(PyObject *self, PyObject*nameobj)
1091bd5e15fdSBram Moolenaar {
1092bd5e15fdSBram Moolenaar     BufferObject *this = (BufferObject *)(self);
1093bd5e15fdSBram Moolenaar 
1094bd5e15fdSBram Moolenaar     char *name = "";
1095bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
1096bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1097bd5e15fdSBram Moolenaar 
1098bd5e15fdSBram Moolenaar     if (CheckBuffer(this))
1099bd5e15fdSBram Moolenaar 	return NULL;
1100bd5e15fdSBram Moolenaar 
1101bd5e15fdSBram Moolenaar     if (strcmp(name, "name") == 0)
1102bd5e15fdSBram Moolenaar 	return Py_BuildValue("s", this->buf->b_ffname);
1103bd5e15fdSBram Moolenaar     else if (strcmp(name, "number") == 0)
1104bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", this->buf->b_fnum);
1105bd5e15fdSBram Moolenaar     else
1106bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1107bd5e15fdSBram Moolenaar }
1108bd5e15fdSBram Moolenaar 
1109170bf1aeSBram Moolenaar     static PyObject *
11107f85d297SBram Moolenaar BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
11117f85d297SBram Moolenaar {
11127f85d297SBram Moolenaar     return Py_BuildValue("[sssss]", "name", "number",
11137f85d297SBram Moolenaar 						   "append", "mark", "range");
11147f85d297SBram Moolenaar }
11157f85d297SBram Moolenaar 
11167f85d297SBram Moolenaar     static PyObject *
1117170bf1aeSBram Moolenaar BufferRepr(PyObject *self)
1118bd5e15fdSBram Moolenaar {
1119bd5e15fdSBram Moolenaar     static char repr[100];
1120bd5e15fdSBram Moolenaar     BufferObject *this = (BufferObject *)(self);
1121bd5e15fdSBram Moolenaar 
1122bd5e15fdSBram Moolenaar     if (this->buf == INVALID_BUFFER_VALUE)
1123bd5e15fdSBram Moolenaar     {
1124bd5e15fdSBram Moolenaar 	vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1125bd5e15fdSBram Moolenaar 	return PyUnicode_FromString(repr);
1126bd5e15fdSBram Moolenaar     }
1127bd5e15fdSBram Moolenaar     else
1128bd5e15fdSBram Moolenaar     {
1129bd5e15fdSBram Moolenaar 	char *name = (char *)this->buf->b_fname;
1130bd5e15fdSBram Moolenaar 	Py_ssize_t len;
1131bd5e15fdSBram Moolenaar 
1132bd5e15fdSBram Moolenaar 	if (name == NULL)
1133bd5e15fdSBram Moolenaar 	    name = "";
1134bd5e15fdSBram Moolenaar 	len = strlen(name);
1135bd5e15fdSBram Moolenaar 
1136bd5e15fdSBram Moolenaar 	if (len > 35)
1137bd5e15fdSBram Moolenaar 	    name = name + (35 - len);
1138bd5e15fdSBram Moolenaar 
1139bd5e15fdSBram Moolenaar 	vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
1140bd5e15fdSBram Moolenaar 
1141bd5e15fdSBram Moolenaar 	return PyUnicode_FromString(repr);
1142bd5e15fdSBram Moolenaar     }
1143bd5e15fdSBram Moolenaar }
1144bd5e15fdSBram Moolenaar 
1145bd5e15fdSBram Moolenaar /******************/
1146bd5e15fdSBram Moolenaar 
1147170bf1aeSBram Moolenaar     static Py_ssize_t
1148170bf1aeSBram Moolenaar BufferLength(PyObject *self)
1149bd5e15fdSBram Moolenaar {
1150bd5e15fdSBram Moolenaar     if (CheckBuffer((BufferObject *)(self)))
1151bd5e15fdSBram Moolenaar 	return -1;
1152bd5e15fdSBram Moolenaar 
1153bd5e15fdSBram Moolenaar     return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1154bd5e15fdSBram Moolenaar }
1155bd5e15fdSBram Moolenaar 
1156170bf1aeSBram Moolenaar     static PyObject *
1157170bf1aeSBram Moolenaar BufferItem(PyObject *self, Py_ssize_t n)
1158bd5e15fdSBram Moolenaar {
1159bd5e15fdSBram Moolenaar     return RBItem((BufferObject *)(self), n, 1,
1160bd5e15fdSBram Moolenaar 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1161bd5e15fdSBram Moolenaar }
1162bd5e15fdSBram Moolenaar 
1163170bf1aeSBram Moolenaar     static PyObject *
1164170bf1aeSBram Moolenaar BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1165170bf1aeSBram Moolenaar {
1166170bf1aeSBram Moolenaar     return RBSlice((BufferObject *)(self), lo, hi, 1,
1167170bf1aeSBram Moolenaar 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1168170bf1aeSBram Moolenaar }
1169170bf1aeSBram Moolenaar 
1170170bf1aeSBram Moolenaar     static PyObject *
1171170bf1aeSBram Moolenaar BufferSubscript(PyObject *self, PyObject* idx)
1172bd5e15fdSBram Moolenaar {
1173db913953SBram Moolenaar     if (PyLong_Check(idx))
1174db913953SBram Moolenaar     {
1175bd5e15fdSBram Moolenaar 	long _idx = PyLong_AsLong(idx);
1176bd5e15fdSBram Moolenaar 	return BufferItem(self,_idx);
1177db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1178db913953SBram Moolenaar     {
1179bd5e15fdSBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1180bd5e15fdSBram Moolenaar 
11819e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1182bd5e15fdSBram Moolenaar 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1183bd5e15fdSBram Moolenaar 	      &start, &stop,
1184db913953SBram Moolenaar 	      &step, &slicelen) < 0)
1185db913953SBram Moolenaar 	{
1186bd5e15fdSBram Moolenaar 	    return NULL;
1187bd5e15fdSBram Moolenaar 	}
118819e60943SBram Moolenaar 	return BufferSlice(self, start, stop);
1189db913953SBram Moolenaar     }
1190db913953SBram Moolenaar     else
1191db913953SBram Moolenaar     {
1192bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1193bd5e15fdSBram Moolenaar 	return NULL;
1194bd5e15fdSBram Moolenaar     }
1195bd5e15fdSBram Moolenaar }
1196bd5e15fdSBram Moolenaar 
119719e60943SBram Moolenaar     static Py_ssize_t
119819e60943SBram Moolenaar BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
119919e60943SBram Moolenaar {
1200db913953SBram Moolenaar     if (PyLong_Check(idx))
1201db913953SBram Moolenaar     {
120219e60943SBram Moolenaar 	long n = PyLong_AsLong(idx);
120319e60943SBram Moolenaar 	return RBAsItem((BufferObject *)(self), n, val, 1,
120419e60943SBram Moolenaar 		    (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
120519e60943SBram Moolenaar 		    NULL);
1206db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1207db913953SBram Moolenaar     {
120819e60943SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
120919e60943SBram Moolenaar 
12109e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
121119e60943SBram Moolenaar 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
121219e60943SBram Moolenaar 	      &start, &stop,
1213db913953SBram Moolenaar 	      &step, &slicelen) < 0)
1214db913953SBram Moolenaar 	{
121519e60943SBram Moolenaar 	    return -1;
121619e60943SBram Moolenaar 	}
121719e60943SBram Moolenaar 	return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
121819e60943SBram Moolenaar 			  (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
121919e60943SBram Moolenaar 			  NULL);
1220db913953SBram Moolenaar     }
1221db913953SBram Moolenaar     else
1222db913953SBram Moolenaar     {
122319e60943SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
122419e60943SBram Moolenaar 	return -1;
122519e60943SBram Moolenaar     }
122619e60943SBram Moolenaar }
122719e60943SBram Moolenaar 
1228bd5e15fdSBram Moolenaar static PySequenceMethods RangeAsSeq = {
1229bd5e15fdSBram Moolenaar     (lenfunc)		RangeLength,	 /* sq_length,	  len(x)   */
123055d5c034SBram Moolenaar     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
123155d5c034SBram Moolenaar     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1232bd5e15fdSBram Moolenaar     (ssizeargfunc)	RangeItem,	 /* sq_item,	  x[i]	   */
1233bd5e15fdSBram Moolenaar     0,					 /* was_sq_slice,     x[i:j]   */
1234bd5e15fdSBram Moolenaar     (ssizeobjargproc)	RangeAsItem,	 /* sq_as_item,  x[i]=v   */
1235bd5e15fdSBram Moolenaar     0,					 /* sq_ass_slice, x[i:j]=v */
1236bd5e15fdSBram Moolenaar     0,					 /* sq_contains */
1237bd5e15fdSBram Moolenaar     0,					 /* sq_inplace_concat */
1238bd5e15fdSBram Moolenaar     0,					 /* sq_inplace_repeat */
1239bd5e15fdSBram Moolenaar };
1240bd5e15fdSBram Moolenaar 
1241bd5e15fdSBram Moolenaar PyMappingMethods RangeAsMapping = {
1242bd5e15fdSBram Moolenaar     /* mp_length	*/ (lenfunc)RangeLength,
1243bd5e15fdSBram Moolenaar     /* mp_subscript     */ (binaryfunc)RangeSubscript,
1244ba4897e6SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
1245bd5e15fdSBram Moolenaar };
1246bd5e15fdSBram Moolenaar 
1247bd5e15fdSBram Moolenaar /* Line range object - Implementation
1248bd5e15fdSBram Moolenaar  */
1249bd5e15fdSBram Moolenaar 
1250170bf1aeSBram Moolenaar     static void
1251170bf1aeSBram Moolenaar RangeDestructor(PyObject *self)
1252bd5e15fdSBram Moolenaar {
1253bd5e15fdSBram Moolenaar     Py_DECREF(((RangeObject *)(self))->buf);
125419e60943SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1255bd5e15fdSBram Moolenaar }
1256bd5e15fdSBram Moolenaar 
1257170bf1aeSBram Moolenaar     static PyObject *
1258170bf1aeSBram Moolenaar RangeGetattro(PyObject *self, PyObject *nameobj)
1259bd5e15fdSBram Moolenaar {
1260bd5e15fdSBram Moolenaar     char *name = "";
1261bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
1262bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1263bd5e15fdSBram Moolenaar 
1264bd5e15fdSBram Moolenaar     if (strcmp(name, "start") == 0)
1265bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
1266bd5e15fdSBram Moolenaar     else if (strcmp(name, "end") == 0)
1267bd5e15fdSBram Moolenaar 	return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
1268bd5e15fdSBram Moolenaar     else
1269bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1270bd5e15fdSBram Moolenaar }
1271bd5e15fdSBram Moolenaar 
1272bd5e15fdSBram Moolenaar /****************/
1273bd5e15fdSBram Moolenaar 
1274170bf1aeSBram Moolenaar     static Py_ssize_t
1275170bf1aeSBram Moolenaar RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
1276bd5e15fdSBram Moolenaar {
1277bd5e15fdSBram Moolenaar     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1278bd5e15fdSBram Moolenaar 		    ((RangeObject *)(self))->start,
1279bd5e15fdSBram Moolenaar 		    ((RangeObject *)(self))->end,
1280bd5e15fdSBram Moolenaar 		    &((RangeObject *)(self))->end);
1281bd5e15fdSBram Moolenaar }
1282bd5e15fdSBram Moolenaar 
1283ba4897e6SBram Moolenaar     static Py_ssize_t
1284ba4897e6SBram Moolenaar RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1285ba4897e6SBram Moolenaar {
1286ba4897e6SBram Moolenaar     return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1287ba4897e6SBram Moolenaar 		    ((RangeObject *)(self))->start,
1288ba4897e6SBram Moolenaar 		    ((RangeObject *)(self))->end,
1289ba4897e6SBram Moolenaar 		    &((RangeObject *)(self))->end);
1290ba4897e6SBram Moolenaar }
1291ba4897e6SBram Moolenaar 
1292170bf1aeSBram Moolenaar     static PyObject *
1293170bf1aeSBram Moolenaar RangeSubscript(PyObject *self, PyObject* idx)
1294bd5e15fdSBram Moolenaar {
1295db913953SBram Moolenaar     if (PyLong_Check(idx))
1296db913953SBram Moolenaar     {
1297bd5e15fdSBram Moolenaar 	long _idx = PyLong_AsLong(idx);
1298bd5e15fdSBram Moolenaar 	return RangeItem(self,_idx);
1299db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1300db913953SBram Moolenaar     {
1301bd5e15fdSBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1302bd5e15fdSBram Moolenaar 
13039e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1304bd5e15fdSBram Moolenaar 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1305bd5e15fdSBram Moolenaar 		&start, &stop,
1306db913953SBram Moolenaar 		&step, &slicelen) < 0)
1307db913953SBram Moolenaar 	{
1308bd5e15fdSBram Moolenaar 	    return NULL;
1309bd5e15fdSBram Moolenaar 	}
1310ba4897e6SBram Moolenaar 	return RangeSlice(self, start, stop);
1311db913953SBram Moolenaar     }
1312db913953SBram Moolenaar     else
1313db913953SBram Moolenaar     {
1314bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1315bd5e15fdSBram Moolenaar 	return NULL;
1316bd5e15fdSBram Moolenaar     }
1317bd5e15fdSBram Moolenaar }
1318bd5e15fdSBram Moolenaar 
1319ba4897e6SBram Moolenaar     static Py_ssize_t
1320ba4897e6SBram Moolenaar RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1321ba4897e6SBram Moolenaar {
1322db913953SBram Moolenaar     if (PyLong_Check(idx))
1323db913953SBram Moolenaar     {
1324ba4897e6SBram Moolenaar 	long n = PyLong_AsLong(idx);
1325ba4897e6SBram Moolenaar 	return RangeAsItem(self, n, val);
1326db913953SBram Moolenaar     } else if (PySlice_Check(idx))
1327db913953SBram Moolenaar     {
1328ba4897e6SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1329ba4897e6SBram Moolenaar 
13309e8edf6eSBram Moolenaar 	if (PySlice_GetIndicesEx((PyObject *)idx,
1331ba4897e6SBram Moolenaar 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1332ba4897e6SBram Moolenaar 		&start, &stop,
1333db913953SBram Moolenaar 		&step, &slicelen) < 0)
1334db913953SBram Moolenaar 	{
1335ba4897e6SBram Moolenaar 	    return -1;
1336ba4897e6SBram Moolenaar 	}
1337ba4897e6SBram Moolenaar 	return RangeAsSlice(self, start, stop, val);
1338db913953SBram Moolenaar     }
1339db913953SBram Moolenaar     else
1340db913953SBram Moolenaar     {
1341ba4897e6SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1342ba4897e6SBram Moolenaar 	return -1;
1343ba4897e6SBram Moolenaar     }
1344ba4897e6SBram Moolenaar }
1345ba4897e6SBram Moolenaar 
1346ba4897e6SBram Moolenaar 
1347bd5e15fdSBram Moolenaar /* Buffer list object - Definitions
1348bd5e15fdSBram Moolenaar  */
1349bd5e15fdSBram Moolenaar 
1350bd5e15fdSBram Moolenaar typedef struct
1351bd5e15fdSBram Moolenaar {
1352bd5e15fdSBram Moolenaar     PyObject_HEAD
1353ca8a4dfeSBram Moolenaar } BufListObject;
1354bd5e15fdSBram Moolenaar 
1355bd5e15fdSBram Moolenaar static PySequenceMethods BufListAsSeq = {
1356bd5e15fdSBram Moolenaar     (lenfunc)		BufListLength,	    /* sq_length,    len(x)   */
1357bd5e15fdSBram Moolenaar     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1358bd5e15fdSBram Moolenaar     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1359bd5e15fdSBram Moolenaar     (ssizeargfunc)	BufListItem,	    /* sq_item,      x[i]     */
1360bd5e15fdSBram Moolenaar     0,					    /* was_sq_slice,	 x[i:j]   */
1361bd5e15fdSBram Moolenaar     (ssizeobjargproc)	0,		    /* sq_as_item,  x[i]=v   */
1362bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1363bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1364bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1365bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1366bd5e15fdSBram Moolenaar };
1367bd5e15fdSBram Moolenaar 
1368bd5e15fdSBram Moolenaar static PyTypeObject BufListType;
1369bd5e15fdSBram Moolenaar 
1370bd5e15fdSBram Moolenaar /* Window object - Definitions
1371bd5e15fdSBram Moolenaar  */
1372bd5e15fdSBram Moolenaar 
1373bd5e15fdSBram Moolenaar static struct PyMethodDef WindowMethods[] = {
1374bd5e15fdSBram Moolenaar     /* name,	    function,		calling,    documentation */
1375bd5e15fdSBram Moolenaar     { NULL,	    NULL,		0,	    NULL }
1376bd5e15fdSBram Moolenaar };
1377bd5e15fdSBram Moolenaar 
137855d5c034SBram Moolenaar static PyTypeObject WindowType;
1379bd5e15fdSBram Moolenaar 
1380bd5e15fdSBram Moolenaar /* Window object - Implementation
1381bd5e15fdSBram Moolenaar  */
1382bd5e15fdSBram Moolenaar 
1383170bf1aeSBram Moolenaar     static PyObject *
1384170bf1aeSBram Moolenaar WindowNew(win_T *win)
1385bd5e15fdSBram Moolenaar {
1386bd5e15fdSBram Moolenaar     /* We need to handle deletion of windows underneath us.
1387bd5e15fdSBram Moolenaar      * If we add a "w_python3_ref" field to the win_T structure,
1388bd5e15fdSBram Moolenaar      * then we can get at it in win_free() in vim. We then
1389bd5e15fdSBram Moolenaar      * need to create only ONE Python object per window - if
1390bd5e15fdSBram Moolenaar      * we try to create a second, just INCREF the existing one
1391bd5e15fdSBram Moolenaar      * and return it. The (single) Python object referring to
1392bd5e15fdSBram Moolenaar      * the window is stored in "w_python3_ref".
1393bd5e15fdSBram Moolenaar      * On a win_free() we set the Python object's win_T* field
1394bd5e15fdSBram Moolenaar      * to an invalid value. We trap all uses of a window
1395bd5e15fdSBram Moolenaar      * object, and reject them if the win_T* field is invalid.
1396bd5e15fdSBram Moolenaar      */
1397bd5e15fdSBram Moolenaar 
1398bd5e15fdSBram Moolenaar     WindowObject *self;
1399bd5e15fdSBram Moolenaar 
1400bd5e15fdSBram Moolenaar     if (win->w_python3_ref)
1401bd5e15fdSBram Moolenaar     {
1402bd5e15fdSBram Moolenaar 	self = win->w_python3_ref;
1403bd5e15fdSBram Moolenaar 	Py_INCREF(self);
1404bd5e15fdSBram Moolenaar     }
1405bd5e15fdSBram Moolenaar     else
1406bd5e15fdSBram Moolenaar     {
1407bd5e15fdSBram Moolenaar 	self = PyObject_NEW(WindowObject, &WindowType);
1408bd5e15fdSBram Moolenaar 	if (self == NULL)
1409bd5e15fdSBram Moolenaar 	    return NULL;
1410bd5e15fdSBram Moolenaar 	self->win = win;
1411bd5e15fdSBram Moolenaar 	win->w_python3_ref = self;
1412bd5e15fdSBram Moolenaar     }
1413bd5e15fdSBram Moolenaar 
1414bd5e15fdSBram Moolenaar     return (PyObject *)(self);
1415bd5e15fdSBram Moolenaar }
1416bd5e15fdSBram Moolenaar 
1417170bf1aeSBram Moolenaar     static void
1418170bf1aeSBram Moolenaar WindowDestructor(PyObject *self)
1419bd5e15fdSBram Moolenaar {
1420bd5e15fdSBram Moolenaar     WindowObject *this = (WindowObject *)(self);
1421bd5e15fdSBram Moolenaar 
1422bd5e15fdSBram Moolenaar     if (this->win && this->win != INVALID_WINDOW_VALUE)
1423bd5e15fdSBram Moolenaar 	this->win->w_python3_ref = NULL;
142419e60943SBram Moolenaar 
142519e60943SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1426bd5e15fdSBram Moolenaar }
1427bd5e15fdSBram Moolenaar 
1428170bf1aeSBram Moolenaar     static PyObject *
1429170bf1aeSBram Moolenaar WindowGetattro(PyObject *self, PyObject *nameobj)
1430bd5e15fdSBram Moolenaar {
1431bd5e15fdSBram Moolenaar     WindowObject *this = (WindowObject *)(self);
1432bd5e15fdSBram Moolenaar 
1433bd5e15fdSBram Moolenaar     char *name = "";
1434bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
1435bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1436bd5e15fdSBram Moolenaar 
1437bd5e15fdSBram Moolenaar 
1438bd5e15fdSBram Moolenaar     if (CheckWindow(this))
1439bd5e15fdSBram Moolenaar 	return NULL;
1440bd5e15fdSBram Moolenaar 
1441bd5e15fdSBram Moolenaar     if (strcmp(name, "buffer") == 0)
1442bd5e15fdSBram Moolenaar 	return (PyObject *)BufferNew(this->win->w_buffer);
1443bd5e15fdSBram Moolenaar     else if (strcmp(name, "cursor") == 0)
1444bd5e15fdSBram Moolenaar     {
1445bd5e15fdSBram Moolenaar 	pos_T *pos = &this->win->w_cursor;
1446bd5e15fdSBram Moolenaar 
1447bd5e15fdSBram Moolenaar 	return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1448bd5e15fdSBram Moolenaar     }
1449bd5e15fdSBram Moolenaar     else if (strcmp(name, "height") == 0)
1450bd5e15fdSBram Moolenaar 	return Py_BuildValue("l", (long)(this->win->w_height));
1451bd5e15fdSBram Moolenaar #ifdef FEAT_VERTSPLIT
1452bd5e15fdSBram Moolenaar     else if (strcmp(name, "width") == 0)
1453bd5e15fdSBram Moolenaar 	return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1454bd5e15fdSBram Moolenaar #endif
1455bd5e15fdSBram Moolenaar     else if (strcmp(name,"__members__") == 0)
1456bd5e15fdSBram Moolenaar 	return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1457bd5e15fdSBram Moolenaar     else
1458bd5e15fdSBram Moolenaar 	return PyObject_GenericGetAttr(self, nameobj);
1459bd5e15fdSBram Moolenaar }
1460bd5e15fdSBram Moolenaar 
1461170bf1aeSBram Moolenaar     static int
1462170bf1aeSBram Moolenaar WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1463bd5e15fdSBram Moolenaar {
1464bd5e15fdSBram Moolenaar     char *name = "";
1465ca8a4dfeSBram Moolenaar 
1466bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
1467bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1468bd5e15fdSBram Moolenaar 
1469ca8a4dfeSBram Moolenaar     return WindowSetattr(self, name, val);
1470bd5e15fdSBram Moolenaar }
1471bd5e15fdSBram Moolenaar 
1472bd5e15fdSBram Moolenaar /* Window list object - Definitions
1473bd5e15fdSBram Moolenaar  */
1474bd5e15fdSBram Moolenaar 
1475bd5e15fdSBram Moolenaar typedef struct
1476bd5e15fdSBram Moolenaar {
1477bd5e15fdSBram Moolenaar     PyObject_HEAD
1478bd5e15fdSBram Moolenaar }
1479bd5e15fdSBram Moolenaar WinListObject;
1480bd5e15fdSBram Moolenaar 
1481bd5e15fdSBram Moolenaar static PySequenceMethods WinListAsSeq = {
1482bd5e15fdSBram Moolenaar     (lenfunc)	     WinListLength,	    /* sq_length,    len(x)   */
1483bd5e15fdSBram Moolenaar     (binaryfunc)     0,			    /* sq_concat,    x+y      */
1484bd5e15fdSBram Moolenaar     (ssizeargfunc)   0,			    /* sq_repeat,    x*n      */
1485bd5e15fdSBram Moolenaar     (ssizeargfunc)   WinListItem,	    /* sq_item,      x[i]     */
1486bd5e15fdSBram Moolenaar     0,					    /* sq_slice,     x[i:j]   */
1487bd5e15fdSBram Moolenaar     (ssizeobjargproc)0,			    /* sq_as_item,  x[i]=v   */
1488bd5e15fdSBram Moolenaar     0,					    /* sq_ass_slice, x[i:j]=v */
1489bd5e15fdSBram Moolenaar     0,					    /* sq_contains */
1490bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_concat */
1491bd5e15fdSBram Moolenaar     0,					    /* sq_inplace_repeat */
1492bd5e15fdSBram Moolenaar };
1493bd5e15fdSBram Moolenaar 
1494bd5e15fdSBram Moolenaar static PyTypeObject WinListType;
1495bd5e15fdSBram Moolenaar 
1496bd5e15fdSBram Moolenaar /* Current items object - Definitions
1497bd5e15fdSBram Moolenaar  */
1498bd5e15fdSBram Moolenaar 
1499bd5e15fdSBram Moolenaar typedef struct
1500bd5e15fdSBram Moolenaar {
1501bd5e15fdSBram Moolenaar     PyObject_HEAD
1502ca8a4dfeSBram Moolenaar } CurrentObject;
1503bd5e15fdSBram Moolenaar 
1504bd5e15fdSBram Moolenaar static PyTypeObject CurrentType;
1505bd5e15fdSBram Moolenaar 
1506bd5e15fdSBram Moolenaar /* Current items object - Implementation
1507bd5e15fdSBram Moolenaar  */
1508170bf1aeSBram Moolenaar     static PyObject *
1509170bf1aeSBram Moolenaar CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
1510bd5e15fdSBram Moolenaar {
1511bd5e15fdSBram Moolenaar     char *name = "";
1512bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
1513bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1514bd5e15fdSBram Moolenaar 
1515bd5e15fdSBram Moolenaar     if (strcmp(name, "buffer") == 0)
1516bd5e15fdSBram Moolenaar 	return (PyObject *)BufferNew(curbuf);
1517bd5e15fdSBram Moolenaar     else if (strcmp(name, "window") == 0)
1518bd5e15fdSBram Moolenaar 	return (PyObject *)WindowNew(curwin);
1519bd5e15fdSBram Moolenaar     else if (strcmp(name, "line") == 0)
1520bd5e15fdSBram Moolenaar 	return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
1521bd5e15fdSBram Moolenaar     else if (strcmp(name, "range") == 0)
1522bd5e15fdSBram Moolenaar 	return RangeNew(curbuf, RangeStart, RangeEnd);
1523bd5e15fdSBram Moolenaar     else if (strcmp(name,"__members__") == 0)
1524bd5e15fdSBram Moolenaar 	return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1525bd5e15fdSBram Moolenaar     else
1526bd5e15fdSBram Moolenaar     {
1527bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_AttributeError, name);
1528bd5e15fdSBram Moolenaar 	return NULL;
1529bd5e15fdSBram Moolenaar     }
1530bd5e15fdSBram Moolenaar }
1531bd5e15fdSBram Moolenaar 
1532170bf1aeSBram Moolenaar     static int
1533170bf1aeSBram Moolenaar CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
1534bd5e15fdSBram Moolenaar {
1535bd5e15fdSBram Moolenaar     char *name = "";
1536bd5e15fdSBram Moolenaar     if (PyUnicode_Check(nameobj))
1537bd5e15fdSBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1538bd5e15fdSBram Moolenaar 
1539bd5e15fdSBram Moolenaar     if (strcmp(name, "line") == 0)
1540bd5e15fdSBram Moolenaar     {
1541bd5e15fdSBram Moolenaar 	if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1542bd5e15fdSBram Moolenaar 	    return -1;
1543bd5e15fdSBram Moolenaar 
1544bd5e15fdSBram Moolenaar 	return 0;
1545bd5e15fdSBram Moolenaar     }
1546bd5e15fdSBram Moolenaar     else
1547bd5e15fdSBram Moolenaar     {
1548bd5e15fdSBram Moolenaar 	PyErr_SetString(PyExc_AttributeError, name);
1549bd5e15fdSBram Moolenaar 	return -1;
1550bd5e15fdSBram Moolenaar     }
1551bd5e15fdSBram Moolenaar }
1552bd5e15fdSBram Moolenaar 
1553db913953SBram Moolenaar /* Dictionary object - Definitions
1554db913953SBram Moolenaar  */
1555db913953SBram Moolenaar 
1556db913953SBram Moolenaar static PyInt DictionaryLength(PyObject *);
1557db913953SBram Moolenaar 
1558db913953SBram Moolenaar static PyMappingMethods DictionaryAsMapping = {
1559db913953SBram Moolenaar     /* mp_length	*/ (lenfunc) DictionaryLength,
1560db913953SBram Moolenaar     /* mp_subscript     */ (binaryfunc) DictionaryItem,
1561db913953SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1562db913953SBram Moolenaar };
1563db913953SBram Moolenaar 
1564db913953SBram Moolenaar static PyTypeObject DictionaryType;
1565db913953SBram Moolenaar 
1566db913953SBram Moolenaar     static void
1567db913953SBram Moolenaar DictionaryDestructor(PyObject *self)
1568db913953SBram Moolenaar {
1569db913953SBram Moolenaar     DictionaryObject *this = (DictionaryObject *)(self);
1570db913953SBram Moolenaar 
1571db913953SBram Moolenaar     pyll_remove(&this->ref, &lastdict);
1572db913953SBram Moolenaar     dict_unref(this->dict);
1573db913953SBram Moolenaar 
1574db913953SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1575db913953SBram Moolenaar }
1576db913953SBram Moolenaar 
1577db913953SBram Moolenaar /* List object - Definitions
1578db913953SBram Moolenaar  */
1579db913953SBram Moolenaar 
1580db913953SBram Moolenaar static PyInt ListLength(PyObject *);
1581db913953SBram Moolenaar static PyObject *ListItem(PyObject *, Py_ssize_t);
1582db913953SBram Moolenaar 
1583db913953SBram Moolenaar static PySequenceMethods ListAsSeq = {
1584db913953SBram Moolenaar     (lenfunc)		ListLength,	 /* sq_length,	  len(x)   */
1585db913953SBram Moolenaar     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
1586db913953SBram Moolenaar     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1587db913953SBram Moolenaar     (ssizeargfunc)	ListItem,	 /* sq_item,	  x[i]	   */
1588db913953SBram Moolenaar     (void *)		0,		 /* was_sq_slice,     x[i:j]   */
1589db913953SBram Moolenaar     (ssizeobjargproc)	ListAssItem,	 /* sq_as_item,  x[i]=v   */
1590db913953SBram Moolenaar     (void *)		0,		 /* was_sq_ass_slice, x[i:j]=v */
1591db913953SBram Moolenaar     0,					 /* sq_contains */
1592db913953SBram Moolenaar     (binaryfunc)	ListConcatInPlace,/* sq_inplace_concat */
1593db913953SBram Moolenaar     0,					 /* sq_inplace_repeat */
1594db913953SBram Moolenaar };
1595db913953SBram Moolenaar 
1596db913953SBram Moolenaar static PyObject *ListSubscript(PyObject *, PyObject *);
1597db913953SBram Moolenaar static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1598db913953SBram Moolenaar 
1599db913953SBram Moolenaar static PyMappingMethods ListAsMapping = {
1600db913953SBram Moolenaar     /* mp_length	*/ (lenfunc) ListLength,
1601db913953SBram Moolenaar     /* mp_subscript     */ (binaryfunc) ListSubscript,
1602db913953SBram Moolenaar     /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1603db913953SBram Moolenaar };
1604db913953SBram Moolenaar 
1605db913953SBram Moolenaar static PyTypeObject ListType;
1606db913953SBram Moolenaar 
1607db913953SBram Moolenaar     static PyObject *
1608db913953SBram Moolenaar ListSubscript(PyObject *self, PyObject* idxObject)
1609db913953SBram Moolenaar {
1610db913953SBram Moolenaar     if (PyLong_Check(idxObject))
1611db913953SBram Moolenaar     {
1612db913953SBram Moolenaar 	long idx = PyLong_AsLong(idxObject);
1613db913953SBram Moolenaar 	return ListItem(self, idx);
1614db913953SBram Moolenaar     }
1615db913953SBram Moolenaar     else if (PySlice_Check(idxObject))
1616db913953SBram Moolenaar     {
1617db913953SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1618db913953SBram Moolenaar 
1619db913953SBram Moolenaar 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1620db913953SBram Moolenaar 				 &step, &slicelen) < 0)
1621db913953SBram Moolenaar 	    return NULL;
1622db913953SBram Moolenaar 	return ListSlice(self, start, stop);
1623db913953SBram Moolenaar     }
1624db913953SBram Moolenaar     else
1625db913953SBram Moolenaar     {
1626db913953SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1627db913953SBram Moolenaar 	return NULL;
1628db913953SBram Moolenaar     }
1629db913953SBram Moolenaar }
1630db913953SBram Moolenaar 
1631db913953SBram Moolenaar     static Py_ssize_t
1632db913953SBram Moolenaar ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1633db913953SBram Moolenaar {
1634db913953SBram Moolenaar     if (PyLong_Check(idxObject))
1635db913953SBram Moolenaar     {
1636db913953SBram Moolenaar 	long idx = PyLong_AsLong(idxObject);
1637db913953SBram Moolenaar 	return ListAssItem(self, idx, obj);
1638db913953SBram Moolenaar     }
1639db913953SBram Moolenaar     else if (PySlice_Check(idxObject))
1640db913953SBram Moolenaar     {
1641db913953SBram Moolenaar 	Py_ssize_t start, stop, step, slicelen;
1642db913953SBram Moolenaar 
1643db913953SBram Moolenaar 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1644db913953SBram Moolenaar 				 &step, &slicelen) < 0)
1645db913953SBram Moolenaar 	    return -1;
1646db913953SBram Moolenaar 	return ListAssSlice(self, start, stop, obj);
1647db913953SBram Moolenaar     }
1648db913953SBram Moolenaar     else
1649db913953SBram Moolenaar     {
1650db913953SBram Moolenaar 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1651db913953SBram Moolenaar 	return -1;
1652db913953SBram Moolenaar     }
1653db913953SBram Moolenaar }
1654db913953SBram Moolenaar 
1655db913953SBram Moolenaar     static void
1656db913953SBram Moolenaar ListDestructor(PyObject *self)
1657db913953SBram Moolenaar {
1658db913953SBram Moolenaar     ListObject *this = (ListObject *)(self);
1659db913953SBram Moolenaar 
1660db913953SBram Moolenaar     pyll_remove(&this->ref, &lastlist);
1661db913953SBram Moolenaar     list_unref(this->list);
1662db913953SBram Moolenaar 
1663db913953SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1664db913953SBram Moolenaar }
1665db913953SBram Moolenaar 
1666db913953SBram Moolenaar /* Function object - Definitions
1667db913953SBram Moolenaar  */
1668db913953SBram Moolenaar 
1669db913953SBram Moolenaar     static void
1670db913953SBram Moolenaar FunctionDestructor(PyObject *self)
1671db913953SBram Moolenaar {
1672db913953SBram Moolenaar     FunctionObject	*this = (FunctionObject *) (self);
1673db913953SBram Moolenaar 
1674db913953SBram Moolenaar     func_unref(this->name);
1675db913953SBram Moolenaar     PyMem_Del(this->name);
1676db913953SBram Moolenaar 
1677db913953SBram Moolenaar     Py_TYPE(self)->tp_free((PyObject*)self);
1678db913953SBram Moolenaar }
1679db913953SBram Moolenaar 
1680db913953SBram Moolenaar     static PyObject *
1681db913953SBram Moolenaar FunctionGetattro(PyObject *self, PyObject *nameobj)
1682db913953SBram Moolenaar {
1683db913953SBram Moolenaar     FunctionObject	*this = (FunctionObject *)(self);
1684db913953SBram Moolenaar     char	*name = "";
1685db913953SBram Moolenaar     if (PyUnicode_Check(nameobj))
1686db913953SBram Moolenaar 	name = _PyUnicode_AsString(nameobj);
1687db913953SBram Moolenaar 
1688db913953SBram Moolenaar     if (strcmp(name, "name") == 0)
1689db913953SBram Moolenaar 	return PyUnicode_FromString((char *)(this->name));
1690db913953SBram Moolenaar 
1691db913953SBram Moolenaar     return PyObject_GenericGetAttr(self, nameobj);
1692db913953SBram Moolenaar }
1693db913953SBram Moolenaar 
1694bd5e15fdSBram Moolenaar /* External interface
1695bd5e15fdSBram Moolenaar  */
1696bd5e15fdSBram Moolenaar 
1697bd5e15fdSBram Moolenaar     void
1698bd5e15fdSBram Moolenaar python3_buffer_free(buf_T *buf)
1699bd5e15fdSBram Moolenaar {
1700bd5e15fdSBram Moolenaar     if (buf->b_python3_ref != NULL)
1701bd5e15fdSBram Moolenaar     {
1702bd5e15fdSBram Moolenaar 	BufferObject *bp = buf->b_python3_ref;
1703bd5e15fdSBram Moolenaar 	bp->buf = INVALID_BUFFER_VALUE;
1704bd5e15fdSBram Moolenaar 	buf->b_python3_ref = NULL;
1705bd5e15fdSBram Moolenaar     }
1706bd5e15fdSBram Moolenaar }
1707bd5e15fdSBram Moolenaar 
1708bd5e15fdSBram Moolenaar #if defined(FEAT_WINDOWS) || defined(PROTO)
1709bd5e15fdSBram Moolenaar     void
1710bd5e15fdSBram Moolenaar python3_window_free(win_T *win)
1711bd5e15fdSBram Moolenaar {
1712bd5e15fdSBram Moolenaar     if (win->w_python3_ref != NULL)
1713bd5e15fdSBram Moolenaar     {
1714bd5e15fdSBram Moolenaar 	WindowObject *wp = win->w_python3_ref;
1715bd5e15fdSBram Moolenaar 	wp->win = INVALID_WINDOW_VALUE;
1716bd5e15fdSBram Moolenaar 	win->w_python3_ref = NULL;
1717bd5e15fdSBram Moolenaar     }
1718bd5e15fdSBram Moolenaar }
1719bd5e15fdSBram Moolenaar #endif
1720bd5e15fdSBram Moolenaar 
1721bd5e15fdSBram Moolenaar static BufListObject TheBufferList =
1722bd5e15fdSBram Moolenaar {
1723bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&BufListType)
1724bd5e15fdSBram Moolenaar };
1725bd5e15fdSBram Moolenaar 
1726bd5e15fdSBram Moolenaar static WinListObject TheWindowList =
1727bd5e15fdSBram Moolenaar {
1728bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&WinListType)
1729bd5e15fdSBram Moolenaar };
1730bd5e15fdSBram Moolenaar 
1731bd5e15fdSBram Moolenaar static CurrentObject TheCurrent =
1732bd5e15fdSBram Moolenaar {
1733bd5e15fdSBram Moolenaar     PyObject_HEAD_INIT(&CurrentType)
1734bd5e15fdSBram Moolenaar };
1735bd5e15fdSBram Moolenaar 
1736bd5e15fdSBram Moolenaar PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1737bd5e15fdSBram Moolenaar 
1738bd5e15fdSBram Moolenaar static struct PyModuleDef vimmodule;
1739bd5e15fdSBram Moolenaar 
174069154f22SBram Moolenaar #ifndef PROTO
174169154f22SBram Moolenaar PyMODINIT_FUNC Py3Init_vim(void)
1742bd5e15fdSBram Moolenaar {
1743bd5e15fdSBram Moolenaar     PyObject *mod;
1744bd5e15fdSBram Moolenaar     /* The special value is removed from sys.path in Python3_Init(). */
1745bd5e15fdSBram Moolenaar     static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1746bd5e15fdSBram Moolenaar 
1747bd5e15fdSBram Moolenaar     PyType_Ready(&BufferType);
1748bd5e15fdSBram Moolenaar     PyType_Ready(&RangeType);
1749bd5e15fdSBram Moolenaar     PyType_Ready(&WindowType);
1750bd5e15fdSBram Moolenaar     PyType_Ready(&BufListType);
1751bd5e15fdSBram Moolenaar     PyType_Ready(&WinListType);
1752bd5e15fdSBram Moolenaar     PyType_Ready(&CurrentType);
1753db913953SBram Moolenaar     PyType_Ready(&DictionaryType);
1754db913953SBram Moolenaar     PyType_Ready(&ListType);
1755db913953SBram Moolenaar     PyType_Ready(&FunctionType);
1756bd5e15fdSBram Moolenaar 
1757bd5e15fdSBram Moolenaar     /* Set sys.argv[] to avoid a crash in warn(). */
1758bd5e15fdSBram Moolenaar     PySys_SetArgv(1, argv);
1759bd5e15fdSBram Moolenaar 
1760bd5e15fdSBram Moolenaar     mod = PyModule_Create(&vimmodule);
176119e60943SBram Moolenaar     if (mod == NULL)
176219e60943SBram Moolenaar 	return NULL;
1763bd5e15fdSBram Moolenaar 
176419e60943SBram Moolenaar     VimError = PyErr_NewException("vim.error", NULL, NULL);
176519e60943SBram Moolenaar     Py_INCREF(VimError);
1766bd5e15fdSBram Moolenaar 
1767bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "error", VimError);
1768bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheBufferList);
1769bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1770bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheCurrent);
1771bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1772bd5e15fdSBram Moolenaar     Py_INCREF((PyObject *)(void *)&TheWindowList);
1773bd5e15fdSBram Moolenaar     PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1774bd5e15fdSBram Moolenaar 
1775bd5e15fdSBram Moolenaar     if (PyErr_Occurred())
1776bd5e15fdSBram Moolenaar 	return NULL;
1777bd5e15fdSBram Moolenaar 
1778bd5e15fdSBram Moolenaar     return mod;
1779bd5e15fdSBram Moolenaar }
178069154f22SBram Moolenaar #endif
1781bd5e15fdSBram Moolenaar 
1782bd5e15fdSBram Moolenaar /*************************************************************************
1783bd5e15fdSBram Moolenaar  * 4. Utility functions for handling the interface between Vim and Python.
1784bd5e15fdSBram Moolenaar  */
1785bd5e15fdSBram Moolenaar 
1786bd5e15fdSBram Moolenaar /* Convert a Vim line into a Python string.
1787bd5e15fdSBram Moolenaar  * All internal newlines are replaced by null characters.
1788bd5e15fdSBram Moolenaar  *
1789bd5e15fdSBram Moolenaar  * On errors, the Python exception data is set, and NULL is returned.
1790bd5e15fdSBram Moolenaar  */
1791170bf1aeSBram Moolenaar     static PyObject *
1792170bf1aeSBram Moolenaar LineToString(const char *str)
1793bd5e15fdSBram Moolenaar {
1794bd5e15fdSBram Moolenaar     PyObject *result;
1795bd5e15fdSBram Moolenaar     Py_ssize_t len = strlen(str);
1796bd5e15fdSBram Moolenaar     char *tmp,*p;
1797bd5e15fdSBram Moolenaar 
1798bd5e15fdSBram Moolenaar     tmp = (char *)alloc((unsigned)(len+1));
1799bd5e15fdSBram Moolenaar     p = tmp;
1800bd5e15fdSBram Moolenaar     if (p == NULL)
1801bd5e15fdSBram Moolenaar     {
1802bd5e15fdSBram Moolenaar 	PyErr_NoMemory();
1803bd5e15fdSBram Moolenaar 	return NULL;
1804bd5e15fdSBram Moolenaar     }
1805bd5e15fdSBram Moolenaar 
1806bd5e15fdSBram Moolenaar     while (*str)
1807bd5e15fdSBram Moolenaar     {
1808bd5e15fdSBram Moolenaar 	if (*str == '\n')
1809bd5e15fdSBram Moolenaar 	    *p = '\0';
1810bd5e15fdSBram Moolenaar 	else
1811bd5e15fdSBram Moolenaar 	    *p = *str;
1812bd5e15fdSBram Moolenaar 
1813bd5e15fdSBram Moolenaar 	++p;
1814bd5e15fdSBram Moolenaar 	++str;
1815bd5e15fdSBram Moolenaar     }
1816bd5e15fdSBram Moolenaar     *p = '\0';
1817bd5e15fdSBram Moolenaar 
18183d64a317SBram Moolenaar     result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1819bd5e15fdSBram Moolenaar 
1820bd5e15fdSBram Moolenaar     vim_free(tmp);
1821bd5e15fdSBram Moolenaar     return result;
1822bd5e15fdSBram Moolenaar }
1823bd5e15fdSBram Moolenaar 
1824db913953SBram Moolenaar     void
1825db913953SBram Moolenaar do_py3eval (char_u *str, typval_T *rettv)
1826db913953SBram Moolenaar {
1827db913953SBram Moolenaar     DoPy3Command(NULL, (char *) str, rettv);
1828db913953SBram Moolenaar     switch(rettv->v_type)
1829db913953SBram Moolenaar     {
1830db913953SBram Moolenaar 	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1831db913953SBram Moolenaar 	case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1832db913953SBram Moolenaar 	case VAR_FUNC: func_ref(rettv->vval.v_string);    break;
183377fceb89SBram Moolenaar 	case VAR_UNKNOWN:
183477fceb89SBram Moolenaar 	    rettv->v_type = VAR_NUMBER;
183577fceb89SBram Moolenaar 	    rettv->vval.v_number = 0;
183677fceb89SBram Moolenaar 	    break;
1837db913953SBram Moolenaar     }
1838db913953SBram Moolenaar }
1839db913953SBram Moolenaar 
1840db913953SBram Moolenaar     void
1841db913953SBram Moolenaar set_ref_in_python3 (int copyID)
1842db913953SBram Moolenaar {
1843db913953SBram Moolenaar     set_ref_in_py(copyID);
1844db913953SBram Moolenaar }
1845db913953SBram Moolenaar 
1846170bf1aeSBram Moolenaar     static void
1847170bf1aeSBram Moolenaar init_structs(void)
1848bd5e15fdSBram Moolenaar {
1849bd5e15fdSBram Moolenaar     vim_memset(&OutputType, 0, sizeof(OutputType));
1850bd5e15fdSBram Moolenaar     OutputType.tp_name = "vim.message";
1851bd5e15fdSBram Moolenaar     OutputType.tp_basicsize = sizeof(OutputObject);
1852bd5e15fdSBram Moolenaar     OutputType.tp_getattro = OutputGetattro;
1853bd5e15fdSBram Moolenaar     OutputType.tp_setattro = OutputSetattro;
1854bd5e15fdSBram Moolenaar     OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1855bd5e15fdSBram Moolenaar     OutputType.tp_doc = "vim message object";
1856bd5e15fdSBram Moolenaar     OutputType.tp_methods = OutputMethods;
1857bd5e15fdSBram Moolenaar     OutputType.tp_alloc = call_PyType_GenericAlloc;
1858bd5e15fdSBram Moolenaar     OutputType.tp_new = call_PyType_GenericNew;
1859bd5e15fdSBram Moolenaar     OutputType.tp_free = call_PyObject_Free;
1860bd5e15fdSBram Moolenaar 
1861bd5e15fdSBram Moolenaar     vim_memset(&BufferType, 0, sizeof(BufferType));
1862bd5e15fdSBram Moolenaar     BufferType.tp_name = "vim.buffer";
1863bd5e15fdSBram Moolenaar     BufferType.tp_basicsize = sizeof(BufferType);
1864bd5e15fdSBram Moolenaar     BufferType.tp_dealloc = BufferDestructor;
1865bd5e15fdSBram Moolenaar     BufferType.tp_repr = BufferRepr;
1866bd5e15fdSBram Moolenaar     BufferType.tp_as_sequence = &BufferAsSeq;
1867bd5e15fdSBram Moolenaar     BufferType.tp_as_mapping = &BufferAsMapping;
1868bd5e15fdSBram Moolenaar     BufferType.tp_getattro = BufferGetattro;
1869bd5e15fdSBram Moolenaar     BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1870bd5e15fdSBram Moolenaar     BufferType.tp_doc = "vim buffer object";
1871bd5e15fdSBram Moolenaar     BufferType.tp_methods = BufferMethods;
1872bd5e15fdSBram Moolenaar     BufferType.tp_alloc = call_PyType_GenericAlloc;
1873bd5e15fdSBram Moolenaar     BufferType.tp_new = call_PyType_GenericNew;
1874bd5e15fdSBram Moolenaar     BufferType.tp_free = call_PyObject_Free;
1875bd5e15fdSBram Moolenaar 
187655d5c034SBram Moolenaar     vim_memset(&WindowType, 0, sizeof(WindowType));
187755d5c034SBram Moolenaar     WindowType.tp_name = "vim.window";
187855d5c034SBram Moolenaar     WindowType.tp_basicsize = sizeof(WindowObject);
187955d5c034SBram Moolenaar     WindowType.tp_dealloc = WindowDestructor;
188055d5c034SBram Moolenaar     WindowType.tp_repr = WindowRepr;
188155d5c034SBram Moolenaar     WindowType.tp_getattro = WindowGetattro;
188255d5c034SBram Moolenaar     WindowType.tp_setattro = WindowSetattro;
188355d5c034SBram Moolenaar     WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
188455d5c034SBram Moolenaar     WindowType.tp_doc = "vim Window object";
188555d5c034SBram Moolenaar     WindowType.tp_methods = WindowMethods;
188655d5c034SBram Moolenaar     WindowType.tp_alloc = call_PyType_GenericAlloc;
188755d5c034SBram Moolenaar     WindowType.tp_new = call_PyType_GenericNew;
188855d5c034SBram Moolenaar     WindowType.tp_free = call_PyObject_Free;
188955d5c034SBram Moolenaar 
1890bd5e15fdSBram Moolenaar     vim_memset(&BufListType, 0, sizeof(BufListType));
1891bd5e15fdSBram Moolenaar     BufListType.tp_name = "vim.bufferlist";
1892bd5e15fdSBram Moolenaar     BufListType.tp_basicsize = sizeof(BufListObject);
1893bd5e15fdSBram Moolenaar     BufListType.tp_as_sequence = &BufListAsSeq;
1894bd5e15fdSBram Moolenaar     BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1895bd5e15fdSBram Moolenaar     BufferType.tp_doc = "vim buffer list";
1896bd5e15fdSBram Moolenaar 
1897bd5e15fdSBram Moolenaar     vim_memset(&WinListType, 0, sizeof(WinListType));
1898bd5e15fdSBram Moolenaar     WinListType.tp_name = "vim.windowlist";
1899bd5e15fdSBram Moolenaar     WinListType.tp_basicsize = sizeof(WinListType);
1900bd5e15fdSBram Moolenaar     WinListType.tp_as_sequence = &WinListAsSeq;
1901bd5e15fdSBram Moolenaar     WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1902bd5e15fdSBram Moolenaar     WinListType.tp_doc = "vim window list";
1903bd5e15fdSBram Moolenaar 
1904bd5e15fdSBram Moolenaar     vim_memset(&RangeType, 0, sizeof(RangeType));
1905bd5e15fdSBram Moolenaar     RangeType.tp_name = "vim.range";
1906bd5e15fdSBram Moolenaar     RangeType.tp_basicsize = sizeof(RangeObject);
1907bd5e15fdSBram Moolenaar     RangeType.tp_dealloc = RangeDestructor;
1908bd5e15fdSBram Moolenaar     RangeType.tp_repr = RangeRepr;
1909bd5e15fdSBram Moolenaar     RangeType.tp_as_sequence = &RangeAsSeq;
1910bd5e15fdSBram Moolenaar     RangeType.tp_as_mapping = &RangeAsMapping;
1911bd5e15fdSBram Moolenaar     RangeType.tp_getattro = RangeGetattro;
1912bd5e15fdSBram Moolenaar     RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1913bd5e15fdSBram Moolenaar     RangeType.tp_doc = "vim Range object";
1914bd5e15fdSBram Moolenaar     RangeType.tp_methods = RangeMethods;
1915bd5e15fdSBram Moolenaar     RangeType.tp_alloc = call_PyType_GenericAlloc;
1916bd5e15fdSBram Moolenaar     RangeType.tp_new = call_PyType_GenericNew;
1917bd5e15fdSBram Moolenaar     RangeType.tp_free = call_PyObject_Free;
1918bd5e15fdSBram Moolenaar 
1919bd5e15fdSBram Moolenaar     vim_memset(&CurrentType, 0, sizeof(CurrentType));
1920bd5e15fdSBram Moolenaar     CurrentType.tp_name = "vim.currentdata";
1921bd5e15fdSBram Moolenaar     CurrentType.tp_basicsize = sizeof(CurrentObject);
1922bd5e15fdSBram Moolenaar     CurrentType.tp_getattro = CurrentGetattro;
1923bd5e15fdSBram Moolenaar     CurrentType.tp_setattro = CurrentSetattro;
1924bd5e15fdSBram Moolenaar     CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1925bd5e15fdSBram Moolenaar     CurrentType.tp_doc = "vim current object";
1926bd5e15fdSBram Moolenaar 
1927db913953SBram Moolenaar     vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1928db913953SBram Moolenaar     DictionaryType.tp_name = "vim.dictionary";
1929db913953SBram Moolenaar     DictionaryType.tp_basicsize = sizeof(DictionaryObject);
1930db913953SBram Moolenaar     DictionaryType.tp_dealloc = DictionaryDestructor;
1931db913953SBram Moolenaar     DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1932db913953SBram Moolenaar     DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1933db913953SBram Moolenaar     DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1934db913953SBram Moolenaar     DictionaryType.tp_methods = DictionaryMethods;
1935db913953SBram Moolenaar 
1936db913953SBram Moolenaar     vim_memset(&ListType, 0, sizeof(ListType));
1937db913953SBram Moolenaar     ListType.tp_name = "vim.list";
1938db913953SBram Moolenaar     ListType.tp_dealloc = ListDestructor;
1939db913953SBram Moolenaar     ListType.tp_basicsize = sizeof(ListObject);
1940db913953SBram Moolenaar     ListType.tp_as_sequence = &ListAsSeq;
1941db913953SBram Moolenaar     ListType.tp_as_mapping = &ListAsMapping;
1942db913953SBram Moolenaar     ListType.tp_flags = Py_TPFLAGS_DEFAULT;
1943db913953SBram Moolenaar     ListType.tp_doc = "list pushing modifications to vim structure";
1944db913953SBram Moolenaar     ListType.tp_methods = ListMethods;
1945db913953SBram Moolenaar 
1946db913953SBram Moolenaar     vim_memset(&FunctionType, 0, sizeof(FunctionType));
1947db913953SBram Moolenaar     FunctionType.tp_name = "vim.list";
1948db913953SBram Moolenaar     FunctionType.tp_basicsize = sizeof(FunctionObject);
1949db913953SBram Moolenaar     FunctionType.tp_getattro = FunctionGetattro;
1950db913953SBram Moolenaar     FunctionType.tp_dealloc = FunctionDestructor;
1951db913953SBram Moolenaar     FunctionType.tp_call = FunctionCall;
1952db913953SBram Moolenaar     FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
1953db913953SBram Moolenaar     FunctionType.tp_doc = "object that calls vim function";
1954db913953SBram Moolenaar     FunctionType.tp_methods = FunctionMethods;
1955db913953SBram Moolenaar 
1956bd5e15fdSBram Moolenaar     vim_memset(&vimmodule, 0, sizeof(vimmodule));
1957bd5e15fdSBram Moolenaar     vimmodule.m_name = "vim";
1958bd5e15fdSBram Moolenaar     vimmodule.m_doc = vim_module_doc;
1959bd5e15fdSBram Moolenaar     vimmodule.m_size = -1;
1960bd5e15fdSBram Moolenaar     vimmodule.m_methods = VimMethods;
1961bd5e15fdSBram Moolenaar }
1962