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