blob: 1de8fe8d2033bee8a11b09b051ff898804041c6b [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 Moolenaar2e2f52a2020-12-21 16:03:02 +010084#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
85#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
86
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020087#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020088#ifndef PyString_Check
89# define PyString_Check(obj) PyUnicode_Check(obj)
90#endif
Bram Moolenaare0324612013-07-09 17:30:55 +020091#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010092 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +020093#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +020094#ifndef PyInt_Check
95# define PyInt_Check(obj) PyLong_Check(obj)
96#endif
Bram Moolenaar77045652012-09-21 13:46:06 +020097#define PyInt_FromLong(i) PyLong_FromLong(i)
98#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +020099#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200100#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200101
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100102#define PyIntArgFunc ssizeargfunc
103#define PyIntObjArgProc ssizeobjargproc
104
Bram Moolenaar922a4662014-03-30 16:11:43 +0200105/*
106 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
107 * to PyObject in Python 3.2 or later.
108 */
109#if PY_VERSION_HEX >= 0x030200f0
110typedef PyObject PySliceObject_T;
111#else
112typedef PySliceObject PySliceObject_T;
113#endif
114
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000115#ifndef MSWIN
116# define HINSTANCE void *
117#endif
118#if defined(DYNAMIC_PYTHON3) || defined(MSWIN)
119static HINSTANCE hinstPy3 = 0; // Instance of python.dll
120#endif
121
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100122#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200123
Bram Moolenaar4f974752019-02-17 17:44:42 +0100124# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200125# include <dlfcn.h>
126# define FARPROC void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100127# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200128# define load_dll(n) dlopen((n), RTLD_LAZY)
129# else
130# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
131# endif
132# define close_dll dlclose
133# define symbol_from_dll dlsym
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200134# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200135# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200136# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200137# define close_dll FreeLibrary
138# define symbol_from_dll GetProcAddress
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200139# define load_dll_error GetWin32Error
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200140# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200141/*
142 * Wrapper defines
143 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200144# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200145# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200146# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200147# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200148# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200149# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200150# define PyDict_SetItemString py3_PyDict_SetItemString
151# define PyErr_BadArgument py3_PyErr_BadArgument
152# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200153# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100154# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200155# define PyErr_NoMemory py3_PyErr_NoMemory
156# define PyErr_Occurred py3_PyErr_Occurred
157# define PyErr_SetNone py3_PyErr_SetNone
158# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200159# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200160# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200161# define PyEval_InitThreads py3_PyEval_InitThreads
162# define PyEval_RestoreThread py3_PyEval_RestoreThread
163# define PyEval_SaveThread py3_PyEval_SaveThread
164# define PyGILState_Ensure py3_PyGILState_Ensure
165# define PyGILState_Release py3_PyGILState_Release
166# define PyLong_AsLong py3_PyLong_AsLong
167# define PyLong_FromLong py3_PyLong_FromLong
168# define PyList_GetItem py3_PyList_GetItem
169# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200170# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200171# define PyList_New py3_PyList_New
172# define PyList_SetItem py3_PyList_SetItem
173# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200174# define PySequence_Check py3_PySequence_Check
175# define PySequence_Size py3_PySequence_Size
176# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200177# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200178# define PyTuple_Size py3_PyTuple_Size
179# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200180# if PY_VERSION_HEX >= 0x030601f0
181# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
182# define PySlice_Unpack py3_PySlice_Unpack
183# endif
184# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200185# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
186# define PyImport_ImportModule py3_PyImport_ImportModule
187# define PyObject_Init py3__PyObject_Init
188# define PyDict_New py3_PyDict_New
189# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200190# define PyDict_Next py3_PyDict_Next
191# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200192# ifndef PyMapping_Keys
193# define PyMapping_Keys py3_PyMapping_Keys
194# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200195# if PY_VERSION_HEX >= 0x030a00b2
196# define PyIter_Check py3_PyIter_Check
197# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200198# define PyIter_Next py3_PyIter_Next
199# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200200# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200201# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200202# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200203# define PyModule_GetDict py3_PyModule_GetDict
204#undef PyRun_SimpleString
205# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200206#undef PyRun_String
207# define PyRun_String py3_PyRun_String
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200208# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200209# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200210# define PyObject_SetAttrString py3_PyObject_SetAttrString
211# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200212# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200213# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200214# define PyEval_GetLocals py3_PyEval_GetLocals
215# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200216# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200217# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200218# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200219# define PyType_Ready py3_PyType_Ready
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200220# if PY_VERSION_HEX >= 0x030900b0
221# define PyType_GetFlags py3_PyType_GetFlags
222# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200223#undef Py_BuildValue
224# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100225# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200226# define Py_Initialize py3_Py_Initialize
227# define Py_Finalize py3_Py_Finalize
228# define Py_IsInitialized py3_Py_IsInitialized
229# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200230# define _Py_FalseStruct (*py3__Py_FalseStruct)
231# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200232# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200233# define PyModule_AddObject py3_PyModule_AddObject
234# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200235# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200236# if PY_VERSION_HEX >= 0x030300f0
237# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200238# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200239# else
240# define _PyUnicode_AsString py3__PyUnicode_AsString
241# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200242# undef PyUnicode_AsEncodedString
243# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
244# undef PyBytes_AsString
245# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200246# ifndef PyBytes_AsStringAndSize
247# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
248# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200249# undef PyBytes_FromString
250# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100251# undef PyBytes_FromStringAndSize
252# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200253# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
254# define _Py_Dealloc py3__Py_Dealloc
255# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200256# define PyFloat_FromDouble py3_PyFloat_FromDouble
257# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200258# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200259# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200260# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200261# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200262# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200263# define PyNumber_Check (*py3_PyNumber_Check)
264# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200265# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200266# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200267# ifdef Py_DEBUG
268# define _Py_NegativeRefcount py3__Py_NegativeRefcount
269# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200270# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
271# else
272# define PyModule_Create2 py3_PyModule_Create2
273# endif
274# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200275# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
276# define _PyObject_DebugFree py3__PyObject_DebugFree
277# else
278# define PyObject_Malloc py3_PyObject_Malloc
279# define PyObject_Free py3_PyObject_Free
280# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200281# define _PyObject_GC_New py3__PyObject_GC_New
282# define PyObject_GC_Del py3_PyObject_GC_Del
283# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284# define PyType_GenericAlloc py3_PyType_GenericAlloc
285# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200286# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200288# ifndef PyUnicode_FromFormat
289# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
290# else
291# define Py_UNICODE_USE_UCS_FUNCTIONS
292# ifdef Py_UNICODE_WIDE
293# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
294# else
295# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
296# endif
297# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200298# undef PyUnicode_Decode
299# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200300# define PyType_IsSubtype py3_PyType_IsSubtype
301# define PyCapsule_New py3_PyCapsule_New
302# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200303
Bram Moolenaar0014a532013-05-29 21:33:39 +0200304# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200305# undef PyObject_NEW
306# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200307( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200308 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200309# elif PY_VERSION_HEX >= 0x030900b0
310# undef PyObject_NEW
311# define PyObject_NEW(type, typeobj) \
312 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200313# endif
314
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200315/*
316 * Pointers for dynamic link
317 */
318static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100319static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200320static void (*py3_Py_Initialize)(void);
321static PyObject* (*py3_PyList_New)(Py_ssize_t size);
322static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
323static void (*py3_PyGILState_Release)(PyGILState_STATE);
324static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200325static PyObject* (*py3_PySys_GetObject)(char *);
326static int (*py3_PyList_Append)(PyObject *, PyObject *);
327static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200328static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200329static int (*py3_PySequence_Check)(PyObject *);
330static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
331static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200332static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200333static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
334static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
335static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200336static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200337# if PY_VERSION_HEX >= 0x030601f0
338static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
339 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
340static int (*py3_PySlice_Unpack)(PyObject *slice,
341 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
342# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200343static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100344 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
345 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200346static PyObject* (*py3_PyErr_NoMemory)(void);
347static void (*py3_Py_Finalize)(void);
348static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200349static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200350static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200351static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200352static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200353static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200354static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100355static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200356static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200357static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200358static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200359static PyObject* (*py3_PyEval_GetGlobals)();
360static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200361static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
362static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200363static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200364static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200365static PyObject* (*py3_PyErr_Occurred)(void);
366static PyObject* (*py3_PyModule_GetDict)(PyObject *);
367static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
368static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200369static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370static PyObject* (*py3_PyLong_FromLong)(long);
371static PyObject* (*py3_PyDict_New)(void);
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200372# if PY_VERSION_HEX >= 0x030a00b2
373static int (*py3_PyIter_Check)(PyObject *o);
374# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200375static PyObject* (*py3_PyIter_Next)(PyObject *);
376static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200377static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200378static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200379static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200380static PyObject* (*py3_Py_BuildValue)(char *, ...);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200381# if PY_VERSION_HEX >= 0x030900b0
382static int (*py3_PyType_GetFlags)(PyTypeObject *o);
383# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200384static int (*py3_PyType_Ready)(PyTypeObject *type);
385static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
386static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200387# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
388static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
389# else
390# ifdef Py_UNICODE_WIDE
391static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
392# else
393static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
394# endif
395# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200396static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
397 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200398static long (*py3_PyLong_AsLong)(PyObject *);
399static void (*py3_PyErr_SetNone)(PyObject *);
400static void (*py3_PyEval_InitThreads)(void);
401static void(*py3_PyEval_RestoreThread)(PyThreadState *);
402static PyThreadState*(*py3_PyEval_SaveThread)(void);
403static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
404static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200405static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200406static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200407static int (*py3_Py_IsInitialized)(void);
408static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200409static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100410static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200411static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200412static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200414static PyObject* py3__Py_FalseStruct;
415static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200416static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
417static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200418# if PY_VERSION_HEX >= 0x030300f0
419static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
420# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200422# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
424static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200425static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200426static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100427static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200428# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
429static void (*py3__Py_Dealloc)(PyObject *obj);
430# endif
431# if PY_VERSION_HEX >= 0x030900b0
432static PyObject* (*py3__PyObject_New)(PyTypeObject *);
433# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200434static PyObject* (*py3_PyFloat_FromDouble)(double num);
435static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200436static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200437static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
438static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200439static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200440static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200441static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200442static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200443static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200444static int (*py3_PyNumber_Check)(PyObject *);
445static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200446static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200447static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
448static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200449# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200450static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
451static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200452static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200453# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200454static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
455# endif
456# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
457static void (*py3__PyObject_DebugFree)(void*);
458static void* (*py3__PyObject_DebugMalloc)(size_t);
459# else
460static void (*py3_PyObject_Free)(void*);
461static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200462# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200463static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
464static void(*py3_PyObject_GC_Del)(void *);
465static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200466static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200467
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100468// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200469static PyObject *p3imp_PyExc_AttributeError;
470static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200471static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200472static PyObject *p3imp_PyExc_KeyboardInterrupt;
473static PyObject *p3imp_PyExc_TypeError;
474static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200475static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200476static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200477static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200478static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200479
480# define PyExc_AttributeError p3imp_PyExc_AttributeError
481# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200482# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200483# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
484# define PyExc_TypeError p3imp_PyExc_TypeError
485# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200486# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200487# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200488# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200489# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200490
491/*
492 * Table of name to function pointer of python.
493 */
494# define PYTHON_PROC FARPROC
495static struct
496{
497 char *name;
498 PYTHON_PROC *ptr;
499} py3_funcname_table[] =
500{
501 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100502 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200503 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200504 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
505 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200506 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200507 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200508 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
509 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
510 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
511 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200512 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200513 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200514 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200515 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200516 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
517 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
518 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200519 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200520 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
521 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200522# if PY_VERSION_HEX >= 0x030601f0
523 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
524 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
525# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200526 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
527 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
528 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
529 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200530 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200531 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200532 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200533 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200534 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200535 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200536 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
537 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200538 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200539 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200540 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
541 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200542 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
543 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200544 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200545 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200546 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
547 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
548 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
549 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200550 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
551 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200552 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200553# if PY_VERSION_HEX >= 0x030a00b2
554 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
555# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200556 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
557 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200558 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200559 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200560 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200561 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
562 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200563# if PY_VERSION_HEX >= 0x030900b0
564 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
565# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200566 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
567 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
568 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
569 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
570 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
571 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
572 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200573 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200574 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200575 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200576 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200577 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
578 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200579 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200580 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100581 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
583 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
584 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200585# if PY_VERSION_HEX >= 0x030300f0
586 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
587# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200588 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200589# endif
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200590# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
591 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
592# else
593# ifdef Py_UNICODE_WIDE
594 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
595# else
596 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
597# endif
598# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200599 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200600 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200601 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100602 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200603# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
604 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
605# endif
606# if PY_VERSION_HEX >= 0x030900b0
607 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
608# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200609 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
610 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200611 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200612 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
613 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200614 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200615 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200616 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200617 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200618 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200619 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
620 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200621 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200622# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200623 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
624 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200625 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
626# else
627 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
628# endif
629# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200630 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
631 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200632# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200633 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
634 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200635# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200636 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
637 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
638 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200639 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
640 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
641 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200642 {"", NULL},
643};
644
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200645# if PY_VERSION_HEX >= 0x030800f0
646 static inline void
647py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
648{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200649 if (--op->ob_refcnt != 0)
650 {
651# ifdef Py_REF_DEBUG
652 if (op->ob_refcnt < 0)
653 {
654 _Py_NegativeRefcount(filename, lineno, op);
655 }
656# endif
657 }
658 else
659 {
660 _Py_Dealloc(op);
661 }
662}
663
664# undef Py_DECREF
665# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
666
667 static inline void
668py3__Py_XDECREF(PyObject *op)
669{
670 if (op != NULL)
671 {
672 Py_DECREF(op);
673 }
674}
675
676# undef Py_XDECREF
677# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
678# endif
679
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200680# if PY_VERSION_HEX >= 0x030900b0
681 static inline int
682py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
683{
684 return ((PyType_GetFlags(type) & feature) != 0);
685}
686# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
687# endif
688
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200689# if PY_VERSION_HEX >= 0x030a00b2
690 static inline int
691py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
692{
693 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
694}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100695# if PY_VERSION_HEX >= 0x030b00b3
696# undef PyObject_TypeCheck
697# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
698# else
699# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
700# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200701# endif
702
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100703# ifdef MSWIN
704/*
705 * Look up the library "libname" using the InstallPath registry key.
706 * Return NULL when failed. Return an allocated string when successful.
707 */
708 static char *
709py3_get_system_libname(const char *libname)
710{
711 const char *cp = libname;
712 char subkey[128];
713 HKEY hKey;
714 char installpath[MAXPATHL];
715 LONG len = sizeof(installpath);
716 LSTATUS rc;
717 size_t sysliblen;
718 char *syslibname;
719
720 while (*cp != '\0')
721 {
722 if (*cp == ':' || *cp == '\\' || *cp == '/')
723 {
724 // Bail out if "libname" contains path separator, assume it is
725 // an absolute path.
726 return NULL;
727 }
728 ++cp;
729 }
730 vim_snprintf(subkey, sizeof(subkey),
731# ifdef _WIN64
732 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
733# else
734 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
735# endif
736 PY_MAJOR_VERSION, PY_MINOR_VERSION);
737 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
738 != ERROR_SUCCESS)
739 return NULL;
740 rc = RegQueryValueA(hKey, NULL, installpath, &len);
741 RegCloseKey(hKey);
742 if (ERROR_SUCCESS != rc)
743 return NULL;
744 cp = installpath + len;
745 // Just in case registry value contains null terminators.
746 while (cp > installpath && *(cp-1) == '\0')
747 --cp;
748 // Remove trailing path separators.
749 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
750 --cp;
751 // Ignore if InstallPath is effectively empty.
752 if (cp <= installpath)
753 return NULL;
754 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
755 syslibname = alloc(sysliblen);
756 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
757 (int)(cp - installpath), installpath, libname);
758 return syslibname;
759}
760# endif
761
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200762/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763 * Load library and get all pointers.
764 * Parameter 'libname' provides name of DLL.
765 * Return OK or FAIL.
766 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200767 static int
768py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200769{
770 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200771 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
772 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
773 PYTHON_PROC *ucs_as_encoded_string =
774 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200775
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100776# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100777 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000778 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100779 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200780 if (python_loaded())
781 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200782 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000783 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200784 return FAIL;
785 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200786# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200787
788 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200789 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790 hinstPy3 = load_dll(libname);
791
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100792# ifdef MSWIN
793 if (!hinstPy3)
794 {
795 // Attempt to use the path from InstallPath as stored in the registry.
796 char *syslibname = py3_get_system_libname(libname);
797
798 if (syslibname != NULL)
799 {
800 hinstPy3 = load_dll(syslibname);
801 vim_free(syslibname);
802 }
803 }
804# endif
805
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200806 if (!hinstPy3)
807 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200808 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000809 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200810 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200811 }
812
813 for (i = 0; py3_funcname_table[i].ptr; ++i)
814 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200815 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
816 py3_funcname_table[i].name)) == NULL)
817 {
818 close_dll(hinstPy3);
819 hinstPy3 = 0;
820 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000821 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200822 return FAIL;
823 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200824 }
825
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100826 // Load unicode functions separately as only the ucs2 or the ucs4 functions
827 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200828# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200829 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
830 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
831 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200832 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200833# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200834 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
835 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200836 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200837 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200838 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200839 if (*ucs_from_string == NULL || *ucs_decode == NULL
840 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200841 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200842 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200843 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200844 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200845 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200846 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200847 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200848 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200849# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200850 if (*ucs_from_string == NULL || *ucs_decode == NULL
851 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200853 close_dll(hinstPy3);
854 hinstPy3 = 0;
855 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000856 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200857 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200858 }
859
860 return OK;
861}
862
863/*
864 * If python is enabled (there is installed python on Windows system) return
865 * TRUE, else FALSE.
866 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200867 int
868python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200869{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100870 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200871}
872
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100873/*
874 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200875 * DLL, as this can cause errors (importing data symbols is not reliable).
876 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200877 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100878get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200879{
880 PyObject *exmod = PyImport_ImportModule("builtins");
881 PyObject *exdict = PyModule_GetDict(exmod);
882 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
883 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200884 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200885 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
886 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
887 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200888 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200889 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200890 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200891 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200892 Py_XINCREF(p3imp_PyExc_AttributeError);
893 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200894 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200895 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
896 Py_XINCREF(p3imp_PyExc_TypeError);
897 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200898 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200899 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200900 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200901 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200902 Py_XDECREF(exmod);
903}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100904#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200905
Bram Moolenaardb913952012-06-29 12:54:53 +0200906static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200907#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200908static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200909
Bram Moolenaar774267b2013-05-21 20:51:59 +0200910#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
Bram Moolenaardb913952012-06-29 12:54:53 +0200911
Bram Moolenaar971db462013-05-12 18:44:48 +0200912#define WIN_PYTHON_REF(win) win->w_python3_ref
913#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200914#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200915
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200916 static void
917call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200918{
Bram Moolenaar0014a532013-05-29 21:33:39 +0200919#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200920 _PyObject_DebugFree(p);
921#else
922 PyObject_Free(p);
923#endif
924}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200925
926 static PyObject *
927call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200928{
929 return PyType_GenericNew(type,args,kwds);
930}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200931
932 static PyObject *
933call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200934{
935 return PyType_GenericAlloc(type,nitems);
936}
937
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200938static PyObject *OutputGetattro(PyObject *, PyObject *);
939static int OutputSetattro(PyObject *, PyObject *, PyObject *);
940static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200941static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200942static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200943static PyObject *WindowGetattro(PyObject *, PyObject *);
944static int WindowSetattro(PyObject *, PyObject *, PyObject *);
945static PyObject *RangeGetattro(PyObject *, PyObject *);
946static PyObject *CurrentGetattro(PyObject *, PyObject *);
947static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
948static PyObject *DictionaryGetattro(PyObject *, PyObject *);
949static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
950static PyObject *ListGetattro(PyObject *, PyObject *);
951static int ListSetattro(PyObject *, PyObject *, PyObject *);
952static PyObject *FunctionGetattro(PyObject *, PyObject *);
953
954static struct PyModuleDef vimmodule;
955
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200956#define PY_CAN_RECURSE
957
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200958/*
959 * Include the code shared with if_python.c
960 */
961#include "if_py_both.h"
962
Bram Moolenaar828bff12019-03-19 22:11:41 +0100963// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200964#define GET_ATTR_STRING(name, nameobj) \
965 char *name = ""; \
966 if (PyUnicode_Check(nameobj)) \
Bram Moolenaar828bff12019-03-19 22:11:41 +0100967 name = (char *)_PyUnicode_AsString(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200968
969#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
970
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100971///////////////////////////////////////////////////////
972// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200973
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100974static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200975
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100976///////////////////////////////////////////////////////
977// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200978
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200979 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100980python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981{
982 static int recurse = 0;
983
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100984 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200985 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200986 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200988 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989 ++recurse;
990
991#ifdef DYNAMIC_PYTHON3
992 if (hinstPy3)
993#endif
994 if (Py_IsInitialized())
995 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100996 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200997 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200998
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200999 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001000 }
1001
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002 --recurse;
1003}
1004
Bram Moolenaar094454f2015-10-07 10:39:55 +02001005#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001006 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001007python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001008{
1009 return (hinstPy3 != 0);
1010}
1011#endif
1012
Bram Moolenaar94073162018-01-31 21:49:05 +01001013static wchar_t *py_home_buf = NULL;
1014
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001015#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001016/*
1017 * Return TRUE if stdin is readable from Python 3.
1018 */
1019 static BOOL
1020is_stdin_readable(void)
1021{
1022 DWORD mode, eventnum;
1023 struct _stat st;
1024 int fd = fileno(stdin);
1025 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1026
1027 // Check if stdin is connected to the console.
1028 if (GetConsoleMode(hstdin, &mode))
1029 // Check if it is opened as input.
1030 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1031
1032 return _fstat(fd, &st) == 0;
1033}
1034
1035// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1036// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001037// Note that the python DLL is linked to its own stdio DLL which can be
1038// differ from Vim's stdio.
1039 static void
1040reset_stdin(void)
1041{
1042 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1043 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001044 HINSTANCE hinst = hinstPy3;
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001045
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001046 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001047 return;
1048
1049 // Get "freopen" and "stdin" which are used in the python DLL.
1050 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1051 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1052 if (py__acrt_iob_func)
1053 {
1054 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001055 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001056 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001057 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001058 }
1059
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001060 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001061 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001062 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001063 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001064 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001065}
1066#else
1067# define reset_stdin()
1068#endif
1069
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001070// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1071// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1072// Install a hook to python dll's exit() and recover from it.
1073#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1074# define HOOK_EXIT
1075# include <setjmp.h>
1076
1077static jmp_buf exit_hook_jump_buf;
1078static void *orig_exit = NULL;
1079
1080/*
1081 * Function that replaces exit() while calling Py_Initialize().
1082 */
1083 static void
1084hooked_exit(int ret)
1085{
1086 // Recover from exit.
1087 longjmp(exit_hook_jump_buf, 1);
1088}
1089
1090/*
1091 * Install a hook to python dll's exit().
1092 */
1093 static void
1094hook_py_exit(void)
1095{
1096 HINSTANCE hinst = hinstPy3;
1097
1098 if (hinst == NULL || orig_exit != NULL)
1099 return;
1100
1101 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1102}
1103
1104/*
1105 * Remove the hook installed by hook_py_exit().
1106 */
1107 static void
1108restore_py_exit(void)
1109{
1110 HINSTANCE hinst = hinstPy3;
1111
1112 if (hinst == NULL)
1113 return;
1114
1115 if (orig_exit != NULL)
1116 hook_dll_import_func(hinst, "exit", orig_exit);
1117 orig_exit = NULL;
1118}
1119#endif
1120
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001121 static int
1122Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123{
1124 if (!py3initialised)
1125 {
1126#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001127 if (!python3_enabled(TRUE))
1128 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001129 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001130 goto fail;
1131 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001132#endif
1133
1134 init_structs();
1135
Bram Moolenaar94073162018-01-31 21:49:05 +01001136 if (*p_py3home != NUL)
1137 {
1138 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001139
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001140 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001141 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001142 if (py_home_buf != NULL && mbstowcs(
1143 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1144 Py_SetPythonHome(py_home_buf);
1145 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001146#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001147 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001148 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001149#endif
1150
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001151 PyImport_AppendInittab("vim", Py3Init_vim);
1152
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001153#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1154 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1155#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001156 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001157
1158#ifdef HOOK_EXIT
1159 // Catch exit() called in Py_Initialize().
1160 hook_py_exit();
1161 if (setjmp(exit_hook_jump_buf) == 0)
1162#endif
1163 {
1164 Py_Initialize();
1165#ifdef HOOK_EXIT
1166 restore_py_exit();
1167#endif
1168 }
1169#ifdef HOOK_EXIT
1170 else
1171 {
1172 // exit() was called in Py_Initialize().
1173 restore_py_exit();
1174 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1175 goto fail;
1176 }
1177#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001178
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001179#if PY_VERSION_HEX < 0x03090000
1180 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001181 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001182#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001183#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001184 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001185#endif
1186
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001187 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001188 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001189
Bram Moolenaardb913952012-06-29 12:54:53 +02001190 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1191
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001192 // Remove the element from sys.path that was added because of our
1193 // argv[0] value in Py3Init_vim(). Previously we used an empty
1194 // string, but depending on the OS we then get an empty entry or
1195 // the current directory in sys.path.
1196 // Only after vim has been imported, the element does exist in
1197 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001198 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 +02001199
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001200 // Without the call to PyEval_SaveThread, thread specific state (such
1201 // as the system trace hook), will be lost between invocations of
1202 // Python code.
1203 // GIL may have been created and acquired in PyEval_InitThreads() and
1204 // thread state is created in Py_Initialize(); there
1205 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1206 // have threads enabled!), so the following does both: unlock GIL and
1207 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001208 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001209
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001210 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211 }
1212
1213 return 0;
1214
1215fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001216 // We call PythonIO_Flush() here to print any Python errors.
1217 // This is OK, as it is possible to call this function even
1218 // if PythonIO_Init_io() has not completed successfully (it will
1219 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001220 PythonIO_Flush();
1221 return -1;
1222}
1223
1224/*
1225 * External interface
1226 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001227 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001228DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001229{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001230#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001231 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001232#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001233 PyObject *cmdstr;
1234 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001235 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001236
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001237 if (python_end_called)
1238 goto theend;
1239
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001240 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001241 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001242
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001243 init_range(arg);
1244
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001245 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001246
1247#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001248 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001249 saved_locale = setlocale(LC_NUMERIC, NULL);
1250 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001252 else
1253 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001254 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001255 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1256 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001257 }
1258#endif
1259
1260 pygilstate = PyGILState_Ensure();
1261
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001262 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1263 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001264 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001265 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1266 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001267 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001268
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001269 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001270 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001271
1272 PyGILState_Release(pygilstate);
1273
1274#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1275 if (saved_locale != NULL)
1276 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001277 (void)setlocale(LC_NUMERIC, saved_locale);
1278 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001279 }
1280#endif
1281
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001282 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001283 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001284
1285theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001286 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001287}
1288
1289/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001290 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001291 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001292 void
1293ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001294{
1295 char_u *script;
1296
1297 script = script_get(eap, eap->arg);
1298 if (!eap->skip)
1299 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001300 if (p_pyx == 0)
1301 p_pyx = 3;
1302
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001303 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
1304 (rangeinitializer) init_range_cmd,
1305 (runner) run_cmd,
1306 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001307 }
1308 vim_free(script);
1309}
1310
1311#define BUFFER_SIZE 2048
1312
1313/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001314 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001315 */
1316 void
1317ex_py3file(exarg_T *eap)
1318{
1319 static char buffer[BUFFER_SIZE];
1320 const char *file;
1321 char *p;
1322 int i;
1323
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001324 if (p_pyx == 0)
1325 p_pyx = 3;
1326
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001327 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1328 // stdio file pointer, but Vim and the Python DLL are compiled with
1329 // different options under Windows, meaning that stdio pointers aren't
1330 // compatible between the two. Yuk.
1331 //
1332 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1333 //
1334 // Using bytes so that Python can detect the source encoding as it normally
1335 // does. The doc does not say "compile" accept bytes, though.
1336 //
1337 // We need to escape any backslashes or single quotes in the file name, so that
1338 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001339
1340 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001341 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001342
1343 for (i=0; i<2; ++i)
1344 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001345 file = (char *)eap->arg;
1346 while (*file && p < buffer + (BUFFER_SIZE - 3))
1347 {
1348 if (*file == '\\' || *file == '\'')
1349 *p++ = '\\';
1350 *p++ = *file++;
1351 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001352 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001353 if (*file != '\0')
1354 return;
1355 if (i==0)
1356 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001357 strcpy(p,"','rb').read(),'");
1358 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 }
1360 else
1361 {
1362 strcpy(p,"','exec'))");
1363 p += 10;
1364 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001365 }
1366
1367
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001368 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001369 DoPyCommand(buffer,
1370 (rangeinitializer) init_range_cmd,
1371 (runner) run_cmd,
1372 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001373}
1374
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001375 void
1376ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001377{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001378 if (p_pyx == 0)
1379 p_pyx = 3;
1380
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001381 DoPyCommand((char *)eap->arg,
1382 (rangeinitializer)init_range_cmd,
1383 (runner)run_do,
1384 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001385}
1386
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001387///////////////////////////////////////////////////////
1388// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001389
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001390// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001391
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001392 static PyObject *
1393OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001394{
Bram Moolenaar77045652012-09-21 13:46:06 +02001395 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001396
1397 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001398 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001399 else if (strcmp(name, "errors") == 0)
1400 return PyString_FromString("strict");
1401 else if (strcmp(name, "encoding") == 0)
1402 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001403
1404 return PyObject_GenericGetAttr(self, nameobj);
1405}
1406
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001407 static int
1408OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001409{
Bram Moolenaar77045652012-09-21 13:46:06 +02001410 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001411
Bram Moolenaard6e39182013-05-21 18:30:34 +02001412 return OutputSetattr((OutputObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001413}
1414
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001415///////////////////////////////////////////////////////
1416// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001417
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001418// Window type - Implementation functions
1419// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001421#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1422
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001423// Buffer type - Implementation functions
1424// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001425
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001426#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1427
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001428static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001429static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001430
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001431// Line range type - Implementation functions
1432// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001433
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1435
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001436static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001437static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1438static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001439
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001440// Current objects type - Implementation functions
1441// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001442
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001444 (lenfunc) BufferLength, // sq_length, len(x)
1445 (binaryfunc) 0, // sq_concat, x+y
1446 (ssizeargfunc) 0, // sq_repeat, x*n
1447 (ssizeargfunc) BufferItem, // sq_item, x[i]
1448 0, // was_sq_slice, x[i:j]
1449 0, // sq_ass_item, x[i]=v
1450 0, // sq_ass_slice, x[i:j]=v
1451 0, // sq_contains
1452 0, // sq_inplace_concat
1453 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001454};
1455
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001456static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001457 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001459 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001460};
1461
1462
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001463// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001464
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001465 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001466BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001467{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001468 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001469
Bram Moolenaar77045652012-09-21 13:46:06 +02001470 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001471
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001472 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1473 return r;
1474
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001475 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001476 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001478 r = BufferAttr((BufferObject *)(self), name);
1479 if (r || PyErr_Occurred())
1480 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001481 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001482 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001483}
1484
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001485 static int
1486BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1487{
1488 GET_ATTR_STRING(name, nameobj);
1489
1490 return BufferSetattr((BufferObject *)(self), name, val);
1491}
1492
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001493//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001495 static PyObject *
1496BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001497{
Bram Moolenaardb913952012-06-29 12:54:53 +02001498 if (PyLong_Check(idx))
1499 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001500 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001501 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001502 } else if (PySlice_Check(idx))
1503 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001504 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001506 if (CheckBuffer((BufferObject *) self))
1507 return NULL;
1508
Bram Moolenaar922a4662014-03-30 16:11:43 +02001509 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001510 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001511 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001512 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001513 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001514 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001515 }
1516 else
1517 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001518 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001520 }
1521}
1522
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001523 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001524BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1525{
Bram Moolenaardb913952012-06-29 12:54:53 +02001526 if (PyLong_Check(idx))
1527 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001528 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001529
1530 if (CheckBuffer((BufferObject *) self))
1531 return -1;
1532
Bram Moolenaar19e60942011-06-19 00:27:51 +02001533 return RBAsItem((BufferObject *)(self), n, val, 1,
1534 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1535 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001536 } else if (PySlice_Check(idx))
1537 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001538 Py_ssize_t start, stop, step, slicelen;
1539
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001540 if (CheckBuffer((BufferObject *) self))
1541 return -1;
1542
Bram Moolenaar922a4662014-03-30 16:11:43 +02001543 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001544 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001545 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001546 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001547 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001548 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1549 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1550 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001551 }
1552 else
1553 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001554 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001555 return -1;
1556 }
1557}
1558
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001559static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001560 (lenfunc) RangeLength, // sq_length, len(x)
1561 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1562 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1563 (ssizeargfunc) RangeItem, // sq_item, x[i]
1564 0, // was_sq_slice, x[i:j]
1565 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1566 0, // sq_ass_slice, x[i:j]=v
1567 0, // sq_contains
1568 0, // sq_inplace_concat
1569 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001570};
1571
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001572static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001573 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001574 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001575 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001576};
1577
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001578// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001579
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001580 static PyObject *
1581RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001582{
Bram Moolenaar77045652012-09-21 13:46:06 +02001583 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001584
1585 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001586 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001587 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001588 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001589 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001590 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001591}
1592
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001593////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001594
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001595 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001596RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001597{
1598 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001599 ((RangeObject *)(self))->start,
1600 ((RangeObject *)(self))->end,
1601 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001602}
1603
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001604 static Py_ssize_t
1605RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1606{
1607 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1608 ((RangeObject *)(self))->start,
1609 ((RangeObject *)(self))->end,
1610 &((RangeObject *)(self))->end);
1611}
1612
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001613 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001614RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001615{
Bram Moolenaardb913952012-06-29 12:54:53 +02001616 if (PyLong_Check(idx))
1617 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001618 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001619 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001620 } else if (PySlice_Check(idx))
1621 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001622 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001623
Bram Moolenaar922a4662014-03-30 16:11:43 +02001624 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001625 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1626 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001627 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001629 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001630 }
1631 else
1632 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001633 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001634 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001635 }
1636}
1637
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001638 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001639RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1640{
Bram Moolenaardb913952012-06-29 12:54:53 +02001641 if (PyLong_Check(idx))
1642 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001643 long n = PyLong_AsLong(idx);
1644 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001645 }
1646 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001647 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001648 Py_ssize_t start, stop, step, slicelen;
1649
Bram Moolenaar922a4662014-03-30 16:11:43 +02001650 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001651 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1652 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001653 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001654 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001655 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001656 }
1657 else
1658 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001659 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001660 return -1;
1661 }
1662}
1663
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001664// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001665
1666 static PyObject *
1667TabPageGetattro(PyObject *self, PyObject *nameobj)
1668{
1669 PyObject *r;
1670
1671 GET_ATTR_STRING(name, nameobj);
1672
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001673 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1674 return r;
1675
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001676 if (CheckTabPage((TabPageObject *)(self)))
1677 return NULL;
1678
1679 r = TabPageAttr((TabPageObject *)(self), name);
1680 if (r || PyErr_Occurred())
1681 return r;
1682 else
1683 return PyObject_GenericGetAttr(self, nameobj);
1684}
1685
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001686// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001687
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001688 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001689WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001690{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001691 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001692
Bram Moolenaar77045652012-09-21 13:46:06 +02001693 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001694
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001695 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1696 return r;
1697
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001698 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001699 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001700
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001701 r = WindowAttr((WindowObject *)(self), name);
1702 if (r || PyErr_Occurred())
1703 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001704 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001705 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001706}
1707
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001708 static int
1709WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001710{
Bram Moolenaar77045652012-09-21 13:46:06 +02001711 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001712
Bram Moolenaard6e39182013-05-21 18:30:34 +02001713 return WindowSetattr((WindowObject *)(self), name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001714}
1715
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001716// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001717
1718static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001719 (lenfunc) TabListLength, // sq_length, len(x)
1720 (binaryfunc) 0, // sq_concat, x+y
1721 (ssizeargfunc) 0, // sq_repeat, x*n
1722 (ssizeargfunc) TabListItem, // sq_item, x[i]
1723 0, // sq_slice, x[i:j]
1724 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1725 0, // sq_ass_slice, x[i:j]=v
1726 0, // sq_contains
1727 0, // sq_inplace_concat
1728 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001729};
1730
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001731// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001732
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001733static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001734 (lenfunc) WinListLength, // sq_length, len(x)
1735 (binaryfunc) 0, // sq_concat, x+y
1736 (ssizeargfunc) 0, // sq_repeat, x*n
1737 (ssizeargfunc) WinListItem, // sq_item, x[i]
1738 0, // sq_slice, x[i:j]
1739 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1740 0, // sq_ass_slice, x[i:j]=v
1741 0, // sq_contains
1742 0, // sq_inplace_concat
1743 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001744};
1745
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001746/*
1747 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001748 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001749 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001750CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001751{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001752 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001753 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001754 if (!(r = CurrentGetattr(self, name)))
1755 return PyObject_GenericGetAttr(self, nameobj);
1756 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001757}
1758
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001759 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001760CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001761{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001762 GET_ATTR_STRING(name, nameobj);
1763 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001764}
1765
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001766// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001767
Bram Moolenaar66b79852012-09-21 14:00:35 +02001768 static PyObject *
1769DictionaryGetattro(PyObject *self, PyObject *nameobj)
1770{
1771 DictionaryObject *this = ((DictionaryObject *) (self));
1772
1773 GET_ATTR_STRING(name, nameobj);
1774
1775 if (strcmp(name, "locked") == 0)
1776 return PyLong_FromLong(this->dict->dv_lock);
1777 else if (strcmp(name, "scope") == 0)
1778 return PyLong_FromLong(this->dict->dv_scope);
1779
1780 return PyObject_GenericGetAttr(self, nameobj);
1781}
1782
1783 static int
1784DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1785{
1786 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001787 return DictionarySetattr((DictionaryObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001788}
1789
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001790// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001791
Bram Moolenaar66b79852012-09-21 14:00:35 +02001792 static PyObject *
1793ListGetattro(PyObject *self, PyObject *nameobj)
1794{
1795 GET_ATTR_STRING(name, nameobj);
1796
1797 if (strcmp(name, "locked") == 0)
1798 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1799
1800 return PyObject_GenericGetAttr(self, nameobj);
1801}
1802
1803 static int
1804ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1805{
1806 GET_ATTR_STRING(name, nameobj);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001807 return ListSetattr((ListObject *)(self), name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001808}
1809
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001810// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001811
Bram Moolenaardb913952012-06-29 12:54:53 +02001812 static PyObject *
1813FunctionGetattro(PyObject *self, PyObject *nameobj)
1814{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001815 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001816 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001817
1818 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001819
Bram Moolenaar8110a092016-04-14 15:56:09 +02001820 r = FunctionAttr(this, name);
1821 if (r || PyErr_Occurred())
1822 return r;
1823 else
1824 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001825}
1826
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001827// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001828
1829 void
1830python3_buffer_free(buf_T *buf)
1831{
Bram Moolenaar971db462013-05-12 18:44:48 +02001832 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001833 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001834 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001835 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001836 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001837 }
1838}
1839
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001840 void
1841python3_window_free(win_T *win)
1842{
Bram Moolenaar971db462013-05-12 18:44:48 +02001843 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001844 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001845 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001846 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001847 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001848 }
1849}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001850
1851 void
1852python3_tabpage_free(tabpage_T *tab)
1853{
1854 if (TAB_PYTHON_REF(tab) != NULL)
1855 {
1856 TabPageObject *tp = TAB_PYTHON_REF(tab);
1857 tp->tab = INVALID_TABPAGE_VALUE;
1858 TAB_PYTHON_REF(tab) = NULL;
1859 }
1860}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001861
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001862 static PyObject *
1863Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001864{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001865 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001866 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1867
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001868 if (init_types())
1869 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001870
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001871 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001872 PySys_SetArgv(1, argv);
1873
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001874 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001875 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001876
Bram Moolenaardee2e312013-06-23 16:35:47 +02001877 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001878 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001879
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001880 if (init_sys_path())
1881 return NULL;
1882
1883 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001884}
1885
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001886//////////////////////////////////////////////////////////////////////////
1887// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001888
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001889/*
1890 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001891 * All internal newlines are replaced by null characters.
1892 *
1893 * On errors, the Python exception data is set, and NULL is returned.
1894 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001895 static PyObject *
1896LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001897{
1898 PyObject *result;
1899 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01001900 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001901
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001902 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001903 p = tmp;
1904 if (p == NULL)
1905 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001906 PyErr_NoMemory();
1907 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001908 }
1909
1910 while (*str)
1911 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001912 if (*str == '\n')
1913 *p = '\0';
1914 else
1915 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001916
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001917 ++p;
1918 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001919 }
1920 *p = '\0';
1921
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001922 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001923
1924 vim_free(tmp);
1925 return result;
1926}
1927
Bram Moolenaardb913952012-06-29 12:54:53 +02001928 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001929do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02001930{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001931 DoPyCommand((char *) str,
1932 (rangeinitializer) init_range_eval,
1933 (runner) run_eval,
1934 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001935 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02001936 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001937 rettv->v_type = VAR_NUMBER;
1938 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001939 }
1940}
1941
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001942 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01001943set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02001944{
Bram Moolenaarb641df42015-02-03 13:16:04 +01001945 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02001946}