blob: ecca163b092283a6b6849ca3766189da49f90725 [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{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200606 if (--op->ob_refcnt != 0)
607 {
608# ifdef Py_REF_DEBUG
609 if (op->ob_refcnt < 0)
610 {
611 _Py_NegativeRefcount(filename, lineno, op);
612 }
613# endif
614 }
615 else
616 {
617 _Py_Dealloc(op);
618 }
619}
620
621# undef Py_DECREF
622# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
623
624 static inline void
625py3__Py_XDECREF(PyObject *op)
626{
627 if (op != NULL)
628 {
629 Py_DECREF(op);
630 }
631}
632
633# undef Py_XDECREF
634# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
635# endif
636
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200637/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200638 * Load library and get all pointers.
639 * Parameter 'libname' provides name of DLL.
640 * Return OK or FAIL.
641 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200642 static int
643py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200644{
645 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200646 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
647 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
648 PYTHON_PROC *ucs_as_encoded_string =
649 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200650
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100651# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100652 // Can't have Python and Python3 loaded at the same time.
653 // It cause a crash, because RTLD_GLOBAL is needed for
654 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200655 if (python_loaded())
656 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200657 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100658 emsg(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200659 return FAIL;
660 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200661# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200662
663 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200664 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200665 hinstPy3 = load_dll(libname);
666
667 if (!hinstPy3)
668 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200669 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100670 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200671 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200672 }
673
674 for (i = 0; py3_funcname_table[i].ptr; ++i)
675 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200676 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
677 py3_funcname_table[i].name)) == NULL)
678 {
679 close_dll(hinstPy3);
680 hinstPy3 = 0;
681 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100682 semsg(_(e_loadfunc), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200683 return FAIL;
684 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200685 }
686
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100687 // Load unicode functions separately as only the ucs2 or the ucs4 functions
688 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200689# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200690 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
691 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
692 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200693 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200694# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200695 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
696 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200697 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200698 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200699 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200700 if (*ucs_from_string == NULL || *ucs_decode == NULL
701 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200702 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200703 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200704 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200705 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200706 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200707 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200708 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200709 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200710# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200711 if (*ucs_from_string == NULL || *ucs_decode == NULL
712 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200713 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200714 close_dll(hinstPy3);
715 hinstPy3 = 0;
716 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 semsg(_(e_loadfunc), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200718 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200719 }
720
721 return OK;
722}
723
724/*
725 * If python is enabled (there is installed python on Windows system) return
726 * TRUE, else FALSE.
727 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200728 int
729python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200730{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100731 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200732}
733
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100734/*
735 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200736 * DLL, as this can cause errors (importing data symbols is not reliable).
737 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200738 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100739get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200740{
741 PyObject *exmod = PyImport_ImportModule("builtins");
742 PyObject *exdict = PyModule_GetDict(exmod);
743 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
744 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200745 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200746 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
747 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
748 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200749 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200750 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200751 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200752 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200753 Py_XINCREF(p3imp_PyExc_AttributeError);
754 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200755 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200756 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
757 Py_XINCREF(p3imp_PyExc_TypeError);
758 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200759 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200760 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200761 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200762 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763 Py_XDECREF(exmod);
764}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100765#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200766
Bram Moolenaardb913952012-06-29 12:54:53 +0200767static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200768#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200769static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200770
Bram Moolenaar774267b2013-05-21 20:51:59 +0200771#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200772
Bram Moolenaar971db462013-05-12 18:44:48 +0200773#define WIN_PYTHON_REF(win) win->w_python3_ref
774#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200775#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200776
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200777 static void
778call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200779{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200780#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200781 _PyObject_DebugFree(p);
782#else
783 PyObject_Free(p);
784#endif
785}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200786
787 static PyObject *
788call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200789{
790 return PyType_GenericNew(type,args,kwds);
791}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200792
793 static PyObject *
794call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200795{
796 return PyType_GenericAlloc(type,nitems);
797}
798
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200799static PyObject *OutputGetattro(PyObject *, PyObject *);
800static int OutputSetattro(PyObject *, PyObject *, PyObject *);
801static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200802static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200803static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200804static PyObject *WindowGetattro(PyObject *, PyObject *);
805static int WindowSetattro(PyObject *, PyObject *, PyObject *);
806static PyObject *RangeGetattro(PyObject *, PyObject *);
807static PyObject *CurrentGetattro(PyObject *, PyObject *);
808static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
809static PyObject *DictionaryGetattro(PyObject *, PyObject *);
810static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
811static PyObject *ListGetattro(PyObject *, PyObject *);
812static int ListSetattro(PyObject *, PyObject *, PyObject *);
813static PyObject *FunctionGetattro(PyObject *, PyObject *);
814
815static struct PyModuleDef vimmodule;
816
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200817#define PY_CAN_RECURSE
818
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200819/*
820 * Include the code shared with if_python.c
821 */
822#include "if_py_both.h"
823
Bram Moolenaar828bff12019-03-19 22:11:41 +0100824// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200825#define GET_ATTR_STRING(name, nameobj) \
826 char *name = ""; \
827 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100828 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200829
830#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
831
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100832///////////////////////////////////////////////////////
833// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200834
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100835static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200836
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100837///////////////////////////////////////////////////////
838// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200839
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200840 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100841python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200842{
843 static int recurse = 0;
844
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100845 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200846 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200847 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200848
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200849 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200850 ++recurse;
851
852#ifdef DYNAMIC_PYTHON3
853 if (hinstPy3)
854#endif
855 if (Py_IsInitialized())
856 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100857 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200858 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200859
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200860 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200861 }
862
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200863 --recurse;
864}
865
Bram Moolenaar094454f2015-10-07 10:39:55 +0200866#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200867 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100868python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200869{
870 return (hinstPy3 != 0);
871}
872#endif
873
Bram Moolenaar94073162018-01-31 21:49:05 +0100874static wchar_t *py_home_buf = NULL;
875
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200876 static int
877Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200878{
879 if (!py3initialised)
880 {
881#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200882 if (!python3_enabled(TRUE))
883 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100884 emsg(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200885 goto fail;
886 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200887#endif
888
889 init_structs();
890
Bram Moolenaar94073162018-01-31 21:49:05 +0100891 if (*p_py3home != NUL)
892 {
893 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100894
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100895 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200896 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +0100897 if (py_home_buf != NULL && mbstowcs(
898 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
899 Py_SetPythonHome(py_home_buf);
900 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100901#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +0100902 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +0100903 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100904#endif
905
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200906 PyImport_AppendInittab("vim", Py3Init_vim);
907
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200908 Py_Initialize();
Bram Moolenaard0573012017-10-28 21:11:06 +0200909
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100910 // Initialise threads, and below save the state using
911 // PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
912 // specific state (such as the system trace hook), will be lost
913 // between invocations of Python code.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200914 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200915#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200916 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917#endif
918
Bram Moolenaar1dc28782013-05-21 19:11:01 +0200919 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200920 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200921
Bram Moolenaardb913952012-06-29 12:54:53 +0200922 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
923
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100924 // Remove the element from sys.path that was added because of our
925 // argv[0] value in Py3Init_vim(). Previously we used an empty
926 // string, but depending on the OS we then get an empty entry or
927 // the current directory in sys.path.
928 // Only after vim has been imported, the element does exist in
929 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +0200930 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 +0200931
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100932 // lock is created and acquired in PyEval_InitThreads() and thread
933 // state is created in Py_Initialize()
934 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
935 // (python must have threads enabled!)
936 // so the following does both: unlock GIL and save thread state in TLS
937 // without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100938 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200939
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200940 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200941 }
942
943 return 0;
944
945fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100946 // We call PythonIO_Flush() here to print any Python errors.
947 // This is OK, as it is possible to call this function even
948 // if PythonIO_Init_io() has not completed successfully (it will
949 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200950 PythonIO_Flush();
951 return -1;
952}
953
954/*
955 * External interface
956 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200957 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200958DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200959{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200960#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200961 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200962#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200963 PyObject *cmdstr;
964 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +0200965 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200966
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200967 if (python_end_called)
968 goto theend;
969
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200970 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200971 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200972
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200973 init_range(arg);
974
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100975 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976
977#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100978 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979 saved_locale = setlocale(LC_NUMERIC, NULL);
980 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200981 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200982 else
983 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100984 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200985 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
986 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987 }
988#endif
989
990 pygilstate = PyGILState_Ensure();
991
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100992 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
993 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200994 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
995 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
996 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200997 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200998
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200999 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001000 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001001
1002 PyGILState_Release(pygilstate);
1003
1004#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1005 if (saved_locale != NULL)
1006 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001007 (void)setlocale(LC_NUMERIC, saved_locale);
1008 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001009 }
1010#endif
1011
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001012 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001013 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001014
1015theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001016 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001017}
1018
1019/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001020 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001021 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001022 void
1023ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001024{
1025 char_u *script;
1026
1027 script = script_get(eap, eap->arg);
1028 if (!eap->skip)
1029 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001030 if (p_pyx == 0)
1031 p_pyx = 3;
1032
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001033 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1034 (rangeinitializer) init_range_cmd,
1035 (runner) run_cmd,
1036 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001037 }
1038 vim_free(script);
1039}
1040
1041#define BUFFER_SIZE 2048
1042
1043/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001044 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001045 */
1046 void
1047ex_py3file(exarg_T *eap)
1048{
1049 static char buffer[BUFFER_SIZE];
1050 const char *file;
1051 char *p;
1052 int i;
1053
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001054 if (p_pyx == 0)
1055 p_pyx = 3;
1056
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001057 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1058 // stdio file pointer, but Vim and the Python DLL are compiled with
1059 // different options under Windows, meaning that stdio pointers aren't
1060 // compatible between the two. Yuk.
1061 //
1062 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1063 //
1064 // Using bytes so that Python can detect the source encoding as it normally
1065 // does. The doc does not say "compile" accept bytes, though.
1066 //
1067 // We need to escape any backslashes or single quotes in the file name, so that
1068 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001069
1070 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001071 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001072
1073 for (i=0; i<2; ++i)
1074 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001075 file = (char *)eap->arg;
1076 while (*file && p < buffer + (BUFFER_SIZE - 3))
1077 {
1078 if (*file == '\\' || *file == '\'')
1079 *p++ = '\\';
1080 *p++ = *file++;
1081 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001082 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001083 if (*file != '\0')
1084 return;
1085 if (i==0)
1086 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001087 strcpy(p,"','rb').read(),'");
1088 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001089 }
1090 else
1091 {
1092 strcpy(p,"','exec'))");
1093 p += 10;
1094 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001095 }
1096
1097
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001098 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001099 DoPyCommand(buffer,
1100 (rangeinitializer) init_range_cmd,
1101 (runner) run_cmd,
1102 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001103}
1104
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001105 void
1106ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001107{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001108 if (p_pyx == 0)
1109 p_pyx = 3;
1110
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001111 DoPyCommand((char *)eap->arg,
1112 (rangeinitializer)init_range_cmd,
1113 (runner)run_do,
1114 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001115}
1116
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001117///////////////////////////////////////////////////////
1118// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001119
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001120// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001121
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001122 static PyObject *
1123OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124{
Bram Moolenaar77045652012-09-21 13:46:06 +02001125 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001126
1127 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001128 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001129 else if (strcmp(name, "errors") == 0)
1130 return PyString_FromString("strict");
1131 else if (strcmp(name, "encoding") == 0)
1132 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001133
1134 return PyObject_GenericGetAttr(self, nameobj);
1135}
1136
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001137 static int
1138OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001139{
Bram Moolenaar77045652012-09-21 13:46:06 +02001140 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001141
Bram Moolenaard6e39182013-05-21 18:30:34 +02001142 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001143}
1144
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001145///////////////////////////////////////////////////////
1146// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001147
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001148// Window type - Implementation functions
1149// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001150
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001151#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1152
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001153// Buffer type - Implementation functions
1154// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001155
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001156#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1157
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001158static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1159static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001160
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001161// Line range type - Implementation functions
1162// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001163
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001164#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1165
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001166static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001167static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001168static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001169
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001170// Current objects type - Implementation functions
1171// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001172
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001173static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001174 (lenfunc) BufferLength, // sq_length, len(x)
1175 (binaryfunc) 0, // sq_concat, x+y
1176 (ssizeargfunc) 0, // sq_repeat, x*n
1177 (ssizeargfunc) BufferItem, // sq_item, x[i]
1178 0, // was_sq_slice, x[i:j]
1179 0, // sq_ass_item, x[i]=v
1180 0, // sq_ass_slice, x[i:j]=v
1181 0, // sq_contains
1182 0, // sq_inplace_concat
1183 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001184};
1185
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001186static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001187 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001189 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001190};
1191
1192
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001193// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001194
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001195 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001196BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001197{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001198 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001199
Bram Moolenaar77045652012-09-21 13:46:06 +02001200 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001201
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001202 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1203 return r;
1204
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001205 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001206 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001207
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001208 r = BufferAttr((BufferObject *)(self), name);
1209 if (r || PyErr_Occurred())
1210 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001212 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001213}
1214
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001215 static int
1216BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1217{
1218 GET_ATTR_STRING(name, nameobj);
1219
1220 return BufferSetattr((BufferObject *)(self), name, val);
1221}
1222
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001223//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001224
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001225 static PyObject *
1226BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001227{
Bram Moolenaardb913952012-06-29 12:54:53 +02001228 if (PyLong_Check(idx))
1229 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001230 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001231 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001232 } else if (PySlice_Check(idx))
1233 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001234 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001236 if (CheckBuffer((BufferObject *) self))
1237 return NULL;
1238
Bram Moolenaar922a4662014-03-30 16:11:43 +02001239 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001240 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001241 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001242 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001243 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001244 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001245 }
1246 else
1247 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001248 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001249 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001250 }
1251}
1252
Bram Moolenaar19e60942011-06-19 00:27:51 +02001253 static Py_ssize_t
1254BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1255{
Bram Moolenaardb913952012-06-29 12:54:53 +02001256 if (PyLong_Check(idx))
1257 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001258 long n = PyLong_AsLong(idx);
1259 return RBAsItem((BufferObject *)(self), n, val, 1,
1260 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1261 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001262 } else if (PySlice_Check(idx))
1263 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001264 Py_ssize_t start, stop, step, slicelen;
1265
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001266 if (CheckBuffer((BufferObject *) self))
1267 return -1;
1268
Bram Moolenaar922a4662014-03-30 16:11:43 +02001269 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001270 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001271 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001272 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001273 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001274 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1275 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1276 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001277 }
1278 else
1279 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001280 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001281 return -1;
1282 }
1283}
1284
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001285static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001286 (lenfunc) RangeLength, // sq_length, len(x)
1287 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1288 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1289 (ssizeargfunc) RangeItem, // sq_item, x[i]
1290 0, // was_sq_slice, x[i:j]
1291 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1292 0, // sq_ass_slice, x[i:j]=v
1293 0, // sq_contains
1294 0, // sq_inplace_concat
1295 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001296};
1297
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001298static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001299 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001300 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001301 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001302};
1303
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001304// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001305
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001306 static PyObject *
1307RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001308{
Bram Moolenaar77045652012-09-21 13:46:06 +02001309 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001310
1311 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001312 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001314 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001315 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001316 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001317}
1318
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001319////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001320
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001321 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001322RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001323{
1324 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001325 ((RangeObject *)(self))->start,
1326 ((RangeObject *)(self))->end,
1327 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001328}
1329
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001330 static Py_ssize_t
1331RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1332{
1333 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1334 ((RangeObject *)(self))->start,
1335 ((RangeObject *)(self))->end,
1336 &((RangeObject *)(self))->end);
1337}
1338
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001339 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001340RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001341{
Bram Moolenaardb913952012-06-29 12:54:53 +02001342 if (PyLong_Check(idx))
1343 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001344 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001345 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001346 } else if (PySlice_Check(idx))
1347 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001349
Bram Moolenaar922a4662014-03-30 16:11:43 +02001350 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1352 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001353 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001355 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001356 }
1357 else
1358 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001359 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001360 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001361 }
1362}
1363
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001364 static Py_ssize_t
1365RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1366{
Bram Moolenaardb913952012-06-29 12:54:53 +02001367 if (PyLong_Check(idx))
1368 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001369 long n = PyLong_AsLong(idx);
1370 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001371 }
1372 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001373 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001374 Py_ssize_t start, stop, step, slicelen;
1375
Bram Moolenaar922a4662014-03-30 16:11:43 +02001376 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001377 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1378 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001379 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001380 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001381 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001382 }
1383 else
1384 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001385 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001386 return -1;
1387 }
1388}
1389
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001390// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001391
1392 static PyObject *
1393TabPageGetattro(PyObject *self, PyObject *nameobj)
1394{
1395 PyObject *r;
1396
1397 GET_ATTR_STRING(name, nameobj);
1398
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001399 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1400 return r;
1401
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001402 if (CheckTabPage((TabPageObject *)(self)))
1403 return NULL;
1404
1405 r = TabPageAttr((TabPageObject *)(self), name);
1406 if (r || PyErr_Occurred())
1407 return r;
1408 else
1409 return PyObject_GenericGetAttr(self, nameobj);
1410}
1411
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001412// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001413
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001414 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001415WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001416{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001417 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001418
Bram Moolenaar77045652012-09-21 13:46:06 +02001419 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001421 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1422 return r;
1423
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001424 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001425 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001426
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001427 r = WindowAttr((WindowObject *)(self), name);
1428 if (r || PyErr_Occurred())
1429 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001430 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001432}
1433
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001434 static int
1435WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
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 Moolenaard6e39182013-05-21 18:30:34 +02001439 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001440}
1441
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001442// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001443
1444static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001445 (lenfunc) TabListLength, // sq_length, len(x)
1446 (binaryfunc) 0, // sq_concat, x+y
1447 (ssizeargfunc) 0, // sq_repeat, x*n
1448 (ssizeargfunc) TabListItem, // sq_item, x[i]
1449 0, // sq_slice, x[i:j]
1450 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1451 0, // sq_ass_slice, x[i:j]=v
1452 0, // sq_contains
1453 0, // sq_inplace_concat
1454 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001455};
1456
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001457// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001459static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001460 (lenfunc) WinListLength, // sq_length, len(x)
1461 (binaryfunc) 0, // sq_concat, x+y
1462 (ssizeargfunc) 0, // sq_repeat, x*n
1463 (ssizeargfunc) WinListItem, // sq_item, x[i]
1464 0, // sq_slice, x[i:j]
1465 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1466 0, // sq_ass_slice, x[i:j]=v
1467 0, // sq_contains
1468 0, // sq_inplace_concat
1469 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001470};
1471
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001472/*
1473 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001474 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001475 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001476CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001478 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001479 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001480 if (!(r = CurrentGetattr(self, name)))
1481 return PyObject_GenericGetAttr(self, nameobj);
1482 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001483}
1484
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001485 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001486CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001487{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001488 GET_ATTR_STRING(name, nameobj);
1489 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001490}
1491
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001493
Bram Moolenaar66b79852012-09-21 14:00:35 +02001494 static PyObject *
1495DictionaryGetattro(PyObject *self, PyObject *nameobj)
1496{
1497 DictionaryObject *this = ((DictionaryObject *) (self));
1498
1499 GET_ATTR_STRING(name, nameobj);
1500
1501 if (strcmp(name, "locked") == 0)
1502 return PyLong_FromLong(this->dict->dv_lock);
1503 else if (strcmp(name, "scope") == 0)
1504 return PyLong_FromLong(this->dict->dv_scope);
1505
1506 return PyObject_GenericGetAttr(self, nameobj);
1507}
1508
1509 static int
1510DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1511{
1512 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001513 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001514}
1515
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001516// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001517
Bram Moolenaar66b79852012-09-21 14:00:35 +02001518 static PyObject *
1519ListGetattro(PyObject *self, PyObject *nameobj)
1520{
1521 GET_ATTR_STRING(name, nameobj);
1522
1523 if (strcmp(name, "locked") == 0)
1524 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1525
1526 return PyObject_GenericGetAttr(self, nameobj);
1527}
1528
1529 static int
1530ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1531{
1532 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001533 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001534}
1535
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001536// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001537
Bram Moolenaardb913952012-06-29 12:54:53 +02001538 static PyObject *
1539FunctionGetattro(PyObject *self, PyObject *nameobj)
1540{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001541 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001542 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001543
1544 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001545
Bram Moolenaar8110a092016-04-14 15:56:09 +02001546 r = FunctionAttr(this, name);
1547 if (r || PyErr_Occurred())
1548 return r;
1549 else
1550 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001551}
1552
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001553// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001554
1555 void
1556python3_buffer_free(buf_T *buf)
1557{
Bram Moolenaar971db462013-05-12 18:44:48 +02001558 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001559 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001560 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001561 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001562 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001563 }
1564}
1565
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001566 void
1567python3_window_free(win_T *win)
1568{
Bram Moolenaar971db462013-05-12 18:44:48 +02001569 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001570 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001571 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001572 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001573 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001574 }
1575}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001576
1577 void
1578python3_tabpage_free(tabpage_T *tab)
1579{
1580 if (TAB_PYTHON_REF(tab) != NULL)
1581 {
1582 TabPageObject *tp = TAB_PYTHON_REF(tab);
1583 tp->tab = INVALID_TABPAGE_VALUE;
1584 TAB_PYTHON_REF(tab) = NULL;
1585 }
1586}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001587
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001588 static PyObject *
1589Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001590{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001591 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001592 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1593
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001594 if (init_types())
1595 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001596
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001597 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001598 PySys_SetArgv(1, argv);
1599
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001600 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001601 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001602
Bram Moolenaardee2e312013-06-23 16:35:47 +02001603 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001604 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001605
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001606 if (init_sys_path())
1607 return NULL;
1608
1609 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610}
1611
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001612//////////////////////////////////////////////////////////////////////////
1613// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001614
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001615/*
1616 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001617 * All internal newlines are replaced by null characters.
1618 *
1619 * On errors, the Python exception data is set, and NULL is returned.
1620 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001621 static PyObject *
1622LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001623{
1624 PyObject *result;
1625 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001626 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001627
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001628 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001629 p = tmp;
1630 if (p == NULL)
1631 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001632 PyErr_NoMemory();
1633 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001634 }
1635
1636 while (*str)
1637 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001638 if (*str == '\n')
1639 *p = '\0';
1640 else
1641 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001642
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643 ++p;
1644 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001645 }
1646 *p = '\0';
1647
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001648 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001649
1650 vim_free(tmp);
1651 return result;
1652}
1653
Bram Moolenaardb913952012-06-29 12:54:53 +02001654 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001655do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001656{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001657 DoPyCommand((char *) str,
1658 (rangeinitializer) init_range_eval,
1659 (runner) run_eval,
1660 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001661 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001662 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001663 rettv->v_type = VAR_NUMBER;
1664 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001665 }
1666}
1667
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001668 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001669set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001670{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001671 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001672}