blob: 8c22514b741cf7d3108811c1ca0dfadded5485f7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002 *
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
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010025// uncomment this if used with the debug version of python
26// #define Py_DEBUG
27// Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting
28// uncomment this if used with the debug version of python, but without its
29// allocator
30// #define Py_DEBUG_NO_PYMALLOC
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020031
32#include "vim.h"
33
34#include <limits.h>
35
Bram Moolenaar4f974752019-02-17 17:44:42 +010036#if defined(MSWIN) && defined(HAVE_FCNTL_H)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020037# undef HAVE_FCNTL_H
38#endif
39
40#ifdef _DEBUG
41# undef _DEBUG
42#endif
43
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020044#ifdef F_BLANK
45# undef F_BLANK
46#endif
47
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010048#ifdef HAVE_STRFTIME
49# undef HAVE_STRFTIME
50#endif
51#ifdef HAVE_STRING_H
52# undef HAVE_STRING_H
53#endif
54#ifdef HAVE_PUTENV
55# undef HAVE_PUTENV
56#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020057#ifdef HAVE_STDARG_H
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010058# undef HAVE_STDARG_H // Python's config.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020059#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010060#ifdef _POSIX_C_SOURCE // defined in feature.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020061# undef _POSIX_C_SOURCE
62#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020063#ifdef _XOPEN_SOURCE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010064# undef _XOPEN_SOURCE // pyconfig.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020065#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020066
Bram Moolenaar0bdda372013-06-10 18:36:24 +020067#define PY_SSIZE_T_CLEAN
68
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020069#include <Python.h>
Bram Moolenaar0bdda372013-06-10 18:36:24 +020070
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010071#undef main // Defined in python.h - aargh
72#undef HAVE_FCNTL_H // Clash with os_win32.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020073
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010074// The "surrogateescape" error handler is new in Python 3.1
Bram Moolenaar3d64a312011-07-15 15:54:44 +020075#if PY_VERSION_HEX >= 0x030100f0
76# define CODEC_ERROR_HANDLER "surrogateescape"
77#else
78# define CODEC_ERROR_HANDLER NULL
79#endif
80
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010081// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020082#define PY_USE_CAPSULE
83
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020084#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020085#ifndef PyString_Check
86# define PyString_Check(obj) PyUnicode_Check(obj)
87#endif
Bram Moolenaare0324612013-07-09 17:30:55 +020088#define PyString_FromString(repr) \
89 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, NULL)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +020090#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020091#ifndef PyInt_Check
92# define PyInt_Check(obj) PyLong_Check(obj)
93#endif
Bram Moolenaar77045652012-09-21 13:46:06 +020094#define PyInt_FromLong(i) PyLong_FromLong(i)
95#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +020096#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +020097#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020098
Bram Moolenaar063a46b2014-01-14 16:36:51 +010099#define PyIntArgFunc ssizeargfunc
100#define PyIntObjArgProc ssizeobjargproc
101
Bram Moolenaar922a4662014-03-30 16:11:43 +0200102/*
103 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
104 * to PyObject in Python 3.2 or later.
105 */
106#if PY_VERSION_HEX >= 0x030200f0
107typedef PyObject PySliceObject_T;
108#else
109typedef PySliceObject PySliceObject_T;
110#endif
111
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100112#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200113
Bram Moolenaar4f974752019-02-17 17:44:42 +0100114# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200115# include <dlfcn.h>
116# define FARPROC void*
117# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100118# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200119# define load_dll(n) dlopen((n), RTLD_LAZY)
120# else
121# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
122# endif
123# define close_dll dlclose
124# define symbol_from_dll dlsym
125# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200126# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200127# define close_dll FreeLibrary
128# define symbol_from_dll GetProcAddress
129# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200130/*
131 * Wrapper defines
132 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200133# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200134# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200135# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200136# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200137# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200138# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200139# define PyDict_SetItemString py3_PyDict_SetItemString
140# define PyErr_BadArgument py3_PyErr_BadArgument
141# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200142# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100143# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200144# define PyErr_NoMemory py3_PyErr_NoMemory
145# define PyErr_Occurred py3_PyErr_Occurred
146# define PyErr_SetNone py3_PyErr_SetNone
147# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200148# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200149# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200150# define PyEval_InitThreads py3_PyEval_InitThreads
151# define PyEval_RestoreThread py3_PyEval_RestoreThread
152# define PyEval_SaveThread py3_PyEval_SaveThread
153# define PyGILState_Ensure py3_PyGILState_Ensure
154# define PyGILState_Release py3_PyGILState_Release
155# define PyLong_AsLong py3_PyLong_AsLong
156# define PyLong_FromLong py3_PyLong_FromLong
157# define PyList_GetItem py3_PyList_GetItem
158# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200159# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200160# define PyList_New py3_PyList_New
161# define PyList_SetItem py3_PyList_SetItem
162# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200163# define PySequence_Check py3_PySequence_Check
164# define PySequence_Size py3_PySequence_Size
165# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200166# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200167# define PyTuple_Size py3_PyTuple_Size
168# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200169# if PY_VERSION_HEX >= 0x030601f0
170# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
171# define PySlice_Unpack py3_PySlice_Unpack
172# endif
173# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200174# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
175# define PyImport_ImportModule py3_PyImport_ImportModule
176# define PyObject_Init py3__PyObject_Init
177# define PyDict_New py3_PyDict_New
178# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200179# define PyDict_Next py3_PyDict_Next
180# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200181# ifndef PyMapping_Keys
182# define PyMapping_Keys py3_PyMapping_Keys
183# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200184# define PyIter_Next py3_PyIter_Next
185# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200186# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200187# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200188# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200189# define PyModule_GetDict py3_PyModule_GetDict
190#undef PyRun_SimpleString
191# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200192#undef PyRun_String
193# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200194# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200195# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define PyObject_SetAttrString py3_PyObject_SetAttrString
197# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200198# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200199# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200200# define PyEval_GetLocals py3_PyEval_GetLocals
201# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200202# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200203# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200204# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200205# define PyType_Ready py3_PyType_Ready
206#undef Py_BuildValue
207# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100208# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200209# define Py_Initialize py3_Py_Initialize
210# define Py_Finalize py3_Py_Finalize
211# define Py_IsInitialized py3_Py_IsInitialized
212# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200213# define _Py_FalseStruct (*py3__Py_FalseStruct)
214# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200215# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200216# define PyModule_AddObject py3_PyModule_AddObject
217# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200218# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200219# if PY_VERSION_HEX >= 0x030300f0
220# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200221# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200222# else
223# define _PyUnicode_AsString py3__PyUnicode_AsString
224# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200225# undef PyUnicode_AsEncodedString
226# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
227# undef PyBytes_AsString
228# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200229# ifndef PyBytes_AsStringAndSize
230# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
231# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200232# undef PyBytes_FromString
233# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100234# undef PyBytes_FromStringAndSize
235# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200236# define PyFloat_FromDouble py3_PyFloat_FromDouble
237# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200238# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200239# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200240# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200241# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200242# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200243# define PyNumber_Check (*py3_PyNumber_Check)
244# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200245# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200246# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200247# ifdef Py_DEBUG
248# define _Py_NegativeRefcount py3__Py_NegativeRefcount
249# define _Py_RefTotal (*py3__Py_RefTotal)
250# define _Py_Dealloc py3__Py_Dealloc
Bram Moolenaar0014a532013-05-29 21:33:39 +0200251# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
252# else
253# define PyModule_Create2 py3_PyModule_Create2
254# endif
255# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200256# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
257# define _PyObject_DebugFree py3__PyObject_DebugFree
258# else
259# define PyObject_Malloc py3_PyObject_Malloc
260# define PyObject_Free py3_PyObject_Free
261# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200262# define _PyObject_GC_New py3__PyObject_GC_New
263# define PyObject_GC_Del py3_PyObject_GC_Del
264# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200265# define PyType_GenericAlloc py3_PyType_GenericAlloc
266# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200267# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200268# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200269# ifndef PyUnicode_FromFormat
270# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
271# else
272# define Py_UNICODE_USE_UCS_FUNCTIONS
273# ifdef Py_UNICODE_WIDE
274# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
275# else
276# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
277# endif
278# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200279# undef PyUnicode_Decode
280# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200281# define PyType_IsSubtype py3_PyType_IsSubtype
282# define PyCapsule_New py3_PyCapsule_New
283# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284
Bram Moolenaar0014a532013-05-29 21:33:39 +0200285# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200286# undef PyObject_NEW
287# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200288( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200289 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200290# endif
291
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200292/*
293 * Pointers for dynamic link
294 */
295static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100296static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297static void (*py3_Py_Initialize)(void);
298static PyObject* (*py3_PyList_New)(Py_ssize_t size);
299static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
300static void (*py3_PyGILState_Release)(PyGILState_STATE);
301static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200302static PyObject* (*py3_PySys_GetObject)(char *);
303static int (*py3_PyList_Append)(PyObject *, PyObject *);
304static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200305static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200306static int (*py3_PySequence_Check)(PyObject *);
307static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
308static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200309static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200310static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
311static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
312static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200313static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200314# if PY_VERSION_HEX >= 0x030601f0
315static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
316 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
317static int (*py3_PySlice_Unpack)(PyObject *slice,
318 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
319# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200320static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100321 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
322 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200323static PyObject* (*py3_PyErr_NoMemory)(void);
324static void (*py3_Py_Finalize)(void);
325static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200326static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200327static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200328static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200329static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200330static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200331static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100332static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200333static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200334static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200335static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200336static PyObject* (*py3_PyEval_GetGlobals)();
337static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200338static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
339static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200340static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200341static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200342static PyObject* (*py3_PyErr_Occurred)(void);
343static PyObject* (*py3_PyModule_GetDict)(PyObject *);
344static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
345static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200346static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200347static PyObject* (*py3_PyLong_FromLong)(long);
348static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200349static PyObject* (*py3_PyIter_Next)(PyObject *);
350static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200351static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200352static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200353static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200354static PyObject* (*py3_Py_BuildValue)(char *, ...);
355static int (*py3_PyType_Ready)(PyTypeObject *type);
356static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
357static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200358# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
359static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
360# else
361# ifdef Py_UNICODE_WIDE
362static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
363# else
364static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
365# endif
366# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200367static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
368 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200369static long (*py3_PyLong_AsLong)(PyObject *);
370static void (*py3_PyErr_SetNone)(PyObject *);
371static void (*py3_PyEval_InitThreads)(void);
372static void(*py3_PyEval_RestoreThread)(PyThreadState *);
373static PyThreadState*(*py3_PyEval_SaveThread)(void);
374static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
375static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200376static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200377static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378static int (*py3_Py_IsInitialized)(void);
379static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200380static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100381static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200382static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200383static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200384static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200385static PyObject* py3__Py_FalseStruct;
386static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200387static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
388static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200389# if PY_VERSION_HEX >= 0x030300f0
390static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
391# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200392static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200393# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200394static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
395static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200396static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200397static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100398static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200399static PyObject* (*py3_PyFloat_FromDouble)(double num);
400static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200401static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200402static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
403static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200404static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200405static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200406static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200407static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200408static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200409static int (*py3_PyNumber_Check)(PyObject *);
410static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200411static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200412static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
413static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200414# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200415static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
416static Py_ssize_t* py3__Py_RefTotal;
417static void (*py3__Py_Dealloc)(PyObject *obj);
418static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200419# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200420static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
421# endif
422# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
423static void (*py3__PyObject_DebugFree)(void*);
424static void* (*py3__PyObject_DebugMalloc)(size_t);
425# else
426static void (*py3_PyObject_Free)(void*);
427static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200428# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200429static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
430static void(*py3_PyObject_GC_Del)(void *);
431static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200432static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200433
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100434static HINSTANCE hinstPy3 = 0; // Instance of python.dll
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200435
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100436// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200437static PyObject *p3imp_PyExc_AttributeError;
438static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200439static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200440static PyObject *p3imp_PyExc_KeyboardInterrupt;
441static PyObject *p3imp_PyExc_TypeError;
442static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200443static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200444static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200445static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200446static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200447
448# define PyExc_AttributeError p3imp_PyExc_AttributeError
449# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200450# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200451# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
452# define PyExc_TypeError p3imp_PyExc_TypeError
453# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200454# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200455# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200456# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200457# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200458
459/*
460 * Table of name to function pointer of python.
461 */
462# define PYTHON_PROC FARPROC
463static struct
464{
465 char *name;
466 PYTHON_PROC *ptr;
467} py3_funcname_table[] =
468{
469 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100470 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200471 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200472 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
473 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200474 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200475 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200476 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
477 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
478 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
479 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200480 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200481 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200482 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200483 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200484 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
485 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
486 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200487 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200488 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
489 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200490# if PY_VERSION_HEX >= 0x030601f0
491 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
492 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
493# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
495 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
496 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
497 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200498 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200499 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200500 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200501 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200502 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200503 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200504 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
505 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200506 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200507 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200508 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
509 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200510 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
511 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200512 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200513 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200514 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
515 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
516 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
517 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200518 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
519 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200520 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Bram Moolenaardb913952012-06-29 12:54:53 +0200521 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
522 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200523 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200524 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200525 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200526 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
527 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200528 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
529 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
530 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
531 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
532 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
533 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
534 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200535 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200536 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200537 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200538 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200539 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
540 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200541 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200542 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100543 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200544 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
545 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
546 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200547# if PY_VERSION_HEX >= 0x030300f0
548 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
549# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200550 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200551# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200552# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
553 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
554# else
555# ifdef Py_UNICODE_WIDE
556 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
557# else
558 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
559# endif
560# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200561 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200562 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200563 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100564 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200565 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
566 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200567 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200568 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
569 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200570 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200571 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200572 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200573 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200574 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200575 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
576 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200577 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200578# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200579 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
580 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
581 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200582 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
583# else
584 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
585# endif
586# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200587 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
588 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200589# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200590 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
591 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200592# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200593 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
594 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
595 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200596 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
597 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
598 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200599 {"", NULL},
600};
601
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200602# if PY_VERSION_HEX >= 0x030800f0
603 static inline void
604py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
605{
606 _Py_DEC_REFTOTAL;
607 if (--op->ob_refcnt != 0)
608 {
609# ifdef Py_REF_DEBUG
610 if (op->ob_refcnt < 0)
611 {
612 _Py_NegativeRefcount(filename, lineno, op);
613 }
614# endif
615 }
616 else
617 {
618 _Py_Dealloc(op);
619 }
620}
621
622# undef Py_DECREF
623# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
624
625 static inline void
626py3__Py_XDECREF(PyObject *op)
627{
628 if (op != NULL)
629 {
630 Py_DECREF(op);
631 }
632}
633
634# undef Py_XDECREF
635# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
636# endif
637
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200638/*
639 * Free python.dll
640 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200641 static void
642end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200643{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200644 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200645 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200646 close_dll(hinstPy3);
647 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200648 }
649}
650
651/*
652 * Load library and get all pointers.
653 * Parameter 'libname' provides name of DLL.
654 * Return OK or FAIL.
655 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200656 static int
657py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200658{
659 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200660 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
661 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
662 PYTHON_PROC *ucs_as_encoded_string =
663 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200664
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100665# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100666 // Can't have Python and Python3 loaded at the same time.
667 // It cause a crash, because RTLD_GLOBAL is needed for
668 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200669 if (python_loaded())
670 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200671 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100672 emsg(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200673 return FAIL;
674 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200675# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200676
677 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200678 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200679 hinstPy3 = load_dll(libname);
680
681 if (!hinstPy3)
682 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200683 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100684 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200685 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200686 }
687
688 for (i = 0; py3_funcname_table[i].ptr; ++i)
689 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200690 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
691 py3_funcname_table[i].name)) == NULL)
692 {
693 close_dll(hinstPy3);
694 hinstPy3 = 0;
695 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100696 semsg(_(e_loadfunc), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200697 return FAIL;
698 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200699 }
700
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100701 // Load unicode functions separately as only the ucs2 or the ucs4 functions
702 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200703# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200704 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
705 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
706 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200707 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200708# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200709 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
710 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200711 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200712 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200713 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200714 if (*ucs_from_string == NULL || *ucs_decode == NULL
715 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200716 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200717 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200718 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200719 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200720 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200721 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200722 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200723 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200724# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200725 if (*ucs_from_string == NULL || *ucs_decode == NULL
726 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200727 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200728 close_dll(hinstPy3);
729 hinstPy3 = 0;
730 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731 semsg(_(e_loadfunc), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200733 }
734
735 return OK;
736}
737
738/*
739 * If python is enabled (there is installed python on Windows system) return
740 * TRUE, else FALSE.
741 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200742 int
743python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200744{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100745 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200746}
747
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100748/*
749 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200750 * DLL, as this can cause errors (importing data symbols is not reliable).
751 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200752 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100753get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200754{
755 PyObject *exmod = PyImport_ImportModule("builtins");
756 PyObject *exdict = PyModule_GetDict(exmod);
757 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
758 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200759 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200760 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
761 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
762 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200763 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200764 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200765 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200766 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200767 Py_XINCREF(p3imp_PyExc_AttributeError);
768 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200769 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200770 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
771 Py_XINCREF(p3imp_PyExc_TypeError);
772 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200773 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200774 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200775 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200776 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200777 Py_XDECREF(exmod);
778}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100779#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200780
Bram Moolenaardb913952012-06-29 12:54:53 +0200781static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200782#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200783static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200784
Bram Moolenaar774267b2013-05-21 20:51:59 +0200785#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200786
Bram Moolenaar971db462013-05-12 18:44:48 +0200787#define WIN_PYTHON_REF(win) win->w_python3_ref
788#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200789#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200790
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200791 static void
792call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200793{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200794#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200795 _PyObject_DebugFree(p);
796#else
797 PyObject_Free(p);
798#endif
799}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200800
801 static PyObject *
802call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200803{
804 return PyType_GenericNew(type,args,kwds);
805}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200806
807 static PyObject *
808call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200809{
810 return PyType_GenericAlloc(type,nitems);
811}
812
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200813static PyObject *OutputGetattro(PyObject *, PyObject *);
814static int OutputSetattro(PyObject *, PyObject *, PyObject *);
815static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200816static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200817static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200818static PyObject *WindowGetattro(PyObject *, PyObject *);
819static int WindowSetattro(PyObject *, PyObject *, PyObject *);
820static PyObject *RangeGetattro(PyObject *, PyObject *);
821static PyObject *CurrentGetattro(PyObject *, PyObject *);
822static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
823static PyObject *DictionaryGetattro(PyObject *, PyObject *);
824static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
825static PyObject *ListGetattro(PyObject *, PyObject *);
826static int ListSetattro(PyObject *, PyObject *, PyObject *);
827static PyObject *FunctionGetattro(PyObject *, PyObject *);
828
829static struct PyModuleDef vimmodule;
830
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200831#define PY_CAN_RECURSE
832
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200833/*
834 * Include the code shared with if_python.c
835 */
836#include "if_py_both.h"
837
Bram Moolenaar828bff12019-03-19 22:11:41 +0100838// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200839#define GET_ATTR_STRING(name, nameobj) \
840 char *name = ""; \
841 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100842 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200843
844#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
845
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100846///////////////////////////////////////////////////////
847// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200848
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100849static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200850
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100851///////////////////////////////////////////////////////
852// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200853
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200854 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100855python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200856{
857 static int recurse = 0;
858
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100859 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200861 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200862
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200863 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200864 ++recurse;
865
866#ifdef DYNAMIC_PYTHON3
867 if (hinstPy3)
868#endif
869 if (Py_IsInitialized())
870 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100871 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200872 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200873
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200874 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200875 }
876
877#ifdef DYNAMIC_PYTHON3
878 end_dynamic_python3();
879#endif
880
881 --recurse;
882}
883
Bram Moolenaar094454f2015-10-07 10:39:55 +0200884#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200885 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100886python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200887{
888 return (hinstPy3 != 0);
889}
890#endif
891
Bram Moolenaar94073162018-01-31 21:49:05 +0100892static wchar_t *py_home_buf = NULL;
893
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200894 static int
895Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200896{
897 if (!py3initialised)
898 {
899#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200900 if (!python3_enabled(TRUE))
901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 emsg(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200903 goto fail;
904 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200905#endif
906
907 init_structs();
908
Bram Moolenaar94073162018-01-31 21:49:05 +0100909 if (*p_py3home != NUL)
910 {
911 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100912
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100913 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200914 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +0100915 if (py_home_buf != NULL && mbstowcs(
916 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
917 Py_SetPythonHome(py_home_buf);
918 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100919#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +0100920 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +0100921 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100922#endif
923
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200924 PyImport_AppendInittab("vim", Py3Init_vim);
925
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200926 Py_Initialize();
Bram Moolenaard0573012017-10-28 21:11:06 +0200927
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100928 // Initialise threads, and below save the state using
929 // PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
930 // specific state (such as the system trace hook), will be lost
931 // between invocations of Python code.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200932 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200933#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200934 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200935#endif
936
Bram Moolenaar1dc28782013-05-21 19:11:01 +0200937 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200938 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200939
Bram Moolenaardb913952012-06-29 12:54:53 +0200940 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
941
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100942 // Remove the element from sys.path that was added because of our
943 // argv[0] value in Py3Init_vim(). Previously we used an empty
944 // string, but depending on the OS we then get an empty entry or
945 // the current directory in sys.path.
946 // Only after vim has been imported, the element does exist in
947 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +0200948 PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200949
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100950 // lock is created and acquired in PyEval_InitThreads() and thread
951 // state is created in Py_Initialize()
952 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
953 // (python must have threads enabled!)
954 // so the following does both: unlock GIL and save thread state in TLS
955 // without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100956 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200957
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200958 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200959 }
960
961 return 0;
962
963fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100964 // We call PythonIO_Flush() here to print any Python errors.
965 // This is OK, as it is possible to call this function even
966 // if PythonIO_Init_io() has not completed successfully (it will
967 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200968 PythonIO_Flush();
969 return -1;
970}
971
972/*
973 * External interface
974 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200975 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200976DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200977{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200978#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200979 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200980#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200981 PyObject *cmdstr;
982 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +0200983 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200984
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200985 if (python_end_called)
986 goto theend;
987
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200988 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200989 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200990
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200991 init_range(arg);
992
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100993 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994
995#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100996 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997 saved_locale = setlocale(LC_NUMERIC, NULL);
998 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200999 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001000 else
1001 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001002 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001003 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1004 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001005 }
1006#endif
1007
1008 pygilstate = PyGILState_Ensure();
1009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001010 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1011 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001012 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
1013 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1014 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001015 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001016
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001017 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001018 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001019
1020 PyGILState_Release(pygilstate);
1021
1022#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1023 if (saved_locale != NULL)
1024 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001025 (void)setlocale(LC_NUMERIC, saved_locale);
1026 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001027 }
1028#endif
1029
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001030 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001031 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001032
1033theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001034 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001035}
1036
1037/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001038 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001039 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001040 void
1041ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001042{
1043 char_u *script;
1044
1045 script = script_get(eap, eap->arg);
1046 if (!eap->skip)
1047 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001048 if (p_pyx == 0)
1049 p_pyx = 3;
1050
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001051 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1052 (rangeinitializer) init_range_cmd,
1053 (runner) run_cmd,
1054 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001055 }
1056 vim_free(script);
1057}
1058
1059#define BUFFER_SIZE 2048
1060
1061/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001062 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001063 */
1064 void
1065ex_py3file(exarg_T *eap)
1066{
1067 static char buffer[BUFFER_SIZE];
1068 const char *file;
1069 char *p;
1070 int i;
1071
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001072 if (p_pyx == 0)
1073 p_pyx = 3;
1074
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001075 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1076 // stdio file pointer, but Vim and the Python DLL are compiled with
1077 // different options under Windows, meaning that stdio pointers aren't
1078 // compatible between the two. Yuk.
1079 //
1080 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1081 //
1082 // Using bytes so that Python can detect the source encoding as it normally
1083 // does. The doc does not say "compile" accept bytes, though.
1084 //
1085 // We need to escape any backslashes or single quotes in the file name, so that
1086 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001087
1088 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001089 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001090
1091 for (i=0; i<2; ++i)
1092 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001093 file = (char *)eap->arg;
1094 while (*file && p < buffer + (BUFFER_SIZE - 3))
1095 {
1096 if (*file == '\\' || *file == '\'')
1097 *p++ = '\\';
1098 *p++ = *file++;
1099 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001100 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001101 if (*file != '\0')
1102 return;
1103 if (i==0)
1104 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001105 strcpy(p,"','rb').read(),'");
1106 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001107 }
1108 else
1109 {
1110 strcpy(p,"','exec'))");
1111 p += 10;
1112 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001113 }
1114
1115
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001116 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001117 DoPyCommand(buffer,
1118 (rangeinitializer) init_range_cmd,
1119 (runner) run_cmd,
1120 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001121}
1122
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001123 void
1124ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001125{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001126 if (p_pyx == 0)
1127 p_pyx = 3;
1128
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001129 DoPyCommand((char *)eap->arg,
1130 (rangeinitializer)init_range_cmd,
1131 (runner)run_do,
1132 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001133}
1134
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001135///////////////////////////////////////////////////////
1136// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001137
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001138// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001139
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001140 static PyObject *
1141OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001142{
Bram Moolenaar77045652012-09-21 13:46:06 +02001143 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001144
1145 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001146 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001147 else if (strcmp(name, "errors") == 0)
1148 return PyString_FromString("strict");
1149 else if (strcmp(name, "encoding") == 0)
1150 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001151
1152 return PyObject_GenericGetAttr(self, nameobj);
1153}
1154
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001155 static int
1156OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001157{
Bram Moolenaar77045652012-09-21 13:46:06 +02001158 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001159
Bram Moolenaard6e39182013-05-21 18:30:34 +02001160 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001161}
1162
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001163///////////////////////////////////////////////////////
1164// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001165
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001166// Window type - Implementation functions
1167// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001168
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001169#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1170
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001171// Buffer type - Implementation functions
1172// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001173
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001174#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1175
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001176static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1177static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001178
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001179// Line range type - Implementation functions
1180// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001181
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001182#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1183
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001184static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001185static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001186static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001187
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001188// Current objects type - Implementation functions
1189// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001190
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001191static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001192 (lenfunc) BufferLength, // sq_length, len(x)
1193 (binaryfunc) 0, // sq_concat, x+y
1194 (ssizeargfunc) 0, // sq_repeat, x*n
1195 (ssizeargfunc) BufferItem, // sq_item, x[i]
1196 0, // was_sq_slice, x[i:j]
1197 0, // sq_ass_item, x[i]=v
1198 0, // sq_ass_slice, x[i:j]=v
1199 0, // sq_contains
1200 0, // sq_inplace_concat
1201 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001202};
1203
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001204static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001205 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001206 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001207 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001208};
1209
1210
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001211// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001212
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001213 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001214BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001215{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001216 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001217
Bram Moolenaar77045652012-09-21 13:46:06 +02001218 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001219
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001220 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1221 return r;
1222
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001223 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001224 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001225
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001226 r = BufferAttr((BufferObject *)(self), name);
1227 if (r || PyErr_Occurred())
1228 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001229 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001230 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001231}
1232
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001233 static int
1234BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1235{
1236 GET_ATTR_STRING(name, nameobj);
1237
1238 return BufferSetattr((BufferObject *)(self), name, val);
1239}
1240
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001241//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001242
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001243 static PyObject *
1244BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001245{
Bram Moolenaardb913952012-06-29 12:54:53 +02001246 if (PyLong_Check(idx))
1247 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001248 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001249 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001250 } else if (PySlice_Check(idx))
1251 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001252 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001253
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001254 if (CheckBuffer((BufferObject *) self))
1255 return NULL;
1256
Bram Moolenaar922a4662014-03-30 16:11:43 +02001257 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001258 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001259 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001260 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001261 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001262 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001263 }
1264 else
1265 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001266 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001267 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001268 }
1269}
1270
Bram Moolenaar19e60942011-06-19 00:27:51 +02001271 static Py_ssize_t
1272BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1273{
Bram Moolenaardb913952012-06-29 12:54:53 +02001274 if (PyLong_Check(idx))
1275 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001276 long n = PyLong_AsLong(idx);
1277 return RBAsItem((BufferObject *)(self), n, val, 1,
1278 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1279 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001280 } else if (PySlice_Check(idx))
1281 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001282 Py_ssize_t start, stop, step, slicelen;
1283
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001284 if (CheckBuffer((BufferObject *) self))
1285 return -1;
1286
Bram Moolenaar922a4662014-03-30 16:11:43 +02001287 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001288 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001289 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001290 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001291 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001292 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1293 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1294 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001295 }
1296 else
1297 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001298 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001299 return -1;
1300 }
1301}
1302
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001303static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001304 (lenfunc) RangeLength, // sq_length, len(x)
1305 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1306 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1307 (ssizeargfunc) RangeItem, // sq_item, x[i]
1308 0, // was_sq_slice, x[i:j]
1309 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1310 0, // sq_ass_slice, x[i:j]=v
1311 0, // sq_contains
1312 0, // sq_inplace_concat
1313 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001314};
1315
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001316static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001317 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001318 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001319 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001320};
1321
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001322// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001323
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001324 static PyObject *
1325RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001326{
Bram Moolenaar77045652012-09-21 13:46:06 +02001327 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001328
1329 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001330 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001331 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001332 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001333 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001334 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001335}
1336
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001337////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001338
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001339 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001340RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001341{
1342 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001343 ((RangeObject *)(self))->start,
1344 ((RangeObject *)(self))->end,
1345 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001346}
1347
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001348 static Py_ssize_t
1349RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1350{
1351 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1352 ((RangeObject *)(self))->start,
1353 ((RangeObject *)(self))->end,
1354 &((RangeObject *)(self))->end);
1355}
1356
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001357 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001358RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001359{
Bram Moolenaardb913952012-06-29 12:54:53 +02001360 if (PyLong_Check(idx))
1361 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001362 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001363 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001364 } else if (PySlice_Check(idx))
1365 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001366 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001367
Bram Moolenaar922a4662014-03-30 16:11:43 +02001368 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001369 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1370 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001371 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001372 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001373 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001374 }
1375 else
1376 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001377 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001378 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001379 }
1380}
1381
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001382 static Py_ssize_t
1383RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1384{
Bram Moolenaardb913952012-06-29 12:54:53 +02001385 if (PyLong_Check(idx))
1386 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001387 long n = PyLong_AsLong(idx);
1388 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001389 }
1390 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001391 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001392 Py_ssize_t start, stop, step, slicelen;
1393
Bram Moolenaar922a4662014-03-30 16:11:43 +02001394 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001395 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1396 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001397 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001398 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001399 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001400 }
1401 else
1402 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001403 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001404 return -1;
1405 }
1406}
1407
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001408// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001409
1410 static PyObject *
1411TabPageGetattro(PyObject *self, PyObject *nameobj)
1412{
1413 PyObject *r;
1414
1415 GET_ATTR_STRING(name, nameobj);
1416
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001417 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1418 return r;
1419
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001420 if (CheckTabPage((TabPageObject *)(self)))
1421 return NULL;
1422
1423 r = TabPageAttr((TabPageObject *)(self), name);
1424 if (r || PyErr_Occurred())
1425 return r;
1426 else
1427 return PyObject_GenericGetAttr(self, nameobj);
1428}
1429
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001430// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001431
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001432 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001433WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001435 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001436
Bram Moolenaar77045652012-09-21 13:46:06 +02001437 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001438
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001439 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1440 return r;
1441
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001442 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001443 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001444
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001445 r = WindowAttr((WindowObject *)(self), name);
1446 if (r || PyErr_Occurred())
1447 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001448 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001449 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001450}
1451
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001452 static int
1453WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001454{
Bram Moolenaar77045652012-09-21 13:46:06 +02001455 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001456
Bram Moolenaard6e39182013-05-21 18:30:34 +02001457 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458}
1459
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001460// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001461
1462static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001463 (lenfunc) TabListLength, // sq_length, len(x)
1464 (binaryfunc) 0, // sq_concat, x+y
1465 (ssizeargfunc) 0, // sq_repeat, x*n
1466 (ssizeargfunc) TabListItem, // sq_item, x[i]
1467 0, // sq_slice, x[i:j]
1468 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1469 0, // sq_ass_slice, x[i:j]=v
1470 0, // sq_contains
1471 0, // sq_inplace_concat
1472 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001473};
1474
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001475// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001476
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001478 (lenfunc) WinListLength, // sq_length, len(x)
1479 (binaryfunc) 0, // sq_concat, x+y
1480 (ssizeargfunc) 0, // sq_repeat, x*n
1481 (ssizeargfunc) WinListItem, // sq_item, x[i]
1482 0, // sq_slice, x[i:j]
1483 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1484 0, // sq_ass_slice, x[i:j]=v
1485 0, // sq_contains
1486 0, // sq_inplace_concat
1487 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001488};
1489
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001490/*
1491 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001492 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001493 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001494CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001495{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001496 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001497 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001498 if (!(r = CurrentGetattr(self, name)))
1499 return PyObject_GenericGetAttr(self, nameobj);
1500 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001501}
1502
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001503 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001506 GET_ATTR_STRING(name, nameobj);
1507 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001508}
1509
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001510// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001511
Bram Moolenaar66b79852012-09-21 14:00:35 +02001512 static PyObject *
1513DictionaryGetattro(PyObject *self, PyObject *nameobj)
1514{
1515 DictionaryObject *this = ((DictionaryObject *) (self));
1516
1517 GET_ATTR_STRING(name, nameobj);
1518
1519 if (strcmp(name, "locked") == 0)
1520 return PyLong_FromLong(this->dict->dv_lock);
1521 else if (strcmp(name, "scope") == 0)
1522 return PyLong_FromLong(this->dict->dv_scope);
1523
1524 return PyObject_GenericGetAttr(self, nameobj);
1525}
1526
1527 static int
1528DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1529{
1530 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001531 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001532}
1533
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001534// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001535
Bram Moolenaar66b79852012-09-21 14:00:35 +02001536 static PyObject *
1537ListGetattro(PyObject *self, PyObject *nameobj)
1538{
1539 GET_ATTR_STRING(name, nameobj);
1540
1541 if (strcmp(name, "locked") == 0)
1542 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1543
1544 return PyObject_GenericGetAttr(self, nameobj);
1545}
1546
1547 static int
1548ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1549{
1550 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001551 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001552}
1553
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001554// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001555
Bram Moolenaardb913952012-06-29 12:54:53 +02001556 static PyObject *
1557FunctionGetattro(PyObject *self, PyObject *nameobj)
1558{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001559 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001560 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001561
1562 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001563
Bram Moolenaar8110a092016-04-14 15:56:09 +02001564 r = FunctionAttr(this, name);
1565 if (r || PyErr_Occurred())
1566 return r;
1567 else
1568 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001569}
1570
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001571// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001572
1573 void
1574python3_buffer_free(buf_T *buf)
1575{
Bram Moolenaar971db462013-05-12 18:44:48 +02001576 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001577 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001578 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001579 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001580 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001581 }
1582}
1583
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001584 void
1585python3_window_free(win_T *win)
1586{
Bram Moolenaar971db462013-05-12 18:44:48 +02001587 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001588 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001589 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001590 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001591 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001592 }
1593}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001594
1595 void
1596python3_tabpage_free(tabpage_T *tab)
1597{
1598 if (TAB_PYTHON_REF(tab) != NULL)
1599 {
1600 TabPageObject *tp = TAB_PYTHON_REF(tab);
1601 tp->tab = INVALID_TABPAGE_VALUE;
1602 TAB_PYTHON_REF(tab) = NULL;
1603 }
1604}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001605
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001606 static PyObject *
1607Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001608{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001609 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1611
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001612 if (init_types())
1613 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001614
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001615 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001616 PySys_SetArgv(1, argv);
1617
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001618 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001619 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001620
Bram Moolenaardee2e312013-06-23 16:35:47 +02001621 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001622 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001623
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001624 if (init_sys_path())
1625 return NULL;
1626
1627 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001628}
1629
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001630//////////////////////////////////////////////////////////////////////////
1631// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001632
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001633/*
1634 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001635 * All internal newlines are replaced by null characters.
1636 *
1637 * On errors, the Python exception data is set, and NULL is returned.
1638 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001639 static PyObject *
1640LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001641{
1642 PyObject *result;
1643 Py_ssize_t len = strlen(str);
1644 char *tmp,*p;
1645
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001646 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001647 p = tmp;
1648 if (p == NULL)
1649 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650 PyErr_NoMemory();
1651 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001652 }
1653
1654 while (*str)
1655 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 if (*str == '\n')
1657 *p = '\0';
1658 else
1659 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001660
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661 ++p;
1662 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001663 }
1664 *p = '\0';
1665
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001666 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001667
1668 vim_free(tmp);
1669 return result;
1670}
1671
Bram Moolenaardb913952012-06-29 12:54:53 +02001672 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001673do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001674{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001675 DoPyCommand((char *) str,
1676 (rangeinitializer) init_range_eval,
1677 (runner) run_eval,
1678 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001679 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001680 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001681 rettv->v_type = VAR_NUMBER;
1682 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001683 }
1684}
1685
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001686 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001687set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001688{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001689 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001690}