blob: fd117f0a715025d2d1a95f85ae6321dc1c6ae5e9 [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
Bram Moolenaardb913952012-06-29 12:54:53 +0200190# define PySequence_Check py3_PySequence_Check
191# define PySequence_Size py3_PySequence_Size
192# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200193# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200194# define PyTuple_Size py3_PyTuple_Size
195# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200196# if PY_VERSION_HEX >= 0x030601f0
197# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
198# define PySlice_Unpack py3_PySlice_Unpack
199# endif
200# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200201# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
202# define PyImport_ImportModule py3_PyImport_ImportModule
203# define PyObject_Init py3__PyObject_Init
204# define PyDict_New py3_PyDict_New
205# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200206# define PyDict_Next py3_PyDict_Next
207# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200208# ifndef PyMapping_Keys
209# define PyMapping_Keys py3_PyMapping_Keys
210# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200211# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
212 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
213# undef PyIter_Check
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200214# define PyIter_Check py3_PyIter_Check
215# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200216# define PyIter_Next py3_PyIter_Next
217# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200218# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200219# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200220# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200221# define PyModule_GetDict py3_PyModule_GetDict
Ken Takata9abd7152024-08-10 09:44:20 +0200222# if defined(USE_LIMITED_API) \
223 && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
224# undef Py_INCREF
225# if Py_LIMITED_API+0 >= 0x030a00A7
226# define _Py_IncRef py3__Py_IncRef
227# define Py_INCREF _Py_IncRef
228# else
229# define Py_IncRef py3_Py_IncRef
230# define Py_INCREF Py_IncRef
231# endif
232# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200233# ifdef USE_LIMITED_API
234# define Py_CompileString py3_Py_CompileString
235# define PyEval_EvalCode py3_PyEval_EvalCode
236# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200237# undef PyRun_SimpleString
238# define PyRun_SimpleString py3_PyRun_SimpleString
239# undef PyRun_String
240# define PyRun_String py3_PyRun_String
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200241# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200242# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200243# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200244# define PyObject_SetAttrString py3_PyObject_SetAttrString
245# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200246# if PY_VERSION_HEX >= 0x030d0000
247# define PyObject_CallFunction py3_PyObject_CallFunction
248# else
249# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
250# endif
Bram Moolenaarf4258302013-06-02 18:20:17 +0200251# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200252# define PyEval_GetLocals py3_PyEval_GetLocals
253# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200254# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200255# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200256# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200257# define PyType_Ready py3_PyType_Ready
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200258# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200259# define PyType_GetFlags py3_PyType_GetFlags
260# endif
Ken Takata119fdd92023-10-04 20:05:05 +0200261# undef Py_BuildValue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200262# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100263# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200264# define Py_Initialize py3_Py_Initialize
265# define Py_Finalize py3_Py_Finalize
266# define Py_IsInitialized py3_Py_IsInitialized
267# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200268# define _Py_FalseStruct (*py3__Py_FalseStruct)
269# define _Py_TrueStruct (*py3__Py_TrueStruct)
Yee Cheng Chin97a5be42024-09-09 19:46:17 +0200270# if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000
271// Private symbol that used to be required as part of PyIter_Check.
Ken Takata119fdd92023-10-04 20:05:05 +0200272# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
273# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274# define PyModule_AddObject py3_PyModule_AddObject
275# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200276# define PyImport_AddModule py3_PyImport_AddModule
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200277# ifdef USE_LIMITED_API
278# if Py_LIMITED_API >= 0x030a0000
279# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
280# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200281# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200282# if PY_VERSION_HEX >= 0x030300f0
283# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
284# else
285# define _PyUnicode_AsString py3__PyUnicode_AsString
286# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200287# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200288# undef PyUnicode_CompareWithASCIIString
289# define PyUnicode_CompareWithASCIIString py3_PyUnicode_CompareWithASCIIString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200290# undef PyUnicode_AsEncodedString
291# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200292# undef PyUnicode_AsUTF8String
293# define PyUnicode_AsUTF8String py3_PyUnicode_AsUTF8String
Bram Moolenaar19e60942011-06-19 00:27:51 +0200294# undef PyBytes_AsString
295# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200296# ifndef PyBytes_AsStringAndSize
297# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
298# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200299# undef PyBytes_FromString
300# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100301# undef PyBytes_FromStringAndSize
302# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200303# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200304# define _Py_Dealloc py3__Py_Dealloc
305# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200306# define PyFloat_FromDouble py3_PyFloat_FromDouble
307# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200308# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200309# define PyType_Type (*py3_PyType_Type)
Ken Takata119fdd92023-10-04 20:05:05 +0200310# ifndef USE_LIMITED_API
311# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
312# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200313# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200314# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200315# define PyNumber_Check (*py3_PyNumber_Check)
316# define PyNumber_Long (*py3_PyNumber_Long)
Ken Takatafa145f22023-10-06 19:27:13 +0200317# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200318# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200319# ifdef Py_DEBUG
320# define _Py_NegativeRefcount py3__Py_NegativeRefcount
321# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200322# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
323# else
324# define PyModule_Create2 py3_PyModule_Create2
325# endif
326# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200327# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
328# define _PyObject_DebugFree py3__PyObject_DebugFree
329# else
330# define PyObject_Malloc py3_PyObject_Malloc
331# define PyObject_Free py3_PyObject_Free
332# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200333# define _PyObject_GC_New py3__PyObject_GC_New
334# define PyObject_GC_Del py3_PyObject_GC_Del
335# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200336# define PyType_GenericAlloc py3_PyType_GenericAlloc
337# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200338# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200339# define PyUnicode_FromString py3_PyUnicode_FromString
Ken Takatac97b3fe2023-10-11 21:27:06 +0200340# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200341# ifdef Py_UNICODE_WIDE
342# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
343# else
344# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
345# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200346# else
347# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200348# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200349# undef PyUnicode_Decode
350# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200351# define PyType_IsSubtype py3_PyType_IsSubtype
352# define PyCapsule_New py3_PyCapsule_New
353# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200354# ifdef USE_LIMITED_API
355# define PyType_GetSlot py3_PyType_GetSlot
356# define PyType_FromSpec py3_PyType_FromSpec
357# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200358
Bram Moolenaar0014a532013-05-29 21:33:39 +0200359# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200360# undef PyObject_NEW
361# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200362( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200363 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200364# elif PY_VERSION_HEX >= 0x030900b0
365# undef PyObject_NEW
366# define PyObject_NEW(type, typeobj) \
367 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200368# endif
369
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370/*
371 * Pointers for dynamic link
372 */
373static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100374static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200375static void (*py3_Py_Initialize)(void);
376static PyObject* (*py3_PyList_New)(Py_ssize_t size);
377static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
378static void (*py3_PyGILState_Release)(PyGILState_STATE);
379static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200380static PyObject* (*py3_PySys_GetObject)(char *);
381static int (*py3_PyList_Append)(PyObject *, PyObject *);
382static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200383static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200384static int (*py3_PySequence_Check)(PyObject *);
385static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
386static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200387static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200388static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
389static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
390static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200391static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200392# if PY_VERSION_HEX >= 0x030601f0
393static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
394 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
395static int (*py3_PySlice_Unpack)(PyObject *slice,
396 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
397# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200398static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100399 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
400 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200401static PyObject* (*py3_PyErr_NoMemory)(void);
402static void (*py3_Py_Finalize)(void);
403static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200404static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200405static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Ken Takata9abd7152024-08-10 09:44:20 +0200406# if defined(USE_LIMITED_API) \
407 && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
408# if Py_LIMITED_API+0 >= 0x030a00A7
409# define _Py_IncRef py3__Py_IncRef
410static void (*py3__Py_IncRef)(PyObject *);
411# else
412static void (*py3_Py_IncRef)(PyObject *);
413# endif
414# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200415# ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200416static PyObject* (*py3_Py_CompileString)(const char *, const char *, int);
417static PyObject* (*py3_PyEval_EvalCode)(PyObject *co, PyObject *globals, PyObject *locals);
Ken Takatac97b3fe2023-10-11 21:27:06 +0200418# else
419static int (*py3_PyRun_SimpleString)(char *);
420static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200421# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200422static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200423static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100424static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200425static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200426# if PY_VERSION_HEX >= 0x030d0000
427static PyObject* (*py3_PyObject_CallFunction)(PyObject *, char *, ...);
428# else
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200429static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200430# endif
Bram Moolenaarf4258302013-06-02 18:20:17 +0200431static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200432static PyObject* (*py3_PyEval_GetGlobals)(void);
433static PyObject* (*py3_PyEval_GetLocals)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200434static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
435static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200436static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200437static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200438static PyObject* (*py3_PyErr_Occurred)(void);
439static PyObject* (*py3_PyModule_GetDict)(PyObject *);
440static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
441static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200442static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200443static PyObject* (*py3_PyLong_FromLong)(long);
444static PyObject* (*py3_PyDict_New)(void);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200445# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
446 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200447static int (*py3_PyIter_Check)(PyObject *o);
448# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200449static PyObject* (*py3_PyIter_Next)(PyObject *);
450static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200451static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200452static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200453static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200454static PyObject* (*py3_Py_BuildValue)(char *, ...);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200455# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200456static int (*py3_PyType_GetFlags)(PyTypeObject *o);
457# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200458static int (*py3_PyType_Ready)(PyTypeObject *type);
459static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
460static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Ken Takatac97b3fe2023-10-11 21:27:06 +0200461# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200462# ifdef Py_UNICODE_WIDE
463static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
464# else
465static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
466# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200467# else
468static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200469# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200470static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
471 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200472static long (*py3_PyLong_AsLong)(PyObject *);
473static void (*py3_PyErr_SetNone)(PyObject *);
474static void (*py3_PyEval_InitThreads)(void);
475static void(*py3_PyEval_RestoreThread)(PyThreadState *);
476static PyThreadState*(*py3_PyEval_SaveThread)(void);
477static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
478static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Yee Cheng Chind606fcc2023-09-20 19:59:47 +0200479static void (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200480static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200481static int (*py3_Py_IsInitialized)(void);
482static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200483static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100484static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200485static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Yee Cheng Chin97a5be42024-09-09 19:46:17 +0200486# if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000
Bram Moolenaardb913952012-06-29 12:54:53 +0200487static iternextfunc py3__PyObject_NextNotImplemented;
Ken Takata119fdd92023-10-04 20:05:05 +0200488# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200489static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200490static PyObject* py3__Py_FalseStruct;
491static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200492static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
493static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200494# ifdef USE_LIMITED_API
495# if Py_LIMITED_API >= 0x030a0000
496static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
497# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200498# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200499# if PY_VERSION_HEX >= 0x030300f0
500static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
501# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200502static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200503# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200504# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200505static int (*py3_PyUnicode_CompareWithASCIIString)(PyObject *unicode, const char* string);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200506static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200507static PyObject* (*py3_PyUnicode_AsUTF8String)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200508static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200509static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200510static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100511static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200512# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200513static void (*py3__Py_Dealloc)(PyObject *obj);
514# endif
515# if PY_VERSION_HEX >= 0x030900b0
516static PyObject* (*py3__PyObject_New)(PyTypeObject *);
517# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200518static PyObject* (*py3_PyFloat_FromDouble)(double num);
519static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200520static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200521static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
522static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200523static PyTypeObject* py3_PyType_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200524# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200525static PyTypeObject* py3_PyStdPrinter_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200526# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200527static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200528static PyTypeObject* py3_PyFloat_Type;
Ken Takatafa145f22023-10-06 19:27:13 +0200529static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200530static int (*py3_PyNumber_Check)(PyObject *);
531static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200532static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200533static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
534static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200535# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200536static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
537static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200538static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200539# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200540static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
541# endif
542# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
543static void (*py3__PyObject_DebugFree)(void*);
544static void* (*py3__PyObject_DebugMalloc)(size_t);
545# else
546static void (*py3_PyObject_Free)(void*);
547static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200548# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200549static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
550static void(*py3_PyObject_GC_Del)(void *);
551static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200552static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200553# ifdef USE_LIMITED_API
554static void* (*py3_PyType_GetSlot)(PyTypeObject *, int);
555static PyObject* (*py3_PyType_FromSpec)(PyType_Spec *);
556# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200557
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100558// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200559static PyObject *p3imp_PyExc_AttributeError;
560static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200561static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200562static PyObject *p3imp_PyExc_KeyboardInterrupt;
563static PyObject *p3imp_PyExc_TypeError;
564static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200565static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200566static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200567static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200568static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200569
570# define PyExc_AttributeError p3imp_PyExc_AttributeError
571# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200572# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200573# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
574# define PyExc_TypeError p3imp_PyExc_TypeError
575# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200576# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200577# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200578# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200579# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200580
581/*
582 * Table of name to function pointer of python.
583 */
584# define PYTHON_PROC FARPROC
585static struct
586{
587 char *name;
588 PYTHON_PROC *ptr;
589} py3_funcname_table[] =
590{
591 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100592 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200593 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200594 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
595 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200596 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200597 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200598 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
599 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
600 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
601 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200602 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200603 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200604 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200605 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200606 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
607 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
608 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200609 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200610 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
611 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200612# if PY_VERSION_HEX >= 0x030601f0
613 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
614 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
615# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200616 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
617 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
618 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
619 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200620 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200621 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Ken Takata9abd7152024-08-10 09:44:20 +0200622# if defined(USE_LIMITED_API) \
623 && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
624# if Py_LIMITED_API+0 >= 0x030a00A7
625 {"_Py_IncRef", (PYTHON_PROC*)&py3__Py_IncRef},
626# else
627 {"Py_IncRef", (PYTHON_PROC*)&py3_Py_IncRef},
628# endif
629# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200630# ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200631 {"Py_CompileString", (PYTHON_PROC*)&py3_Py_CompileString},
632 {"PyEval_EvalCode", (PYTHON_PROC*)&PyEval_EvalCode},
Ken Takatac97b3fe2023-10-11 21:27:06 +0200633# else
634 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
635 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200636# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200637 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200638 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200639 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
640 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200641# if PY_VERSION_HEX >= 0x030d0000
642 {"PyObject_CallFunction", (PYTHON_PROC*)&py3_PyObject_CallFunction},
643# else
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200644 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Yaakov Selkowitz4179f192024-07-04 16:36:05 +0200645# endif
Bram Moolenaarf4258302013-06-02 18:20:17 +0200646 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200647 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
648 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200649 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
650 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200651 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200652 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200653 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
654 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
655 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
656 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200657 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
658 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200659 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200660# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
661 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200662 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
663# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200664 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
665 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200666 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200667 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200668 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200669 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
670 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200671# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200672 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
673# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200674 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
675 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
676 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
677 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
678 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
679 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
680 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200681 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200682 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Yee Cheng Chin97a5be42024-09-09 19:46:17 +0200683# if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000
Bram Moolenaardb913952012-06-29 12:54:53 +0200684 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Ken Takata119fdd92023-10-04 20:05:05 +0200685# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200686 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200687 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
688 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200689 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200690 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100691 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200692 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
693 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
694 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200695# ifdef USE_LIMITED_API
696# if Py_LIMITED_API >= 0x030a0000
697 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
698# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200699# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200700# if PY_VERSION_HEX >= 0x030300f0
701 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
702# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200703 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200704# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200705# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200706 {"PyUnicode_CompareWithASCIIString", (PYTHON_PROC*)&py3_PyUnicode_CompareWithASCIIString},
707 {"PyUnicode_AsUTF8String", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8String},
Ken Takatac97b3fe2023-10-11 21:27:06 +0200708# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200709# ifdef Py_UNICODE_WIDE
710 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
711# else
712 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
713# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200714# else
715 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200716# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200717 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200718 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200719 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100720 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200721# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200722 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
723# endif
724# if PY_VERSION_HEX >= 0x030900b0
725 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
726# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200727 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
728 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200729 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200730 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
731 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200732 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200733# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200734 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200735# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200736 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200737 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200738 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200739 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
740 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200741 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200742# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200743 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
744 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200745 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
746# else
747 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
748# endif
749# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200750 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
751 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200752# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200753 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
754 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200755# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200756 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
757 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
758 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200759 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
760 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
761 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200762# ifdef USE_LIMITED_API
763# if PY_VERSION_HEX >= 0x03040000
764 {"PyType_GetSlot", (PYTHON_PROC*)&py3_PyType_GetSlot},
765# endif
766 {"PyType_FromSpec", (PYTHON_PROC*)&py3_PyType_FromSpec},
767# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200768 {"", NULL},
769};
770
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200771# if PY_VERSION_HEX >= 0x030800f0
772 static inline void
773py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
774{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200775 if (--op->ob_refcnt != 0)
776 {
777# ifdef Py_REF_DEBUG
778 if (op->ob_refcnt < 0)
779 {
780 _Py_NegativeRefcount(filename, lineno, op);
781 }
782# endif
783 }
784 else
785 {
786 _Py_Dealloc(op);
787 }
788}
789
790# undef Py_DECREF
791# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
792
793 static inline void
794py3__Py_XDECREF(PyObject *op)
795{
796 if (op != NULL)
797 {
798 Py_DECREF(op);
799 }
800}
801
802# undef Py_XDECREF
803# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
804# endif
805
Ken Takata9abd7152024-08-10 09:44:20 +0200806# if defined(USE_LIMITED_API) \
807 && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
808 static inline void
809py3__Py_XINCREF(PyObject *op)
810{
811 if (op != NULL)
812 {
813 Py_INCREF(op);
814 }
815}
816# undef Py_XINCREF
817# define Py_XINCREF(op) py3__Py_XINCREF(_PyObject_CAST(op))
818# endif
819
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200820# if PY_VERSION_HEX >= 0x030900b0
821 static inline int
822py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
823{
824 return ((PyType_GetFlags(type) & feature) != 0);
825}
826# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
827# endif
828
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200829# if PY_VERSION_HEX >= 0x030a00b2
830 static inline int
831py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
832{
833 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
834}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100835# if PY_VERSION_HEX >= 0x030b00b3
836# undef PyObject_TypeCheck
837# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
838# else
839# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
840# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200841# endif
842
Ken Takatafa145f22023-10-06 19:27:13 +0200843# if !defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x030c00b0
Yee Cheng Chin396058a2023-10-17 10:38:11 +0200844// PyTuple_GET_SIZE/PyList_GET_SIZE are inlined functions that use Py_SIZE(),
845// which started to introduce linkage dependency from Python 3.12. When we
846// build Python in dynamic mode, we don't link against it in build time, and
847// this would fail to build. Just use the non-inlined version instead.
Ken Takatafa145f22023-10-06 19:27:13 +0200848# undef PyTuple_GET_SIZE
Yee Cheng Chin396058a2023-10-17 10:38:11 +0200849# define PyTuple_GET_SIZE(o) PyTuple_Size(o)
Ken Takatafa145f22023-10-06 19:27:13 +0200850# undef PyList_GET_SIZE
Yee Cheng Chin396058a2023-10-17 10:38:11 +0200851# define PyList_GET_SIZE(o) PyList_Size(o)
Ken Takatafa145f22023-10-06 19:27:13 +0200852# endif
853
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100854# ifdef MSWIN
855/*
856 * Look up the library "libname" using the InstallPath registry key.
857 * Return NULL when failed. Return an allocated string when successful.
858 */
Ken Takataae3cfa42023-10-14 11:49:09 +0200859 static WCHAR *
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100860py3_get_system_libname(const char *libname)
861{
Ken Takataae3cfa42023-10-14 11:49:09 +0200862 const WCHAR *pythoncore = L"Software\\Python\\PythonCore";
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100863 const char *cp = libname;
Ken Takataae3cfa42023-10-14 11:49:09 +0200864 WCHAR subkey[128];
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100865 HKEY hKey;
Ken Takataae3cfa42023-10-14 11:49:09 +0200866 int i;
867 DWORD j, len;
868 LSTATUS ret;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100869
870 while (*cp != '\0')
871 {
872 if (*cp == ':' || *cp == '\\' || *cp == '/')
873 {
874 // Bail out if "libname" contains path separator, assume it is
875 // an absolute path.
876 return NULL;
877 }
878 ++cp;
879 }
Ken Takataae3cfa42023-10-14 11:49:09 +0200880
881 WCHAR keyfound[32];
882 HKEY hKeyTop[] = {HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
883 HKEY hKeyFound = NULL;
884# ifdef USE_LIMITED_API
885 long maxminor = -1;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100886# endif
Ken Takataae3cfa42023-10-14 11:49:09 +0200887 for (i = 0; i < ARRAY_LENGTH(hKeyTop); i++)
888 {
889 long major, minor;
890
891 ret = RegOpenKeyExW(hKeyTop[i], pythoncore, 0, KEY_READ, &hKey);
892 if (ret != ERROR_SUCCESS)
893 continue;
894 for (j = 0;; j++)
895 {
896 WCHAR keyname[32];
897 WCHAR *wp;
898
899 len = ARRAY_LENGTH(keyname);
900 ret = RegEnumKeyExW(hKey, j, keyname, &len,
901 NULL, NULL, NULL, NULL);
902 if (ret == ERROR_NO_MORE_ITEMS)
903 break;
904
905 major = wcstol(keyname, &wp, 10);
Ken Takata982ef162023-10-18 12:02:24 +0200906 if (*wp != L'.')
907 continue;
908 minor = wcstol(wp + 1, &wp, 10);
Ken Takataae3cfa42023-10-14 11:49:09 +0200909# ifdef _WIN64
910 if (*wp != L'\0')
911 continue;
912# else
913 if (wcscmp(wp, L"-32") != 0)
914 continue;
915# endif
916
917 if (major != PY_MAJOR_VERSION)
918 continue;
919# ifdef USE_LIMITED_API
920 // Search the latest version.
921 if ((minor > maxminor)
922 && (minor >= ((Py_LIMITED_API >> 16) & 0xff)))
923 {
924 maxminor = minor;
925 wcscpy(keyfound, keyname);
926 hKeyFound = hKeyTop[i];
927 }
928# else
929 // Check if it matches with the compiled version.
930 if (minor == PY_MINOR_VERSION)
931 {
932 wcscpy(keyfound, keyname);
933 hKeyFound = hKeyTop[i];
934 break;
935 }
936# endif
937 }
938 RegCloseKey(hKey);
939# ifdef USE_LIMITED_API
940 if (hKeyFound != NULL)
941 break;
942# endif
943 }
944 if (hKeyFound == NULL)
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100945 return NULL;
Ken Takataae3cfa42023-10-14 11:49:09 +0200946
947 swprintf(subkey, ARRAY_LENGTH(subkey), L"%ls\\%ls\\InstallPath",
948 pythoncore, keyfound);
949 ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ,
950 NULL, NULL, &len);
951 if (ret != ERROR_MORE_DATA && ret != ERROR_SUCCESS)
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100952 return NULL;
Ken Takataae3cfa42023-10-14 11:49:09 +0200953 size_t len2 = len / sizeof(WCHAR) + 1 + strlen(libname);
954 WCHAR *path = alloc(len2 * sizeof(WCHAR));
955 if (path == NULL)
956 return NULL;
957 ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ,
958 NULL, path, &len);
959 if (ret != ERROR_SUCCESS)
960 {
961 vim_free(path);
962 return NULL;
963 }
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100964 // Remove trailing path separators.
Ken Takataae3cfa42023-10-14 11:49:09 +0200965 size_t len3 = wcslen(path);
966 if ((len3 > 0) && (path[len3 - 1] == L'/' || path[len3 - 1] == L'\\'))
967 --len3;
968 swprintf(path + len3, len2 - len3, L"\\%hs", libname);
969 return path;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100970}
971# endif
972
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200973/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200974 * Load library and get all pointers.
975 * Parameter 'libname' provides name of DLL.
976 * Return OK or FAIL.
977 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200978 static int
979py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200980{
981 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200982 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
983 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
984 PYTHON_PROC *ucs_as_encoded_string =
985 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200986
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100987# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100988 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000989 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100990 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200991 if (python_loaded())
992 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200993 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000994 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200995 return FAIL;
996 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200997# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200998
999 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001000 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001001 hinstPy3 = load_dll(libname);
1002
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +01001003# ifdef MSWIN
1004 if (!hinstPy3)
1005 {
1006 // Attempt to use the path from InstallPath as stored in the registry.
Ken Takataae3cfa42023-10-14 11:49:09 +02001007 WCHAR *syslibname = py3_get_system_libname(libname);
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +01001008
1009 if (syslibname != NULL)
1010 {
Ken Takataae3cfa42023-10-14 11:49:09 +02001011 hinstPy3 = LoadLibraryExW(syslibname, NULL,
1012 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
1013 LOAD_LIBRARY_SEARCH_SYSTEM32);
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +01001014 vim_free(syslibname);
1015 }
1016 }
1017# endif
1018
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001019 if (!hinstPy3)
1020 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001021 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001022 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001023 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001024 }
1025
1026 for (i = 0; py3_funcname_table[i].ptr; ++i)
1027 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001028 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
1029 py3_funcname_table[i].name)) == NULL)
1030 {
1031 close_dll(hinstPy3);
1032 hinstPy3 = 0;
1033 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001034 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001035 return FAIL;
1036 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001037 }
1038
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001039 // Load unicode functions separately as only the ucs2 or the ucs4 functions
1040 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001041# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001042 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
1043 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
1044 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001045 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001046# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001047 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
1048 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001049 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001050 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001051 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001052 if (*ucs_from_string == NULL || *ucs_decode == NULL
1053 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001054 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001055 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001056 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001057 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001058 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001059 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001060 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001061 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001062# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001063 if (*ucs_from_string == NULL || *ucs_decode == NULL
1064 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001065 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001066 close_dll(hinstPy3);
1067 hinstPy3 = 0;
1068 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001069 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001070 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001071 }
1072
1073 return OK;
1074}
1075
1076/*
1077 * If python is enabled (there is installed python on Windows system) return
1078 * TRUE, else FALSE.
1079 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001080 int
1081python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001082{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +01001083 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001084}
1085
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001086/*
1087 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001088 * DLL, as this can cause errors (importing data symbols is not reliable).
1089 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001090 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001091get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001092{
1093 PyObject *exmod = PyImport_ImportModule("builtins");
1094 PyObject *exdict = PyModule_GetDict(exmod);
1095 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
1096 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +02001097 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001098 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
1099 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
1100 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +02001101 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +02001102 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001103 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +02001104 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001105 Py_XINCREF(p3imp_PyExc_AttributeError);
1106 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +02001107 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001108 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
1109 Py_XINCREF(p3imp_PyExc_TypeError);
1110 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +02001111 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +02001112 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001113 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02001114 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001115 Py_XDECREF(exmod);
1116}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001117#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001118
Bram Moolenaardb913952012-06-29 12:54:53 +02001119static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001120#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001121static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02001122
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001123#ifdef USE_LIMITED_API
1124# define DESTRUCTOR_FINISH(self) \
Ken Takata9abd7152024-08-10 09:44:20 +02001125 ((freefunc)PyType_GetSlot(Py_TYPE((PyObject*)self), Py_tp_free))((PyObject*)self)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001126#else
1127# define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
1128#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02001129
Bram Moolenaar971db462013-05-12 18:44:48 +02001130#define WIN_PYTHON_REF(win) win->w_python3_ref
1131#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001132#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +02001133
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001134 static void
1135call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001136{
Bram Moolenaar0014a532013-05-29 21:33:39 +02001137#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001138 _PyObject_DebugFree(p);
1139#else
1140 PyObject_Free(p);
1141#endif
1142}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001143
1144 static PyObject *
1145call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001146{
1147 return PyType_GenericNew(type,args,kwds);
1148}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001149
1150 static PyObject *
1151call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001152{
1153 return PyType_GenericAlloc(type,nitems);
1154}
1155
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001156static PyObject *OutputGetattro(PyObject *, PyObject *);
1157static int OutputSetattro(PyObject *, PyObject *, PyObject *);
1158static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001159static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001160static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001161static PyObject *WindowGetattro(PyObject *, PyObject *);
1162static int WindowSetattro(PyObject *, PyObject *, PyObject *);
1163static PyObject *RangeGetattro(PyObject *, PyObject *);
1164static PyObject *CurrentGetattro(PyObject *, PyObject *);
1165static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
1166static PyObject *DictionaryGetattro(PyObject *, PyObject *);
1167static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
1168static PyObject *ListGetattro(PyObject *, PyObject *);
1169static int ListSetattro(PyObject *, PyObject *, PyObject *);
1170static PyObject *FunctionGetattro(PyObject *, PyObject *);
1171
1172static struct PyModuleDef vimmodule;
1173
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02001174#define PY_CAN_RECURSE
1175
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001176/*
1177 * Include the code shared with if_python.c
1178 */
1179#include "if_py_both.h"
1180
Ken Takatac97b3fe2023-10-11 21:27:06 +02001181#ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001182# if Py_LIMITED_API >= 0x030A0000
1183# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1184# else
1185// Python limited API before 3.10 lack easy ways to query the raw UTF-8 chars.
1186// We need to first convert the string to bytes, and then extract the chars.
1187// This function is only used for attribute string comparisons, which have
1188// known short length. As such, just allocate a short static buffer to hold
1189// the characters instead of having to allocate/deallcoate it.
1190//
1191// An alternative would be to convert all attribute string comparisons to use
1192// PyUnicode_CompareWithASCIIString to skip having to extract the chars.
1193static char py3_unicode_utf8_chars[20];
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02001194static char* PY_UNICODE_GET_UTF8_CHARS(PyObject* str)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001195{
1196 py3_unicode_utf8_chars[0] = '\0';
1197 PyObject* bytes = PyUnicode_AsUTF8String(str);
1198 if (bytes)
1199 {
1200 char *chars;
1201 Py_ssize_t len;
1202 if (PyBytes_AsStringAndSize(bytes, &chars, &len) != -1)
1203 {
1204 if (len < (Py_ssize_t)sizeof(py3_unicode_utf8_chars))
1205 // PyBytes_AsStringAndSize guarantees null-termination
1206 memcpy(py3_unicode_utf8_chars, chars, len + 1);
1207 }
1208 Py_DECREF(bytes);
1209 }
1210 return py3_unicode_utf8_chars;
1211}
1212# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +02001213#else // !USE_LIMITED_API
1214# if PY_VERSION_HEX >= 0x030300f0
1215# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1216# else
1217# define PY_UNICODE_GET_UTF8_CHARS _PyUnicode_AsString
1218# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001219#endif
1220
Bram Moolenaar828bff12019-03-19 22:11:41 +01001221// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001222#define GET_ATTR_STRING(name, nameobj) \
1223 char *name = ""; \
1224 if (PyUnicode_Check(nameobj)) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001225 name = (char *)PY_UNICODE_GET_UTF8_CHARS(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001226
1227#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
1228
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001229///////////////////////////////////////////////////////
1230// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001231
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001232static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001233
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001234///////////////////////////////////////////////////////
1235// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001236
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001237 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001238python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001239{
1240 static int recurse = 0;
1241
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001242 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001244 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001245
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001246 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247 ++recurse;
1248
1249#ifdef DYNAMIC_PYTHON3
1250 if (hinstPy3)
1251#endif
1252 if (Py_IsInitialized())
1253 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001254#ifdef USE_LIMITED_API
1255 shutdown_types();
1256#endif
1257
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001258 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001259 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001261 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262 }
1263
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001264 --recurse;
1265}
1266
Bram Moolenaar094454f2015-10-07 10:39:55 +02001267#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001268 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001269python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001270{
1271 return (hinstPy3 != 0);
1272}
1273#endif
1274
Bram Moolenaar94073162018-01-31 21:49:05 +01001275static wchar_t *py_home_buf = NULL;
1276
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001277#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001278/*
1279 * Return TRUE if stdin is readable from Python 3.
1280 */
1281 static BOOL
1282is_stdin_readable(void)
1283{
1284 DWORD mode, eventnum;
1285 struct _stat st;
1286 int fd = fileno(stdin);
1287 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1288
1289 // Check if stdin is connected to the console.
1290 if (GetConsoleMode(hstdin, &mode))
1291 // Check if it is opened as input.
1292 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1293
1294 return _fstat(fd, &st) == 0;
1295}
1296
1297// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1298// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001299// Note that the python DLL is linked to its own stdio DLL which can be
1300// differ from Vim's stdio.
1301 static void
1302reset_stdin(void)
1303{
1304 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1305 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Ken Takata119fdd92023-10-04 20:05:05 +02001306 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001307
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001308 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001309 return;
1310
1311 // Get "freopen" and "stdin" which are used in the python DLL.
1312 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1313 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1314 if (py__acrt_iob_func)
1315 {
1316 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001317 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001318 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001319 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001320 }
1321
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001322 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001323 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001324 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001325 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001326 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001327}
1328#else
1329# define reset_stdin()
1330#endif
1331
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001332// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1333// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1334// Install a hook to python dll's exit() and recover from it.
1335#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1336# define HOOK_EXIT
1337# include <setjmp.h>
1338
1339static jmp_buf exit_hook_jump_buf;
1340static void *orig_exit = NULL;
1341
1342/*
1343 * Function that replaces exit() while calling Py_Initialize().
1344 */
1345 static void
1346hooked_exit(int ret)
1347{
1348 // Recover from exit.
1349 longjmp(exit_hook_jump_buf, 1);
1350}
1351
1352/*
1353 * Install a hook to python dll's exit().
1354 */
1355 static void
1356hook_py_exit(void)
1357{
Ken Takata119fdd92023-10-04 20:05:05 +02001358 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001359
1360 if (hinst == NULL || orig_exit != NULL)
1361 return;
1362
1363 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1364}
1365
1366/*
1367 * Remove the hook installed by hook_py_exit().
1368 */
1369 static void
1370restore_py_exit(void)
1371{
1372 HINSTANCE hinst = hinstPy3;
1373
1374 if (hinst == NULL)
1375 return;
1376
1377 if (orig_exit != NULL)
1378 hook_dll_import_func(hinst, "exit", orig_exit);
1379 orig_exit = NULL;
1380}
1381#endif
1382
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001383 static int
1384Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001385{
1386 if (!py3initialised)
1387 {
1388#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001389 if (!python3_enabled(TRUE))
1390 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001391 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001392 goto fail;
1393 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001394#endif
1395
1396 init_structs();
1397
Bram Moolenaar94073162018-01-31 21:49:05 +01001398 if (*p_py3home != NUL)
1399 {
1400 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001401
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001402 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001403 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001404 if (py_home_buf != NULL && mbstowcs(
1405 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1406 Py_SetPythonHome(py_home_buf);
1407 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001408#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001409 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001410 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001411#endif
1412
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001413 PyImport_AppendInittab("vim", Py3Init_vim);
1414
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001415#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1416 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1417#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001418 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001419
1420#ifdef HOOK_EXIT
1421 // Catch exit() called in Py_Initialize().
1422 hook_py_exit();
1423 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001424 {
1425 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001426 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001427 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001428 else
1429 {
1430 // exit() was called in Py_Initialize().
1431 restore_py_exit();
1432 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1433 goto fail;
1434 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001435#else
1436 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001437#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001438
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001439#if PY_VERSION_HEX < 0x03090000
1440 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001441 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001442#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001444 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001445#endif
1446
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001447 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001448 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001449
Bram Moolenaardb913952012-06-29 12:54:53 +02001450 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1451
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001452 // Remove the element from sys.path that was added because of our
1453 // argv[0] value in Py3Init_vim(). Previously we used an empty
1454 // string, but depending on the OS we then get an empty entry or
1455 // the current directory in sys.path.
1456 // Only after vim has been imported, the element does exist in
1457 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001458 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 +02001459
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001460 // Without the call to PyEval_SaveThread, thread specific state (such
1461 // as the system trace hook), will be lost between invocations of
1462 // Python code.
1463 // GIL may have been created and acquired in PyEval_InitThreads() and
1464 // thread state is created in Py_Initialize(); there
1465 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1466 // have threads enabled!), so the following does both: unlock GIL and
1467 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001468 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001469
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001470 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001471 }
1472
1473 return 0;
1474
1475fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001476 // We call PythonIO_Flush() here to print any Python errors.
1477 // This is OK, as it is possible to call this function even
1478 // if PythonIO_Init_io() has not completed successfully (it will
1479 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480 PythonIO_Flush();
1481 return -1;
1482}
1483
1484/*
1485 * External interface
1486 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001487 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001488DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001489{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001490#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001491 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001492#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001493 PyObject *cmdstr;
1494 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001495 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001496
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001497 if (python_end_called)
1498 goto theend;
1499
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001500 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001501 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001502
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001503 init_range(arg);
1504
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001505 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001506
1507#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001508 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509 saved_locale = setlocale(LC_NUMERIC, NULL);
1510 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001511 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001512 else
1513 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001514 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001515 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1516 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001517 }
1518#endif
1519
1520 pygilstate = PyGILState_Ensure();
1521
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001522 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1523 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001524 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001525 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1526 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001527 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001528
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001529 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001530 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001531
1532 PyGILState_Release(pygilstate);
1533
1534#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1535 if (saved_locale != NULL)
1536 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 (void)setlocale(LC_NUMERIC, saved_locale);
1538 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001539 }
1540#endif
1541
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001542 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001543 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001544
1545theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001546 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001547}
1548
1549/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001550 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001551 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001552 void
1553ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001554{
1555 char_u *script;
1556
1557 script = script_get(eap, eap->arg);
1558 if (!eap->skip)
1559 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001560 if (p_pyx == 0)
1561 p_pyx = 3;
1562
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001563 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001564 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001565 (runner) run_cmd,
1566 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001567 }
1568 vim_free(script);
1569}
1570
1571#define BUFFER_SIZE 2048
1572
1573/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001574 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001575 */
1576 void
1577ex_py3file(exarg_T *eap)
1578{
1579 static char buffer[BUFFER_SIZE];
1580 const char *file;
1581 char *p;
1582 int i;
1583
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001584 if (p_pyx == 0)
1585 p_pyx = 3;
1586
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001587 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1588 // stdio file pointer, but Vim and the Python DLL are compiled with
1589 // different options under Windows, meaning that stdio pointers aren't
1590 // compatible between the two. Yuk.
1591 //
1592 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1593 //
1594 // Using bytes so that Python can detect the source encoding as it normally
1595 // does. The doc does not say "compile" accept bytes, though.
1596 //
1597 // We need to escape any backslashes or single quotes in the file name, so that
1598 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001599
1600 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001601 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001602
1603 for (i=0; i<2; ++i)
1604 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 file = (char *)eap->arg;
1606 while (*file && p < buffer + (BUFFER_SIZE - 3))
1607 {
1608 if (*file == '\\' || *file == '\'')
1609 *p++ = '\\';
1610 *p++ = *file++;
1611 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001612 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001613 if (*file != '\0')
1614 return;
1615 if (i==0)
1616 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001617 strcpy(p,"','rb').read(),'");
1618 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001619 }
1620 else
1621 {
1622 strcpy(p,"','exec'))");
1623 p += 10;
1624 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001625 }
1626
1627
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001628 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001629 DoPyCommand(buffer,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001630 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001631 (runner) run_cmd,
1632 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001633}
1634
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001635 void
1636ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001637{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001638 if (p_pyx == 0)
1639 p_pyx = 3;
1640
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001641 DoPyCommand((char *)eap->arg,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001642 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001643 (runner)run_do,
1644 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001645}
1646
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001647///////////////////////////////////////////////////////
1648// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001649
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001650// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001651
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001652 static PyObject *
1653OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001654{
Bram Moolenaar77045652012-09-21 13:46:06 +02001655 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001656
1657 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001659 else if (strcmp(name, "errors") == 0)
1660 return PyString_FromString("strict");
1661 else if (strcmp(name, "encoding") == 0)
1662 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001663
1664 return PyObject_GenericGetAttr(self, nameobj);
1665}
1666
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001667 static int
1668OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001669{
Bram Moolenaar77045652012-09-21 13:46:06 +02001670 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001671
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001672 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001673}
1674
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001675///////////////////////////////////////////////////////
1676// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001677
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001678// Window type - Implementation functions
1679// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001680
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001681#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1682
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001683// Buffer type - Implementation functions
1684// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001685
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001686#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1687
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001688static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001689static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001690
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001691// Line range type - Implementation functions
1692// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001693
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001694#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1695
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001696static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001697static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1698static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001699
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001700// Current objects type - Implementation functions
1701// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001702
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001703static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001704 (lenfunc) BufferLength, // sq_length, len(x)
1705 (binaryfunc) 0, // sq_concat, x+y
1706 (ssizeargfunc) 0, // sq_repeat, x*n
1707 (ssizeargfunc) BufferItem, // sq_item, x[i]
1708 0, // was_sq_slice, x[i:j]
1709 0, // sq_ass_item, x[i]=v
1710 0, // sq_ass_slice, x[i:j]=v
1711 0, // sq_contains
1712 0, // sq_inplace_concat
1713 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001714};
1715
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001716static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001717 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001718 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001719 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001720};
1721
1722
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001723// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001724
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001725 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001726BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001727{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001728 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001729
Bram Moolenaar77045652012-09-21 13:46:06 +02001730 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001731
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001732 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1733 return r;
1734
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001735 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001736 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001737
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001738 r = BufferAttr((BufferObject *)(self), name);
1739 if (r || PyErr_Occurred())
1740 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001741 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001742 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001743}
1744
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001745 static int
1746BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1747{
1748 GET_ATTR_STRING(name, nameobj);
1749
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001750 return BufferSetattr(self, name, val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001751}
1752
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001753//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001754
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001755 static PyObject *
1756BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001757{
Bram Moolenaardb913952012-06-29 12:54:53 +02001758 if (PyLong_Check(idx))
1759 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001760 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001761 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001762 }
1763 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001764 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001765 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001766
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001767 if (CheckBuffer((BufferObject *) self))
1768 return NULL;
1769
Bram Moolenaar922a4662014-03-30 16:11:43 +02001770 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001771 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001772 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001773 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001774 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001775 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001776 }
1777 else
1778 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001779 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001780 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001781 }
1782}
1783
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001784 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001785BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1786{
Bram Moolenaardb913952012-06-29 12:54:53 +02001787 if (PyLong_Check(idx))
1788 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001789 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001790
1791 if (CheckBuffer((BufferObject *) self))
1792 return -1;
1793
Bram Moolenaar19e60942011-06-19 00:27:51 +02001794 return RBAsItem((BufferObject *)(self), n, val, 1,
1795 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1796 NULL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001797 }
1798 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001799 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001800 Py_ssize_t start, stop, step, slicelen;
1801
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001802 if (CheckBuffer((BufferObject *) self))
1803 return -1;
1804
Bram Moolenaar922a4662014-03-30 16:11:43 +02001805 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001806 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001807 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001808 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001809 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001810 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1811 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1812 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001813 }
1814 else
1815 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001816 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001817 return -1;
1818 }
1819}
1820
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001821static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001822 (lenfunc) RangeLength, // sq_length, len(x)
1823 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1824 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1825 (ssizeargfunc) RangeItem, // sq_item, x[i]
1826 0, // was_sq_slice, x[i:j]
1827 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1828 0, // sq_ass_slice, x[i:j]=v
1829 0, // sq_contains
1830 0, // sq_inplace_concat
1831 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001832};
1833
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001834static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001835 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001836 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001837 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001838};
1839
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001840// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001841
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001842 static PyObject *
1843RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001844{
Bram Moolenaar77045652012-09-21 13:46:06 +02001845 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001846
1847 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001848 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001849 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001850 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001851 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001852 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001853}
1854
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001855////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001856
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001857 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001858RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001859{
1860 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001861 ((RangeObject *)(self))->start,
1862 ((RangeObject *)(self))->end,
1863 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001864}
1865
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001866 static Py_ssize_t
1867RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1868{
1869 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1870 ((RangeObject *)(self))->start,
1871 ((RangeObject *)(self))->end,
1872 &((RangeObject *)(self))->end);
1873}
1874
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001875 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001876RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001877{
Bram Moolenaardb913952012-06-29 12:54:53 +02001878 if (PyLong_Check(idx))
1879 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001880 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001881 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001882 }
1883 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001884 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001885 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001886
Bram Moolenaar922a4662014-03-30 16:11:43 +02001887 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001888 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1889 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001890 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001891 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001892 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 }
1894 else
1895 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001896 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001897 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001898 }
1899}
1900
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001901 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001902RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1903{
Bram Moolenaardb913952012-06-29 12:54:53 +02001904 if (PyLong_Check(idx))
1905 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001906 long n = PyLong_AsLong(idx);
1907 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001908 }
1909 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001910 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001911 Py_ssize_t start, stop, step, slicelen;
1912
Bram Moolenaar922a4662014-03-30 16:11:43 +02001913 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001914 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1915 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001916 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001917 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001918 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001919 }
1920 else
1921 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001922 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001923 return -1;
1924 }
1925}
1926
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001927// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001928
1929 static PyObject *
1930TabPageGetattro(PyObject *self, PyObject *nameobj)
1931{
1932 PyObject *r;
1933
1934 GET_ATTR_STRING(name, nameobj);
1935
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001936 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1937 return r;
1938
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001939 if (CheckTabPage((TabPageObject *)(self)))
1940 return NULL;
1941
1942 r = TabPageAttr((TabPageObject *)(self), name);
1943 if (r || PyErr_Occurred())
1944 return r;
1945 else
1946 return PyObject_GenericGetAttr(self, nameobj);
1947}
1948
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001949// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001950
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001951 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001952WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001953{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001954 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001955
Bram Moolenaar77045652012-09-21 13:46:06 +02001956 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001957
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001958 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1959 return r;
1960
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001961 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001962 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001963
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001964 r = WindowAttr((WindowObject *)(self), name);
1965 if (r || PyErr_Occurred())
1966 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001967 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001968 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001969}
1970
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001971 static int
1972WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001973{
Bram Moolenaar77045652012-09-21 13:46:06 +02001974 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001975
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001976 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001977}
1978
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001979// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001980
1981static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001982 (lenfunc) TabListLength, // sq_length, len(x)
1983 (binaryfunc) 0, // sq_concat, x+y
1984 (ssizeargfunc) 0, // sq_repeat, x*n
1985 (ssizeargfunc) TabListItem, // sq_item, x[i]
1986 0, // sq_slice, x[i:j]
1987 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1988 0, // sq_ass_slice, x[i:j]=v
1989 0, // sq_contains
1990 0, // sq_inplace_concat
1991 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001992};
1993
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001994// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001995
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001996static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001997 (lenfunc) WinListLength, // sq_length, len(x)
1998 (binaryfunc) 0, // sq_concat, x+y
1999 (ssizeargfunc) 0, // sq_repeat, x*n
2000 (ssizeargfunc) WinListItem, // sq_item, x[i]
2001 0, // sq_slice, x[i:j]
2002 (ssizeobjargproc)0, // sq_as_item, x[i]=v
2003 0, // sq_ass_slice, x[i:j]=v
2004 0, // sq_contains
2005 0, // sq_inplace_concat
2006 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002007};
2008
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002009/*
2010 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002011 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002012 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002013CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002014{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002015 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02002016 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002017 if (!(r = CurrentGetattr(self, name)))
2018 return PyObject_GenericGetAttr(self, nameobj);
2019 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002020}
2021
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002022 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002023CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002024{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002025 GET_ATTR_STRING(name, nameobj);
2026 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002027}
2028
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002029// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002030
Bram Moolenaar66b79852012-09-21 14:00:35 +02002031 static PyObject *
2032DictionaryGetattro(PyObject *self, PyObject *nameobj)
2033{
2034 DictionaryObject *this = ((DictionaryObject *) (self));
2035
2036 GET_ATTR_STRING(name, nameobj);
2037
2038 if (strcmp(name, "locked") == 0)
2039 return PyLong_FromLong(this->dict->dv_lock);
2040 else if (strcmp(name, "scope") == 0)
2041 return PyLong_FromLong(this->dict->dv_scope);
2042
2043 return PyObject_GenericGetAttr(self, nameobj);
2044}
2045
2046 static int
2047DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
2048{
2049 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002050 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02002051}
2052
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002053// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002054
Bram Moolenaar66b79852012-09-21 14:00:35 +02002055 static PyObject *
2056ListGetattro(PyObject *self, PyObject *nameobj)
2057{
2058 GET_ATTR_STRING(name, nameobj);
2059
2060 if (strcmp(name, "locked") == 0)
2061 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
2062
2063 return PyObject_GenericGetAttr(self, nameobj);
2064}
2065
2066 static int
2067ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
2068{
2069 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002070 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02002071}
2072
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002073// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002074
Bram Moolenaardb913952012-06-29 12:54:53 +02002075 static PyObject *
2076FunctionGetattro(PyObject *self, PyObject *nameobj)
2077{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002078 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02002079 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02002080
2081 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002082
Bram Moolenaar8110a092016-04-14 15:56:09 +02002083 r = FunctionAttr(this, name);
2084 if (r || PyErr_Occurred())
2085 return r;
2086 else
2087 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002088}
2089
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002090// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002091
2092 void
2093python3_buffer_free(buf_T *buf)
2094{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002095 BufferObject *bp = BUF_PYTHON_REF(buf);
2096 if (bp == NULL)
2097 return;
2098 bp->buf = INVALID_BUFFER_VALUE;
2099 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002100}
2101
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002102 void
2103python3_window_free(win_T *win)
2104{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002105 WindowObject *wp = WIN_PYTHON_REF(win);
2106 if (wp == NULL)
2107 return;
2108 wp->win = INVALID_WINDOW_VALUE;
2109 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002110}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002111
2112 void
2113python3_tabpage_free(tabpage_T *tab)
2114{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002115 TabPageObject *tp = TAB_PYTHON_REF(tab);
2116 if (tp == NULL)
2117 return;
2118 tp->tab = INVALID_TABPAGE_VALUE;
2119 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002120}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002121
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01002122 static PyObject *
2123Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002124{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002125 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002126 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
2127
Bram Moolenaar1dc28782013-05-21 19:11:01 +02002128 if (init_types())
2129 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002130
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002131 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002132 PySys_SetArgv(1, argv);
2133
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002134 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002135 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002136
Bram Moolenaardee2e312013-06-23 16:35:47 +02002137 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002138 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002139
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002140 if (init_sys_path())
2141 return NULL;
2142
2143 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002144}
2145
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002146//////////////////////////////////////////////////////////////////////////
2147// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002148
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002149/*
2150 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002151 * All internal newlines are replaced by null characters.
2152 *
2153 * On errors, the Python exception data is set, and NULL is returned.
2154 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002155 static PyObject *
2156LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002157{
2158 PyObject *result;
2159 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01002160 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002161
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002162 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002163 p = tmp;
2164 if (p == NULL)
2165 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002166 PyErr_NoMemory();
2167 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002168 }
2169
2170 while (*str)
2171 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002172 if (*str == '\n')
2173 *p = '\0';
2174 else
2175 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002176
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002177 ++p;
2178 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002179 }
2180 *p = '\0';
2181
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01002182 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002183
2184 vim_free(tmp);
2185 return result;
2186}
2187
Bram Moolenaardb913952012-06-29 12:54:53 +02002188 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002189do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02002190{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002191 DoPyCommand((char *) str,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02002192 init_range_eval,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002193 (runner) run_eval,
2194 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002195 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02002196 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002197 rettv->v_type = VAR_NUMBER;
2198 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02002199 }
2200}
2201
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002202 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002203set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02002204{
Bram Moolenaarb641df42015-02-03 13:16:04 +01002205 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02002206}
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002207
2208 int
Dominique Pellé4927bc72023-09-24 16:12:07 +02002209python3_version(void)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002210{
2211#ifdef USE_LIMITED_API
2212 return Py_LIMITED_API;
2213#else
2214 return PY_VERSION_HEX;
2215#endif
2216}