xref: /vim-8.2.3635/src/if_python3.c (revision 30b65817)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved    by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 /*
10  * Python extensions by Paul Moore.
11  * Changes for Unix by David Leonard.
12  *
13  * This consists of four parts:
14  * 1. Python interpreter main program
15  * 2. Python output stream: writes output via [e]msg().
16  * 3. Implementation of the Vim module for Python
17  * 4. Utility functions for handling the interface between Vim and Python.
18  */
19 
20 /*
21  * Roland Puntaier 2009/sept/16:
22  * Adaptations to support both python3.x and python2.x
23  */
24 
25 /* uncomment this if used with the debug version of python */
26 /* #define Py_DEBUG */
27 
28 #include "vim.h"
29 
30 #include <limits.h>
31 
32 /* Python.h defines _POSIX_THREADS itself (if needed) */
33 #ifdef _POSIX_THREADS
34 # undef _POSIX_THREADS
35 #endif
36 
37 #if defined(_WIN32) && defined(HAVE_FCNTL_H)
38 # undef HAVE_FCNTL_H
39 #endif
40 
41 #ifdef _DEBUG
42 # undef _DEBUG
43 #endif
44 
45 #define PY_SSIZE_T_CLEAN
46 
47 #ifdef F_BLANK
48 # undef F_BLANK
49 #endif
50 
51 #ifdef HAVE_STDARG_H
52 # undef HAVE_STDARG_H   /* Python's config.h defines it as well. */
53 #endif
54 #ifdef _POSIX_C_SOURCE  /* defined in feature.h */
55 # undef _POSIX_C_SOURCE
56 #endif
57 #ifdef _XOPEN_SOURCE
58 # undef _XOPEN_SOURCE	/* pyconfig.h defines it as well. */
59 #endif
60 
61 #include <Python.h>
62 #if defined(MACOS) && !defined(MACOS_X_UNIX)
63 # include "macglue.h"
64 # include <CodeFragments.h>
65 #endif
66 #undef main /* Defined in python.h - aargh */
67 #undef HAVE_FCNTL_H /* Clash with os_win32.h */
68 
69 static void init_structs(void);
70 
71 /* The "surrogateescape" error handler is new in Python 3.1 */
72 #if PY_VERSION_HEX >= 0x030100f0
73 # define CODEC_ERROR_HANDLER "surrogateescape"
74 #else
75 # define CODEC_ERROR_HANDLER NULL
76 #endif
77 
78 /* Python 3 does not support CObjects, always use Capsules */
79 #define PY_USE_CAPSULE
80 
81 #define PyInt Py_ssize_t
82 #define PyString_Check(obj) PyUnicode_Check(obj)
83 #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
84 #define PyString_FreeBytes(obj) Py_XDECREF(bytes)
85 #define PyString_AsString(obj) PyBytes_AsString(obj)
86 #define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
87 #define PyString_FromString(repr) PyUnicode_FromString(repr)
88 
89 #if defined(DYNAMIC_PYTHON3) || defined(PROTO)
90 
91 # ifndef WIN3264
92 #  include <dlfcn.h>
93 #  define FARPROC void*
94 #  define HINSTANCE void*
95 #  if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
96 #   define load_dll(n) dlopen((n), RTLD_LAZY)
97 #  else
98 #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
99 #  endif
100 #  define close_dll dlclose
101 #  define symbol_from_dll dlsym
102 # else
103 #  define load_dll vimLoadLib
104 #  define close_dll FreeLibrary
105 #  define symbol_from_dll GetProcAddress
106 # endif
107 /*
108  * Wrapper defines
109  */
110 # undef PyArg_Parse
111 # define PyArg_Parse py3_PyArg_Parse
112 # undef PyArg_ParseTuple
113 # define PyArg_ParseTuple py3_PyArg_ParseTuple
114 # define PyMem_Free py3_PyMem_Free
115 # define PyMem_Malloc py3_PyMem_Malloc
116 # define PyDict_SetItemString py3_PyDict_SetItemString
117 # define PyErr_BadArgument py3_PyErr_BadArgument
118 # define PyErr_Clear py3_PyErr_Clear
119 # define PyErr_NoMemory py3_PyErr_NoMemory
120 # define PyErr_Occurred py3_PyErr_Occurred
121 # define PyErr_SetNone py3_PyErr_SetNone
122 # define PyErr_SetString py3_PyErr_SetString
123 # define PyEval_InitThreads py3_PyEval_InitThreads
124 # define PyEval_RestoreThread py3_PyEval_RestoreThread
125 # define PyEval_SaveThread py3_PyEval_SaveThread
126 # define PyGILState_Ensure py3_PyGILState_Ensure
127 # define PyGILState_Release py3_PyGILState_Release
128 # define PyLong_AsLong py3_PyLong_AsLong
129 # define PyLong_FromLong py3_PyLong_FromLong
130 # define PyList_GetItem py3_PyList_GetItem
131 # define PyList_Append py3_PyList_Append
132 # define PyList_New py3_PyList_New
133 # define PyList_SetItem py3_PyList_SetItem
134 # define PyList_Size py3_PyList_Size
135 # define PySequence_Check py3_PySequence_Check
136 # define PySequence_Size py3_PySequence_Size
137 # define PySequence_GetItem py3_PySequence_GetItem
138 # define PyTuple_Size py3_PyTuple_Size
139 # define PyTuple_GetItem py3_PyTuple_GetItem
140 # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
141 # define PyImport_ImportModule py3_PyImport_ImportModule
142 # define PyImport_AddModule py3_PyImport_AddModule
143 # define PyObject_Init py3__PyObject_Init
144 # define PyDict_New py3_PyDict_New
145 # define PyDict_GetItemString py3_PyDict_GetItemString
146 # define PyDict_Next py3_PyDict_Next
147 # define PyMapping_Check py3_PyMapping_Check
148 # define PyMapping_Items py3_PyMapping_Items
149 # define PyIter_Next py3_PyIter_Next
150 # define PyObject_GetIter py3_PyObject_GetIter
151 # define PyModule_GetDict py3_PyModule_GetDict
152 #undef PyRun_SimpleString
153 # define PyRun_SimpleString py3_PyRun_SimpleString
154 #undef PyRun_String
155 # define PyRun_String py3_PyRun_String
156 # define PySys_SetObject py3_PySys_SetObject
157 # define PySys_SetArgv py3_PySys_SetArgv
158 # define PyType_Type (*py3_PyType_Type)
159 # define PyType_Ready py3_PyType_Ready
160 #undef Py_BuildValue
161 # define Py_BuildValue py3_Py_BuildValue
162 # define Py_SetPythonHome py3_Py_SetPythonHome
163 # define Py_Initialize py3_Py_Initialize
164 # define Py_Finalize py3_Py_Finalize
165 # define Py_IsInitialized py3_Py_IsInitialized
166 # define _Py_NoneStruct (*py3__Py_NoneStruct)
167 # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
168 # define PyModule_AddObject py3_PyModule_AddObject
169 # define PyImport_AppendInittab py3_PyImport_AppendInittab
170 # define _PyUnicode_AsString py3__PyUnicode_AsString
171 # undef PyUnicode_AsEncodedString
172 # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
173 # undef PyBytes_AsString
174 # define PyBytes_AsString py3_PyBytes_AsString
175 # undef PyBytes_FromString
176 # define PyBytes_FromString py3_PyBytes_FromString
177 # define PyFloat_FromDouble py3_PyFloat_FromDouble
178 # define PyFloat_AsDouble py3_PyFloat_AsDouble
179 # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
180 # define PySlice_Type (*py3_PySlice_Type)
181 # define PyFloat_Type (*py3_PyFloat_Type)
182 # define PyErr_NewException py3_PyErr_NewException
183 # ifdef Py_DEBUG
184 #  define _Py_NegativeRefcount py3__Py_NegativeRefcount
185 #  define _Py_RefTotal (*py3__Py_RefTotal)
186 #  define _Py_Dealloc py3__Py_Dealloc
187 #  define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
188 #  define _PyObject_DebugFree py3__PyObject_DebugFree
189 # else
190 #  define PyObject_Malloc py3_PyObject_Malloc
191 #  define PyObject_Free py3_PyObject_Free
192 # endif
193 # define PyType_GenericAlloc py3_PyType_GenericAlloc
194 # define PyType_GenericNew py3_PyType_GenericNew
195 # define PyModule_Create2 py3_PyModule_Create2
196 # undef PyUnicode_FromString
197 # define PyUnicode_FromString py3_PyUnicode_FromString
198 # undef PyUnicode_Decode
199 # define PyUnicode_Decode py3_PyUnicode_Decode
200 # define PyType_IsSubtype py3_PyType_IsSubtype
201 # define PyCapsule_New py3_PyCapsule_New
202 # define PyCapsule_GetPointer py3_PyCapsule_GetPointer
203 
204 # ifdef Py_DEBUG
205 #  undef PyObject_NEW
206 #  define PyObject_NEW(type, typeobj) \
207 ( (type *) PyObject_Init( \
208 	(PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
209 # endif
210 
211 /*
212  * Pointers for dynamic link
213  */
214 static int (*py3_PySys_SetArgv)(int, wchar_t **);
215 static void (*py3_Py_SetPythonHome)(wchar_t *home);
216 static void (*py3_Py_Initialize)(void);
217 static PyObject* (*py3_PyList_New)(Py_ssize_t size);
218 static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
219 static void (*py3_PyGILState_Release)(PyGILState_STATE);
220 static int (*py3_PySys_SetObject)(char *, PyObject *);
221 static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
222 static Py_ssize_t (*py3_PyList_Size)(PyObject *);
223 static int (*py3_PySequence_Check)(PyObject *);
224 static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
225 static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
226 static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
227 static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
228 static int (*py3_PyMapping_Check)(PyObject *);
229 static PyObject* (*py3_PyMapping_Items)(PyObject *);
230 static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
231 		     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
232 static PyObject* (*py3_PyErr_NoMemory)(void);
233 static void (*py3_Py_Finalize)(void);
234 static void (*py3_PyErr_SetString)(PyObject *, const char *);
235 static int (*py3_PyRun_SimpleString)(char *);
236 static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
237 static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
238 static PyObject* (*py3_PyImport_ImportModule)(const char *);
239 static PyObject* (*py3_PyImport_AddModule)(const char *);
240 static int (*py3_PyErr_BadArgument)(void);
241 static PyTypeObject* py3_PyType_Type;
242 static PyObject* (*py3_PyErr_Occurred)(void);
243 static PyObject* (*py3_PyModule_GetDict)(PyObject *);
244 static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
245 static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
246 static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
247 static PyObject* (*py3_PyLong_FromLong)(long);
248 static PyObject* (*py3_PyDict_New)(void);
249 static PyObject* (*py3_PyIter_Next)(PyObject *);
250 static PyObject* (*py3_PyObject_GetIter)(PyObject *);
251 static PyObject* (*py3_Py_BuildValue)(char *, ...);
252 static int (*py3_PyType_Ready)(PyTypeObject *type);
253 static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
254 static PyObject* (*py3_PyUnicode_FromString)(const char *u);
255 static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
256 	const char *encoding, const char *errors);
257 static long (*py3_PyLong_AsLong)(PyObject *);
258 static void (*py3_PyErr_SetNone)(PyObject *);
259 static void (*py3_PyEval_InitThreads)(void);
260 static void(*py3_PyEval_RestoreThread)(PyThreadState *);
261 static PyThreadState*(*py3_PyEval_SaveThread)(void);
262 static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
263 static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
264 static int (*py3_PyMem_Free)(void *);
265 static void* (*py3_PyMem_Malloc)(size_t);
266 static int (*py3_Py_IsInitialized)(void);
267 static void (*py3_PyErr_Clear)(void);
268 static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
269 static iternextfunc py3__PyObject_NextNotImplemented;
270 static PyObject* py3__Py_NoneStruct;
271 static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
272 static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
273 static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
274 static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
275 static char* (*py3_PyBytes_AsString)(PyObject *bytes);
276 static PyObject* (*py3_PyBytes_FromString)(char *str);
277 static PyObject* (*py3_PyFloat_FromDouble)(double num);
278 static double (*py3_PyFloat_AsDouble)(PyObject *);
279 static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
280 static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
281 static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
282 static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
283 static PyTypeObject* py3_PySlice_Type;
284 static PyTypeObject* py3_PyFloat_Type;
285 static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
286 static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
287 static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
288 # ifdef Py_DEBUG
289     static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
290     static Py_ssize_t* py3__Py_RefTotal;
291     static void (*py3__Py_Dealloc)(PyObject *obj);
292     static void (*py3__PyObject_DebugFree)(void*);
293     static void* (*py3__PyObject_DebugMalloc)(size_t);
294 # else
295     static void (*py3_PyObject_Free)(void*);
296     static void* (*py3_PyObject_Malloc)(size_t);
297 # endif
298 static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
299 
300 static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
301 
302 /* Imported exception objects */
303 static PyObject *p3imp_PyExc_AttributeError;
304 static PyObject *p3imp_PyExc_IndexError;
305 static PyObject *p3imp_PyExc_KeyboardInterrupt;
306 static PyObject *p3imp_PyExc_TypeError;
307 static PyObject *p3imp_PyExc_ValueError;
308 
309 # define PyExc_AttributeError p3imp_PyExc_AttributeError
310 # define PyExc_IndexError p3imp_PyExc_IndexError
311 # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
312 # define PyExc_TypeError p3imp_PyExc_TypeError
313 # define PyExc_ValueError p3imp_PyExc_ValueError
314 
315 /*
316  * Table of name to function pointer of python.
317  */
318 # define PYTHON_PROC FARPROC
319 static struct
320 {
321     char *name;
322     PYTHON_PROC *ptr;
323 } py3_funcname_table[] =
324 {
325     {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
326     {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
327     {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
328     {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
329     {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
330     {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
331     {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
332     {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
333     {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
334     {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
335     {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
336     {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
337     {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
338     {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
339     {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
340     {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
341     {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
342     {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
343     {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
344     {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
345     {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
346     {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
347     {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
348     {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
349     {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
350     {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
351     {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
352     {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
353     {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
354     {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
355     {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
356     {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
357     {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
358     {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
359     {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
360     {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
361     {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
362     {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
363     {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
364     {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
365     {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
366     {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
367     {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
368     {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
369     {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
370     {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
371     {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
372     {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
373     {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
374     {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
375     {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
376     {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
377     {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
378     {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
379     {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
380     {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
381     {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
382     {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
383     {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
384     {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
385     {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
386     {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
387     {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
388     {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
389     {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
390     {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
391     {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
392 # ifdef Py_DEBUG
393     {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
394     {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
395     {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
396     {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
397     {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
398 # else
399     {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
400     {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
401 # endif
402     {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
403     {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
404     {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
405     {"", NULL},
406 };
407 
408 /*
409  * Free python.dll
410  */
411     static void
412 end_dynamic_python3(void)
413 {
414     if (hinstPy3 != 0)
415     {
416 	close_dll(hinstPy3);
417 	hinstPy3 = 0;
418     }
419 }
420 
421 /*
422  * Load library and get all pointers.
423  * Parameter 'libname' provides name of DLL.
424  * Return OK or FAIL.
425  */
426     static int
427 py3_runtime_link_init(char *libname, int verbose)
428 {
429     int i;
430     void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
431 
432 # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
433     /* Can't have Python and Python3 loaded at the same time.
434      * It cause a crash, because RTLD_GLOBAL is needed for
435      * standard C extension libraries of one or both python versions. */
436     if (python_loaded())
437     {
438 	if (verbose)
439 	    EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
440 	return FAIL;
441     }
442 # endif
443 
444     if (hinstPy3 != 0)
445 	return OK;
446     hinstPy3 = load_dll(libname);
447 
448     if (!hinstPy3)
449     {
450 	if (verbose)
451 	    EMSG2(_(e_loadlib), libname);
452 	return FAIL;
453     }
454 
455     for (i = 0; py3_funcname_table[i].ptr; ++i)
456     {
457 	if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
458 			py3_funcname_table[i].name)) == NULL)
459 	{
460 	    close_dll(hinstPy3);
461 	    hinstPy3 = 0;
462 	    if (verbose)
463 		EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
464 	    return FAIL;
465 	}
466     }
467 
468     /* Load unicode functions separately as only the ucs2 or the ucs4 functions
469      * will be present in the library. */
470     ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
471     ucs_decode = symbol_from_dll(hinstPy3,
472 	    "PyUnicodeUCS2_Decode");
473     ucs_as_encoded_string = symbol_from_dll(hinstPy3,
474 	    "PyUnicodeUCS2_AsEncodedString");
475     if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
476     {
477 	ucs_from_string = symbol_from_dll(hinstPy3,
478 		"PyUnicodeUCS4_FromString");
479 	ucs_decode = symbol_from_dll(hinstPy3,
480 		"PyUnicodeUCS4_Decode");
481 	ucs_as_encoded_string = symbol_from_dll(hinstPy3,
482 		"PyUnicodeUCS4_AsEncodedString");
483     }
484     if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
485     {
486 	py3_PyUnicode_FromString = ucs_from_string;
487 	py3_PyUnicode_Decode = ucs_decode;
488 	py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
489     }
490     else
491     {
492 	close_dll(hinstPy3);
493 	hinstPy3 = 0;
494 	if (verbose)
495 	    EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
496 	return FAIL;
497     }
498 
499     return OK;
500 }
501 
502 /*
503  * If python is enabled (there is installed python on Windows system) return
504  * TRUE, else FALSE.
505  */
506     int
507 python3_enabled(int verbose)
508 {
509     return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
510 }
511 
512 /* Load the standard Python exceptions - don't import the symbols from the
513  * DLL, as this can cause errors (importing data symbols is not reliable).
514  */
515 static void get_py3_exceptions __ARGS((void));
516 
517     static void
518 get_py3_exceptions()
519 {
520     PyObject *exmod = PyImport_ImportModule("builtins");
521     PyObject *exdict = PyModule_GetDict(exmod);
522     p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
523     p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
524     p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
525     p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
526     p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
527     Py_XINCREF(p3imp_PyExc_AttributeError);
528     Py_XINCREF(p3imp_PyExc_IndexError);
529     Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
530     Py_XINCREF(p3imp_PyExc_TypeError);
531     Py_XINCREF(p3imp_PyExc_ValueError);
532     Py_XDECREF(exmod);
533 }
534 #endif /* DYNAMIC_PYTHON3 */
535 
536 static PyObject *BufferNew (buf_T *);
537 static PyObject *WindowNew(win_T *);
538 static PyObject *LineToString(const char *);
539 static PyObject *BufferDir(PyObject *, PyObject *);
540 
541 static PyTypeObject RangeType;
542 
543 static int py3initialised = 0;
544 
545 #define PYINITIALISED py3initialised
546 
547 /* Add conversion from PyInt? */
548 #define DICTKEY_GET(err) \
549     if (PyBytes_Check(keyObject)) \
550 	key = (char_u *) PyBytes_AsString(keyObject); \
551     else if (PyUnicode_Check(keyObject)) \
552     { \
553 	bytes = PyString_AsBytes(keyObject); \
554 	if (bytes == NULL) \
555 	    return err; \
556 	key = (char_u *) PyBytes_AsString(bytes); \
557 	if (key == NULL) \
558 	    return err; \
559     } \
560     else \
561     { \
562 	PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
563 	return err; \
564     }
565 #define DICTKEY_UNREF \
566     if (bytes != NULL) \
567 	Py_XDECREF(bytes);
568 
569 #define DICTKEY_DECL PyObject *bytes = NULL;
570 
571 /*
572  * Include the code shared with if_python.c
573  */
574 #include "if_py_both.h"
575 
576 #define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
577 
578     static void
579 call_PyObject_Free(void *p)
580 {
581 #ifdef Py_DEBUG
582     _PyObject_DebugFree(p);
583 #else
584     PyObject_Free(p);
585 #endif
586 }
587 
588     static PyObject *
589 call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
590 {
591     return PyType_GenericNew(type,args,kwds);
592 }
593 
594     static PyObject *
595 call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
596 {
597     return PyType_GenericAlloc(type,nitems);
598 }
599 
600 /******************************************************
601  * Internal function prototypes.
602  */
603 
604 static Py_ssize_t RangeStart;
605 static Py_ssize_t RangeEnd;
606 
607 static PyObject *globals;
608 
609 static int PythonIO_Init(void);
610 static void PythonIO_Fini(void);
611 PyMODINIT_FUNC Py3Init_vim(void);
612 
613 /******************************************************
614  * 1. Python interpreter main program.
615  */
616 
617 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
618 
619     void
620 python3_end()
621 {
622     static int recurse = 0;
623 
624     /* If a crash occurs while doing this, don't try again. */
625     if (recurse != 0)
626 	return;
627 
628     ++recurse;
629 
630 #ifdef DYNAMIC_PYTHON3
631     if (hinstPy3)
632 #endif
633     if (Py_IsInitialized())
634     {
635 	// acquire lock before finalizing
636 	pygilstate = PyGILState_Ensure();
637 
638 	PythonIO_Fini();
639 	Py_Finalize();
640     }
641 
642 #ifdef DYNAMIC_PYTHON3
643     end_dynamic_python3();
644 #endif
645 
646     --recurse;
647 }
648 
649 #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
650     int
651 python3_loaded()
652 {
653     return (hinstPy3 != 0);
654 }
655 #endif
656 
657     static int
658 Python3_Init(void)
659 {
660     if (!py3initialised)
661     {
662 #ifdef DYNAMIC_PYTHON3
663 	if (!python3_enabled(TRUE))
664 	{
665 	    EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
666 	    goto fail;
667 	}
668 #endif
669 
670 	init_structs();
671 
672 
673 #ifdef PYTHON3_HOME
674 	Py_SetPythonHome(PYTHON3_HOME);
675 #endif
676 
677 #if !defined(MACOS) || defined(MACOS_X_UNIX)
678 	Py_Initialize();
679 #else
680 	PyMac_Initialize();
681 #endif
682 	/* initialise threads, must be after Py_Initialize() */
683 	PyEval_InitThreads();
684 
685 #ifdef DYNAMIC_PYTHON3
686 	get_py3_exceptions();
687 #endif
688 
689 	if (PythonIO_Init())
690 	    goto fail;
691 
692 	PyImport_AppendInittab("vim", Py3Init_vim);
693 
694 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
695 
696 	/* Remove the element from sys.path that was added because of our
697 	 * argv[0] value in Py3Init_vim().  Previously we used an empty
698 	 * string, but dependinding on the OS we then get an empty entry or
699 	 * the current directory in sys.path.
700 	 * Only after vim has been imported, the element does exist in
701 	 * sys.path.
702 	 */
703 	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
704 
705 	// lock is created and acquired in PyEval_InitThreads() and thread
706 	// state is created in Py_Initialize()
707 	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
708 	// (python must have threads enabled!)
709 	// so the following does both: unlock GIL and save thread state in TLS
710 	// without deleting thread state
711 	PyGILState_Release(pygilstate);
712 
713 	py3initialised = 1;
714     }
715 
716     return 0;
717 
718 fail:
719     /* We call PythonIO_Flush() here to print any Python errors.
720      * This is OK, as it is possible to call this function even
721      * if PythonIO_Init() has not completed successfully (it will
722      * not do anything in this case).
723      */
724     PythonIO_Flush();
725     return -1;
726 }
727 
728 /*
729  * External interface
730  */
731     static void
732 DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
733 {
734 #if defined(MACOS) && !defined(MACOS_X_UNIX)
735     GrafPtr		oldPort;
736 #endif
737 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
738     char		*saved_locale;
739 #endif
740     PyObject		*cmdstr;
741     PyObject		*cmdbytes;
742 
743 #if defined(MACOS) && !defined(MACOS_X_UNIX)
744     GetPort(&oldPort);
745     /* Check if the Python library is available */
746     if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
747 	goto theend;
748 #endif
749     if (Python3_Init())
750 	goto theend;
751 
752     if (rettv == NULL)
753     {
754 	RangeStart = eap->line1;
755 	RangeEnd = eap->line2;
756     }
757     else
758     {
759 	RangeStart = (PyInt) curwin->w_cursor.lnum;
760 	RangeEnd = RangeStart;
761     }
762     Python_Release_Vim();	    /* leave vim */
763 
764 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
765     /* Python only works properly when the LC_NUMERIC locale is "C". */
766     saved_locale = setlocale(LC_NUMERIC, NULL);
767     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
768 	saved_locale = NULL;
769     else
770     {
771 	/* Need to make a copy, value may change when setting new locale. */
772 	saved_locale = (char *)vim_strsave((char_u *)saved_locale);
773 	(void)setlocale(LC_NUMERIC, "C");
774     }
775 #endif
776 
777     pygilstate = PyGILState_Ensure();
778 
779     /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
780      * SyntaxError (unicode error). */
781     cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
782 					(char *)ENC_OPT, CODEC_ERROR_HANDLER);
783     cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
784     Py_XDECREF(cmdstr);
785     if (rettv == NULL)
786 	PyRun_SimpleString(PyBytes_AsString(cmdbytes));
787     else
788     {
789 	PyObject	*r;
790 
791 	r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
792 			 globals, globals);
793 	if (r == NULL)
794 	    EMSG(_("E860: Eval did not return a valid python 3 object"));
795 	else
796 	{
797 	    if (ConvertFromPyObject(r, rettv) == -1)
798 		EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
799 	    Py_DECREF(r);
800 	}
801 	PyErr_Clear();
802     }
803     Py_XDECREF(cmdbytes);
804 
805     PyGILState_Release(pygilstate);
806 
807 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
808     if (saved_locale != NULL)
809     {
810 	(void)setlocale(LC_NUMERIC, saved_locale);
811 	vim_free(saved_locale);
812     }
813 #endif
814 
815     Python_Lock_Vim();		    /* enter vim */
816     PythonIO_Flush();
817 #if defined(MACOS) && !defined(MACOS_X_UNIX)
818     SetPort(oldPort);
819 #endif
820 
821 theend:
822     return;	    /* keeps lint happy */
823 }
824 
825 /*
826  * ":py3"
827  */
828     void
829 ex_py3(exarg_T *eap)
830 {
831     char_u *script;
832 
833     script = script_get(eap, eap->arg);
834     if (!eap->skip)
835     {
836 	if (script == NULL)
837 	    DoPy3Command(eap, (char *)eap->arg, NULL);
838 	else
839 	    DoPy3Command(eap, (char *)script, NULL);
840     }
841     vim_free(script);
842 }
843 
844 #define BUFFER_SIZE 2048
845 
846 /*
847  * ":py3file"
848  */
849     void
850 ex_py3file(exarg_T *eap)
851 {
852     static char buffer[BUFFER_SIZE];
853     const char *file;
854     char *p;
855     int i;
856 
857     /* Have to do it like this. PyRun_SimpleFile requires you to pass a
858      * stdio file pointer, but Vim and the Python DLL are compiled with
859      * different options under Windows, meaning that stdio pointers aren't
860      * compatible between the two. Yuk.
861      *
862      * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
863      *
864      * Using bytes so that Python can detect the source encoding as it normally
865      * does. The doc does not say "compile" accept bytes, though.
866      *
867      * We need to escape any backslashes or single quotes in the file name, so that
868      * Python won't mangle the file name.
869      */
870 
871     strcpy(buffer, "exec(compile(open('");
872     p = buffer + 19; /* size of "exec(compile(open('" */
873 
874     for (i=0; i<2; ++i)
875     {
876 	file = (char *)eap->arg;
877 	while (*file && p < buffer + (BUFFER_SIZE - 3))
878 	{
879 	    if (*file == '\\' || *file == '\'')
880 		*p++ = '\\';
881 	    *p++ = *file++;
882 	}
883 	/* If we didn't finish the file name, we hit a buffer overflow */
884 	if (*file != '\0')
885 	    return;
886 	if (i==0)
887 	{
888 	    strcpy(p,"','rb').read(),'");
889 	    p += 16;
890 	}
891 	else
892 	{
893 	    strcpy(p,"','exec'))");
894 	    p += 10;
895 	}
896     }
897 
898 
899     /* Execute the file */
900     DoPy3Command(eap, buffer, NULL);
901 }
902 
903 /******************************************************
904  * 2. Python output stream: writes output via [e]msg().
905  */
906 
907 /* Implementation functions
908  */
909 
910     static PyObject *
911 OutputGetattro(PyObject *self, PyObject *nameobj)
912 {
913     char *name = "";
914     if (PyUnicode_Check(nameobj))
915 	name = _PyUnicode_AsString(nameobj);
916 
917     if (strcmp(name, "softspace") == 0)
918 	return PyLong_FromLong(((OutputObject *)(self))->softspace);
919 
920     return PyObject_GenericGetAttr(self, nameobj);
921 }
922 
923     static int
924 OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
925 {
926     char *name = "";
927     if (PyUnicode_Check(nameobj))
928 	name = _PyUnicode_AsString(nameobj);
929 
930     if (val == NULL)
931     {
932 	PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
933 	return -1;
934     }
935 
936     if (strcmp(name, "softspace") == 0)
937     {
938 	if (!PyLong_Check(val))
939 	{
940 	    PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
941 	    return -1;
942 	}
943 
944 	((OutputObject *)(self))->softspace = PyLong_AsLong(val);
945 	return 0;
946     }
947 
948     PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
949     return -1;
950 }
951 
952 /***************/
953 
954     static int
955 PythonIO_Init(void)
956 {
957     PyType_Ready(&OutputType);
958     return PythonIO_Init_io();
959 }
960 
961     static void
962 PythonIO_Fini(void)
963 {
964     PySys_SetObject("stdout", NULL);
965     PySys_SetObject("stderr", NULL);
966 }
967 
968 /******************************************************
969  * 3. Implementation of the Vim module for Python
970  */
971 
972 /* Window type - Implementation functions
973  * --------------------------------------
974  */
975 
976 #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
977 
978 /* Buffer type - Implementation functions
979  * --------------------------------------
980  */
981 
982 #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
983 
984 static Py_ssize_t BufferLength(PyObject *);
985 static PyObject *BufferItem(PyObject *, Py_ssize_t);
986 static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
987 static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
988 
989 
990 /* Line range type - Implementation functions
991  * --------------------------------------
992  */
993 
994 #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
995 
996 static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
997 static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
998 static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
999 
1000 /* Current objects type - Implementation functions
1001  * -----------------------------------------------
1002  */
1003 
1004 static PySequenceMethods BufferAsSeq = {
1005     (lenfunc)		BufferLength,	    /* sq_length,    len(x)   */
1006     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1007     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1008     (ssizeargfunc)	BufferItem,	    /* sq_item,      x[i]     */
1009     0,					    /* was_sq_slice,	 x[i:j]   */
1010     0,					    /* sq_ass_item,  x[i]=v   */
1011     0,					    /* sq_ass_slice, x[i:j]=v */
1012     0,					    /* sq_contains */
1013     0,					    /* sq_inplace_concat */
1014     0,					    /* sq_inplace_repeat */
1015 };
1016 
1017 PyMappingMethods BufferAsMapping = {
1018     /* mp_length	*/ (lenfunc)BufferLength,
1019     /* mp_subscript     */ (binaryfunc)BufferSubscript,
1020     /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
1021 };
1022 
1023 
1024 /* Buffer object - Definitions
1025  */
1026 
1027 static PyTypeObject BufferType;
1028 
1029     static PyObject *
1030 BufferNew(buf_T *buf)
1031 {
1032     /* We need to handle deletion of buffers underneath us.
1033      * If we add a "b_python3_ref" field to the buf_T structure,
1034      * then we can get at it in buf_freeall() in vim. We then
1035      * need to create only ONE Python object per buffer - if
1036      * we try to create a second, just INCREF the existing one
1037      * and return it. The (single) Python object referring to
1038      * the buffer is stored in "b_python3_ref".
1039      * Question: what to do on a buf_freeall(). We'll probably
1040      * have to either delete the Python object (DECREF it to
1041      * zero - a bad idea, as it leaves dangling refs!) or
1042      * set the buf_T * value to an invalid value (-1?), which
1043      * means we need checks in all access functions... Bah.
1044      */
1045 
1046     BufferObject *self;
1047 
1048     if (buf->b_python3_ref != NULL)
1049     {
1050 	self = buf->b_python3_ref;
1051 	Py_INCREF(self);
1052     }
1053     else
1054     {
1055 	self = PyObject_NEW(BufferObject, &BufferType);
1056 	buf->b_python3_ref = self;
1057 	if (self == NULL)
1058 	    return NULL;
1059 	self->buf = buf;
1060     }
1061 
1062     return (PyObject *)(self);
1063 }
1064 
1065     static void
1066 BufferDestructor(PyObject *self)
1067 {
1068     BufferObject *this = (BufferObject *)(self);
1069 
1070     if (this->buf && this->buf != INVALID_BUFFER_VALUE)
1071 	this->buf->b_python3_ref = NULL;
1072 
1073     Py_TYPE(self)->tp_free((PyObject*)self);
1074 }
1075 
1076     static PyObject *
1077 BufferGetattro(PyObject *self, PyObject*nameobj)
1078 {
1079     BufferObject *this = (BufferObject *)(self);
1080 
1081     char *name = "";
1082     if (PyUnicode_Check(nameobj))
1083 	name = _PyUnicode_AsString(nameobj);
1084 
1085     if (CheckBuffer(this))
1086 	return NULL;
1087 
1088     if (strcmp(name, "name") == 0)
1089 	return Py_BuildValue("s", this->buf->b_ffname);
1090     else if (strcmp(name, "number") == 0)
1091 	return Py_BuildValue("n", this->buf->b_fnum);
1092     else
1093 	return PyObject_GenericGetAttr(self, nameobj);
1094 }
1095 
1096     static PyObject *
1097 BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1098 {
1099     return Py_BuildValue("[sssss]", "name", "number",
1100 						   "append", "mark", "range");
1101 }
1102 
1103     static PyObject *
1104 BufferRepr(PyObject *self)
1105 {
1106     static char repr[100];
1107     BufferObject *this = (BufferObject *)(self);
1108 
1109     if (this->buf == INVALID_BUFFER_VALUE)
1110     {
1111 	vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1112 	return PyUnicode_FromString(repr);
1113     }
1114     else
1115     {
1116 	char *name = (char *)this->buf->b_fname;
1117 	Py_ssize_t len;
1118 
1119 	if (name == NULL)
1120 	    name = "";
1121 	len = strlen(name);
1122 
1123 	if (len > 35)
1124 	    name = name + (35 - len);
1125 
1126 	vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
1127 
1128 	return PyUnicode_FromString(repr);
1129     }
1130 }
1131 
1132 /******************/
1133 
1134     static Py_ssize_t
1135 BufferLength(PyObject *self)
1136 {
1137     if (CheckBuffer((BufferObject *)(self)))
1138 	return -1;
1139 
1140     return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1141 }
1142 
1143     static PyObject *
1144 BufferItem(PyObject *self, Py_ssize_t n)
1145 {
1146     return RBItem((BufferObject *)(self), n, 1,
1147 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1148 }
1149 
1150     static PyObject *
1151 BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1152 {
1153     return RBSlice((BufferObject *)(self), lo, hi, 1,
1154 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1155 }
1156 
1157     static PyObject *
1158 BufferSubscript(PyObject *self, PyObject* idx)
1159 {
1160     if (PyLong_Check(idx))
1161     {
1162 	long _idx = PyLong_AsLong(idx);
1163 	return BufferItem(self,_idx);
1164     } else if (PySlice_Check(idx))
1165     {
1166 	Py_ssize_t start, stop, step, slicelen;
1167 
1168 	if (PySlice_GetIndicesEx((PyObject *)idx,
1169 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1170 	      &start, &stop,
1171 	      &step, &slicelen) < 0)
1172 	{
1173 	    return NULL;
1174 	}
1175 	return BufferSlice(self, start, stop);
1176     }
1177     else
1178     {
1179 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1180 	return NULL;
1181     }
1182 }
1183 
1184     static Py_ssize_t
1185 BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1186 {
1187     if (PyLong_Check(idx))
1188     {
1189 	long n = PyLong_AsLong(idx);
1190 	return RBAsItem((BufferObject *)(self), n, val, 1,
1191 		    (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1192 		    NULL);
1193     } else if (PySlice_Check(idx))
1194     {
1195 	Py_ssize_t start, stop, step, slicelen;
1196 
1197 	if (PySlice_GetIndicesEx((PyObject *)idx,
1198 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1199 	      &start, &stop,
1200 	      &step, &slicelen) < 0)
1201 	{
1202 	    return -1;
1203 	}
1204 	return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1205 			  (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1206 			  NULL);
1207     }
1208     else
1209     {
1210 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1211 	return -1;
1212     }
1213 }
1214 
1215 static PySequenceMethods RangeAsSeq = {
1216     (lenfunc)		RangeLength,	 /* sq_length,	  len(x)   */
1217     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
1218     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1219     (ssizeargfunc)	RangeItem,	 /* sq_item,	  x[i]	   */
1220     0,					 /* was_sq_slice,     x[i:j]   */
1221     (ssizeobjargproc)	RangeAsItem,	 /* sq_as_item,  x[i]=v   */
1222     0,					 /* sq_ass_slice, x[i:j]=v */
1223     0,					 /* sq_contains */
1224     0,					 /* sq_inplace_concat */
1225     0,					 /* sq_inplace_repeat */
1226 };
1227 
1228 PyMappingMethods RangeAsMapping = {
1229     /* mp_length	*/ (lenfunc)RangeLength,
1230     /* mp_subscript     */ (binaryfunc)RangeSubscript,
1231     /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
1232 };
1233 
1234 /* Line range object - Implementation
1235  */
1236 
1237     static void
1238 RangeDestructor(PyObject *self)
1239 {
1240     Py_DECREF(((RangeObject *)(self))->buf);
1241     Py_TYPE(self)->tp_free((PyObject*)self);
1242 }
1243 
1244     static PyObject *
1245 RangeGetattro(PyObject *self, PyObject *nameobj)
1246 {
1247     char *name = "";
1248     if (PyUnicode_Check(nameobj))
1249 	name = _PyUnicode_AsString(nameobj);
1250 
1251     if (strcmp(name, "start") == 0)
1252 	return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
1253     else if (strcmp(name, "end") == 0)
1254 	return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
1255     else
1256 	return PyObject_GenericGetAttr(self, nameobj);
1257 }
1258 
1259 /****************/
1260 
1261     static Py_ssize_t
1262 RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
1263 {
1264     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1265 		    ((RangeObject *)(self))->start,
1266 		    ((RangeObject *)(self))->end,
1267 		    &((RangeObject *)(self))->end);
1268 }
1269 
1270     static Py_ssize_t
1271 RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1272 {
1273     return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1274 		    ((RangeObject *)(self))->start,
1275 		    ((RangeObject *)(self))->end,
1276 		    &((RangeObject *)(self))->end);
1277 }
1278 
1279     static PyObject *
1280 RangeSubscript(PyObject *self, PyObject* idx)
1281 {
1282     if (PyLong_Check(idx))
1283     {
1284 	long _idx = PyLong_AsLong(idx);
1285 	return RangeItem(self,_idx);
1286     } else if (PySlice_Check(idx))
1287     {
1288 	Py_ssize_t start, stop, step, slicelen;
1289 
1290 	if (PySlice_GetIndicesEx((PyObject *)idx,
1291 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1292 		&start, &stop,
1293 		&step, &slicelen) < 0)
1294 	{
1295 	    return NULL;
1296 	}
1297 	return RangeSlice(self, start, stop);
1298     }
1299     else
1300     {
1301 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1302 	return NULL;
1303     }
1304 }
1305 
1306     static Py_ssize_t
1307 RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1308 {
1309     if (PyLong_Check(idx))
1310     {
1311 	long n = PyLong_AsLong(idx);
1312 	return RangeAsItem(self, n, val);
1313     } else if (PySlice_Check(idx))
1314     {
1315 	Py_ssize_t start, stop, step, slicelen;
1316 
1317 	if (PySlice_GetIndicesEx((PyObject *)idx,
1318 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1319 		&start, &stop,
1320 		&step, &slicelen) < 0)
1321 	{
1322 	    return -1;
1323 	}
1324 	return RangeAsSlice(self, start, stop, val);
1325     }
1326     else
1327     {
1328 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1329 	return -1;
1330     }
1331 }
1332 
1333 
1334 /* Buffer list object - Definitions
1335  */
1336 
1337 typedef struct
1338 {
1339     PyObject_HEAD
1340 } BufListObject;
1341 
1342 static PySequenceMethods BufListAsSeq = {
1343     (lenfunc)		BufListLength,	    /* sq_length,    len(x)   */
1344     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1345     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1346     (ssizeargfunc)	BufListItem,	    /* sq_item,      x[i]     */
1347     0,					    /* was_sq_slice,	 x[i:j]   */
1348     (ssizeobjargproc)	0,		    /* sq_as_item,  x[i]=v   */
1349     0,					    /* sq_ass_slice, x[i:j]=v */
1350     0,					    /* sq_contains */
1351     0,					    /* sq_inplace_concat */
1352     0,					    /* sq_inplace_repeat */
1353 };
1354 
1355 static PyTypeObject BufListType;
1356 
1357 /* Window object - Definitions
1358  */
1359 
1360 static struct PyMethodDef WindowMethods[] = {
1361     /* name,	    function,		calling,    documentation */
1362     { NULL,	    NULL,		0,	    NULL }
1363 };
1364 
1365 static PyTypeObject WindowType;
1366 
1367 /* Window object - Implementation
1368  */
1369 
1370     static PyObject *
1371 WindowNew(win_T *win)
1372 {
1373     /* We need to handle deletion of windows underneath us.
1374      * If we add a "w_python3_ref" field to the win_T structure,
1375      * then we can get at it in win_free() in vim. We then
1376      * need to create only ONE Python object per window - if
1377      * we try to create a second, just INCREF the existing one
1378      * and return it. The (single) Python object referring to
1379      * the window is stored in "w_python3_ref".
1380      * On a win_free() we set the Python object's win_T* field
1381      * to an invalid value. We trap all uses of a window
1382      * object, and reject them if the win_T* field is invalid.
1383      */
1384 
1385     WindowObject *self;
1386 
1387     if (win->w_python3_ref)
1388     {
1389 	self = win->w_python3_ref;
1390 	Py_INCREF(self);
1391     }
1392     else
1393     {
1394 	self = PyObject_NEW(WindowObject, &WindowType);
1395 	if (self == NULL)
1396 	    return NULL;
1397 	self->win = win;
1398 	win->w_python3_ref = self;
1399     }
1400 
1401     return (PyObject *)(self);
1402 }
1403 
1404     static void
1405 WindowDestructor(PyObject *self)
1406 {
1407     WindowObject *this = (WindowObject *)(self);
1408 
1409     if (this->win && this->win != INVALID_WINDOW_VALUE)
1410 	this->win->w_python3_ref = NULL;
1411 
1412     Py_TYPE(self)->tp_free((PyObject*)self);
1413 }
1414 
1415     static PyObject *
1416 WindowGetattro(PyObject *self, PyObject *nameobj)
1417 {
1418     WindowObject *this = (WindowObject *)(self);
1419 
1420     char *name = "";
1421     if (PyUnicode_Check(nameobj))
1422 	name = _PyUnicode_AsString(nameobj);
1423 
1424 
1425     if (CheckWindow(this))
1426 	return NULL;
1427 
1428     if (strcmp(name, "buffer") == 0)
1429 	return (PyObject *)BufferNew(this->win->w_buffer);
1430     else if (strcmp(name, "cursor") == 0)
1431     {
1432 	pos_T *pos = &this->win->w_cursor;
1433 
1434 	return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1435     }
1436     else if (strcmp(name, "height") == 0)
1437 	return Py_BuildValue("l", (long)(this->win->w_height));
1438 #ifdef FEAT_VERTSPLIT
1439     else if (strcmp(name, "width") == 0)
1440 	return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1441 #endif
1442     else if (strcmp(name,"__members__") == 0)
1443 	return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1444     else
1445 	return PyObject_GenericGetAttr(self, nameobj);
1446 }
1447 
1448     static int
1449 WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1450 {
1451     char *name = "";
1452 
1453     if (PyUnicode_Check(nameobj))
1454 	name = _PyUnicode_AsString(nameobj);
1455 
1456     return WindowSetattr(self, name, val);
1457 }
1458 
1459 /* Window list object - Definitions
1460  */
1461 
1462 typedef struct
1463 {
1464     PyObject_HEAD
1465 }
1466 WinListObject;
1467 
1468 static PySequenceMethods WinListAsSeq = {
1469     (lenfunc)	     WinListLength,	    /* sq_length,    len(x)   */
1470     (binaryfunc)     0,			    /* sq_concat,    x+y      */
1471     (ssizeargfunc)   0,			    /* sq_repeat,    x*n      */
1472     (ssizeargfunc)   WinListItem,	    /* sq_item,      x[i]     */
1473     0,					    /* sq_slice,     x[i:j]   */
1474     (ssizeobjargproc)0,			    /* sq_as_item,  x[i]=v   */
1475     0,					    /* sq_ass_slice, x[i:j]=v */
1476     0,					    /* sq_contains */
1477     0,					    /* sq_inplace_concat */
1478     0,					    /* sq_inplace_repeat */
1479 };
1480 
1481 static PyTypeObject WinListType;
1482 
1483 /* Current items object - Definitions
1484  */
1485 
1486 typedef struct
1487 {
1488     PyObject_HEAD
1489 } CurrentObject;
1490 
1491 static PyTypeObject CurrentType;
1492 
1493 /* Current items object - Implementation
1494  */
1495     static PyObject *
1496 CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
1497 {
1498     char *name = "";
1499     if (PyUnicode_Check(nameobj))
1500 	name = _PyUnicode_AsString(nameobj);
1501 
1502     if (strcmp(name, "buffer") == 0)
1503 	return (PyObject *)BufferNew(curbuf);
1504     else if (strcmp(name, "window") == 0)
1505 	return (PyObject *)WindowNew(curwin);
1506     else if (strcmp(name, "line") == 0)
1507 	return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
1508     else if (strcmp(name, "range") == 0)
1509 	return RangeNew(curbuf, RangeStart, RangeEnd);
1510     else if (strcmp(name,"__members__") == 0)
1511 	return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1512     else
1513     {
1514 	PyErr_SetString(PyExc_AttributeError, name);
1515 	return NULL;
1516     }
1517 }
1518 
1519     static int
1520 CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
1521 {
1522     char *name = "";
1523     if (PyUnicode_Check(nameobj))
1524 	name = _PyUnicode_AsString(nameobj);
1525 
1526     if (strcmp(name, "line") == 0)
1527     {
1528 	if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1529 	    return -1;
1530 
1531 	return 0;
1532     }
1533     else
1534     {
1535 	PyErr_SetString(PyExc_AttributeError, name);
1536 	return -1;
1537     }
1538 }
1539 
1540 /* Dictionary object - Definitions
1541  */
1542 
1543 static PyInt DictionaryLength(PyObject *);
1544 
1545 static PyMappingMethods DictionaryAsMapping = {
1546     /* mp_length	*/ (lenfunc) DictionaryLength,
1547     /* mp_subscript     */ (binaryfunc) DictionaryItem,
1548     /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1549 };
1550 
1551 static PyTypeObject DictionaryType;
1552 
1553     static void
1554 DictionaryDestructor(PyObject *self)
1555 {
1556     DictionaryObject *this = (DictionaryObject *)(self);
1557 
1558     pyll_remove(&this->ref, &lastdict);
1559     dict_unref(this->dict);
1560 
1561     Py_TYPE(self)->tp_free((PyObject*)self);
1562 }
1563 
1564 /* List object - Definitions
1565  */
1566 
1567 static PyInt ListLength(PyObject *);
1568 static PyObject *ListItem(PyObject *, Py_ssize_t);
1569 
1570 static PySequenceMethods ListAsSeq = {
1571     (lenfunc)		ListLength,	 /* sq_length,	  len(x)   */
1572     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
1573     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1574     (ssizeargfunc)	ListItem,	 /* sq_item,	  x[i]	   */
1575     (void *)		0,		 /* was_sq_slice,     x[i:j]   */
1576     (ssizeobjargproc)	ListAssItem,	 /* sq_as_item,  x[i]=v   */
1577     (void *)		0,		 /* was_sq_ass_slice, x[i:j]=v */
1578     0,					 /* sq_contains */
1579     (binaryfunc)	ListConcatInPlace,/* sq_inplace_concat */
1580     0,					 /* sq_inplace_repeat */
1581 };
1582 
1583 static PyObject *ListSubscript(PyObject *, PyObject *);
1584 static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1585 
1586 static PyMappingMethods ListAsMapping = {
1587     /* mp_length	*/ (lenfunc) ListLength,
1588     /* mp_subscript     */ (binaryfunc) ListSubscript,
1589     /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1590 };
1591 
1592 static PyTypeObject ListType;
1593 
1594     static PyObject *
1595 ListSubscript(PyObject *self, PyObject* idxObject)
1596 {
1597     if (PyLong_Check(idxObject))
1598     {
1599 	long idx = PyLong_AsLong(idxObject);
1600 	return ListItem(self, idx);
1601     }
1602     else if (PySlice_Check(idxObject))
1603     {
1604 	Py_ssize_t start, stop, step, slicelen;
1605 
1606 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1607 				 &step, &slicelen) < 0)
1608 	    return NULL;
1609 	return ListSlice(self, start, stop);
1610     }
1611     else
1612     {
1613 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1614 	return NULL;
1615     }
1616 }
1617 
1618     static Py_ssize_t
1619 ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1620 {
1621     if (PyLong_Check(idxObject))
1622     {
1623 	long idx = PyLong_AsLong(idxObject);
1624 	return ListAssItem(self, idx, obj);
1625     }
1626     else if (PySlice_Check(idxObject))
1627     {
1628 	Py_ssize_t start, stop, step, slicelen;
1629 
1630 	if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1631 				 &step, &slicelen) < 0)
1632 	    return -1;
1633 	return ListAssSlice(self, start, stop, obj);
1634     }
1635     else
1636     {
1637 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1638 	return -1;
1639     }
1640 }
1641 
1642     static void
1643 ListDestructor(PyObject *self)
1644 {
1645     ListObject *this = (ListObject *)(self);
1646 
1647     pyll_remove(&this->ref, &lastlist);
1648     list_unref(this->list);
1649 
1650     Py_TYPE(self)->tp_free((PyObject*)self);
1651 }
1652 
1653 /* Function object - Definitions
1654  */
1655 
1656     static void
1657 FunctionDestructor(PyObject *self)
1658 {
1659     FunctionObject	*this = (FunctionObject *) (self);
1660 
1661     func_unref(this->name);
1662     PyMem_Del(this->name);
1663 
1664     Py_TYPE(self)->tp_free((PyObject*)self);
1665 }
1666 
1667     static PyObject *
1668 FunctionGetattro(PyObject *self, PyObject *nameobj)
1669 {
1670     FunctionObject	*this = (FunctionObject *)(self);
1671     char	*name = "";
1672     if (PyUnicode_Check(nameobj))
1673 	name = _PyUnicode_AsString(nameobj);
1674 
1675     if (strcmp(name, "name") == 0)
1676 	return PyUnicode_FromString((char *)(this->name));
1677 
1678     return PyObject_GenericGetAttr(self, nameobj);
1679 }
1680 
1681 /* External interface
1682  */
1683 
1684     void
1685 python3_buffer_free(buf_T *buf)
1686 {
1687     if (buf->b_python3_ref != NULL)
1688     {
1689 	BufferObject *bp = buf->b_python3_ref;
1690 	bp->buf = INVALID_BUFFER_VALUE;
1691 	buf->b_python3_ref = NULL;
1692     }
1693 }
1694 
1695 #if defined(FEAT_WINDOWS) || defined(PROTO)
1696     void
1697 python3_window_free(win_T *win)
1698 {
1699     if (win->w_python3_ref != NULL)
1700     {
1701 	WindowObject *wp = win->w_python3_ref;
1702 	wp->win = INVALID_WINDOW_VALUE;
1703 	win->w_python3_ref = NULL;
1704     }
1705 }
1706 #endif
1707 
1708 static BufListObject TheBufferList =
1709 {
1710     PyObject_HEAD_INIT(&BufListType)
1711 };
1712 
1713 static WinListObject TheWindowList =
1714 {
1715     PyObject_HEAD_INIT(&WinListType)
1716 };
1717 
1718 static CurrentObject TheCurrent =
1719 {
1720     PyObject_HEAD_INIT(&CurrentType)
1721 };
1722 
1723 PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1724 
1725 static struct PyModuleDef vimmodule;
1726 
1727 #ifndef PROTO
1728 PyMODINIT_FUNC Py3Init_vim(void)
1729 {
1730     PyObject *mod;
1731     /* The special value is removed from sys.path in Python3_Init(). */
1732     static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1733 
1734     PyType_Ready(&BufferType);
1735     PyType_Ready(&RangeType);
1736     PyType_Ready(&WindowType);
1737     PyType_Ready(&BufListType);
1738     PyType_Ready(&WinListType);
1739     PyType_Ready(&CurrentType);
1740     PyType_Ready(&DictionaryType);
1741     PyType_Ready(&ListType);
1742     PyType_Ready(&FunctionType);
1743 
1744     /* Set sys.argv[] to avoid a crash in warn(). */
1745     PySys_SetArgv(1, argv);
1746 
1747     mod = PyModule_Create(&vimmodule);
1748     if (mod == NULL)
1749 	return NULL;
1750 
1751     VimError = PyErr_NewException("vim.error", NULL, NULL);
1752     Py_INCREF(VimError);
1753 
1754     PyModule_AddObject(mod, "error", VimError);
1755     Py_INCREF((PyObject *)(void *)&TheBufferList);
1756     PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1757     Py_INCREF((PyObject *)(void *)&TheCurrent);
1758     PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1759     Py_INCREF((PyObject *)(void *)&TheWindowList);
1760     PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1761 
1762     if (PyErr_Occurred())
1763 	return NULL;
1764 
1765     return mod;
1766 }
1767 #endif
1768 
1769 /*************************************************************************
1770  * 4. Utility functions for handling the interface between Vim and Python.
1771  */
1772 
1773 /* Convert a Vim line into a Python string.
1774  * All internal newlines are replaced by null characters.
1775  *
1776  * On errors, the Python exception data is set, and NULL is returned.
1777  */
1778     static PyObject *
1779 LineToString(const char *str)
1780 {
1781     PyObject *result;
1782     Py_ssize_t len = strlen(str);
1783     char *tmp,*p;
1784 
1785     tmp = (char *)alloc((unsigned)(len+1));
1786     p = tmp;
1787     if (p == NULL)
1788     {
1789 	PyErr_NoMemory();
1790 	return NULL;
1791     }
1792 
1793     while (*str)
1794     {
1795 	if (*str == '\n')
1796 	    *p = '\0';
1797 	else
1798 	    *p = *str;
1799 
1800 	++p;
1801 	++str;
1802     }
1803     *p = '\0';
1804 
1805     result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1806 
1807     vim_free(tmp);
1808     return result;
1809 }
1810 
1811     void
1812 do_py3eval (char_u *str, typval_T *rettv)
1813 {
1814     DoPy3Command(NULL, (char *) str, rettv);
1815     switch(rettv->v_type)
1816     {
1817 	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1818 	case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1819 	case VAR_FUNC: func_ref(rettv->vval.v_string);    break;
1820     }
1821 }
1822 
1823     void
1824 set_ref_in_python3 (int copyID)
1825 {
1826     set_ref_in_py(copyID);
1827 }
1828 
1829     static void
1830 init_structs(void)
1831 {
1832     vim_memset(&OutputType, 0, sizeof(OutputType));
1833     OutputType.tp_name = "vim.message";
1834     OutputType.tp_basicsize = sizeof(OutputObject);
1835     OutputType.tp_getattro = OutputGetattro;
1836     OutputType.tp_setattro = OutputSetattro;
1837     OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1838     OutputType.tp_doc = "vim message object";
1839     OutputType.tp_methods = OutputMethods;
1840     OutputType.tp_alloc = call_PyType_GenericAlloc;
1841     OutputType.tp_new = call_PyType_GenericNew;
1842     OutputType.tp_free = call_PyObject_Free;
1843 
1844     vim_memset(&BufferType, 0, sizeof(BufferType));
1845     BufferType.tp_name = "vim.buffer";
1846     BufferType.tp_basicsize = sizeof(BufferType);
1847     BufferType.tp_dealloc = BufferDestructor;
1848     BufferType.tp_repr = BufferRepr;
1849     BufferType.tp_as_sequence = &BufferAsSeq;
1850     BufferType.tp_as_mapping = &BufferAsMapping;
1851     BufferType.tp_getattro = BufferGetattro;
1852     BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1853     BufferType.tp_doc = "vim buffer object";
1854     BufferType.tp_methods = BufferMethods;
1855     BufferType.tp_alloc = call_PyType_GenericAlloc;
1856     BufferType.tp_new = call_PyType_GenericNew;
1857     BufferType.tp_free = call_PyObject_Free;
1858 
1859     vim_memset(&WindowType, 0, sizeof(WindowType));
1860     WindowType.tp_name = "vim.window";
1861     WindowType.tp_basicsize = sizeof(WindowObject);
1862     WindowType.tp_dealloc = WindowDestructor;
1863     WindowType.tp_repr = WindowRepr;
1864     WindowType.tp_getattro = WindowGetattro;
1865     WindowType.tp_setattro = WindowSetattro;
1866     WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1867     WindowType.tp_doc = "vim Window object";
1868     WindowType.tp_methods = WindowMethods;
1869     WindowType.tp_alloc = call_PyType_GenericAlloc;
1870     WindowType.tp_new = call_PyType_GenericNew;
1871     WindowType.tp_free = call_PyObject_Free;
1872 
1873     vim_memset(&BufListType, 0, sizeof(BufListType));
1874     BufListType.tp_name = "vim.bufferlist";
1875     BufListType.tp_basicsize = sizeof(BufListObject);
1876     BufListType.tp_as_sequence = &BufListAsSeq;
1877     BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1878     BufferType.tp_doc = "vim buffer list";
1879 
1880     vim_memset(&WinListType, 0, sizeof(WinListType));
1881     WinListType.tp_name = "vim.windowlist";
1882     WinListType.tp_basicsize = sizeof(WinListType);
1883     WinListType.tp_as_sequence = &WinListAsSeq;
1884     WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1885     WinListType.tp_doc = "vim window list";
1886 
1887     vim_memset(&RangeType, 0, sizeof(RangeType));
1888     RangeType.tp_name = "vim.range";
1889     RangeType.tp_basicsize = sizeof(RangeObject);
1890     RangeType.tp_dealloc = RangeDestructor;
1891     RangeType.tp_repr = RangeRepr;
1892     RangeType.tp_as_sequence = &RangeAsSeq;
1893     RangeType.tp_as_mapping = &RangeAsMapping;
1894     RangeType.tp_getattro = RangeGetattro;
1895     RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1896     RangeType.tp_doc = "vim Range object";
1897     RangeType.tp_methods = RangeMethods;
1898     RangeType.tp_alloc = call_PyType_GenericAlloc;
1899     RangeType.tp_new = call_PyType_GenericNew;
1900     RangeType.tp_free = call_PyObject_Free;
1901 
1902     vim_memset(&CurrentType, 0, sizeof(CurrentType));
1903     CurrentType.tp_name = "vim.currentdata";
1904     CurrentType.tp_basicsize = sizeof(CurrentObject);
1905     CurrentType.tp_getattro = CurrentGetattro;
1906     CurrentType.tp_setattro = CurrentSetattro;
1907     CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1908     CurrentType.tp_doc = "vim current object";
1909 
1910     vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1911     DictionaryType.tp_name = "vim.dictionary";
1912     DictionaryType.tp_basicsize = sizeof(DictionaryObject);
1913     DictionaryType.tp_dealloc = DictionaryDestructor;
1914     DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1915     DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1916     DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1917     DictionaryType.tp_methods = DictionaryMethods;
1918 
1919     vim_memset(&ListType, 0, sizeof(ListType));
1920     ListType.tp_name = "vim.list";
1921     ListType.tp_dealloc = ListDestructor;
1922     ListType.tp_basicsize = sizeof(ListObject);
1923     ListType.tp_as_sequence = &ListAsSeq;
1924     ListType.tp_as_mapping = &ListAsMapping;
1925     ListType.tp_flags = Py_TPFLAGS_DEFAULT;
1926     ListType.tp_doc = "list pushing modifications to vim structure";
1927     ListType.tp_methods = ListMethods;
1928 
1929     vim_memset(&FunctionType, 0, sizeof(FunctionType));
1930     FunctionType.tp_name = "vim.list";
1931     FunctionType.tp_basicsize = sizeof(FunctionObject);
1932     FunctionType.tp_getattro = FunctionGetattro;
1933     FunctionType.tp_dealloc = FunctionDestructor;
1934     FunctionType.tp_call = FunctionCall;
1935     FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
1936     FunctionType.tp_doc = "object that calls vim function";
1937     FunctionType.tp_methods = FunctionMethods;
1938 
1939     vim_memset(&vimmodule, 0, sizeof(vimmodule));
1940     vimmodule.m_name = "vim";
1941     vimmodule.m_doc = vim_module_doc;
1942     vimmodule.m_size = -1;
1943     vimmodule.m_methods = VimMethods;
1944 }
1945