blob: a1773717202c663d9e780fca64eebb412b9d0580 [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 Takatac97b3fe2023-10-11 21:27:06 +0200222# ifdef USE_LIMITED_API
223# define Py_CompileString py3_Py_CompileString
224# define PyEval_EvalCode py3_PyEval_EvalCode
225# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200226# undef PyRun_SimpleString
227# define PyRun_SimpleString py3_PyRun_SimpleString
228# undef PyRun_String
229# define PyRun_String py3_PyRun_String
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200230# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200231# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200232# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200233# define PyObject_SetAttrString py3_PyObject_SetAttrString
234# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200235# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200236# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200237# define PyEval_GetLocals py3_PyEval_GetLocals
238# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200239# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200240# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200241# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200242# define PyType_Ready py3_PyType_Ready
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200243# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200244# define PyType_GetFlags py3_PyType_GetFlags
245# endif
Ken Takata119fdd92023-10-04 20:05:05 +0200246# undef Py_BuildValue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200247# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100248# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200249# define Py_Initialize py3_Py_Initialize
250# define Py_Finalize py3_Py_Finalize
251# define Py_IsInitialized py3_Py_IsInitialized
252# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200253# define _Py_FalseStruct (*py3__Py_FalseStruct)
254# define _Py_TrueStruct (*py3__Py_TrueStruct)
Ken Takata119fdd92023-10-04 20:05:05 +0200255# ifndef USE_LIMITED_API
256# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
257# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200258# define PyModule_AddObject py3_PyModule_AddObject
259# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200260# define PyImport_AddModule py3_PyImport_AddModule
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200261# ifdef USE_LIMITED_API
262# if Py_LIMITED_API >= 0x030a0000
263# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
264# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200265# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200266# if PY_VERSION_HEX >= 0x030300f0
267# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
268# else
269# define _PyUnicode_AsString py3__PyUnicode_AsString
270# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200271# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200272# undef PyUnicode_CompareWithASCIIString
273# define PyUnicode_CompareWithASCIIString py3_PyUnicode_CompareWithASCIIString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200274# undef PyUnicode_AsEncodedString
275# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200276# undef PyUnicode_AsUTF8String
277# define PyUnicode_AsUTF8String py3_PyUnicode_AsUTF8String
Bram Moolenaar19e60942011-06-19 00:27:51 +0200278# undef PyBytes_AsString
279# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200280# ifndef PyBytes_AsStringAndSize
281# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
282# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200283# undef PyBytes_FromString
284# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100285# undef PyBytes_FromStringAndSize
286# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200287# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200288# define _Py_Dealloc py3__Py_Dealloc
289# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200290# define PyFloat_FromDouble py3_PyFloat_FromDouble
291# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200292# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200293# define PyType_Type (*py3_PyType_Type)
Ken Takata119fdd92023-10-04 20:05:05 +0200294# ifndef USE_LIMITED_API
295# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
296# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200298# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200299# define PyNumber_Check (*py3_PyNumber_Check)
300# define PyNumber_Long (*py3_PyNumber_Long)
Ken Takatafa145f22023-10-06 19:27:13 +0200301# define PyBool_Type (*py3_PyBool_Type)
302# if PY_VERSION_HEX >= 0x030c00b0
303# define PyLong_Type (*py3_PyLong_Type)
304# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200305# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200306# ifdef Py_DEBUG
307# define _Py_NegativeRefcount py3__Py_NegativeRefcount
308# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200309# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
310# else
311# define PyModule_Create2 py3_PyModule_Create2
312# endif
313# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200314# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
315# define _PyObject_DebugFree py3__PyObject_DebugFree
316# else
317# define PyObject_Malloc py3_PyObject_Malloc
318# define PyObject_Free py3_PyObject_Free
319# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200320# define _PyObject_GC_New py3__PyObject_GC_New
321# define PyObject_GC_Del py3_PyObject_GC_Del
322# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200323# define PyType_GenericAlloc py3_PyType_GenericAlloc
324# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200325# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200326# define PyUnicode_FromString py3_PyUnicode_FromString
Ken Takatac97b3fe2023-10-11 21:27:06 +0200327# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200328# ifdef Py_UNICODE_WIDE
329# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
330# else
331# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
332# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200333# else
334# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200335# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200336# undef PyUnicode_Decode
337# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200338# define PyType_IsSubtype py3_PyType_IsSubtype
339# define PyCapsule_New py3_PyCapsule_New
340# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200341# ifdef USE_LIMITED_API
342# define PyType_GetSlot py3_PyType_GetSlot
343# define PyType_FromSpec py3_PyType_FromSpec
344# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200345
Bram Moolenaar0014a532013-05-29 21:33:39 +0200346# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200347# undef PyObject_NEW
348# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200349( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200350 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200351# elif PY_VERSION_HEX >= 0x030900b0
352# undef PyObject_NEW
353# define PyObject_NEW(type, typeobj) \
354 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200355# endif
356
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200357/*
358 * Pointers for dynamic link
359 */
360static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100361static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200362static void (*py3_Py_Initialize)(void);
363static PyObject* (*py3_PyList_New)(Py_ssize_t size);
364static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
365static void (*py3_PyGILState_Release)(PyGILState_STATE);
366static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200367static PyObject* (*py3_PySys_GetObject)(char *);
368static int (*py3_PyList_Append)(PyObject *, PyObject *);
369static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200371static int (*py3_PySequence_Check)(PyObject *);
372static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
373static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200374static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200375static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
376static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
377static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200378static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200379# if PY_VERSION_HEX >= 0x030601f0
380static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
381 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
382static int (*py3_PySlice_Unpack)(PyObject *slice,
383 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
384# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200385static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100386 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
387 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200388static PyObject* (*py3_PyErr_NoMemory)(void);
389static void (*py3_Py_Finalize)(void);
390static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200391static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200392static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Ken Takatac97b3fe2023-10-11 21:27:06 +0200393# ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200394static PyObject* (*py3_Py_CompileString)(const char *, const char *, int);
395static PyObject* (*py3_PyEval_EvalCode)(PyObject *co, PyObject *globals, PyObject *locals);
Ken Takatac97b3fe2023-10-11 21:27:06 +0200396# else
397static int (*py3_PyRun_SimpleString)(char *);
398static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200399# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200400static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200401static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100402static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200403static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200404static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200405static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200406static PyObject* (*py3_PyEval_GetGlobals)(void);
407static PyObject* (*py3_PyEval_GetLocals)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
409static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200410static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200411static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200412static PyObject* (*py3_PyErr_Occurred)(void);
413static PyObject* (*py3_PyModule_GetDict)(PyObject *);
414static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
415static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200416static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200417static PyObject* (*py3_PyLong_FromLong)(long);
418static PyObject* (*py3_PyDict_New)(void);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200419# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
420 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200421static int (*py3_PyIter_Check)(PyObject *o);
422# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200423static PyObject* (*py3_PyIter_Next)(PyObject *);
424static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200425static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200426static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200427static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200428static PyObject* (*py3_Py_BuildValue)(char *, ...);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200429# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200430static int (*py3_PyType_GetFlags)(PyTypeObject *o);
431# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200432static int (*py3_PyType_Ready)(PyTypeObject *type);
433static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
434static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Ken Takatac97b3fe2023-10-11 21:27:06 +0200435# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200436# ifdef Py_UNICODE_WIDE
437static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
438# else
439static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
440# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200441# else
442static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200443# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200444static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
445 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200446static long (*py3_PyLong_AsLong)(PyObject *);
447static void (*py3_PyErr_SetNone)(PyObject *);
448static void (*py3_PyEval_InitThreads)(void);
449static void(*py3_PyEval_RestoreThread)(PyThreadState *);
450static PyThreadState*(*py3_PyEval_SaveThread)(void);
451static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
452static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Yee Cheng Chind606fcc2023-09-20 19:59:47 +0200453static void (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200454static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200455static int (*py3_Py_IsInitialized)(void);
456static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200457static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100458static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200459static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Ken Takata119fdd92023-10-04 20:05:05 +0200460# ifndef USE_LIMITED_API
Bram Moolenaardb913952012-06-29 12:54:53 +0200461static iternextfunc py3__PyObject_NextNotImplemented;
Ken Takata119fdd92023-10-04 20:05:05 +0200462# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200464static PyObject* py3__Py_FalseStruct;
465static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200466static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
467static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200468# ifdef USE_LIMITED_API
469# if Py_LIMITED_API >= 0x030a0000
470static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
471# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200472# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200473# if PY_VERSION_HEX >= 0x030300f0
474static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
475# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200476static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200477# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200478# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200479static int (*py3_PyUnicode_CompareWithASCIIString)(PyObject *unicode, const char* string);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200480static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200481static PyObject* (*py3_PyUnicode_AsUTF8String)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200482static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200483static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200484static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100485static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200486# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200487static void (*py3__Py_Dealloc)(PyObject *obj);
488# endif
489# if PY_VERSION_HEX >= 0x030900b0
490static PyObject* (*py3__PyObject_New)(PyTypeObject *);
491# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200492static PyObject* (*py3_PyFloat_FromDouble)(double num);
493static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200495static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
496static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200497static PyTypeObject* py3_PyType_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200498# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200499static PyTypeObject* py3_PyStdPrinter_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200500# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200501static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200502static PyTypeObject* py3_PyFloat_Type;
Ken Takatafa145f22023-10-06 19:27:13 +0200503static PyTypeObject* py3_PyBool_Type;
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200504# if PY_VERSION_HEX >= 0x030c00b0
Ken Takatafa145f22023-10-06 19:27:13 +0200505static PyTypeObject* py3_PyLong_Type;
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200506# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200507static int (*py3_PyNumber_Check)(PyObject *);
508static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200509static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200510static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
511static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200512# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200513static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
514static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200515static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200516# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200517static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
518# endif
519# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
520static void (*py3__PyObject_DebugFree)(void*);
521static void* (*py3__PyObject_DebugMalloc)(size_t);
522# else
523static void (*py3_PyObject_Free)(void*);
524static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200525# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200526static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
527static void(*py3_PyObject_GC_Del)(void *);
528static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200529static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200530# ifdef USE_LIMITED_API
531static void* (*py3_PyType_GetSlot)(PyTypeObject *, int);
532static PyObject* (*py3_PyType_FromSpec)(PyType_Spec *);
533# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200534
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100535// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200536static PyObject *p3imp_PyExc_AttributeError;
537static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200538static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200539static PyObject *p3imp_PyExc_KeyboardInterrupt;
540static PyObject *p3imp_PyExc_TypeError;
541static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200542static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200543static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200544static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200545static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200546
547# define PyExc_AttributeError p3imp_PyExc_AttributeError
548# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200549# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200550# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
551# define PyExc_TypeError p3imp_PyExc_TypeError
552# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200553# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200554# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200555# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200556# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200557
558/*
559 * Table of name to function pointer of python.
560 */
561# define PYTHON_PROC FARPROC
562static struct
563{
564 char *name;
565 PYTHON_PROC *ptr;
566} py3_funcname_table[] =
567{
568 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100569 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200570 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200571 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
572 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200573 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200574 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200575 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
576 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
577 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
578 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200579 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200580 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200581 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200583 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
584 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
585 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200586 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200587 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
588 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200589# if PY_VERSION_HEX >= 0x030601f0
590 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
591 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
592# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200593 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
594 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
595 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
596 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200597 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200598 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Ken Takatac97b3fe2023-10-11 21:27:06 +0200599# ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200600 {"Py_CompileString", (PYTHON_PROC*)&py3_Py_CompileString},
601 {"PyEval_EvalCode", (PYTHON_PROC*)&PyEval_EvalCode},
Ken Takatac97b3fe2023-10-11 21:27:06 +0200602# else
603 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
604 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200605# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200606 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200607 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200608 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
609 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200610 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200611 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200612 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
613 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200614 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
615 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200616 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200617 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200618 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
619 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
620 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
621 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200622 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
623 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200624 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200625# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
626 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200627 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
628# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200629 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
630 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200631 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200632 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200633 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200634 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
635 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200636# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200637 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
638# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200639 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
640 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
641 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
642 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
643 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
644 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
645 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200646 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200647 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Ken Takata119fdd92023-10-04 20:05:05 +0200648# ifndef USE_LIMITED_API
Bram Moolenaardb913952012-06-29 12:54:53 +0200649 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Ken Takata119fdd92023-10-04 20:05:05 +0200650# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200651 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200652 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
653 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200654 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200655 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100656 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200657 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
658 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
659 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200660# ifdef USE_LIMITED_API
661# if Py_LIMITED_API >= 0x030a0000
662 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
663# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200664# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200665# if PY_VERSION_HEX >= 0x030300f0
666 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
667# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200668 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200669# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200670# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200671 {"PyUnicode_CompareWithASCIIString", (PYTHON_PROC*)&py3_PyUnicode_CompareWithASCIIString},
672 {"PyUnicode_AsUTF8String", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8String},
Ken Takatac97b3fe2023-10-11 21:27:06 +0200673# ifdef Py_UNICODE_USE_UCS_FUNCTIONS
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200674# ifdef Py_UNICODE_WIDE
675 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
676# else
677 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
678# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +0200679# else
680 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200681# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200682 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200683 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200684 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100685 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200686# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200687 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
688# endif
689# if PY_VERSION_HEX >= 0x030900b0
690 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
691# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200692 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
693 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200694 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200695 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
696 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200697 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200698# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200699 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200700# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200701 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200702 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200703 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Ken Takatafa145f22023-10-06 19:27:13 +0200704# if PY_VERSION_HEX >= 0x030c00b0
705 {"PyLong_Type", (PYTHON_PROC*)&py3_PyLong_Type},
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200706# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200707 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
708 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200709 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200710# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200711 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
712 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200713 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
714# else
715 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
716# endif
717# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200718 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
719 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200720# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200721 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
722 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200723# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200724 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
725 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
726 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200727 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
728 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
729 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200730# ifdef USE_LIMITED_API
731# if PY_VERSION_HEX >= 0x03040000
732 {"PyType_GetSlot", (PYTHON_PROC*)&py3_PyType_GetSlot},
733# endif
734 {"PyType_FromSpec", (PYTHON_PROC*)&py3_PyType_FromSpec},
735# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200736 {"", NULL},
737};
738
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200739# if PY_VERSION_HEX >= 0x030800f0
740 static inline void
741py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
742{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200743 if (--op->ob_refcnt != 0)
744 {
745# ifdef Py_REF_DEBUG
746 if (op->ob_refcnt < 0)
747 {
748 _Py_NegativeRefcount(filename, lineno, op);
749 }
750# endif
751 }
752 else
753 {
754 _Py_Dealloc(op);
755 }
756}
757
758# undef Py_DECREF
759# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
760
761 static inline void
762py3__Py_XDECREF(PyObject *op)
763{
764 if (op != NULL)
765 {
766 Py_DECREF(op);
767 }
768}
769
770# undef Py_XDECREF
771# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
772# endif
773
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200774# if PY_VERSION_HEX >= 0x030900b0
775 static inline int
776py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
777{
778 return ((PyType_GetFlags(type) & feature) != 0);
779}
780# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
781# endif
782
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200783# if PY_VERSION_HEX >= 0x030a00b2
784 static inline int
785py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
786{
787 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
788}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100789# if PY_VERSION_HEX >= 0x030b00b3
790# undef PyObject_TypeCheck
791# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
792# else
793# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
794# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200795# endif
796
Ken Takatafa145f22023-10-06 19:27:13 +0200797# if !defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x030c00b0
798// Py_SIZE() uses PyLong_Type and PyBool_Type starting from Python 3.12.
799// We need to define our own Py_SIZE() to replace Py{Bool,Long}_Type with
800// py3_Py{Bool,Long}_Type.
801// We also need to redefine PyTuple_GET_SIZE() and PyList_GET_SIZE(), because
802// they use Py_SIZE().
803 static inline Py_ssize_t
804py3_Py_SIZE(PyObject *ob)
805{
806 assert(ob->ob_type != &PyLong_Type);
807 assert(ob->ob_type != &PyBool_Type);
808 PyVarObject *var_ob = _PyVarObject_CAST(ob);
809 return var_ob->ob_size;
810}
811# undef Py_SIZE
812# define Py_SIZE(ob) py3_Py_SIZE(_PyObject_CAST(ob))
813
814 static inline Py_ssize_t
815py3_PyTuple_GET_SIZE(PyObject *op)
816{
817 PyTupleObject *tuple = _PyTuple_CAST(op);
818 return Py_SIZE(tuple);
819}
820# undef PyTuple_GET_SIZE
821# define PyTuple_GET_SIZE(op) py3_PyTuple_GET_SIZE(_PyObject_CAST(op))
822
823 static inline
824Py_ssize_t py3_PyList_GET_SIZE(PyObject *op)
825{
826 PyListObject *list = _PyList_CAST(op);
827 return Py_SIZE(list);
828}
829# undef PyList_GET_SIZE
830# define PyList_GET_SIZE(op) py3_PyList_GET_SIZE(_PyObject_CAST(op))
831# endif
832
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100833# ifdef MSWIN
834/*
835 * Look up the library "libname" using the InstallPath registry key.
836 * Return NULL when failed. Return an allocated string when successful.
837 */
Ken Takataae3cfa42023-10-14 11:49:09 +0200838 static WCHAR *
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100839py3_get_system_libname(const char *libname)
840{
Ken Takataae3cfa42023-10-14 11:49:09 +0200841 const WCHAR *pythoncore = L"Software\\Python\\PythonCore";
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100842 const char *cp = libname;
Ken Takataae3cfa42023-10-14 11:49:09 +0200843 WCHAR subkey[128];
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100844 HKEY hKey;
Ken Takataae3cfa42023-10-14 11:49:09 +0200845 int i;
846 DWORD j, len;
847 LSTATUS ret;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100848
849 while (*cp != '\0')
850 {
851 if (*cp == ':' || *cp == '\\' || *cp == '/')
852 {
853 // Bail out if "libname" contains path separator, assume it is
854 // an absolute path.
855 return NULL;
856 }
857 ++cp;
858 }
Ken Takataae3cfa42023-10-14 11:49:09 +0200859
860 WCHAR keyfound[32];
861 HKEY hKeyTop[] = {HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
862 HKEY hKeyFound = NULL;
863# ifdef USE_LIMITED_API
864 long maxminor = -1;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100865# endif
Ken Takataae3cfa42023-10-14 11:49:09 +0200866 for (i = 0; i < ARRAY_LENGTH(hKeyTop); i++)
867 {
868 long major, minor;
869
870 ret = RegOpenKeyExW(hKeyTop[i], pythoncore, 0, KEY_READ, &hKey);
871 if (ret != ERROR_SUCCESS)
872 continue;
873 for (j = 0;; j++)
874 {
875 WCHAR keyname[32];
876 WCHAR *wp;
877
878 len = ARRAY_LENGTH(keyname);
879 ret = RegEnumKeyExW(hKey, j, keyname, &len,
880 NULL, NULL, NULL, NULL);
881 if (ret == ERROR_NO_MORE_ITEMS)
882 break;
883
884 major = wcstol(keyname, &wp, 10);
885 if (*wp == L'.')
886 minor = wcstol(wp + 1, &wp, 10);
887# ifdef _WIN64
888 if (*wp != L'\0')
889 continue;
890# else
891 if (wcscmp(wp, L"-32") != 0)
892 continue;
893# endif
894
895 if (major != PY_MAJOR_VERSION)
896 continue;
897# ifdef USE_LIMITED_API
898 // Search the latest version.
899 if ((minor > maxminor)
900 && (minor >= ((Py_LIMITED_API >> 16) & 0xff)))
901 {
902 maxminor = minor;
903 wcscpy(keyfound, keyname);
904 hKeyFound = hKeyTop[i];
905 }
906# else
907 // Check if it matches with the compiled version.
908 if (minor == PY_MINOR_VERSION)
909 {
910 wcscpy(keyfound, keyname);
911 hKeyFound = hKeyTop[i];
912 break;
913 }
914# endif
915 }
916 RegCloseKey(hKey);
917# ifdef USE_LIMITED_API
918 if (hKeyFound != NULL)
919 break;
920# endif
921 }
922 if (hKeyFound == NULL)
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100923 return NULL;
Ken Takataae3cfa42023-10-14 11:49:09 +0200924
925 swprintf(subkey, ARRAY_LENGTH(subkey), L"%ls\\%ls\\InstallPath",
926 pythoncore, keyfound);
927 ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ,
928 NULL, NULL, &len);
929 if (ret != ERROR_MORE_DATA && ret != ERROR_SUCCESS)
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100930 return NULL;
Ken Takataae3cfa42023-10-14 11:49:09 +0200931 size_t len2 = len / sizeof(WCHAR) + 1 + strlen(libname);
932 WCHAR *path = alloc(len2 * sizeof(WCHAR));
933 if (path == NULL)
934 return NULL;
935 ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ,
936 NULL, path, &len);
937 if (ret != ERROR_SUCCESS)
938 {
939 vim_free(path);
940 return NULL;
941 }
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100942 // Remove trailing path separators.
Ken Takataae3cfa42023-10-14 11:49:09 +0200943 size_t len3 = wcslen(path);
944 if ((len3 > 0) && (path[len3 - 1] == L'/' || path[len3 - 1] == L'\\'))
945 --len3;
946 swprintf(path + len3, len2 - len3, L"\\%hs", libname);
947 return path;
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100948}
949# endif
950
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200951/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200952 * Load library and get all pointers.
953 * Parameter 'libname' provides name of DLL.
954 * Return OK or FAIL.
955 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200956 static int
957py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200958{
959 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200960 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
961 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
962 PYTHON_PROC *ucs_as_encoded_string =
963 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200964
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100965# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100966 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000967 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100968 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200969 if (python_loaded())
970 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200971 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000972 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200973 return FAIL;
974 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200975# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200976
977 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200978 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979 hinstPy3 = load_dll(libname);
980
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100981# ifdef MSWIN
982 if (!hinstPy3)
983 {
984 // Attempt to use the path from InstallPath as stored in the registry.
Ken Takataae3cfa42023-10-14 11:49:09 +0200985 WCHAR *syslibname = py3_get_system_libname(libname);
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100986
987 if (syslibname != NULL)
988 {
Ken Takataae3cfa42023-10-14 11:49:09 +0200989 hinstPy3 = LoadLibraryExW(syslibname, NULL,
990 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
991 LOAD_LIBRARY_SEARCH_SYSTEM32);
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100992 vim_free(syslibname);
993 }
994 }
995# endif
996
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997 if (!hinstPy3)
998 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200999 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001000 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001001 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002 }
1003
1004 for (i = 0; py3_funcname_table[i].ptr; ++i)
1005 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001006 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
1007 py3_funcname_table[i].name)) == NULL)
1008 {
1009 close_dll(hinstPy3);
1010 hinstPy3 = 0;
1011 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001012 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001013 return FAIL;
1014 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001015 }
1016
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001017 // Load unicode functions separately as only the ucs2 or the ucs4 functions
1018 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001019# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001020 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
1021 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
1022 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001023 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001024# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001025 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
1026 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001027 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001028 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001029 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001030 if (*ucs_from_string == NULL || *ucs_decode == NULL
1031 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001032 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001033 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001034 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001035 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001036 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001037 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001038 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001039 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +02001040# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +02001041 if (*ucs_from_string == NULL || *ucs_decode == NULL
1042 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001043 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001044 close_dll(hinstPy3);
1045 hinstPy3 = 0;
1046 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001047 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001048 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049 }
1050
1051 return OK;
1052}
1053
1054/*
1055 * If python is enabled (there is installed python on Windows system) return
1056 * TRUE, else FALSE.
1057 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001058 int
1059python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001060{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +01001061 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001062}
1063
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001064/*
1065 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001066 * DLL, as this can cause errors (importing data symbols is not reliable).
1067 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001068 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001069get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001070{
1071 PyObject *exmod = PyImport_ImportModule("builtins");
1072 PyObject *exdict = PyModule_GetDict(exmod);
1073 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
1074 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +02001075 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001076 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
1077 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
1078 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +02001079 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +02001080 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001081 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +02001082 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001083 Py_XINCREF(p3imp_PyExc_AttributeError);
1084 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +02001085 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001086 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
1087 Py_XINCREF(p3imp_PyExc_TypeError);
1088 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +02001089 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +02001090 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001091 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02001092 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001093 Py_XDECREF(exmod);
1094}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001095#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001096
Bram Moolenaardb913952012-06-29 12:54:53 +02001097static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02001098#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001099static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02001100
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001101#ifdef USE_LIMITED_API
1102# define DESTRUCTOR_FINISH(self) \
1103 ((freefunc)PyType_GetSlot(Py_TYPE(self), Py_tp_free))((PyObject*)self)
1104#else
1105# define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
1106#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02001107
Bram Moolenaar971db462013-05-12 18:44:48 +02001108#define WIN_PYTHON_REF(win) win->w_python3_ref
1109#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001110#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +02001111
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001112 static void
1113call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001114{
Bram Moolenaar0014a532013-05-29 21:33:39 +02001115#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001116 _PyObject_DebugFree(p);
1117#else
1118 PyObject_Free(p);
1119#endif
1120}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001121
1122 static PyObject *
1123call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124{
1125 return PyType_GenericNew(type,args,kwds);
1126}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001127
1128 static PyObject *
1129call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001130{
1131 return PyType_GenericAlloc(type,nitems);
1132}
1133
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001134static PyObject *OutputGetattro(PyObject *, PyObject *);
1135static int OutputSetattro(PyObject *, PyObject *, PyObject *);
1136static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001137static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001138static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001139static PyObject *WindowGetattro(PyObject *, PyObject *);
1140static int WindowSetattro(PyObject *, PyObject *, PyObject *);
1141static PyObject *RangeGetattro(PyObject *, PyObject *);
1142static PyObject *CurrentGetattro(PyObject *, PyObject *);
1143static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
1144static PyObject *DictionaryGetattro(PyObject *, PyObject *);
1145static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
1146static PyObject *ListGetattro(PyObject *, PyObject *);
1147static int ListSetattro(PyObject *, PyObject *, PyObject *);
1148static PyObject *FunctionGetattro(PyObject *, PyObject *);
1149
1150static struct PyModuleDef vimmodule;
1151
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02001152#define PY_CAN_RECURSE
1153
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001154/*
1155 * Include the code shared with if_python.c
1156 */
1157#include "if_py_both.h"
1158
Ken Takatac97b3fe2023-10-11 21:27:06 +02001159#ifdef USE_LIMITED_API
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001160# if Py_LIMITED_API >= 0x030A0000
1161# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1162# else
1163// Python limited API before 3.10 lack easy ways to query the raw UTF-8 chars.
1164// We need to first convert the string to bytes, and then extract the chars.
1165// This function is only used for attribute string comparisons, which have
1166// known short length. As such, just allocate a short static buffer to hold
1167// the characters instead of having to allocate/deallcoate it.
1168//
1169// An alternative would be to convert all attribute string comparisons to use
1170// PyUnicode_CompareWithASCIIString to skip having to extract the chars.
1171static char py3_unicode_utf8_chars[20];
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02001172static char* PY_UNICODE_GET_UTF8_CHARS(PyObject* str)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001173{
1174 py3_unicode_utf8_chars[0] = '\0';
1175 PyObject* bytes = PyUnicode_AsUTF8String(str);
1176 if (bytes)
1177 {
1178 char *chars;
1179 Py_ssize_t len;
1180 if (PyBytes_AsStringAndSize(bytes, &chars, &len) != -1)
1181 {
1182 if (len < (Py_ssize_t)sizeof(py3_unicode_utf8_chars))
1183 // PyBytes_AsStringAndSize guarantees null-termination
1184 memcpy(py3_unicode_utf8_chars, chars, len + 1);
1185 }
1186 Py_DECREF(bytes);
1187 }
1188 return py3_unicode_utf8_chars;
1189}
1190# endif
Ken Takatac97b3fe2023-10-11 21:27:06 +02001191#else // !USE_LIMITED_API
1192# if PY_VERSION_HEX >= 0x030300f0
1193# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1194# else
1195# define PY_UNICODE_GET_UTF8_CHARS _PyUnicode_AsString
1196# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001197#endif
1198
Bram Moolenaar828bff12019-03-19 22:11:41 +01001199// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001200#define GET_ATTR_STRING(name, nameobj) \
1201 char *name = ""; \
1202 if (PyUnicode_Check(nameobj)) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001203 name = (char *)PY_UNICODE_GET_UTF8_CHARS(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001204
1205#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
1206
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001207///////////////////////////////////////////////////////
1208// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001209
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001210static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001212///////////////////////////////////////////////////////
1213// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001214
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001215 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001216python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001217{
1218 static int recurse = 0;
1219
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001220 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001221 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001222 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001223
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001224 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001225 ++recurse;
1226
1227#ifdef DYNAMIC_PYTHON3
1228 if (hinstPy3)
1229#endif
1230 if (Py_IsInitialized())
1231 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001232#ifdef USE_LIMITED_API
1233 shutdown_types();
1234#endif
1235
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001236 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001237 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001238
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001240 }
1241
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001242 --recurse;
1243}
1244
Bram Moolenaar094454f2015-10-07 10:39:55 +02001245#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001246 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001247python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001248{
1249 return (hinstPy3 != 0);
1250}
1251#endif
1252
Bram Moolenaar94073162018-01-31 21:49:05 +01001253static wchar_t *py_home_buf = NULL;
1254
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001255#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001256/*
1257 * Return TRUE if stdin is readable from Python 3.
1258 */
1259 static BOOL
1260is_stdin_readable(void)
1261{
1262 DWORD mode, eventnum;
1263 struct _stat st;
1264 int fd = fileno(stdin);
1265 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1266
1267 // Check if stdin is connected to the console.
1268 if (GetConsoleMode(hstdin, &mode))
1269 // Check if it is opened as input.
1270 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1271
1272 return _fstat(fd, &st) == 0;
1273}
1274
1275// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1276// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001277// Note that the python DLL is linked to its own stdio DLL which can be
1278// differ from Vim's stdio.
1279 static void
1280reset_stdin(void)
1281{
1282 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1283 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Ken Takata119fdd92023-10-04 20:05:05 +02001284 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001285
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001286 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001287 return;
1288
1289 // Get "freopen" and "stdin" which are used in the python DLL.
1290 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1291 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1292 if (py__acrt_iob_func)
1293 {
1294 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001295 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001296 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001297 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001298 }
1299
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001300 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001301 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001302 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001303 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001304 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001305}
1306#else
1307# define reset_stdin()
1308#endif
1309
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001310// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1311// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1312// Install a hook to python dll's exit() and recover from it.
1313#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1314# define HOOK_EXIT
1315# include <setjmp.h>
1316
1317static jmp_buf exit_hook_jump_buf;
1318static void *orig_exit = NULL;
1319
1320/*
1321 * Function that replaces exit() while calling Py_Initialize().
1322 */
1323 static void
1324hooked_exit(int ret)
1325{
1326 // Recover from exit.
1327 longjmp(exit_hook_jump_buf, 1);
1328}
1329
1330/*
1331 * Install a hook to python dll's exit().
1332 */
1333 static void
1334hook_py_exit(void)
1335{
Ken Takata119fdd92023-10-04 20:05:05 +02001336 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001337
1338 if (hinst == NULL || orig_exit != NULL)
1339 return;
1340
1341 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1342}
1343
1344/*
1345 * Remove the hook installed by hook_py_exit().
1346 */
1347 static void
1348restore_py_exit(void)
1349{
1350 HINSTANCE hinst = hinstPy3;
1351
1352 if (hinst == NULL)
1353 return;
1354
1355 if (orig_exit != NULL)
1356 hook_dll_import_func(hinst, "exit", orig_exit);
1357 orig_exit = NULL;
1358}
1359#endif
1360
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001361 static int
1362Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001363{
1364 if (!py3initialised)
1365 {
1366#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 if (!python3_enabled(TRUE))
1368 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001369 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 goto fail;
1371 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001372#endif
1373
1374 init_structs();
1375
Bram Moolenaar94073162018-01-31 21:49:05 +01001376 if (*p_py3home != NUL)
1377 {
1378 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001379
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001380 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001381 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001382 if (py_home_buf != NULL && mbstowcs(
1383 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1384 Py_SetPythonHome(py_home_buf);
1385 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001386#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001387 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001388 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001389#endif
1390
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001391 PyImport_AppendInittab("vim", Py3Init_vim);
1392
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001393#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1394 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1395#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001396 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001397
1398#ifdef HOOK_EXIT
1399 // Catch exit() called in Py_Initialize().
1400 hook_py_exit();
1401 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001402 {
1403 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001404 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001405 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001406 else
1407 {
1408 // exit() was called in Py_Initialize().
1409 restore_py_exit();
1410 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1411 goto fail;
1412 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001413#else
1414 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001415#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001416
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001417#if PY_VERSION_HEX < 0x03090000
1418 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001419 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001420#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001421#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001423#endif
1424
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001425 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427
Bram Moolenaardb913952012-06-29 12:54:53 +02001428 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1429
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001430 // Remove the element from sys.path that was added because of our
1431 // argv[0] value in Py3Init_vim(). Previously we used an empty
1432 // string, but depending on the OS we then get an empty entry or
1433 // the current directory in sys.path.
1434 // Only after vim has been imported, the element does exist in
1435 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001436 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 +02001437
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001438 // Without the call to PyEval_SaveThread, thread specific state (such
1439 // as the system trace hook), will be lost between invocations of
1440 // Python code.
1441 // GIL may have been created and acquired in PyEval_InitThreads() and
1442 // thread state is created in Py_Initialize(); there
1443 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1444 // have threads enabled!), so the following does both: unlock GIL and
1445 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001446 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001447
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001448 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001449 }
1450
1451 return 0;
1452
1453fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001454 // We call PythonIO_Flush() here to print any Python errors.
1455 // This is OK, as it is possible to call this function even
1456 // if PythonIO_Init_io() has not completed successfully (it will
1457 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458 PythonIO_Flush();
1459 return -1;
1460}
1461
1462/*
1463 * External interface
1464 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001465 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001466DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001467{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001468#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001469 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001470#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001471 PyObject *cmdstr;
1472 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001473 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001474
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001475 if (python_end_called)
1476 goto theend;
1477
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001478 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001479 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001481 init_range(arg);
1482
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001483 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484
1485#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001486 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001487 saved_locale = setlocale(LC_NUMERIC, NULL);
1488 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001489 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001490 else
1491 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001493 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1494 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001495 }
1496#endif
1497
1498 pygilstate = PyGILState_Ensure();
1499
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001500 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1501 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001502 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001503 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1504 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001505 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001506
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001507 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001508 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509
1510 PyGILState_Release(pygilstate);
1511
1512#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1513 if (saved_locale != NULL)
1514 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001515 (void)setlocale(LC_NUMERIC, saved_locale);
1516 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001517 }
1518#endif
1519
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001520 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001521 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001522
1523theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001524 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001525}
1526
1527/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001528 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001529 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001530 void
1531ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001532{
1533 char_u *script;
1534
1535 script = script_get(eap, eap->arg);
1536 if (!eap->skip)
1537 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001538 if (p_pyx == 0)
1539 p_pyx = 3;
1540
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001541 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001542 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001543 (runner) run_cmd,
1544 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001545 }
1546 vim_free(script);
1547}
1548
1549#define BUFFER_SIZE 2048
1550
1551/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001552 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001553 */
1554 void
1555ex_py3file(exarg_T *eap)
1556{
1557 static char buffer[BUFFER_SIZE];
1558 const char *file;
1559 char *p;
1560 int i;
1561
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001562 if (p_pyx == 0)
1563 p_pyx = 3;
1564
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001565 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1566 // stdio file pointer, but Vim and the Python DLL are compiled with
1567 // different options under Windows, meaning that stdio pointers aren't
1568 // compatible between the two. Yuk.
1569 //
1570 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1571 //
1572 // Using bytes so that Python can detect the source encoding as it normally
1573 // does. The doc does not say "compile" accept bytes, though.
1574 //
1575 // We need to escape any backslashes or single quotes in the file name, so that
1576 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001577
1578 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001579 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001580
1581 for (i=0; i<2; ++i)
1582 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001583 file = (char *)eap->arg;
1584 while (*file && p < buffer + (BUFFER_SIZE - 3))
1585 {
1586 if (*file == '\\' || *file == '\'')
1587 *p++ = '\\';
1588 *p++ = *file++;
1589 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001590 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001591 if (*file != '\0')
1592 return;
1593 if (i==0)
1594 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001595 strcpy(p,"','rb').read(),'");
1596 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001597 }
1598 else
1599 {
1600 strcpy(p,"','exec'))");
1601 p += 10;
1602 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001603 }
1604
1605
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001606 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001607 DoPyCommand(buffer,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001608 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001609 (runner) run_cmd,
1610 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001611}
1612
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001613 void
1614ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001615{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001616 if (p_pyx == 0)
1617 p_pyx = 3;
1618
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001619 DoPyCommand((char *)eap->arg,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001620 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001621 (runner)run_do,
1622 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001623}
1624
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001625///////////////////////////////////////////////////////
1626// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001627
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001628// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001629
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001630 static PyObject *
1631OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001632{
Bram Moolenaar77045652012-09-21 13:46:06 +02001633 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001634
1635 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001637 else if (strcmp(name, "errors") == 0)
1638 return PyString_FromString("strict");
1639 else if (strcmp(name, "encoding") == 0)
1640 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001641
1642 return PyObject_GenericGetAttr(self, nameobj);
1643}
1644
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001645 static int
1646OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001647{
Bram Moolenaar77045652012-09-21 13:46:06 +02001648 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001649
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001650 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001651}
1652
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001653///////////////////////////////////////////////////////
1654// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001655
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001656// Window type - Implementation functions
1657// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001658
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001659#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1660
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001661// Buffer type - Implementation functions
1662// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001663
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001664#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1665
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001666static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001667static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001668
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001669// Line range type - Implementation functions
1670// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001671
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001672#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1673
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001674static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001675static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1676static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001677
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001678// Current objects type - Implementation functions
1679// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001680
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001681static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001682 (lenfunc) BufferLength, // sq_length, len(x)
1683 (binaryfunc) 0, // sq_concat, x+y
1684 (ssizeargfunc) 0, // sq_repeat, x*n
1685 (ssizeargfunc) BufferItem, // sq_item, x[i]
1686 0, // was_sq_slice, x[i:j]
1687 0, // sq_ass_item, x[i]=v
1688 0, // sq_ass_slice, x[i:j]=v
1689 0, // sq_contains
1690 0, // sq_inplace_concat
1691 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001692};
1693
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001694static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001695 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001696 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001697 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001698};
1699
1700
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001701// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001702
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001703 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001704BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001705{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001706 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001707
Bram Moolenaar77045652012-09-21 13:46:06 +02001708 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001709
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001710 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1711 return r;
1712
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001713 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001714 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001715
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001716 r = BufferAttr((BufferObject *)(self), name);
1717 if (r || PyErr_Occurred())
1718 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001719 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001720 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001721}
1722
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001723 static int
1724BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1725{
1726 GET_ATTR_STRING(name, nameobj);
1727
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001728 return BufferSetattr(self, name, val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001729}
1730
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001731//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001732
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001733 static PyObject *
1734BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001735{
Bram Moolenaardb913952012-06-29 12:54:53 +02001736 if (PyLong_Check(idx))
1737 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001738 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001739 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001740 }
1741 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001742 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001743 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001744
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001745 if (CheckBuffer((BufferObject *) self))
1746 return NULL;
1747
Bram Moolenaar922a4662014-03-30 16:11:43 +02001748 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001749 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001750 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001751 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001752 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001753 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001754 }
1755 else
1756 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001757 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001758 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001759 }
1760}
1761
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001762 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001763BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1764{
Bram Moolenaardb913952012-06-29 12:54:53 +02001765 if (PyLong_Check(idx))
1766 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001767 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001768
1769 if (CheckBuffer((BufferObject *) self))
1770 return -1;
1771
Bram Moolenaar19e60942011-06-19 00:27:51 +02001772 return RBAsItem((BufferObject *)(self), n, val, 1,
1773 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1774 NULL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001775 }
1776 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001777 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001778 Py_ssize_t start, stop, step, slicelen;
1779
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001780 if (CheckBuffer((BufferObject *) self))
1781 return -1;
1782
Bram Moolenaar922a4662014-03-30 16:11:43 +02001783 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001784 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001785 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001787 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001788 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1789 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1790 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001791 }
1792 else
1793 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001794 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001795 return -1;
1796 }
1797}
1798
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001799static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001800 (lenfunc) RangeLength, // sq_length, len(x)
1801 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1802 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1803 (ssizeargfunc) RangeItem, // sq_item, x[i]
1804 0, // was_sq_slice, x[i:j]
1805 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1806 0, // sq_ass_slice, x[i:j]=v
1807 0, // sq_contains
1808 0, // sq_inplace_concat
1809 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001810};
1811
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001812static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001813 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001814 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001815 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001816};
1817
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001818// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001819
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001820 static PyObject *
1821RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001822{
Bram Moolenaar77045652012-09-21 13:46:06 +02001823 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001824
1825 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001826 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001827 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001828 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001829 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001830 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001831}
1832
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001833////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001834
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001835 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001836RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001837{
1838 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001839 ((RangeObject *)(self))->start,
1840 ((RangeObject *)(self))->end,
1841 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001842}
1843
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001844 static Py_ssize_t
1845RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1846{
1847 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1848 ((RangeObject *)(self))->start,
1849 ((RangeObject *)(self))->end,
1850 &((RangeObject *)(self))->end);
1851}
1852
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001853 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001854RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001855{
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 if (PyLong_Check(idx))
1857 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001858 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001859 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001860 }
1861 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001862 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001863 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001864
Bram Moolenaar922a4662014-03-30 16:11:43 +02001865 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001866 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1867 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001868 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001869 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001870 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001871 }
1872 else
1873 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001874 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001875 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001876 }
1877}
1878
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001879 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001880RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1881{
Bram Moolenaardb913952012-06-29 12:54:53 +02001882 if (PyLong_Check(idx))
1883 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001884 long n = PyLong_AsLong(idx);
1885 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001886 }
1887 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001888 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001889 Py_ssize_t start, stop, step, slicelen;
1890
Bram Moolenaar922a4662014-03-30 16:11:43 +02001891 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001892 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1893 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001894 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001895 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001896 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001897 }
1898 else
1899 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001900 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001901 return -1;
1902 }
1903}
1904
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001905// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001906
1907 static PyObject *
1908TabPageGetattro(PyObject *self, PyObject *nameobj)
1909{
1910 PyObject *r;
1911
1912 GET_ATTR_STRING(name, nameobj);
1913
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001914 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1915 return r;
1916
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001917 if (CheckTabPage((TabPageObject *)(self)))
1918 return NULL;
1919
1920 r = TabPageAttr((TabPageObject *)(self), name);
1921 if (r || PyErr_Occurred())
1922 return r;
1923 else
1924 return PyObject_GenericGetAttr(self, nameobj);
1925}
1926
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001927// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001928
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001929 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001930WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001931{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001932 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001933
Bram Moolenaar77045652012-09-21 13:46:06 +02001934 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001935
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001936 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1937 return r;
1938
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001939 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001940 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001941
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001942 r = WindowAttr((WindowObject *)(self), name);
1943 if (r || PyErr_Occurred())
1944 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001945 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001946 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001947}
1948
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001949 static int
1950WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001951{
Bram Moolenaar77045652012-09-21 13:46:06 +02001952 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001953
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001954 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001955}
1956
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001957// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001958
1959static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001960 (lenfunc) TabListLength, // sq_length, len(x)
1961 (binaryfunc) 0, // sq_concat, x+y
1962 (ssizeargfunc) 0, // sq_repeat, x*n
1963 (ssizeargfunc) TabListItem, // sq_item, x[i]
1964 0, // sq_slice, x[i:j]
1965 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1966 0, // sq_ass_slice, x[i:j]=v
1967 0, // sq_contains
1968 0, // sq_inplace_concat
1969 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001970};
1971
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001972// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001973
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001974static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001975 (lenfunc) WinListLength, // sq_length, len(x)
1976 (binaryfunc) 0, // sq_concat, x+y
1977 (ssizeargfunc) 0, // sq_repeat, x*n
1978 (ssizeargfunc) WinListItem, // sq_item, x[i]
1979 0, // sq_slice, x[i:j]
1980 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1981 0, // sq_ass_slice, x[i:j]=v
1982 0, // sq_contains
1983 0, // sq_inplace_concat
1984 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001985};
1986
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001987/*
1988 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001989 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001990 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001991CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001992{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001993 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001994 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001995 if (!(r = CurrentGetattr(self, name)))
1996 return PyObject_GenericGetAttr(self, nameobj);
1997 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001998}
1999
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002000 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002001CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002002{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002003 GET_ATTR_STRING(name, nameobj);
2004 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002005}
2006
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002007// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002008
Bram Moolenaar66b79852012-09-21 14:00:35 +02002009 static PyObject *
2010DictionaryGetattro(PyObject *self, PyObject *nameobj)
2011{
2012 DictionaryObject *this = ((DictionaryObject *) (self));
2013
2014 GET_ATTR_STRING(name, nameobj);
2015
2016 if (strcmp(name, "locked") == 0)
2017 return PyLong_FromLong(this->dict->dv_lock);
2018 else if (strcmp(name, "scope") == 0)
2019 return PyLong_FromLong(this->dict->dv_scope);
2020
2021 return PyObject_GenericGetAttr(self, nameobj);
2022}
2023
2024 static int
2025DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
2026{
2027 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002028 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02002029}
2030
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002031// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002032
Bram Moolenaar66b79852012-09-21 14:00:35 +02002033 static PyObject *
2034ListGetattro(PyObject *self, PyObject *nameobj)
2035{
2036 GET_ATTR_STRING(name, nameobj);
2037
2038 if (strcmp(name, "locked") == 0)
2039 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
2040
2041 return PyObject_GenericGetAttr(self, nameobj);
2042}
2043
2044 static int
2045ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
2046{
2047 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02002048 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02002049}
2050
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002051// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02002052
Bram Moolenaardb913952012-06-29 12:54:53 +02002053 static PyObject *
2054FunctionGetattro(PyObject *self, PyObject *nameobj)
2055{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002056 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02002057 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02002058
2059 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002060
Bram Moolenaar8110a092016-04-14 15:56:09 +02002061 r = FunctionAttr(this, name);
2062 if (r || PyErr_Occurred())
2063 return r;
2064 else
2065 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02002066}
2067
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002068// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002069
2070 void
2071python3_buffer_free(buf_T *buf)
2072{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002073 BufferObject *bp = BUF_PYTHON_REF(buf);
2074 if (bp == NULL)
2075 return;
2076 bp->buf = INVALID_BUFFER_VALUE;
2077 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002078}
2079
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002080 void
2081python3_window_free(win_T *win)
2082{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002083 WindowObject *wp = WIN_PYTHON_REF(win);
2084 if (wp == NULL)
2085 return;
2086 wp->win = INVALID_WINDOW_VALUE;
2087 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002088}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002089
2090 void
2091python3_tabpage_free(tabpage_T *tab)
2092{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002093 TabPageObject *tp = TAB_PYTHON_REF(tab);
2094 if (tp == NULL)
2095 return;
2096 tp->tab = INVALID_TABPAGE_VALUE;
2097 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002098}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002099
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01002100 static PyObject *
2101Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002102{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002103 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002104 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
2105
Bram Moolenaar1dc28782013-05-21 19:11:01 +02002106 if (init_types())
2107 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002108
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002109 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002110 PySys_SetArgv(1, argv);
2111
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002112 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002113 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002114
Bram Moolenaardee2e312013-06-23 16:35:47 +02002115 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002116 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002117
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002118 if (init_sys_path())
2119 return NULL;
2120
2121 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002122}
2123
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002124//////////////////////////////////////////////////////////////////////////
2125// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002126
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002127/*
2128 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002129 * All internal newlines are replaced by null characters.
2130 *
2131 * On errors, the Python exception data is set, and NULL is returned.
2132 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002133 static PyObject *
2134LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002135{
2136 PyObject *result;
2137 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01002138 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002139
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002140 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002141 p = tmp;
2142 if (p == NULL)
2143 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002144 PyErr_NoMemory();
2145 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002146 }
2147
2148 while (*str)
2149 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002150 if (*str == '\n')
2151 *p = '\0';
2152 else
2153 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002154
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002155 ++p;
2156 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002157 }
2158 *p = '\0';
2159
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01002160 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002161
2162 vim_free(tmp);
2163 return result;
2164}
2165
Bram Moolenaardb913952012-06-29 12:54:53 +02002166 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002167do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02002168{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002169 DoPyCommand((char *) str,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02002170 init_range_eval,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002171 (runner) run_eval,
2172 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002173 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02002174 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002175 rettv->v_type = VAR_NUMBER;
2176 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02002177 }
2178}
2179
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002180 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002181set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02002182{
Bram Moolenaarb641df42015-02-03 13:16:04 +01002183 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02002184}
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002185
2186 int
Dominique Pellé4927bc72023-09-24 16:12:07 +02002187python3_version(void)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002188{
2189#ifdef USE_LIMITED_API
2190 return Py_LIMITED_API;
2191#else
2192 return PY_VERSION_HEX;
2193#endif
2194}