xref: /vim-8.2.3635/src/if_python3.c (revision 0ee8df9c)
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 #define PyInt Py_ssize_t
72 #define PyString_Check(obj) PyUnicode_Check(obj)
73 #define PyString_AsString(obj) _PyUnicode_AsString(obj)
74 #define PyString_Size(obj) PyUnicode_GET_SIZE(obj)
75 #define PyString_FromString(repr) PyUnicode_FromString(repr)
76 
77 #if defined(DYNAMIC_PYTHON3)
78 
79 # ifndef WIN3264
80 #  include <dlfcn.h>
81 #  define FARPROC void*
82 #  define HINSTANCE void*
83 #  ifdef FEAT_PYTHON
84    /* Don't use RTLD_GLOBAL, it may cause a crash if both :python and :py3 are
85     * used. But without it importing may fail, e.g., for termios. */
86 #   define load_dll(n) dlopen((n), RTLD_LAZY)
87 #  else
88 #   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
89 #  endif
90 #  define close_dll dlclose
91 #  define symbol_from_dll dlsym
92 # else
93 #  define load_dll LoadLibrary
94 #  define close_dll FreeLibrary
95 #  define symbol_from_dll GetProcAddress
96 # endif
97 /*
98  * Wrapper defines
99  */
100 # undef PyArg_Parse
101 # define PyArg_Parse py3_PyArg_Parse
102 # undef PyArg_ParseTuple
103 # define PyArg_ParseTuple py3_PyArg_ParseTuple
104 # define PyDict_SetItemString py3_PyDict_SetItemString
105 # define PyErr_BadArgument py3_PyErr_BadArgument
106 # define PyErr_Clear py3_PyErr_Clear
107 # define PyErr_NoMemory py3_PyErr_NoMemory
108 # define PyErr_Occurred py3_PyErr_Occurred
109 # define PyErr_SetNone py3_PyErr_SetNone
110 # define PyErr_SetString py3_PyErr_SetString
111 # define PyEval_InitThreads py3_PyEval_InitThreads
112 # define PyEval_RestoreThread py3_PyEval_RestoreThread
113 # define PyEval_SaveThread py3_PyEval_SaveThread
114 # define PyGILState_Ensure py3_PyGILState_Ensure
115 # define PyGILState_Release py3_PyGILState_Release
116 # define PyLong_AsLong py3_PyLong_AsLong
117 # define PyLong_FromLong py3_PyLong_FromLong
118 # define PyList_GetItem py3_PyList_GetItem
119 # define PyList_Append py3_PyList_Append
120 # define PyList_New py3_PyList_New
121 # define PyList_SetItem py3_PyList_SetItem
122 # define PyList_Size py3_PyList_Size
123 # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
124 # define PyImport_ImportModule py3_PyImport_ImportModule
125 # define PyObject_Init py3__PyObject_Init
126 # define PyDict_New py3_PyDict_New
127 # define PyDict_GetItemString py3_PyDict_GetItemString
128 # define PyModule_GetDict py3_PyModule_GetDict
129 #undef PyRun_SimpleString
130 # define PyRun_SimpleString py3_PyRun_SimpleString
131 # define PySys_SetObject py3_PySys_SetObject
132 # define PySys_SetArgv py3_PySys_SetArgv
133 # define PyType_Type (*py3_PyType_Type)
134 # define PyType_Ready py3_PyType_Ready
135 #undef Py_BuildValue
136 # define Py_BuildValue py3_Py_BuildValue
137 # define Py_Initialize py3_Py_Initialize
138 # define Py_Finalize py3_Py_Finalize
139 # define Py_IsInitialized py3_Py_IsInitialized
140 # define _Py_NoneStruct (*py3__Py_NoneStruct)
141 # define PyModule_AddObject py3_PyModule_AddObject
142 # define PyImport_AppendInittab py3_PyImport_AppendInittab
143 # define _PyUnicode_AsString py3__PyUnicode_AsString
144 # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
145 # define PySlice_Type (*py3_PySlice_Type)
146 # ifdef Py_DEBUG
147 #  define _Py_NegativeRefcount py3__Py_NegativeRefcount
148 #  define _Py_RefTotal (*py3__Py_RefTotal)
149 #  define _Py_Dealloc py3__Py_Dealloc
150 #  define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
151 #  define _PyObject_DebugFree py3__PyObject_DebugFree
152 # else
153 #  define PyObject_Malloc py3_PyObject_Malloc
154 #  define PyObject_Free py3_PyObject_Free
155 # endif
156 # define PyType_GenericAlloc py3_PyType_GenericAlloc
157 # define PyType_GenericNew py3_PyType_GenericNew
158 # define PyModule_Create2 py3_PyModule_Create2
159 # undef PyUnicode_FromString
160 # define PyUnicode_FromString py3_PyUnicode_FromString
161 # undef PyUnicode_FromStringAndSize
162 # define PyUnicode_FromStringAndSize py3_PyUnicode_FromStringAndSize
163 
164 # ifdef Py_DEBUG
165 #  undef PyObject_NEW
166 #  define PyObject_NEW(type, typeobj) \
167 ( (type *) PyObject_Init( \
168 	(PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
169 # endif
170 
171 /*
172  * Pointers for dynamic link
173  */
174 static int (*py3_PySys_SetArgv)(int, wchar_t **);
175 static void (*py3_Py_Initialize)(void);
176 static PyObject* (*py3_PyList_New)(Py_ssize_t size);
177 static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
178 static void (*py3_PyGILState_Release)(PyGILState_STATE);
179 static int (*py3_PySys_SetObject)(char *, PyObject *);
180 static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
181 static Py_ssize_t (*py3_PyList_Size)(PyObject *);
182 static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length,
183 		     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
184 static PyObject* (*py3_PyErr_NoMemory)(void);
185 static void (*py3_Py_Finalize)(void);
186 static void (*py3_PyErr_SetString)(PyObject *, const char *);
187 static int (*py3_PyRun_SimpleString)(char *);
188 static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
189 static PyObject* (*py3_PyImport_ImportModule)(const char *);
190 static int (*py3_PyErr_BadArgument)(void);
191 static PyTypeObject* py3_PyType_Type;
192 static PyObject* (*py3_PyErr_Occurred)(void);
193 static PyObject* (*py3_PyModule_GetDict)(PyObject *);
194 static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
195 static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
196 static PyObject* (*py3_PyLong_FromLong)(long);
197 static PyObject* (*py3_PyDict_New)(void);
198 static PyObject* (*py3_Py_BuildValue)(char *, ...);
199 static int (*py3_PyType_Ready)(PyTypeObject *type);
200 static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
201 static PyObject* (*py3_PyUnicode_FromString)(const char *u);
202 static PyObject* (*py3_PyUnicode_FromStringAndSize)(const char *u, Py_ssize_t size);
203 static long (*py3_PyLong_AsLong)(PyObject *);
204 static void (*py3_PyErr_SetNone)(PyObject *);
205 static void (*py3_PyEval_InitThreads)(void);
206 static void(*py3_PyEval_RestoreThread)(PyThreadState *);
207 static PyThreadState*(*py3_PyEval_SaveThread)(void);
208 static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
209 static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
210 static int (*py3_Py_IsInitialized)(void);
211 static void (*py3_PyErr_Clear)(void);
212 static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
213 static PyObject* py3__Py_NoneStruct;
214 static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
215 static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
216 static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
217 static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
218 static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
219 static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
220 static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
221 static PyTypeObject* py3_PySlice_Type;
222 # ifdef Py_DEBUG
223     static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
224     static Py_ssize_t* py3__Py_RefTotal;
225     static void (*py3__Py_Dealloc)(PyObject *obj);
226     static void (*py3__PyObject_DebugFree)(void*);
227     static void* (*py3__PyObject_DebugMalloc)(size_t);
228 # else
229     static void (*py3_PyObject_Free)(void*);
230     static void* (*py3_PyObject_Malloc)(size_t);
231 # endif
232 
233 static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
234 
235 /* Imported exception objects */
236 static PyObject *p3imp_PyExc_AttributeError;
237 static PyObject *p3imp_PyExc_IndexError;
238 static PyObject *p3imp_PyExc_KeyboardInterrupt;
239 static PyObject *p3imp_PyExc_TypeError;
240 static PyObject *p3imp_PyExc_ValueError;
241 
242 # define PyExc_AttributeError p3imp_PyExc_AttributeError
243 # define PyExc_IndexError p3imp_PyExc_IndexError
244 # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
245 # define PyExc_TypeError p3imp_PyExc_TypeError
246 # define PyExc_ValueError p3imp_PyExc_ValueError
247 
248 /*
249  * Table of name to function pointer of python.
250  */
251 # define PYTHON_PROC FARPROC
252 static struct
253 {
254     char *name;
255     PYTHON_PROC *ptr;
256 } py3_funcname_table[] =
257 {
258     {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
259     {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
260     {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
261     {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
262     {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
263     {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
264     {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
265     {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
266     {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
267     {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
268     {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
269     {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
270     {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
271     {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
272     {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
273     {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
274     {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
275     {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
276     {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
277     {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
278     {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
279     {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
280     {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
281     {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
282     {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
283     {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
284     {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
285     {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
286     {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
287     {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
288     {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
289     {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
290     {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
291     {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
292     {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
293     {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
294     {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
295     {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
296     {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
297     {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
298     {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
299     {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
300     {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
301     {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
302     {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
303     {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
304 # ifdef Py_DEBUG
305     {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
306     {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
307     {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
308     {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
309     {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
310 # else
311     {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
312     {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
313 # endif
314     {"", NULL},
315 };
316 
317 /*
318  * Free python.dll
319  */
320     static void
321 end_dynamic_python3(void)
322 {
323     if (hinstPy3 != 0)
324     {
325 	close_dll(hinstPy3);
326 	hinstPy3 = 0;
327     }
328 }
329 
330 /*
331  * Load library and get all pointers.
332  * Parameter 'libname' provides name of DLL.
333  * Return OK or FAIL.
334  */
335     static int
336 py3_runtime_link_init(char *libname, int verbose)
337 {
338     int i;
339     void *ucs_from_string, *ucs_from_string_and_size;
340 
341 # if 0  /* this should be OK now that we don't use RTLD_GLOBAL */
342 # if defined(UNIX) && defined(FEAT_PYTHON)
343     /* Can't have Python and Python3 loaded at the same time, it may cause a
344      * crash. */
345     if (python_loaded())
346     {
347 	EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
348 	return FAIL;
349     }
350 # endif
351 # endif
352 
353     if (hinstPy3 != 0)
354 	return OK;
355     hinstPy3 = load_dll(libname);
356 
357     if (!hinstPy3)
358     {
359 	if (verbose)
360 	    EMSG2(_(e_loadlib), libname);
361 	return FAIL;
362     }
363 
364     for (i = 0; py3_funcname_table[i].ptr; ++i)
365     {
366 	if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
367 			py3_funcname_table[i].name)) == NULL)
368 	{
369 	    close_dll(hinstPy3);
370 	    hinstPy3 = 0;
371 	    if (verbose)
372 		EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
373 	    return FAIL;
374 	}
375     }
376 
377     /* Load unicode functions separately as only the ucs2 or the ucs4 functions
378      * will be present in the library. */
379     ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
380     ucs_from_string_and_size = symbol_from_dll(hinstPy3,
381 	    "PyUnicodeUCS2_FromStringAndSize");
382     if (!ucs_from_string || !ucs_from_string_and_size)
383     {
384 	ucs_from_string = symbol_from_dll(hinstPy3,
385 		"PyUnicodeUCS4_FromString");
386 	ucs_from_string_and_size = symbol_from_dll(hinstPy3,
387 		"PyUnicodeUCS4_FromStringAndSize");
388     }
389     if (ucs_from_string && ucs_from_string_and_size)
390     {
391 	py3_PyUnicode_FromString = ucs_from_string;
392 	py3_PyUnicode_FromStringAndSize = ucs_from_string_and_size;
393     }
394     else
395     {
396 	close_dll(hinstPy3);
397 	hinstPy3 = 0;
398 	if (verbose)
399 	    EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
400 	return FAIL;
401     }
402 
403     return OK;
404 }
405 
406 /*
407  * If python is enabled (there is installed python on Windows system) return
408  * TRUE, else FALSE.
409  */
410     int
411 python3_enabled(int verbose)
412 {
413     return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
414 }
415 
416 /* Load the standard Python exceptions - don't import the symbols from the
417  * DLL, as this can cause errors (importing data symbols is not reliable).
418  */
419 static void get_py3_exceptions __ARGS((void));
420 
421     static void
422 get_py3_exceptions()
423 {
424     PyObject *exmod = PyImport_ImportModule("builtins");
425     PyObject *exdict = PyModule_GetDict(exmod);
426     p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
427     p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
428     p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
429     p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
430     p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
431     Py_XINCREF(p3imp_PyExc_AttributeError);
432     Py_XINCREF(p3imp_PyExc_IndexError);
433     Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
434     Py_XINCREF(p3imp_PyExc_TypeError);
435     Py_XINCREF(p3imp_PyExc_ValueError);
436     Py_XDECREF(exmod);
437 }
438 #endif /* DYNAMIC_PYTHON3 */
439 
440 static PyObject *BufferNew (buf_T *);
441 static PyObject *WindowNew(win_T *);
442 static PyObject *LineToString(const char *);
443 
444 static PyTypeObject RangeType;
445 
446 /*
447  * Include the code shared with if_python.c
448  */
449 #include "if_py_both.h"
450 
451     static void
452 call_PyObject_Free(void *p)
453 {
454 #ifdef Py_DEBUG
455     _PyObject_DebugFree(p);
456 #else
457     PyObject_Free(p);
458 #endif
459 }
460 
461     static PyObject *
462 call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
463 {
464     return PyType_GenericNew(type,args,kwds);
465 }
466 
467     static PyObject *
468 call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
469 {
470     return PyType_GenericAlloc(type,nitems);
471 }
472 
473 /******************************************************
474  * Internal function prototypes.
475  */
476 
477 static Py_ssize_t RangeStart;
478 static Py_ssize_t RangeEnd;
479 
480 static int PythonIO_Init(void);
481 static void PythonIO_Fini(void);
482 PyMODINIT_FUNC Py3Init_vim(void);
483 
484 /******************************************************
485  * 1. Python interpreter main program.
486  */
487 
488 static int py3initialised = 0;
489 
490 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
491 
492     void
493 python3_end()
494 {
495     static int recurse = 0;
496 
497     /* If a crash occurs while doing this, don't try again. */
498     if (recurse != 0)
499 	return;
500 
501     ++recurse;
502 
503 #ifdef DYNAMIC_PYTHON3
504     if (hinstPy3)
505 #endif
506     if (Py_IsInitialized())
507     {
508 	// acquire lock before finalizing
509 	pygilstate = PyGILState_Ensure();
510 
511 	PythonIO_Fini();
512 	Py_Finalize();
513     }
514 
515 #ifdef DYNAMIC_PYTHON3
516     end_dynamic_python3();
517 #endif
518 
519     --recurse;
520 }
521 
522 #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
523     int
524 python3_loaded()
525 {
526     return (hinstPy3 != 0);
527 }
528 #endif
529 
530     static int
531 Python3_Init(void)
532 {
533     if (!py3initialised)
534     {
535 #ifdef DYNAMIC_PYTHON3
536 	if (!python3_enabled(TRUE))
537 	{
538 	    EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
539 	    goto fail;
540 	}
541 #endif
542 
543 	init_structs();
544 
545 	/* initialise threads */
546 	PyEval_InitThreads();
547 
548 #if !defined(MACOS) || defined(MACOS_X_UNIX)
549 	Py_Initialize();
550 #else
551 	PyMac_Initialize();
552 #endif
553 
554 #ifdef DYNAMIC_PYTHON3
555 	get_py3_exceptions();
556 #endif
557 
558 	if (PythonIO_Init())
559 	    goto fail;
560 
561 	PyImport_AppendInittab("vim", Py3Init_vim);
562 
563 	/* Remove the element from sys.path that was added because of our
564 	 * argv[0] value in Py3Init_vim().  Previously we used an empty
565 	 * string, but dependinding on the OS we then get an empty entry or
566 	 * the current directory in sys.path. */
567 	PyRun_SimpleString("import sys; sys.path = list(filter(lambda x: x != '/must>not&exist', sys.path))");
568 
569 	// lock is created and acquired in PyEval_InitThreads() and thread
570 	// state is created in Py_Initialize()
571 	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
572 	// (python must have threads enabled!)
573 	// so the following does both: unlock GIL and save thread state in TLS
574 	// without deleting thread state
575 	PyGILState_Release(pygilstate);
576 
577 	py3initialised = 1;
578     }
579 
580     return 0;
581 
582 fail:
583     /* We call PythonIO_Flush() here to print any Python errors.
584      * This is OK, as it is possible to call this function even
585      * if PythonIO_Init() has not completed successfully (it will
586      * not do anything in this case).
587      */
588     PythonIO_Flush();
589     return -1;
590 }
591 
592 /*
593  * External interface
594  */
595     static void
596 DoPy3Command(exarg_T *eap, const char *cmd)
597 {
598 #if defined(MACOS) && !defined(MACOS_X_UNIX)
599     GrafPtr		oldPort;
600 #endif
601 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
602     char		*saved_locale;
603 #endif
604 
605 #if defined(MACOS) && !defined(MACOS_X_UNIX)
606     GetPort(&oldPort);
607     /* Check if the Python library is available */
608     if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
609 	goto theend;
610 #endif
611     if (Python3_Init())
612 	goto theend;
613 
614     RangeStart = eap->line1;
615     RangeEnd = eap->line2;
616     Python_Release_Vim();	    /* leave vim */
617 
618 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
619     /* Python only works properly when the LC_NUMERIC locale is "C". */
620     saved_locale = setlocale(LC_NUMERIC, NULL);
621     if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
622 	saved_locale = NULL;
623     else
624     {
625 	/* Need to make a copy, value may change when setting new locale. */
626 	saved_locale = (char *)vim_strsave((char_u *)saved_locale);
627 	(void)setlocale(LC_NUMERIC, "C");
628     }
629 #endif
630 
631     pygilstate = PyGILState_Ensure();
632 
633     PyRun_SimpleString((char *)(cmd));
634 
635     PyGILState_Release(pygilstate);
636 
637 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
638     if (saved_locale != NULL)
639     {
640 	(void)setlocale(LC_NUMERIC, saved_locale);
641 	vim_free(saved_locale);
642     }
643 #endif
644 
645     Python_Lock_Vim();		    /* enter vim */
646     PythonIO_Flush();
647 #if defined(MACOS) && !defined(MACOS_X_UNIX)
648     SetPort(oldPort);
649 #endif
650 
651 theend:
652     return;	    /* keeps lint happy */
653 }
654 
655 /*
656  * ":py3"
657  */
658     void
659 ex_py3(exarg_T *eap)
660 {
661     char_u *script;
662 
663     script = script_get(eap, eap->arg);
664     if (!eap->skip)
665     {
666 	if (script == NULL)
667 	    DoPy3Command(eap, (char *)eap->arg);
668 	else
669 	    DoPy3Command(eap, (char *)script);
670     }
671     vim_free(script);
672 }
673 
674 #define BUFFER_SIZE 2048
675 
676 /*
677  * ":py3file"
678  */
679     void
680 ex_py3file(exarg_T *eap)
681 {
682     static char buffer[BUFFER_SIZE];
683     const char *file;
684     char *p;
685     int i;
686 
687     /* Have to do it like this. PyRun_SimpleFile requires you to pass a
688      * stdio file pointer, but Vim and the Python DLL are compiled with
689      * different options under Windows, meaning that stdio pointers aren't
690      * compatible between the two. Yuk.
691      *
692      * construct: exec(compile(open('a_filename').read(), 'a_filename', 'exec'))
693      *
694      * We need to escape any backslashes or single quotes in the file name, so that
695      * Python won't mangle the file name.
696      */
697 
698     strcpy(buffer, "exec(compile(open('");
699     p = buffer + 19; /* size of "exec(compile(open('" */
700 
701     for (i=0; i<2; ++i)
702     {
703 	file = (char *)eap->arg;
704 	while (*file && p < buffer + (BUFFER_SIZE - 3))
705 	{
706 	    if (*file == '\\' || *file == '\'')
707 		*p++ = '\\';
708 	    *p++ = *file++;
709 	}
710 	/* If we didn't finish the file name, we hit a buffer overflow */
711 	if (*file != '\0')
712 	    return;
713 	if (i==0)
714 	{
715 	    strcpy(p,"').read(),'");
716 	    p += 11;
717 	}
718 	else
719 	{
720 	    strcpy(p,"','exec'))");
721 	    p += 10;
722 	}
723     }
724 
725 
726     /* Execute the file */
727     DoPy3Command(eap, buffer);
728 }
729 
730 /******************************************************
731  * 2. Python output stream: writes output via [e]msg().
732  */
733 
734 /* Implementation functions
735  */
736 
737     static PyObject *
738 OutputGetattro(PyObject *self, PyObject *nameobj)
739 {
740     char *name = "";
741     if (PyUnicode_Check(nameobj))
742 	name = _PyUnicode_AsString(nameobj);
743 
744     if (strcmp(name, "softspace") == 0)
745 	return PyLong_FromLong(((OutputObject *)(self))->softspace);
746 
747     return PyObject_GenericGetAttr(self, nameobj);
748 }
749 
750     static int
751 OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
752 {
753     char *name = "";
754     if (PyUnicode_Check(nameobj))
755 	name = _PyUnicode_AsString(nameobj);
756 
757     if (val == NULL) {
758 	PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
759 	return -1;
760     }
761 
762     if (strcmp(name, "softspace") == 0)
763     {
764 	if (!PyLong_Check(val)) {
765 	    PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
766 	    return -1;
767 	}
768 
769 	((OutputObject *)(self))->softspace = PyLong_AsLong(val);
770 	return 0;
771     }
772 
773     PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
774     return -1;
775 }
776 
777 /***************/
778 
779     static int
780 PythonIO_Init(void)
781 {
782     PyType_Ready(&OutputType);
783     return PythonIO_Init_io();
784 }
785 
786     static void
787 PythonIO_Fini(void)
788 {
789     PySys_SetObject("stdout", NULL);
790     PySys_SetObject("stderr", NULL);
791 }
792 
793 /******************************************************
794  * 3. Implementation of the Vim module for Python
795  */
796 
797 /* Window type - Implementation functions
798  * --------------------------------------
799  */
800 
801 #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
802 
803 /* Buffer type - Implementation functions
804  * --------------------------------------
805  */
806 
807 #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
808 
809 static Py_ssize_t BufferLength(PyObject *);
810 static PyObject *BufferItem(PyObject *, Py_ssize_t);
811 static Py_ssize_t BufferAsItem(PyObject *, Py_ssize_t, PyObject *);
812 static PyObject* BufferSubscript(PyObject *self, PyObject* idx);
813 
814 
815 /* Line range type - Implementation functions
816  * --------------------------------------
817  */
818 
819 #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
820 
821 static PyObject* RangeSubscript(PyObject *self, PyObject* idx);
822 static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
823 
824 /* Current objects type - Implementation functions
825  * -----------------------------------------------
826  */
827 
828 static PySequenceMethods BufferAsSeq = {
829     (lenfunc)		BufferLength,	    /* sq_length,    len(x)   */
830     (binaryfunc)	0,		    /* sq_concat,    x+y      */
831     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
832     (ssizeargfunc)	BufferItem,	    /* sq_item,      x[i]     */
833     0,					    /* was_sq_slice,	 x[i:j]   */
834     (ssizeobjargproc)	BufferAsItem,	    /* sq_ass_item,  x[i]=v   */
835     0,					    /* sq_ass_slice, x[i:j]=v */
836     0,					    /* sq_contains */
837     0,					    /* sq_inplace_concat */
838     0,					    /* sq_inplace_repeat */
839 };
840 
841 PyMappingMethods BufferAsMapping = {
842     /* mp_length	*/ (lenfunc)BufferLength,
843     /* mp_subscript     */ (binaryfunc)BufferSubscript,
844     /* mp_ass_subscript */ (objobjargproc)0,
845 };
846 
847 
848 /* Buffer object - Definitions
849  */
850 
851 static PyTypeObject BufferType;
852 
853     static PyObject *
854 BufferNew(buf_T *buf)
855 {
856     /* We need to handle deletion of buffers underneath us.
857      * If we add a "b_python3_ref" field to the buf_T structure,
858      * then we can get at it in buf_freeall() in vim. We then
859      * need to create only ONE Python object per buffer - if
860      * we try to create a second, just INCREF the existing one
861      * and return it. The (single) Python object referring to
862      * the buffer is stored in "b_python3_ref".
863      * Question: what to do on a buf_freeall(). We'll probably
864      * have to either delete the Python object (DECREF it to
865      * zero - a bad idea, as it leaves dangling refs!) or
866      * set the buf_T * value to an invalid value (-1?), which
867      * means we need checks in all access functions... Bah.
868      */
869 
870     BufferObject *self;
871 
872     if (buf->b_python3_ref != NULL)
873     {
874 	self = buf->b_python3_ref;
875 	Py_INCREF(self);
876     }
877     else
878     {
879 	self = PyObject_NEW(BufferObject, &BufferType);
880 	buf->b_python3_ref = self;
881 	if (self == NULL)
882 	    return NULL;
883 	self->buf = buf;
884     }
885 
886     return (PyObject *)(self);
887 }
888 
889     static void
890 BufferDestructor(PyObject *self)
891 {
892     BufferObject *this = (BufferObject *)(self);
893 
894     if (this->buf && this->buf != INVALID_BUFFER_VALUE)
895 	this->buf->b_python3_ref = NULL;
896 }
897 
898     static PyObject *
899 BufferGetattro(PyObject *self, PyObject*nameobj)
900 {
901     BufferObject *this = (BufferObject *)(self);
902 
903     char *name = "";
904     if (PyUnicode_Check(nameobj))
905 	name = _PyUnicode_AsString(nameobj);
906 
907     if (CheckBuffer(this))
908 	return NULL;
909 
910     if (strcmp(name, "name") == 0)
911 	return Py_BuildValue("s", this->buf->b_ffname);
912     else if (strcmp(name, "number") == 0)
913 	return Py_BuildValue("n", this->buf->b_fnum);
914     else if (strcmp(name,"__members__") == 0)
915 	return Py_BuildValue("[ss]", "name", "number");
916     else
917 	return PyObject_GenericGetAttr(self, nameobj);
918 }
919 
920     static PyObject *
921 BufferRepr(PyObject *self)
922 {
923     static char repr[100];
924     BufferObject *this = (BufferObject *)(self);
925 
926     if (this->buf == INVALID_BUFFER_VALUE)
927     {
928 	vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
929 	return PyUnicode_FromString(repr);
930     }
931     else
932     {
933 	char *name = (char *)this->buf->b_fname;
934 	Py_ssize_t len;
935 
936 	if (name == NULL)
937 	    name = "";
938 	len = strlen(name);
939 
940 	if (len > 35)
941 	    name = name + (35 - len);
942 
943 	vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
944 
945 	return PyUnicode_FromString(repr);
946     }
947 }
948 
949 /******************/
950 
951     static Py_ssize_t
952 BufferLength(PyObject *self)
953 {
954     if (CheckBuffer((BufferObject *)(self)))
955 	return -1;
956 
957     return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
958 }
959 
960     static PyObject *
961 BufferItem(PyObject *self, Py_ssize_t n)
962 {
963     return RBItem((BufferObject *)(self), n, 1,
964 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
965 }
966 
967     static PyObject *
968 BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
969 {
970     return RBSlice((BufferObject *)(self), lo, hi, 1,
971 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
972 }
973 
974     static Py_ssize_t
975 BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
976 {
977     return RBAsItem((BufferObject *)(self), n, val, 1,
978 		(Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
979 		NULL);
980 }
981 
982 
983     static PyObject *
984 BufferSubscript(PyObject *self, PyObject* idx)
985 {
986     if (PyLong_Check(idx)) {
987 	long _idx = PyLong_AsLong(idx);
988 	return BufferItem(self,_idx);
989     } else if (PySlice_Check(idx)) {
990 	Py_ssize_t start, stop, step, slicelen;
991 
992 	if (PySlice_GetIndicesEx((PySliceObject *)idx,
993 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
994 	      &start, &stop,
995 	      &step, &slicelen) < 0) {
996 	    return NULL;
997 	}
998 	return BufferSlice(self,start,stop+1);
999     } else {
1000 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1001 	return NULL;
1002     }
1003 }
1004 
1005 static PySequenceMethods RangeAsSeq = {
1006     (lenfunc)		RangeLength,	 /* sq_length,	  len(x)   */
1007     (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
1008     (ssizeargfunc)	0,		 /* RangeRepeat, sq_repeat,  x*n   */
1009     (ssizeargfunc)	RangeItem,	 /* sq_item,	  x[i]	   */
1010     0,					 /* was_sq_slice,     x[i:j]   */
1011     (ssizeobjargproc)	RangeAsItem,	 /* sq_as_item,  x[i]=v   */
1012     0,					 /* sq_ass_slice, x[i:j]=v */
1013     0,					 /* sq_contains */
1014     0,					 /* sq_inplace_concat */
1015     0,					 /* sq_inplace_repeat */
1016 };
1017 
1018 PyMappingMethods RangeAsMapping = {
1019     /* mp_length	*/ (lenfunc)RangeLength,
1020     /* mp_subscript     */ (binaryfunc)RangeSubscript,
1021     /* mp_ass_subscript */ (objobjargproc)0,
1022 };
1023 
1024 /* Line range object - Implementation
1025  */
1026 
1027     static void
1028 RangeDestructor(PyObject *self)
1029 {
1030     Py_DECREF(((RangeObject *)(self))->buf);
1031 }
1032 
1033     static PyObject *
1034 RangeGetattro(PyObject *self, PyObject *nameobj)
1035 {
1036     char *name = "";
1037     if (PyUnicode_Check(nameobj))
1038 	name = _PyUnicode_AsString(nameobj);
1039 
1040     if (strcmp(name, "start") == 0)
1041 	return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
1042     else if (strcmp(name, "end") == 0)
1043 	return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
1044     else
1045 	return PyObject_GenericGetAttr(self, nameobj);
1046 }
1047 
1048 /****************/
1049 
1050     static Py_ssize_t
1051 RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
1052 {
1053     return RBAsItem(((RangeObject *)(self))->buf, n, val,
1054 		    ((RangeObject *)(self))->start,
1055 		    ((RangeObject *)(self))->end,
1056 		    &((RangeObject *)(self))->end);
1057 }
1058 
1059     static PyObject *
1060 RangeSubscript(PyObject *self, PyObject* idx)
1061 {
1062     if (PyLong_Check(idx)) {
1063 	long _idx = PyLong_AsLong(idx);
1064 	return RangeItem(self,_idx);
1065     } else if (PySlice_Check(idx)) {
1066 	Py_ssize_t start, stop, step, slicelen;
1067 
1068 	if (PySlice_GetIndicesEx((PySliceObject *)idx,
1069 		((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1070 		&start, &stop,
1071 		&step, &slicelen) < 0) {
1072 	    return NULL;
1073 	}
1074 	return RangeSlice(self,start,stop+1);
1075     } else {
1076 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1077 	return NULL;
1078     }
1079 }
1080 
1081 /* Buffer list object - Definitions
1082  */
1083 
1084 typedef struct
1085 {
1086     PyObject_HEAD
1087 } BufListObject;
1088 
1089 static PySequenceMethods BufListAsSeq = {
1090     (lenfunc)		BufListLength,	    /* sq_length,    len(x)   */
1091     (binaryfunc)	0,		    /* sq_concat,    x+y      */
1092     (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
1093     (ssizeargfunc)	BufListItem,	    /* sq_item,      x[i]     */
1094     0,					    /* was_sq_slice,	 x[i:j]   */
1095     (ssizeobjargproc)	0,		    /* sq_as_item,  x[i]=v   */
1096     0,					    /* sq_ass_slice, x[i:j]=v */
1097     0,					    /* sq_contains */
1098     0,					    /* sq_inplace_concat */
1099     0,					    /* sq_inplace_repeat */
1100 };
1101 
1102 static PyTypeObject BufListType;
1103 
1104 /* Window object - Definitions
1105  */
1106 
1107 static struct PyMethodDef WindowMethods[] = {
1108     /* name,	    function,		calling,    documentation */
1109     { NULL,	    NULL,		0,	    NULL }
1110 };
1111 
1112 static PyTypeObject WindowType;
1113 
1114 /* Window object - Implementation
1115  */
1116 
1117     static PyObject *
1118 WindowNew(win_T *win)
1119 {
1120     /* We need to handle deletion of windows underneath us.
1121      * If we add a "w_python3_ref" field to the win_T structure,
1122      * then we can get at it in win_free() in vim. We then
1123      * need to create only ONE Python object per window - if
1124      * we try to create a second, just INCREF the existing one
1125      * and return it. The (single) Python object referring to
1126      * the window is stored in "w_python3_ref".
1127      * On a win_free() we set the Python object's win_T* field
1128      * to an invalid value. We trap all uses of a window
1129      * object, and reject them if the win_T* field is invalid.
1130      */
1131 
1132     WindowObject *self;
1133 
1134     if (win->w_python3_ref)
1135     {
1136 	self = win->w_python3_ref;
1137 	Py_INCREF(self);
1138     }
1139     else
1140     {
1141 	self = PyObject_NEW(WindowObject, &WindowType);
1142 	if (self == NULL)
1143 	    return NULL;
1144 	self->win = win;
1145 	win->w_python3_ref = self;
1146     }
1147 
1148     return (PyObject *)(self);
1149 }
1150 
1151     static void
1152 WindowDestructor(PyObject *self)
1153 {
1154     WindowObject *this = (WindowObject *)(self);
1155 
1156     if (this->win && this->win != INVALID_WINDOW_VALUE)
1157 	this->win->w_python3_ref = NULL;
1158 }
1159 
1160     static PyObject *
1161 WindowGetattro(PyObject *self, PyObject *nameobj)
1162 {
1163     WindowObject *this = (WindowObject *)(self);
1164 
1165     char *name = "";
1166     if (PyUnicode_Check(nameobj))
1167 	name = _PyUnicode_AsString(nameobj);
1168 
1169 
1170     if (CheckWindow(this))
1171 	return NULL;
1172 
1173     if (strcmp(name, "buffer") == 0)
1174 	return (PyObject *)BufferNew(this->win->w_buffer);
1175     else if (strcmp(name, "cursor") == 0)
1176     {
1177 	pos_T *pos = &this->win->w_cursor;
1178 
1179 	return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1180     }
1181     else if (strcmp(name, "height") == 0)
1182 	return Py_BuildValue("l", (long)(this->win->w_height));
1183 #ifdef FEAT_VERTSPLIT
1184     else if (strcmp(name, "width") == 0)
1185 	return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1186 #endif
1187     else if (strcmp(name,"__members__") == 0)
1188 	return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1189     else
1190 	return PyObject_GenericGetAttr(self, nameobj);
1191 }
1192 
1193     static int
1194 WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1195 {
1196     char *name = "";
1197 
1198     if (PyUnicode_Check(nameobj))
1199 	name = _PyUnicode_AsString(nameobj);
1200 
1201     return WindowSetattr(self, name, val);
1202 }
1203 
1204 /* Window list object - Definitions
1205  */
1206 
1207 typedef struct
1208 {
1209     PyObject_HEAD
1210 }
1211 WinListObject;
1212 
1213 static PySequenceMethods WinListAsSeq = {
1214     (lenfunc)	     WinListLength,	    /* sq_length,    len(x)   */
1215     (binaryfunc)     0,			    /* sq_concat,    x+y      */
1216     (ssizeargfunc)   0,			    /* sq_repeat,    x*n      */
1217     (ssizeargfunc)   WinListItem,	    /* sq_item,      x[i]     */
1218     0,					    /* sq_slice,     x[i:j]   */
1219     (ssizeobjargproc)0,			    /* sq_as_item,  x[i]=v   */
1220     0,					    /* sq_ass_slice, x[i:j]=v */
1221     0,					    /* sq_contains */
1222     0,					    /* sq_inplace_concat */
1223     0,					    /* sq_inplace_repeat */
1224 };
1225 
1226 static PyTypeObject WinListType;
1227 
1228 /* Current items object - Definitions
1229  */
1230 
1231 typedef struct
1232 {
1233     PyObject_HEAD
1234 } CurrentObject;
1235 
1236 static PyTypeObject CurrentType;
1237 
1238 /* Current items object - Implementation
1239  */
1240     static PyObject *
1241 CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
1242 {
1243     char *name = "";
1244     if (PyUnicode_Check(nameobj))
1245 	name = _PyUnicode_AsString(nameobj);
1246 
1247     if (strcmp(name, "buffer") == 0)
1248 	return (PyObject *)BufferNew(curbuf);
1249     else if (strcmp(name, "window") == 0)
1250 	return (PyObject *)WindowNew(curwin);
1251     else if (strcmp(name, "line") == 0)
1252 	return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
1253     else if (strcmp(name, "range") == 0)
1254 	return RangeNew(curbuf, RangeStart, RangeEnd);
1255     else if (strcmp(name,"__members__") == 0)
1256 	return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1257     else
1258     {
1259 	PyErr_SetString(PyExc_AttributeError, name);
1260 	return NULL;
1261     }
1262 }
1263 
1264     static int
1265 CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
1266 {
1267     char *name = "";
1268     if (PyUnicode_Check(nameobj))
1269 	name = _PyUnicode_AsString(nameobj);
1270 
1271     if (strcmp(name, "line") == 0)
1272     {
1273 	if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1274 	    return -1;
1275 
1276 	return 0;
1277     }
1278     else
1279     {
1280 	PyErr_SetString(PyExc_AttributeError, name);
1281 	return -1;
1282     }
1283 }
1284 
1285 /* External interface
1286  */
1287 
1288     void
1289 python3_buffer_free(buf_T *buf)
1290 {
1291     if (buf->b_python3_ref != NULL)
1292     {
1293 	BufferObject *bp = buf->b_python3_ref;
1294 	bp->buf = INVALID_BUFFER_VALUE;
1295 	buf->b_python3_ref = NULL;
1296     }
1297 }
1298 
1299 #if defined(FEAT_WINDOWS) || defined(PROTO)
1300     void
1301 python3_window_free(win_T *win)
1302 {
1303     if (win->w_python3_ref != NULL)
1304     {
1305 	WindowObject *wp = win->w_python3_ref;
1306 	wp->win = INVALID_WINDOW_VALUE;
1307 	win->w_python3_ref = NULL;
1308     }
1309 }
1310 #endif
1311 
1312 static BufListObject TheBufferList =
1313 {
1314     PyObject_HEAD_INIT(&BufListType)
1315 };
1316 
1317 static WinListObject TheWindowList =
1318 {
1319     PyObject_HEAD_INIT(&WinListType)
1320 };
1321 
1322 static CurrentObject TheCurrent =
1323 {
1324     PyObject_HEAD_INIT(&CurrentType)
1325 };
1326 
1327 PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1328 
1329 static struct PyModuleDef vimmodule;
1330 
1331 #ifndef PROTO
1332 PyMODINIT_FUNC Py3Init_vim(void)
1333 {
1334     PyObject *mod;
1335     /* The special value is removed from sys.path in Python3_Init(). */
1336     static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1337 
1338     PyType_Ready(&BufferType);
1339     PyType_Ready(&RangeType);
1340     PyType_Ready(&WindowType);
1341     PyType_Ready(&BufListType);
1342     PyType_Ready(&WinListType);
1343     PyType_Ready(&CurrentType);
1344 
1345     /* Set sys.argv[] to avoid a crash in warn(). */
1346     PySys_SetArgv(1, argv);
1347 
1348     mod = PyModule_Create(&vimmodule);
1349 
1350     VimError = Py_BuildValue("s", "vim.error");
1351 
1352     PyModule_AddObject(mod, "error", VimError);
1353     Py_INCREF((PyObject *)(void *)&TheBufferList);
1354     PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1355     Py_INCREF((PyObject *)(void *)&TheCurrent);
1356     PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1357     Py_INCREF((PyObject *)(void *)&TheWindowList);
1358     PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1359 
1360     if (PyErr_Occurred())
1361 	return NULL;
1362 
1363     return mod;
1364 }
1365 #endif
1366 
1367 /*************************************************************************
1368  * 4. Utility functions for handling the interface between Vim and Python.
1369  */
1370 
1371 /* Convert a Vim line into a Python string.
1372  * All internal newlines are replaced by null characters.
1373  *
1374  * On errors, the Python exception data is set, and NULL is returned.
1375  */
1376     static PyObject *
1377 LineToString(const char *str)
1378 {
1379     PyObject *result;
1380     Py_ssize_t len = strlen(str);
1381     char *tmp,*p;
1382 
1383     tmp = (char *)alloc((unsigned)(len+1));
1384     p = tmp;
1385     if (p == NULL)
1386     {
1387 	PyErr_NoMemory();
1388 	return NULL;
1389     }
1390 
1391     while (*str)
1392     {
1393 	if (*str == '\n')
1394 	    *p = '\0';
1395 	else
1396 	    *p = *str;
1397 
1398 	++p;
1399 	++str;
1400     }
1401     *p = '\0';
1402 
1403     result = PyUnicode_FromStringAndSize(tmp, len);
1404 
1405     vim_free(tmp);
1406     return result;
1407 }
1408 
1409     static void
1410 init_structs(void)
1411 {
1412     vim_memset(&OutputType, 0, sizeof(OutputType));
1413     OutputType.tp_name = "vim.message";
1414     OutputType.tp_basicsize = sizeof(OutputObject);
1415     OutputType.tp_getattro = OutputGetattro;
1416     OutputType.tp_setattro = OutputSetattro;
1417     OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1418     OutputType.tp_doc = "vim message object";
1419     OutputType.tp_methods = OutputMethods;
1420     OutputType.tp_alloc = call_PyType_GenericAlloc;
1421     OutputType.tp_new = call_PyType_GenericNew;
1422     OutputType.tp_free = call_PyObject_Free;
1423 
1424     vim_memset(&BufferType, 0, sizeof(BufferType));
1425     BufferType.tp_name = "vim.buffer";
1426     BufferType.tp_basicsize = sizeof(BufferType);
1427     BufferType.tp_dealloc = BufferDestructor;
1428     BufferType.tp_repr = BufferRepr;
1429     BufferType.tp_as_sequence = &BufferAsSeq;
1430     BufferType.tp_as_mapping = &BufferAsMapping;
1431     BufferType.tp_getattro = BufferGetattro;
1432     BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1433     BufferType.tp_doc = "vim buffer object";
1434     BufferType.tp_methods = BufferMethods;
1435     BufferType.tp_alloc = call_PyType_GenericAlloc;
1436     BufferType.tp_new = call_PyType_GenericNew;
1437     BufferType.tp_free = call_PyObject_Free;
1438 
1439     vim_memset(&WindowType, 0, sizeof(WindowType));
1440     WindowType.tp_name = "vim.window";
1441     WindowType.tp_basicsize = sizeof(WindowObject);
1442     WindowType.tp_dealloc = WindowDestructor;
1443     WindowType.tp_repr = WindowRepr;
1444     WindowType.tp_getattro = WindowGetattro;
1445     WindowType.tp_setattro = WindowSetattro;
1446     WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1447     WindowType.tp_doc = "vim Window object";
1448     WindowType.tp_methods = WindowMethods;
1449     WindowType.tp_alloc = call_PyType_GenericAlloc;
1450     WindowType.tp_new = call_PyType_GenericNew;
1451     WindowType.tp_free = call_PyObject_Free;
1452 
1453     vim_memset(&BufListType, 0, sizeof(BufListType));
1454     BufListType.tp_name = "vim.bufferlist";
1455     BufListType.tp_basicsize = sizeof(BufListObject);
1456     BufListType.tp_as_sequence = &BufListAsSeq;
1457     BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1458     BufferType.tp_doc = "vim buffer list";
1459 
1460     vim_memset(&WinListType, 0, sizeof(WinListType));
1461     WinListType.tp_name = "vim.windowlist";
1462     WinListType.tp_basicsize = sizeof(WinListType);
1463     WinListType.tp_as_sequence = &WinListAsSeq;
1464     WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1465     WinListType.tp_doc = "vim window list";
1466 
1467     vim_memset(&RangeType, 0, sizeof(RangeType));
1468     RangeType.tp_name = "vim.range";
1469     RangeType.tp_basicsize = sizeof(RangeObject);
1470     RangeType.tp_dealloc = RangeDestructor;
1471     RangeType.tp_repr = RangeRepr;
1472     RangeType.tp_as_sequence = &RangeAsSeq;
1473     RangeType.tp_as_mapping = &RangeAsMapping;
1474     RangeType.tp_getattro = RangeGetattro;
1475     RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1476     RangeType.tp_doc = "vim Range object";
1477     RangeType.tp_methods = RangeMethods;
1478     RangeType.tp_alloc = call_PyType_GenericAlloc;
1479     RangeType.tp_new = call_PyType_GenericNew;
1480     RangeType.tp_free = call_PyObject_Free;
1481 
1482     vim_memset(&CurrentType, 0, sizeof(CurrentType));
1483     CurrentType.tp_name = "vim.currentdata";
1484     CurrentType.tp_basicsize = sizeof(CurrentObject);
1485     CurrentType.tp_getattro = CurrentGetattro;
1486     CurrentType.tp_setattro = CurrentSetattro;
1487     CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1488     CurrentType.tp_doc = "vim current object";
1489 
1490     vim_memset(&vimmodule, 0, sizeof(vimmodule));
1491     vimmodule.m_name = "vim";
1492     vimmodule.m_doc = vim_module_doc;
1493     vimmodule.m_size = -1;
1494     vimmodule.m_methods = VimMethods;
1495 }
1496