blob: b2eb1d473b4eb0f8056a438785df1c667d0dabba [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
ichizok6c3d3e62022-11-04 22:38:11 +000048#ifdef HAVE_DUP
49# undef HAVE_DUP
50#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010051#ifdef HAVE_STRFTIME
52# undef HAVE_STRFTIME
53#endif
54#ifdef HAVE_STRING_H
55# undef HAVE_STRING_H
56#endif
57#ifdef HAVE_PUTENV
58# undef HAVE_PUTENV
59#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020060#ifdef HAVE_STDARG_H
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010061# undef HAVE_STDARG_H // Python's config.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020062#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010063#ifdef _POSIX_C_SOURCE // defined in feature.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020064# undef _POSIX_C_SOURCE
65#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020066#ifdef _XOPEN_SOURCE
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010067# undef _XOPEN_SOURCE // pyconfig.h defines it as well.
Bram Moolenaar6df6f472010-07-18 18:04:50 +020068#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020069
Bram Moolenaar0bdda372013-06-10 18:36:24 +020070#define PY_SSIZE_T_CLEAN
71
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +020072#ifdef Py_LIMITED_API
73# define USE_LIMITED_API // Using Python 3 limited ABI
74#endif
75
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020076#include <Python.h>
Bram Moolenaar0bdda372013-06-10 18:36:24 +020077
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010078#undef main // Defined in python.h - aargh
79#undef HAVE_FCNTL_H // Clash with os_win32.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020080
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010081// The "surrogateescape" error handler is new in Python 3.1
Bram Moolenaar3d64a312011-07-15 15:54:44 +020082#if PY_VERSION_HEX >= 0x030100f0
83# define CODEC_ERROR_HANDLER "surrogateescape"
84#else
85# define CODEC_ERROR_HANDLER NULL
86#endif
87
Philip H5a5f17f2022-11-05 14:05:31 +000088// Suppress Python 3.11 depreciations to see useful warnings
Philip H422b9dc2023-08-11 22:38:48 +020089#ifdef __GNUC__
90# pragma GCC diagnostic push
91# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Philip H5a5f17f2022-11-05 14:05:31 +000092#endif
93
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010094// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020095#define PY_USE_CAPSULE
96
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010097#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
98#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
99
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200100#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200101#ifndef PyString_Check
102# define PyString_Check(obj) PyUnicode_Check(obj)
103#endif
Bram Moolenaare0324612013-07-09 17:30:55 +0200104#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100105 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200106#define PyString_FromFormat PyUnicode_FromFormat
Ken Takatac97b3fe2023-10-11 21:27:06 +0200107#ifdef PyUnicode_FromFormat
108# define Py_UNICODE_USE_UCS_FUNCTIONS
109#endif
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200110#ifndef PyInt_Check
111# define PyInt_Check(obj) PyLong_Check(obj)
112#endif
Bram Moolenaar77045652012-09-21 13:46:06 +0200113#define PyInt_FromLong(i) PyLong_FromLong(i)
114#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200115#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200116#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200117
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100118#define PyIntArgFunc ssizeargfunc
119#define PyIntObjArgProc ssizeobjargproc
120
Bram Moolenaar922a4662014-03-30 16:11:43 +0200121/*
122 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
123 * to PyObject in Python 3.2 or later.
124 */
125#if PY_VERSION_HEX >= 0x030200f0
126typedef PyObject PySliceObject_T;
127#else
128typedef PySliceObject PySliceObject_T;
129#endif
130
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000131#ifndef MSWIN
132# define HINSTANCE void *
133#endif
134#if defined(DYNAMIC_PYTHON3) || defined(MSWIN)
135static HINSTANCE hinstPy3 = 0; // Instance of python.dll
136#endif
137
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100138#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200139
Ken Takatac97b3fe2023-10-11 21:27:06 +0200140# ifdef MSWIN
141# define load_dll vimLoadLib
142# define close_dll FreeLibrary
143# define symbol_from_dll GetProcAddress
144# define load_dll_error GetWin32Error
145# else
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200146# include <dlfcn.h>
147# define FARPROC void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100148# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200149# define load_dll(n) dlopen((n), RTLD_LAZY)
150# else
151# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
152# endif
153# define close_dll dlclose
154# define symbol_from_dll dlsym
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200155# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200156# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200157/*
158 * Wrapper defines
159 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200160# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200161# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200162# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200163# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200164# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200165# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200166# define PyDict_SetItemString py3_PyDict_SetItemString
167# define PyErr_BadArgument py3_PyErr_BadArgument
168# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200169# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100170# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200171# define PyErr_NoMemory py3_PyErr_NoMemory
172# define PyErr_Occurred py3_PyErr_Occurred
173# define PyErr_SetNone py3_PyErr_SetNone
174# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200175# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200176# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200177# define PyEval_InitThreads py3_PyEval_InitThreads
178# define PyEval_RestoreThread py3_PyEval_RestoreThread
179# define PyEval_SaveThread py3_PyEval_SaveThread
180# define PyGILState_Ensure py3_PyGILState_Ensure
181# define PyGILState_Release py3_PyGILState_Release
182# define PyLong_AsLong py3_PyLong_AsLong
183# define PyLong_FromLong py3_PyLong_FromLong
184# define PyList_GetItem py3_PyList_GetItem
185# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200186# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200187# define PyList_New py3_PyList_New
188# define PyList_SetItem py3_PyList_SetItem
189# define PyList_Size py3_PyList_Size
Yegappan Lakshmanan038be272025-03-26 18:46:21 +0100190# define PyTuple_New py3_PyTuple_New
191# define PyTuple_GetItem py3_PyTuple_GetItem
192# define PyTuple_SetItem py3_PyTuple_SetItem
193# undef PyTuple_SET_ITEM
194# define PyTuple_SET_ITEM py3_PyTuple_SET_ITEM
195# define PyTuple_Size py3_PyTuple_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200196# define PySequence_Check py3_PySequence_Check
197# define PySequence_Size py3_PySequence_Size
198# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200199# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200200# if PY_VERSION_HEX >= 0x030601f0
201# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
202# define PySlice_Unpack py3_PySlice_Unpack
203# endif
204# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200205# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
206# define PyImport_ImportModule py3_PyImport_ImportModule
207# define PyObject_Init py3__PyObject_Init
208# define PyDict_New py3_PyDict_New
209# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200210# define PyDict_Next py3_PyDict_Next
211# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200212# ifndef PyMapping_Keys
213# define PyMapping_Keys py3_PyMapping_Keys
214# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200215# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
216 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
217# undef PyIter_Check
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200218# define PyIter_Check py3_PyIter_Check
219# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200220# define PyIter_Next py3_PyIter_Next
221# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200222# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200223# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200224# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200225# define PyModule_GetDict py3_PyModule_GetDict
Yee Cheng Chin50840362024-09-10 20:56:13 +0200226# if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000
Yee Cheng Chinc2285a82024-09-09 19:55:24 +0200227# define Py_IncRef py3_Py_IncRef
228# define Py_DecRef py3_Py_DecRef
Ken Takata9abd7152024-08-10 09:44:20 +0200229# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200230# ifdef USE_LIMITED_API
231# define Py_CompileString py3_Py_CompileString
232# define PyEval_EvalCode py3_PyEval_EvalCode
233# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200234# undef PyRun_SimpleString
235# define PyRun_SimpleString py3_PyRun_SimpleString
236# undef PyRun_String
237# define PyRun_String py3_PyRun_String
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200238# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200239# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200240# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200241# define PyObject_SetAttrString py3_PyObject_SetAttrString
242# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200243# if PY_VERSION_HEX >= 0x030d0000
244# define PyObject_CallFunction py3_PyObject_CallFunction
245# else
246# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
247# endif
Bram Moolenaarf4258302013-06-02 18:20:17 +0200248# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200249# define PyEval_GetLocals py3_PyEval_GetLocals
250# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200251# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200252# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200253# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200254# define PyType_Ready py3_PyType_Ready
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200255# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200256# define PyType_GetFlags py3_PyType_GetFlags
257# endif
Ken Takata119fdd92023-10-04 20:05:05 +0200258# undef Py_BuildValue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200259# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100260# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200261# define Py_Initialize py3_Py_Initialize
262# define Py_Finalize py3_Py_Finalize
263# define Py_IsInitialized py3_Py_IsInitialized
264# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200265# define _Py_FalseStruct (*py3__Py_FalseStruct)
266# define _Py_TrueStruct (*py3__Py_TrueStruct)
Yee Cheng Chin97a5be42024-09-09 19:46:17 +0200267# if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000
268// Private symbol that used to be required as part of PyIter_Check.
Ken Takata119fdd92023-10-04 20:05:05 +0200269# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
270# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200271# define PyModule_AddObject py3_PyModule_AddObject
272# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200273# define PyImport_AddModule py3_PyImport_AddModule
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200274# ifdef USE_LIMITED_API
275# if Py_LIMITED_API >= 0x030a0000
276# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
277# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200278# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200279# if PY_VERSION_HEX >= 0x030300f0
280# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
281# else
282# define _PyUnicode_AsString py3__PyUnicode_AsString
283# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200284# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200285# undef PyUnicode_CompareWithASCIIString
286# define PyUnicode_CompareWithASCIIString py3_PyUnicode_CompareWithASCIIString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200287# undef PyUnicode_AsEncodedString
288# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200289# undef PyUnicode_AsUTF8String
290# define PyUnicode_AsUTF8String py3_PyUnicode_AsUTF8String
Bram Moolenaar19e60942011-06-19 00:27:51 +0200291# undef PyBytes_AsString
292# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200293# ifndef PyBytes_AsStringAndSize
294# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
295# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200296# undef PyBytes_FromString
297# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100298# undef PyBytes_FromStringAndSize
299# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200300# define PyFloat_FromDouble py3_PyFloat_FromDouble
301# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200302# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200303# define PyType_Type (*py3_PyType_Type)
Ken Takata119fdd92023-10-04 20:05:05 +0200304# ifndef USE_LIMITED_API
305# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
306# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200307# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200308# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200309# define PyNumber_Check (*py3_PyNumber_Check)
310# define PyNumber_Long (*py3_PyNumber_Long)
Ken Takatafa145f22023-10-06 19:27:13 +0200311# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200312# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200313# ifdef Py_DEBUG
314# define _Py_NegativeRefcount py3__Py_NegativeRefcount
315# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200316# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
317# else
318# define PyModule_Create2 py3_PyModule_Create2
319# endif
320# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200321# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
322# define _PyObject_DebugFree py3__PyObject_DebugFree
323# else
324# define PyObject_Malloc py3_PyObject_Malloc
325# define PyObject_Free py3_PyObject_Free
326# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200327# define _PyObject_GC_New py3__PyObject_GC_New
328# define PyObject_GC_Del py3_PyObject_GC_Del
329# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200330# define PyType_GenericAlloc py3_PyType_GenericAlloc
331# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200332# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200333# define PyUnicode_FromString py3_PyUnicode_FromString
Ken Takatac97b3fe2023-10-11 21:27:06 +0200334# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200335# ifdef Py_UNICODE_WIDE
336# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
337# else
338# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
339# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200340# else
341# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200342# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200343# undef PyUnicode_Decode
344# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200345# define PyType_IsSubtype py3_PyType_IsSubtype
346# define PyCapsule_New py3_PyCapsule_New
347# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200348# ifdef USE_LIMITED_API
349# define PyType_GetSlot py3_PyType_GetSlot
350# define PyType_FromSpec py3_PyType_FromSpec
351# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200352
Bram Moolenaar0014a532013-05-29 21:33:39 +0200353# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200354# undef PyObject_NEW
355# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200356( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200357 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200358# elif PY_VERSION_HEX >= 0x030900b0
359# undef PyObject_NEW
360# define PyObject_NEW(type, typeobj) \
361 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200362# endif
363
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200364/*
365 * Pointers for dynamic link
366 */
367static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100368static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200369static void (*py3_Py_Initialize)(void);
370static PyObject* (*py3_PyList_New)(Py_ssize_t size);
371static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
372static void (*py3_PyGILState_Release)(PyGILState_STATE);
373static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200374static PyObject* (*py3_PySys_GetObject)(char *);
375static int (*py3_PyList_Append)(PyObject *, PyObject *);
376static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200377static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Yegappan Lakshmanan038be272025-03-26 18:46:21 +0100378static PyObject* (*py3_PyTuple_New)(Py_ssize_t size);
379static int (*py3_PyTuple_SetItem)(PyObject *, Py_ssize_t, PyObject *);
380static int (*py3_PyTuple_SET_ITEM)(PyObject *, Py_ssize_t, PyObject *);
381static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200382static int (*py3_PySequence_Check)(PyObject *);
383static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
384static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200385static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200386static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
387static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200388static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200389# if PY_VERSION_HEX >= 0x030601f0
390static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
391 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
392static int (*py3_PySlice_Unpack)(PyObject *slice,
393 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
394# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200395static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100396 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
397 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200398static PyObject* (*py3_PyErr_NoMemory)(void);
399static void (*py3_Py_Finalize)(void);
400static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200401static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200402static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Yee Cheng Chin50840362024-09-10 20:56:13 +0200403# if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000
Ken Takata9abd7152024-08-10 09:44:20 +0200404static void (*py3_Py_IncRef)(PyObject *);
Yee Cheng Chinc2285a82024-09-09 19:55:24 +0200405static void (*py3_Py_DecRef)(PyObject *);
Ken Takata9abd7152024-08-10 09:44:20 +0200406# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200407# ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200408static PyObject* (*py3_Py_CompileString)(const char *, const char *, int);
409static PyObject* (*py3_PyEval_EvalCode)(PyObject *co, PyObject *globals, PyObject *locals);
Ken Takatac97b3fe2023-10-11 21:27:06 +0200410# else
411static int (*py3_PyRun_SimpleString)(char *);
412static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200413# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200414static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200415static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100416static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200417static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200418# if PY_VERSION_HEX >= 0x030d0000
419static PyObject* (*py3_PyObject_CallFunction)(PyObject *, char *, ...);
420# else
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200421static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200422# endif
Bram Moolenaarf4258302013-06-02 18:20:17 +0200423static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200424static PyObject* (*py3_PyEval_GetGlobals)(void);
425static PyObject* (*py3_PyEval_GetLocals)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200426static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
427static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200428static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200430static PyObject* (*py3_PyErr_Occurred)(void);
431static PyObject* (*py3_PyModule_GetDict)(PyObject *);
432static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
433static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200434static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200435static PyObject* (*py3_PyLong_FromLong)(long);
436static PyObject* (*py3_PyDict_New)(void);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200437# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
438 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200439static int (*py3_PyIter_Check)(PyObject *o);
440# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200441static PyObject* (*py3_PyIter_Next)(PyObject *);
442static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200443static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200444static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200445static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200446static PyObject* (*py3_Py_BuildValue)(char *, ...);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200447# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200448static int (*py3_PyType_GetFlags)(PyTypeObject *o);
449# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200450static int (*py3_PyType_Ready)(PyTypeObject *type);
451static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
452static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Ken Takatac97b3fe2023-10-11 21:27:06 +0200453# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200454# ifdef Py_UNICODE_WIDE
455static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
456# else
457static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
458# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200459# else
460static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200461# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200462static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
463 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200464static long (*py3_PyLong_AsLong)(PyObject *);
465static void (*py3_PyErr_SetNone)(PyObject *);
466static void (*py3_PyEval_InitThreads)(void);
467static void(*py3_PyEval_RestoreThread)(PyThreadState *);
468static PyThreadState*(*py3_PyEval_SaveThread)(void);
469static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
470static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Yee Cheng Chind606fcc2023-09-20 19:59:47 +0200471static void (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200472static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200473static int (*py3_Py_IsInitialized)(void);
474static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200475static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100476static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200477static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Yee Cheng Chin97a5be42024-09-09 19:46:17 +0200478# if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000
Bram Moolenaardb913952012-06-29 12:54:53 +0200479static iternextfunc py3__PyObject_NextNotImplemented;
Ken Takata119fdd92023-10-04 20:05:05 +0200480# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200481static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200482static PyObject* py3__Py_FalseStruct;
483static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200484static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
485static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200486# ifdef USE_LIMITED_API
487# if Py_LIMITED_API >= 0x030a0000
488static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
489# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200490# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200491# if PY_VERSION_HEX >= 0x030300f0
492static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
493# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200495# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200496# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200497static int (*py3_PyUnicode_CompareWithASCIIString)(PyObject *unicode, const char* string);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200498static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200499static PyObject* (*py3_PyUnicode_AsUTF8String)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200500static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200501static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200502static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100503static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200504# if PY_VERSION_HEX >= 0x030900b0
505static PyObject* (*py3__PyObject_New)(PyTypeObject *);
506# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200507static PyObject* (*py3_PyFloat_FromDouble)(double num);
508static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200509static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200510static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
511static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200512static PyTypeObject* py3_PyType_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200513# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200514static PyTypeObject* py3_PyStdPrinter_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200515# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200516static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200517static PyTypeObject* py3_PyFloat_Type;
Ken Takatafa145f22023-10-06 19:27:13 +0200518static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200519static int (*py3_PyNumber_Check)(PyObject *);
520static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200521static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200522static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
523static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200524# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200525static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
526static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200527static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200528# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200529static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
530# endif
531# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
532static void (*py3__PyObject_DebugFree)(void*);
533static void* (*py3__PyObject_DebugMalloc)(size_t);
534# else
535static void (*py3_PyObject_Free)(void*);
536static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200537# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200538static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
539static void(*py3_PyObject_GC_Del)(void *);
540static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200541static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200542# ifdef USE_LIMITED_API
543static void* (*py3_PyType_GetSlot)(PyTypeObject *, int);
544static PyObject* (*py3_PyType_FromSpec)(PyType_Spec *);
545# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200546
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100547// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200548static PyObject *p3imp_PyExc_AttributeError;
549static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200550static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200551static PyObject *p3imp_PyExc_KeyboardInterrupt;
552static PyObject *p3imp_PyExc_TypeError;
553static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200554static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200555static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200556static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200557static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200558
559# define PyExc_AttributeError p3imp_PyExc_AttributeError
560# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200561# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200562# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
563# define PyExc_TypeError p3imp_PyExc_TypeError
564# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200565# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200566# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200567# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200568# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200569
570/*
571 * Table of name to function pointer of python.
572 */
573# define PYTHON_PROC FARPROC
574static struct
575{
576 char *name;
577 PYTHON_PROC *ptr;
578} py3_funcname_table[] =
579{
580 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100581 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200583 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
584 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200585 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200586 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200587 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
588 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
589 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
590 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200591 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200592 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200593 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200594 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Yegappan Lakshmanan038be272025-03-26 18:46:21 +0100595 {"PyTuple_New", (PYTHON_PROC*)&py3_PyTuple_New},
596 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
597 {"PyTuple_SetItem", (PYTHON_PROC*)&py3_PyTuple_SetItem},
598 {"PyTuple_SET_ITEM", (PYTHON_PROC*)&py3_PyTuple_SET_ITEM},
599 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200600 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
601 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
602 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200603 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200604# if PY_VERSION_HEX >= 0x030601f0
605 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
606 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
607# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200608 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
609 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
610 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
611 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200612 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200613 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Yee Cheng Chin50840362024-09-10 20:56:13 +0200614# if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000
Ken Takata9abd7152024-08-10 09:44:20 +0200615 {"Py_IncRef", (PYTHON_PROC*)&py3_Py_IncRef},
Yee Cheng Chinc2285a82024-09-09 19:55:24 +0200616 {"Py_DecRef", (PYTHON_PROC*)&py3_Py_DecRef},
Ken Takata9abd7152024-08-10 09:44:20 +0200617# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200618# ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200619 {"Py_CompileString", (PYTHON_PROC*)&py3_Py_CompileString},
620 {"PyEval_EvalCode", (PYTHON_PROC*)&PyEval_EvalCode},
Ken Takatac97b3fe2023-10-11 21:27:06 +0200621# else
622 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
623 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200624# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200625 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200626 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200627 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
628 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200629# if PY_VERSION_HEX >= 0x030d0000
630 {"PyObject_CallFunction", (PYTHON_PROC*)&py3_PyObject_CallFunction},
631# else
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200632 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200633# endif
Bram Moolenaarf4258302013-06-02 18:20:17 +0200634 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200635 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
636 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200637 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
638 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200639 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200640 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200641 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
642 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
643 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
644 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200645 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
646 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200647 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200648# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
649 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200650 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
651# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200652 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
653 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200654 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200655 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200656 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200657 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
658 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200659# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200660 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
661# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200662 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
663 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
664 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
665 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
666 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
667 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
668 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200669 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200670 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Yee Cheng Chin97a5be42024-09-09 19:46:17 +0200671# if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000
Bram Moolenaardb913952012-06-29 12:54:53 +0200672 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Ken Takata119fdd92023-10-04 20:05:05 +0200673# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200674 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200675 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
676 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200677 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200678 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100679 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200680 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
681 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
682 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200683# ifdef USE_LIMITED_API
684# if Py_LIMITED_API >= 0x030a0000
685 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
686# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200687# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200688# if PY_VERSION_HEX >= 0x030300f0
689 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
690# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200691 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200692# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200693# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200694 {"PyUnicode_CompareWithASCIIString", (PYTHON_PROC*)&py3_PyUnicode_CompareWithASCIIString},
695 {"PyUnicode_AsUTF8String", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8String},
Ken Takatac97b3fe2023-10-11 21:27:06 +0200696# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200697# ifdef Py_UNICODE_WIDE
698 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
699# else
700 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
701# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200702# else
703 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200704# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200705 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200706 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200707 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100708 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200709# if PY_VERSION_HEX >= 0x030900b0
710 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
711# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200712 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
713 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200714 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200715 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
716 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200717 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200718# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200719 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200720# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200721 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200722 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200723 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200724 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
725 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200726 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200727# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200728 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
729 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200730 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
731# else
732 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
733# endif
734# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200735 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
736 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200737# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200738 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
739 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200740# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200741 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
742 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
743 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200744 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
745 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
746 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200747# ifdef USE_LIMITED_API
748# if PY_VERSION_HEX >= 0x03040000
749 {"PyType_GetSlot", (PYTHON_PROC*)&py3_PyType_GetSlot},
750# endif
751 {"PyType_FromSpec", (PYTHON_PROC*)&py3_PyType_FromSpec},
752# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200753 {"", NULL},
754};
755
Yee Cheng Chin50840362024-09-10 20:56:13 +0200756# if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000
Yee Cheng Chinc2285a82024-09-09 19:55:24 +0200757// Use stable versions of inc/dec ref. Note that these always null-check and
758// therefore there's no difference between XINCREF and INCREF.
Yee Cheng Chin50840362024-09-10 20:56:13 +0200759//
760// For 3.8 or above, we also use this version even if not using limited API.
761// The Py_DECREF macros in 3.8+ include references to internal functions which
762// cause link errors when building Vim. The stable versions are exposed as API
763// functions and don't have these problems (albeit slightly slower as they
764// require function calls rather than an inlined macro).
Yee Cheng Chinc2285a82024-09-09 19:55:24 +0200765# undef Py_INCREF
766# define Py_INCREF(obj) Py_IncRef((PyObject *)obj)
Ken Takata9abd7152024-08-10 09:44:20 +0200767# undef Py_XINCREF
Yee Cheng Chinc2285a82024-09-09 19:55:24 +0200768# define Py_XINCREF(obj) Py_IncRef((PyObject *)obj)
769
770# undef Py_DECREF
771# define Py_DECREF(obj) Py_DecRef((PyObject *)obj)
772# undef Py_XDECREF
773# define Py_XDECREF(obj) Py_DecRef((PyObject *)obj)
Ken Takata9abd7152024-08-10 09:44:20 +0200774# endif
775
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200776# if PY_VERSION_HEX >= 0x030900b0
777 static inline int
778py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
779{
780 return ((PyType_GetFlags(type) & feature) != 0);
781}
782# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
783# endif
784
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200785# if PY_VERSION_HEX >= 0x030a00b2
786 static inline int
787py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
788{
789 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
790}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100791# if PY_VERSION_HEX >= 0x030b00b3
792# undef PyObject_TypeCheck
793# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
794# else
795# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
796# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200797# endif
798
Ken Takatafa145f22023-10-06 19:27:13 +0200799# if !defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x030c00b0
Yee Cheng Chin396058a2023-10-17 10:38:11 +0200800// PyTuple_GET_SIZE/PyList_GET_SIZE are inlined functions that use Py_SIZE(),
801// which started to introduce linkage dependency from Python 3.12. When we
802// build Python in dynamic mode, we don't link against it in build time, and
803// this would fail to build. Just use the non-inlined version instead.
Ken Takatafa145f22023-10-06 19:27:13 +0200804# undef PyTuple_GET_SIZE
Yee Cheng Chin396058a2023-10-17 10:38:11 +0200805# define PyTuple_GET_SIZE(o) PyTuple_Size(o)
Ken Takatafa145f22023-10-06 19:27:13 +0200806# undef PyList_GET_SIZE
Yee Cheng Chin396058a2023-10-17 10:38:11 +0200807# define PyList_GET_SIZE(o) PyList_Size(o)
Ken Takatafa145f22023-10-06 19:27:13 +0200808# endif
809
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100810# ifdef MSWIN
811/*
812 * Look up the library "libname" using the InstallPath registry key.
813 * Return NULL when failed. Return an allocated string when successful.
814 */
Ken Takataae3cfa42023-10-14 11:49:09 +0200815 static WCHAR *
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100816py3_get_system_libname(const char *libname)
817{
Ken Takataae3cfa42023-10-14 11:49:09 +0200818 const WCHAR *pythoncore = L"Software\\Python\\PythonCore";
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100819 const char *cp = libname;
Ken Takataae3cfa42023-10-14 11:49:09 +0200820 WCHAR subkey[128];
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100821 HKEY hKey;
Ken Takataae3cfa42023-10-14 11:49:09 +0200822 int i;
823 DWORD j, len;
824 LSTATUS ret;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100825
826 while (*cp != '\0')
827 {
828 if (*cp == ':' || *cp == '\\' || *cp == '/')
829 {
830 // Bail out if "libname" contains path separator, assume it is
831 // an absolute path.
832 return NULL;
833 }
834 ++cp;
835 }
Ken Takataae3cfa42023-10-14 11:49:09 +0200836
837 WCHAR keyfound[32];
838 HKEY hKeyTop[] = {HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
839 HKEY hKeyFound = NULL;
840# ifdef USE_LIMITED_API
841 long maxminor = -1;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100842# endif
Ken Takataae3cfa42023-10-14 11:49:09 +0200843 for (i = 0; i < ARRAY_LENGTH(hKeyTop); i++)
844 {
845 long major, minor;
846
847 ret = RegOpenKeyExW(hKeyTop[i], pythoncore, 0, KEY_READ, &hKey);
848 if (ret != ERROR_SUCCESS)
849 continue;
850 for (j = 0;; j++)
851 {
852 WCHAR keyname[32];
853 WCHAR *wp;
854
855 len = ARRAY_LENGTH(keyname);
856 ret = RegEnumKeyExW(hKey, j, keyname, &len,
857 NULL, NULL, NULL, NULL);
858 if (ret == ERROR_NO_MORE_ITEMS)
859 break;
860
861 major = wcstol(keyname, &wp, 10);
Ken Takata982ef162023-10-18 12:02:24 +0200862 if (*wp != L'.')
863 continue;
864 minor = wcstol(wp + 1, &wp, 10);
Ken Takataae3cfa42023-10-14 11:49:09 +0200865# ifdef _WIN64
866 if (*wp != L'\0')
867 continue;
868# else
869 if (wcscmp(wp, L"-32") != 0)
870 continue;
871# endif
872
873 if (major != PY_MAJOR_VERSION)
874 continue;
875# ifdef USE_LIMITED_API
876 // Search the latest version.
877 if ((minor > maxminor)
878 && (minor >= ((Py_LIMITED_API >> 16) & 0xff)))
879 {
880 maxminor = minor;
881 wcscpy(keyfound, keyname);
882 hKeyFound = hKeyTop[i];
883 }
884# else
885 // Check if it matches with the compiled version.
886 if (minor == PY_MINOR_VERSION)
887 {
888 wcscpy(keyfound, keyname);
889 hKeyFound = hKeyTop[i];
890 break;
891 }
892# endif
893 }
894 RegCloseKey(hKey);
895# ifdef USE_LIMITED_API
896 if (hKeyFound != NULL)
897 break;
898# endif
899 }
900 if (hKeyFound == NULL)
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100901 return NULL;
Ken Takataae3cfa42023-10-14 11:49:09 +0200902
903 swprintf(subkey, ARRAY_LENGTH(subkey), L"%ls\\%ls\\InstallPath",
904 pythoncore, keyfound);
905 ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ,
906 NULL, NULL, &len);
907 if (ret != ERROR_MORE_DATA && ret != ERROR_SUCCESS)
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100908 return NULL;
Ken Takataae3cfa42023-10-14 11:49:09 +0200909 size_t len2 = len / sizeof(WCHAR) + 1 + strlen(libname);
910 WCHAR *path = alloc(len2 * sizeof(WCHAR));
911 if (path == NULL)
912 return NULL;
913 ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ,
914 NULL, path, &len);
915 if (ret != ERROR_SUCCESS)
916 {
917 vim_free(path);
918 return NULL;
919 }
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100920 // Remove trailing path separators.
Ken Takataae3cfa42023-10-14 11:49:09 +0200921 size_t len3 = wcslen(path);
922 if ((len3 > 0) && (path[len3 - 1] == L'/' || path[len3 - 1] == L'\\'))
923 --len3;
924 swprintf(path + len3, len2 - len3, L"\\%hs", libname);
925 return path;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100926}
927# endif
928
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200929/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200930 * Load library and get all pointers.
931 * Parameter 'libname' provides name of DLL.
932 * Return OK or FAIL.
933 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200934 static int
935py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200936{
937 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200938 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
939 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
940 PYTHON_PROC *ucs_as_encoded_string =
941 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200942
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100943# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100944 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000945 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100946 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200947 if (python_loaded())
948 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200949 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000950 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200951 return FAIL;
952 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200953# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200954
955 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200956 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200957 hinstPy3 = load_dll(libname);
958
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100959# ifdef MSWIN
960 if (!hinstPy3)
961 {
962 // Attempt to use the path from InstallPath as stored in the registry.
Ken Takataae3cfa42023-10-14 11:49:09 +0200963 WCHAR *syslibname = py3_get_system_libname(libname);
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100964
965 if (syslibname != NULL)
966 {
Ken Takataae3cfa42023-10-14 11:49:09 +0200967 hinstPy3 = LoadLibraryExW(syslibname, NULL,
968 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
969 LOAD_LIBRARY_SEARCH_SYSTEM32);
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100970 vim_free(syslibname);
971 }
972 }
973# endif
974
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200975 if (!hinstPy3)
976 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200977 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000978 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200979 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200980 }
981
982 for (i = 0; py3_funcname_table[i].ptr; ++i)
983 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200984 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
985 py3_funcname_table[i].name)) == NULL)
986 {
987 close_dll(hinstPy3);
988 hinstPy3 = 0;
989 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000990 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200991 return FAIL;
992 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200993 }
994
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100995 // Load unicode functions separately as only the ucs2 or the ucs4 functions
996 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200997# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200998 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
999 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
1000 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001001 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001002# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001003 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
1004 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001005 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001006 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001007 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001008 if (*ucs_from_string == NULL || *ucs_decode == NULL
1009 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001011 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001012 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001013 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001014 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001015 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001016 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001017 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001018# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001019 if (*ucs_from_string == NULL || *ucs_decode == NULL
1020 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001021 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001022 close_dll(hinstPy3);
1023 hinstPy3 = 0;
1024 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001025 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001026 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001027 }
1028
1029 return OK;
1030}
1031
1032/*
1033 * If python is enabled (there is installed python on Windows system) return
1034 * TRUE, else FALSE.
1035 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001036 int
1037python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001038{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +01001039 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001040}
1041
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001042/*
1043 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001044 * DLL, as this can cause errors (importing data symbols is not reliable).
1045 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001046 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001047get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001048{
1049 PyObject *exmod = PyImport_ImportModule("builtins");
1050 PyObject *exdict = PyModule_GetDict(exmod);
1051 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
1052 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +02001053 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001054 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
1055 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
1056 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +02001057 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +02001058 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001059 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +02001060 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001061 Py_XINCREF(p3imp_PyExc_AttributeError);
1062 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +02001063 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001064 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
1065 Py_XINCREF(p3imp_PyExc_TypeError);
1066 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +02001067 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +02001068 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001069 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02001070 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001071 Py_XDECREF(exmod);
1072}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001073#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001074
Bram Moolenaardb913952012-06-29 12:54:53 +02001075static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001076#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001077static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02001078
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001079#ifdef USE_LIMITED_API
1080# define DESTRUCTOR_FINISH(self) \
Ken Takata9abd7152024-08-10 09:44:20 +02001081 ((freefunc)PyType_GetSlot(Py_TYPE((PyObject*)self), Py_tp_free))((PyObject*)self)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001082#else
1083# define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
1084#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02001085
Bram Moolenaar971db462013-05-12 18:44:48 +02001086#define WIN_PYTHON_REF(win) win->w_python3_ref
1087#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001088#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +02001089
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001090 static void
1091call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001092{
Bram Moolenaar0014a532013-05-29 21:33:39 +02001093#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001094 _PyObject_DebugFree(p);
1095#else
1096 PyObject_Free(p);
1097#endif
1098}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001099
1100 static PyObject *
1101call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001102{
1103 return PyType_GenericNew(type,args,kwds);
1104}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001105
1106 static PyObject *
1107call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001108{
1109 return PyType_GenericAlloc(type,nitems);
1110}
1111
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001112static PyObject *OutputGetattro(PyObject *, PyObject *);
1113static int OutputSetattro(PyObject *, PyObject *, PyObject *);
1114static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001115static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001116static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001117static PyObject *WindowGetattro(PyObject *, PyObject *);
1118static int WindowSetattro(PyObject *, PyObject *, PyObject *);
1119static PyObject *RangeGetattro(PyObject *, PyObject *);
1120static PyObject *CurrentGetattro(PyObject *, PyObject *);
1121static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
1122static PyObject *DictionaryGetattro(PyObject *, PyObject *);
1123static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
1124static PyObject *ListGetattro(PyObject *, PyObject *);
1125static int ListSetattro(PyObject *, PyObject *, PyObject *);
Yegappan Lakshmanan038be272025-03-26 18:46:21 +01001126static PyObject *TupleGetattro(PyObject *, PyObject *);
1127static int TupleSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001128static PyObject *FunctionGetattro(PyObject *, PyObject *);
1129
1130static struct PyModuleDef vimmodule;
1131
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02001132#define PY_CAN_RECURSE
1133
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001134/*
1135 * Include the code shared with if_python.c
1136 */
1137#include "if_py_both.h"
1138
Ken Takatac97b3fe2023-10-11 21:27:06 +02001139#ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001140# if Py_LIMITED_API >= 0x030A0000
1141# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1142# else
1143// Python limited API before 3.10 lack easy ways to query the raw UTF-8 chars.
1144// We need to first convert the string to bytes, and then extract the chars.
1145// This function is only used for attribute string comparisons, which have
1146// known short length. As such, just allocate a short static buffer to hold
1147// the characters instead of having to allocate/deallcoate it.
1148//
1149// An alternative would be to convert all attribute string comparisons to use
1150// PyUnicode_CompareWithASCIIString to skip having to extract the chars.
1151static char py3_unicode_utf8_chars[20];
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02001152static char* PY_UNICODE_GET_UTF8_CHARS(PyObject* str)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001153{
1154 py3_unicode_utf8_chars[0] = '\0';
1155 PyObject* bytes = PyUnicode_AsUTF8String(str);
1156 if (bytes)
1157 {
1158 char *chars;
1159 Py_ssize_t len;
1160 if (PyBytes_AsStringAndSize(bytes, &chars, &len) != -1)
1161 {
1162 if (len < (Py_ssize_t)sizeof(py3_unicode_utf8_chars))
1163 // PyBytes_AsStringAndSize guarantees null-termination
1164 memcpy(py3_unicode_utf8_chars, chars, len + 1);
1165 }
1166 Py_DECREF(bytes);
1167 }
1168 return py3_unicode_utf8_chars;
1169}
1170# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +02001171#else // !USE_LIMITED_API
1172# if PY_VERSION_HEX >= 0x030300f0
1173# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1174# else
1175# define PY_UNICODE_GET_UTF8_CHARS _PyUnicode_AsString
1176# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001177#endif
1178
Bram Moolenaar828bff12019-03-19 22:11:41 +01001179// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001180#define GET_ATTR_STRING(name, nameobj) \
1181 char *name = ""; \
1182 if (PyUnicode_Check(nameobj)) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001183 name = (char *)PY_UNICODE_GET_UTF8_CHARS(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001184
1185#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
1186
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001187///////////////////////////////////////////////////////
1188// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001189
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001190static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001191
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001192///////////////////////////////////////////////////////
1193// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001194
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001195 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001196python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001197{
1198 static int recurse = 0;
1199
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001200 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001201 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001202 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001204 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001205 ++recurse;
1206
1207#ifdef DYNAMIC_PYTHON3
1208 if (hinstPy3)
1209#endif
1210 if (Py_IsInitialized())
1211 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001212#ifdef USE_LIMITED_API
1213 shutdown_types();
1214#endif
1215
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001216 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001217 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001218
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001219 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001220 }
1221
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001222 --recurse;
1223}
1224
Bram Moolenaar094454f2015-10-07 10:39:55 +02001225#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001226 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001227python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001228{
1229 return (hinstPy3 != 0);
1230}
1231#endif
1232
Bram Moolenaar94073162018-01-31 21:49:05 +01001233static wchar_t *py_home_buf = NULL;
1234
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001235#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001236/*
1237 * Return TRUE if stdin is readable from Python 3.
1238 */
1239 static BOOL
1240is_stdin_readable(void)
1241{
1242 DWORD mode, eventnum;
1243 struct _stat st;
1244 int fd = fileno(stdin);
1245 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1246
1247 // Check if stdin is connected to the console.
1248 if (GetConsoleMode(hstdin, &mode))
1249 // Check if it is opened as input.
1250 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1251
1252 return _fstat(fd, &st) == 0;
1253}
1254
1255// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1256// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001257// Note that the python DLL is linked to its own stdio DLL which can be
1258// differ from Vim's stdio.
1259 static void
1260reset_stdin(void)
1261{
1262 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1263 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Ken Takata119fdd92023-10-04 20:05:05 +02001264 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001265
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001266 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001267 return;
1268
1269 // Get "freopen" and "stdin" which are used in the python DLL.
1270 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1271 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1272 if (py__acrt_iob_func)
1273 {
1274 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001275 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001276 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001277 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001278 }
1279
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001280 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001281 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001282 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001283 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001284 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001285}
1286#else
1287# define reset_stdin()
1288#endif
1289
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001290// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1291// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1292// Install a hook to python dll's exit() and recover from it.
1293#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1294# define HOOK_EXIT
1295# include <setjmp.h>
1296
1297static jmp_buf exit_hook_jump_buf;
1298static void *orig_exit = NULL;
1299
1300/*
1301 * Function that replaces exit() while calling Py_Initialize().
1302 */
1303 static void
1304hooked_exit(int ret)
1305{
1306 // Recover from exit.
1307 longjmp(exit_hook_jump_buf, 1);
1308}
1309
1310/*
1311 * Install a hook to python dll's exit().
1312 */
1313 static void
1314hook_py_exit(void)
1315{
Ken Takata119fdd92023-10-04 20:05:05 +02001316 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001317
1318 if (hinst == NULL || orig_exit != NULL)
1319 return;
1320
1321 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1322}
1323
1324/*
1325 * Remove the hook installed by hook_py_exit().
1326 */
1327 static void
1328restore_py_exit(void)
1329{
1330 HINSTANCE hinst = hinstPy3;
1331
1332 if (hinst == NULL)
1333 return;
1334
1335 if (orig_exit != NULL)
1336 hook_dll_import_func(hinst, "exit", orig_exit);
1337 orig_exit = NULL;
1338}
1339#endif
1340
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001341 static int
1342Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001343{
1344 if (!py3initialised)
1345 {
1346#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 if (!python3_enabled(TRUE))
1348 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001349 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001350 goto fail;
1351 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001352#endif
Boris Staletic83a06702024-10-14 20:28:39 +02001353 // Python 3.13 introduced a really useful feature: colorized exceptions.
1354 // This is great if you're reading them from the terminal, but useless
1355 // and broken everywhere else (such as in log files, or text editors).
1356 // Opt out, forcefully.
1357 vim_setenv((char_u*)"PYTHON_COLORS", (char_u*)"0");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001358
1359 init_structs();
1360
Bram Moolenaar94073162018-01-31 21:49:05 +01001361 if (*p_py3home != NUL)
1362 {
1363 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001364
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001365 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001366 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001367 if (py_home_buf != NULL && mbstowcs(
1368 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1369 Py_SetPythonHome(py_home_buf);
1370 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001371#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001372 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001373 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001374#endif
1375
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001376 PyImport_AppendInittab("vim", Py3Init_vim);
1377
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001378#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1379 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1380#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001381 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001382
1383#ifdef HOOK_EXIT
1384 // Catch exit() called in Py_Initialize().
1385 hook_py_exit();
1386 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001387 {
1388 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001389 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001390 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001391 else
1392 {
1393 // exit() was called in Py_Initialize().
1394 restore_py_exit();
1395 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1396 goto fail;
1397 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001398#else
1399 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001400#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001401
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001402#if PY_VERSION_HEX < 0x03090000
1403 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001404 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001405#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001406#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001407 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001408#endif
1409
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001410 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001411 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001412
Bram Moolenaardb913952012-06-29 12:54:53 +02001413 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1414
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001415 // Remove the element from sys.path that was added because of our
1416 // argv[0] value in Py3Init_vim(). Previously we used an empty
1417 // string, but depending on the OS we then get an empty entry or
1418 // the current directory in sys.path.
1419 // Only after vim has been imported, the element does exist in
1420 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001421 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 +02001422
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001423 // Without the call to PyEval_SaveThread, thread specific state (such
1424 // as the system trace hook), will be lost between invocations of
1425 // Python code.
1426 // GIL may have been created and acquired in PyEval_InitThreads() and
1427 // thread state is created in Py_Initialize(); there
1428 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1429 // have threads enabled!), so the following does both: unlock GIL and
1430 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001431 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001432
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001433 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434 }
1435
1436 return 0;
1437
1438fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001439 // We call PythonIO_Flush() here to print any Python errors.
1440 // This is OK, as it is possible to call this function even
1441 // if PythonIO_Init_io() has not completed successfully (it will
1442 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443 PythonIO_Flush();
1444 return -1;
1445}
1446
1447/*
1448 * External interface
1449 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001450 static void
Ben Jacksonea19e782024-11-06 21:50:05 +01001451DoPyCommand(const char *cmd,
1452 dict_T* locals,
1453 rangeinitializer init_range,
1454 runner run,
1455 void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001456{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001457#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001458 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001459#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001460 PyObject *cmdstr;
1461 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001462 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001463
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001464 if (python_end_called)
1465 goto theend;
1466
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001467 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001468 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001469
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001470 init_range(arg);
1471
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001472 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001473
1474#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001475 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001476 saved_locale = setlocale(LC_NUMERIC, NULL);
1477 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001478 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001479 else
1480 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001481 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001482 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1483 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484 }
1485#endif
1486
1487 pygilstate = PyGILState_Ensure();
1488
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001489 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1490 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001491 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001492 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1493 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001494 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001495
Ben Jacksonea19e782024-11-06 21:50:05 +01001496 run(PyBytes_AsString(cmdbytes), locals, arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001497 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001498
1499 PyGILState_Release(pygilstate);
1500
1501#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1502 if (saved_locale != NULL)
1503 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001504 (void)setlocale(LC_NUMERIC, saved_locale);
1505 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001506 }
1507#endif
1508
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001509 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001510 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511
1512theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001513 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001514}
1515
1516/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001517 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001518 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001519 void
1520ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001521{
1522 char_u *script;
1523
1524 script = script_get(eap, eap->arg);
1525 if (!eap->skip)
1526 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001527 if (p_pyx == 0)
1528 p_pyx = 3;
1529
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001530 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
Ben Jacksonea19e782024-11-06 21:50:05 +01001531 NULL,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001532 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001533 (runner) run_cmd,
1534 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001535 }
1536 vim_free(script);
1537}
1538
1539#define BUFFER_SIZE 2048
1540
1541/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001542 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001543 */
1544 void
1545ex_py3file(exarg_T *eap)
1546{
1547 static char buffer[BUFFER_SIZE];
1548 const char *file;
1549 char *p;
1550 int i;
1551
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001552 if (p_pyx == 0)
1553 p_pyx = 3;
1554
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001555 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1556 // stdio file pointer, but Vim and the Python DLL are compiled with
1557 // different options under Windows, meaning that stdio pointers aren't
1558 // compatible between the two. Yuk.
1559 //
1560 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1561 //
1562 // Using bytes so that Python can detect the source encoding as it normally
1563 // does. The doc does not say "compile" accept bytes, though.
1564 //
1565 // We need to escape any backslashes or single quotes in the file name, so that
1566 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001567
1568 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001569 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001570
1571 for (i=0; i<2; ++i)
1572 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001573 file = (char *)eap->arg;
1574 while (*file && p < buffer + (BUFFER_SIZE - 3))
1575 {
1576 if (*file == '\\' || *file == '\'')
1577 *p++ = '\\';
1578 *p++ = *file++;
1579 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001580 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001581 if (*file != '\0')
1582 return;
1583 if (i==0)
1584 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001585 strcpy(p,"','rb').read(),'");
1586 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001587 }
1588 else
1589 {
1590 strcpy(p,"','exec'))");
1591 p += 10;
1592 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001593 }
1594
1595
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001596 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001597 DoPyCommand(buffer,
Ben Jacksonea19e782024-11-06 21:50:05 +01001598 NULL,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001599 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001600 (runner) run_cmd,
1601 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001602}
1603
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001604 void
1605ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001606{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001607 if (p_pyx == 0)
1608 p_pyx = 3;
1609
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001610 DoPyCommand((char *)eap->arg,
Ben Jacksonea19e782024-11-06 21:50:05 +01001611 NULL,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001612 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001613 (runner)run_do,
1614 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001615}
1616
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001617///////////////////////////////////////////////////////
1618// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001619
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001620// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001621
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001622 static PyObject *
1623OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001624{
Bram Moolenaar77045652012-09-21 13:46:06 +02001625 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001626
1627 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001629 else if (strcmp(name, "errors") == 0)
1630 return PyString_FromString("strict");
1631 else if (strcmp(name, "encoding") == 0)
1632 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001633
1634 return PyObject_GenericGetAttr(self, nameobj);
1635}
1636
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001637 static int
1638OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001639{
Bram Moolenaar77045652012-09-21 13:46:06 +02001640 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001641
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001642 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001643}
1644
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001645///////////////////////////////////////////////////////
1646// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001647
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001648// Window type - Implementation functions
1649// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001650
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001651#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1652
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001653// Buffer type - Implementation functions
1654// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001655
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001656#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1657
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001658static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001659static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001660
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001661// Line range type - Implementation functions
1662// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001663
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001664#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1665
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001666static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001667static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1668static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001669
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001670// Current objects type - Implementation functions
1671// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001672
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001673static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001674 (lenfunc) BufferLength, // sq_length, len(x)
1675 (binaryfunc) 0, // sq_concat, x+y
1676 (ssizeargfunc) 0, // sq_repeat, x*n
1677 (ssizeargfunc) BufferItem, // sq_item, x[i]
1678 0, // was_sq_slice, x[i:j]
1679 0, // sq_ass_item, x[i]=v
1680 0, // sq_ass_slice, x[i:j]=v
1681 0, // sq_contains
1682 0, // sq_inplace_concat
1683 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001684};
1685
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001686static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001687 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001688 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001689 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001690};
1691
1692
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001693// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001694
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001695 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001696BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001697{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001698 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001699
Bram Moolenaar77045652012-09-21 13:46:06 +02001700 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001701
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001702 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1703 return r;
1704
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001705 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001706 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001707
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001708 r = BufferAttr((BufferObject *)(self), name);
1709 if (r || PyErr_Occurred())
1710 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001711 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001712 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001713}
1714
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001715 static int
1716BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1717{
1718 GET_ATTR_STRING(name, nameobj);
1719
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001720 return BufferSetattr(self, name, val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001721}
1722
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001723//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001724
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001725 static PyObject *
1726BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001727{
Bram Moolenaardb913952012-06-29 12:54:53 +02001728 if (PyLong_Check(idx))
1729 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001730 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001731 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001732 }
1733 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001734 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001735 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001736
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001737 if (CheckBuffer((BufferObject *) self))
1738 return NULL;
1739
Bram Moolenaar922a4662014-03-30 16:11:43 +02001740 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001741 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001742 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001743 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001744 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001745 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001746 }
1747 else
1748 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001749 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001750 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001751 }
1752}
1753
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001754 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001755BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1756{
Bram Moolenaardb913952012-06-29 12:54:53 +02001757 if (PyLong_Check(idx))
1758 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001759 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001760
1761 if (CheckBuffer((BufferObject *) self))
1762 return -1;
1763
Bram Moolenaar19e60942011-06-19 00:27:51 +02001764 return RBAsItem((BufferObject *)(self), n, val, 1,
1765 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1766 NULL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001767 }
1768 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001769 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001770 Py_ssize_t start, stop, step, slicelen;
1771
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001772 if (CheckBuffer((BufferObject *) self))
1773 return -1;
1774
Bram Moolenaar922a4662014-03-30 16:11:43 +02001775 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001776 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001777 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001778 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001779 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001780 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1781 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1782 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001783 }
1784 else
1785 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001786 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001787 return -1;
1788 }
1789}
1790
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001791static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001792 (lenfunc) RangeLength, // sq_length, len(x)
1793 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1794 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1795 (ssizeargfunc) RangeItem, // sq_item, x[i]
1796 0, // was_sq_slice, x[i:j]
1797 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1798 0, // sq_ass_slice, x[i:j]=v
1799 0, // sq_contains
1800 0, // sq_inplace_concat
1801 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001802};
1803
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001804static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001805 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001806 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001807 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001808};
1809
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001810// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001811
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001812 static PyObject *
1813RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001814{
Bram Moolenaar77045652012-09-21 13:46:06 +02001815 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001816
1817 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001818 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001819 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001820 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001821 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001822 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001823}
1824
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001825////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001826
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001827 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001828RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001829{
1830 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001831 ((RangeObject *)(self))->start,
1832 ((RangeObject *)(self))->end,
1833 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001834}
1835
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001836 static Py_ssize_t
1837RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1838{
1839 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1840 ((RangeObject *)(self))->start,
1841 ((RangeObject *)(self))->end,
1842 &((RangeObject *)(self))->end);
1843}
1844
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001845 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001846RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001847{
Bram Moolenaardb913952012-06-29 12:54:53 +02001848 if (PyLong_Check(idx))
1849 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001850 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001851 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001852 }
1853 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001854 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001855 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001856
Bram Moolenaar922a4662014-03-30 16:11:43 +02001857 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001858 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1859 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001860 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001861 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001862 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001863 }
1864 else
1865 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001866 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001867 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001868 }
1869}
1870
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001871 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001872RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1873{
Bram Moolenaardb913952012-06-29 12:54:53 +02001874 if (PyLong_Check(idx))
1875 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001876 long n = PyLong_AsLong(idx);
1877 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001878 }
1879 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001880 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001881 Py_ssize_t start, stop, step, slicelen;
1882
Bram Moolenaar922a4662014-03-30 16:11:43 +02001883 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001884 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1885 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001886 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001887 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001888 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001889 }
1890 else
1891 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001892 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001893 return -1;
1894 }
1895}
1896
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001897// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001898
1899 static PyObject *
1900TabPageGetattro(PyObject *self, PyObject *nameobj)
1901{
1902 PyObject *r;
1903
1904 GET_ATTR_STRING(name, nameobj);
1905
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001906 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1907 return r;
1908
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001909 if (CheckTabPage((TabPageObject *)(self)))
1910 return NULL;
1911
1912 r = TabPageAttr((TabPageObject *)(self), name);
1913 if (r || PyErr_Occurred())
1914 return r;
1915 else
1916 return PyObject_GenericGetAttr(self, nameobj);
1917}
1918
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001919// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001920
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001921 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001922WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001923{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001924 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001925
Bram Moolenaar77045652012-09-21 13:46:06 +02001926 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001927
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001928 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1929 return r;
1930
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001931 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001932 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001933
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001934 r = WindowAttr((WindowObject *)(self), name);
1935 if (r || PyErr_Occurred())
1936 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001937 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001938 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001939}
1940
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001941 static int
1942WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001943{
Bram Moolenaar77045652012-09-21 13:46:06 +02001944 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001945
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001946 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001947}
1948
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001949// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001950
1951static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001952 (lenfunc) TabListLength, // sq_length, len(x)
1953 (binaryfunc) 0, // sq_concat, x+y
1954 (ssizeargfunc) 0, // sq_repeat, x*n
1955 (ssizeargfunc) TabListItem, // sq_item, x[i]
1956 0, // sq_slice, x[i:j]
1957 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1958 0, // sq_ass_slice, x[i:j]=v
1959 0, // sq_contains
1960 0, // sq_inplace_concat
1961 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001962};
1963
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001964// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001965
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001966static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001967 (lenfunc) WinListLength, // sq_length, len(x)
1968 (binaryfunc) 0, // sq_concat, x+y
1969 (ssizeargfunc) 0, // sq_repeat, x*n
1970 (ssizeargfunc) WinListItem, // sq_item, x[i]
1971 0, // sq_slice, x[i:j]
1972 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1973 0, // sq_ass_slice, x[i:j]=v
1974 0, // sq_contains
1975 0, // sq_inplace_concat
1976 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001977};
1978
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001979/*
1980 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001981 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001982 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001983CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001984{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001985 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001986 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001987 if (!(r = CurrentGetattr(self, name)))
1988 return PyObject_GenericGetAttr(self, nameobj);
1989 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001990}
1991
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001992 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001993CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001994{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001995 GET_ATTR_STRING(name, nameobj);
1996 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001997}
1998
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001999// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002000
Bram Moolenaar66b79852012-09-21 14:00:35 +02002001 static PyObject *
2002DictionaryGetattro(PyObject *self, PyObject *nameobj)
2003{
2004 DictionaryObject *this = ((DictionaryObject *) (self));
2005
2006 GET_ATTR_STRING(name, nameobj);
2007
2008 if (strcmp(name, "locked") == 0)
2009 return PyLong_FromLong(this->dict->dv_lock);
2010 else if (strcmp(name, "scope") == 0)
2011 return PyLong_FromLong(this->dict->dv_scope);
2012
2013 return PyObject_GenericGetAttr(self, nameobj);
2014}
2015
2016 static int
2017DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
2018{
2019 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002020 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02002021}
2022
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002023// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002024
Bram Moolenaar66b79852012-09-21 14:00:35 +02002025 static PyObject *
2026ListGetattro(PyObject *self, PyObject *nameobj)
2027{
2028 GET_ATTR_STRING(name, nameobj);
2029
2030 if (strcmp(name, "locked") == 0)
2031 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
2032
2033 return PyObject_GenericGetAttr(self, nameobj);
2034}
2035
2036 static int
2037ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
2038{
2039 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002040 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02002041}
2042
Yegappan Lakshmanan038be272025-03-26 18:46:21 +01002043// Tuple object - Definitions
2044
2045 static PyObject *
2046TupleGetattro(PyObject *self, PyObject *nameobj)
2047{
2048 GET_ATTR_STRING(name, nameobj);
2049
2050 if (strcmp(name, "locked") == 0)
2051 return PyLong_FromLong(((TupleObject *) (self))->tuple->tv_lock);
2052
2053 return PyObject_GenericGetAttr(self, nameobj);
2054}
2055
2056 static int
2057TupleSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
2058{
2059 GET_ATTR_STRING(name, nameobj);
2060 return TupleSetattr(self, name, val);
2061}
2062
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002063// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002064
Bram Moolenaardb913952012-06-29 12:54:53 +02002065 static PyObject *
2066FunctionGetattro(PyObject *self, PyObject *nameobj)
2067{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002068 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02002069 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02002070
2071 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002072
Bram Moolenaar8110a092016-04-14 15:56:09 +02002073 r = FunctionAttr(this, name);
2074 if (r || PyErr_Occurred())
2075 return r;
2076 else
2077 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002078}
2079
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002080// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002081
2082 void
2083python3_buffer_free(buf_T *buf)
2084{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002085 BufferObject *bp = BUF_PYTHON_REF(buf);
2086 if (bp == NULL)
2087 return;
2088 bp->buf = INVALID_BUFFER_VALUE;
2089 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002090}
2091
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002092 void
2093python3_window_free(win_T *win)
2094{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002095 WindowObject *wp = WIN_PYTHON_REF(win);
2096 if (wp == NULL)
2097 return;
2098 wp->win = INVALID_WINDOW_VALUE;
2099 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002100}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002101
2102 void
2103python3_tabpage_free(tabpage_T *tab)
2104{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002105 TabPageObject *tp = TAB_PYTHON_REF(tab);
2106 if (tp == NULL)
2107 return;
2108 tp->tab = INVALID_TABPAGE_VALUE;
2109 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002110}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002111
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01002112 static PyObject *
2113Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002114{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002115 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002116 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
2117
Bram Moolenaar1dc28782013-05-21 19:11:01 +02002118 if (init_types())
2119 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002120
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002121 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002122 PySys_SetArgv(1, argv);
2123
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002124 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002125 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002126
Bram Moolenaardee2e312013-06-23 16:35:47 +02002127 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002128 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002129
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002130 if (init_sys_path())
2131 return NULL;
2132
2133 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002134}
2135
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002136//////////////////////////////////////////////////////////////////////////
2137// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002138
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002139/*
2140 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002141 * All internal newlines are replaced by null characters.
2142 *
2143 * On errors, the Python exception data is set, and NULL is returned.
2144 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002145 static PyObject *
2146LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002147{
2148 PyObject *result;
2149 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01002150 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002151
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002152 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002153 p = tmp;
2154 if (p == NULL)
2155 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002156 PyErr_NoMemory();
2157 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002158 }
2159
2160 while (*str)
2161 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002162 if (*str == '\n')
2163 *p = '\0';
2164 else
2165 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002166
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002167 ++p;
2168 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002169 }
2170 *p = '\0';
2171
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01002172 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002173
2174 vim_free(tmp);
2175 return result;
2176}
2177
Bram Moolenaardb913952012-06-29 12:54:53 +02002178 void
Ben Jacksonea19e782024-11-06 21:50:05 +01002179do_py3eval(char_u *str, dict_T *locals, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02002180{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002181 DoPyCommand((char *) str,
Ben Jacksonea19e782024-11-06 21:50:05 +01002182 locals,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02002183 init_range_eval,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002184 (runner) run_eval,
2185 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002186 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02002187 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002188 rettv->v_type = VAR_NUMBER;
2189 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02002190 }
2191}
2192
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002193 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002194set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02002195{
Bram Moolenaarb641df42015-02-03 13:16:04 +01002196 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02002197}
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002198
2199 int
Dominique Pellé4927bc72023-09-24 16:12:07 +02002200python3_version(void)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002201{
2202#ifdef USE_LIMITED_API
2203 return Py_LIMITED_API;
2204#else
2205 return PY_VERSION_HEX;
2206#endif
2207}