blob: bbbebc39f151973715a85ea8f95ccf07b9bc4304 [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
Zdenek Dohnale5e47092023-08-13 19:37:09 +020071#define PyLong_Type (*py3_PyLong_Type)
72#define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar0bdda372013-06-10 18:36:24 +020073
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +020074#ifdef Py_LIMITED_API
75# define USE_LIMITED_API // Using Python 3 limited ABI
76#endif
77
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020078#include <Python.h>
Bram Moolenaar0bdda372013-06-10 18:36:24 +020079
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010080#undef main // Defined in python.h - aargh
81#undef HAVE_FCNTL_H // Clash with os_win32.h
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020082
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010083// The "surrogateescape" error handler is new in Python 3.1
Bram Moolenaar3d64a312011-07-15 15:54:44 +020084#if PY_VERSION_HEX >= 0x030100f0
85# define CODEC_ERROR_HANDLER "surrogateescape"
86#else
87# define CODEC_ERROR_HANDLER NULL
88#endif
89
Philip H5a5f17f2022-11-05 14:05:31 +000090// Suppress Python 3.11 depreciations to see useful warnings
Philip H422b9dc2023-08-11 22:38:48 +020091#ifdef __GNUC__
92# pragma GCC diagnostic push
93# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Philip H5a5f17f2022-11-05 14:05:31 +000094#endif
95
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010096// Python 3 does not support CObjects, always use Capsules
Bram Moolenaar2afa3232012-06-29 16:28:28 +020097#define PY_USE_CAPSULE
98
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +010099#define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER
100#define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG
101
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200102#define PyInt Py_ssize_t
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200103#ifndef PyString_Check
104# define PyString_Check(obj) PyUnicode_Check(obj)
105#endif
Bram Moolenaare0324612013-07-09 17:30:55 +0200106#define PyString_FromString(repr) \
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +0100107 PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200108#define PyString_FromFormat PyUnicode_FromFormat
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200109#ifndef PyInt_Check
110# define PyInt_Check(obj) PyLong_Check(obj)
111#endif
Bram Moolenaar77045652012-09-21 13:46:06 +0200112#define PyInt_FromLong(i) PyLong_FromLong(i)
113#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200114#define Py_ssize_t_fmt "n"
Bram Moolenaara9922d62013-05-30 13:01:18 +0200115#define Py_bytes_fmt "y"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200116
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100117#define PyIntArgFunc ssizeargfunc
118#define PyIntObjArgProc ssizeobjargproc
119
Bram Moolenaar922a4662014-03-30 16:11:43 +0200120/*
121 * PySlice_GetIndicesEx(): first argument type changed from PySliceObject
122 * to PyObject in Python 3.2 or later.
123 */
124#if PY_VERSION_HEX >= 0x030200f0
125typedef PyObject PySliceObject_T;
126#else
127typedef PySliceObject PySliceObject_T;
128#endif
129
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000130#ifndef MSWIN
131# define HINSTANCE void *
132#endif
133#if defined(DYNAMIC_PYTHON3) || defined(MSWIN)
134static HINSTANCE hinstPy3 = 0; // Instance of python.dll
135#endif
136
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +0100137#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200138
Bram Moolenaar4f974752019-02-17 17:44:42 +0100139# ifndef MSWIN
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200140# include <dlfcn.h>
141# define FARPROC void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100142# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200143# define load_dll(n) dlopen((n), RTLD_LAZY)
144# else
145# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
146# endif
147# define close_dll dlclose
148# define symbol_from_dll dlsym
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200149# define load_dll_error dlerror
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200150# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200151# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200152# define close_dll FreeLibrary
153# define symbol_from_dll GetProcAddress
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200154# define load_dll_error GetWin32Error
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200155# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200156/*
157 * Wrapper defines
158 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200159# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200160# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200161# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200162# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200163# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200164# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200165# define PyDict_SetItemString py3_PyDict_SetItemString
166# define PyErr_BadArgument py3_PyErr_BadArgument
167# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaarc476e522013-06-23 13:46:40 +0200168# define PyErr_Format py3_PyErr_Format
Bram Moolenaar4d369872013-02-20 16:09:43 +0100169# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200170# define PyErr_NoMemory py3_PyErr_NoMemory
171# define PyErr_Occurred py3_PyErr_Occurred
172# define PyErr_SetNone py3_PyErr_SetNone
173# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200174# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200175# define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200176# define PyEval_InitThreads py3_PyEval_InitThreads
177# define PyEval_RestoreThread py3_PyEval_RestoreThread
178# define PyEval_SaveThread py3_PyEval_SaveThread
179# define PyGILState_Ensure py3_PyGILState_Ensure
180# define PyGILState_Release py3_PyGILState_Release
181# define PyLong_AsLong py3_PyLong_AsLong
182# define PyLong_FromLong py3_PyLong_FromLong
183# define PyList_GetItem py3_PyList_GetItem
184# define PyList_Append py3_PyList_Append
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200185# define PyList_Insert py3_PyList_Insert
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200186# define PyList_New py3_PyList_New
187# define PyList_SetItem py3_PyList_SetItem
188# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200189# define PySequence_Check py3_PySequence_Check
190# define PySequence_Size py3_PySequence_Size
191# define PySequence_GetItem py3_PySequence_GetItem
Bram Moolenaara9922d62013-05-30 13:01:18 +0200192# define PySequence_Fast py3_PySequence_Fast
Bram Moolenaardb913952012-06-29 12:54:53 +0200193# define PyTuple_Size py3_PyTuple_Size
194# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200195# if PY_VERSION_HEX >= 0x030601f0
196# define PySlice_AdjustIndices py3_PySlice_AdjustIndices
197# define PySlice_Unpack py3_PySlice_Unpack
198# endif
199# undef PySlice_GetIndicesEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200200# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
201# define PyImport_ImportModule py3_PyImport_ImportModule
202# define PyObject_Init py3__PyObject_Init
203# define PyDict_New py3_PyDict_New
204# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200205# define PyDict_Next py3_PyDict_Next
206# define PyMapping_Check py3_PyMapping_Check
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200207# ifndef PyMapping_Keys
208# define PyMapping_Keys py3_PyMapping_Keys
209# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200210# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
211 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
212# undef PyIter_Check
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200213# define PyIter_Check py3_PyIter_Check
214# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200215# define PyIter_Next py3_PyIter_Next
216# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200217# define PyObject_Repr py3_PyObject_Repr
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200218# define PyObject_GetItem py3_PyObject_GetItem
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200219# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200220# define PyModule_GetDict py3_PyModule_GetDict
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200221# ifndef USE_LIMITED_API
222# undef PyRun_SimpleString
223# define PyRun_SimpleString py3_PyRun_SimpleString
224# undef PyRun_String
225# define PyRun_String py3_PyRun_String
226# else
227# define Py_CompileString py3_Py_CompileString
228# define PyEval_EvalCode py3_PyEval_EvalCode
229# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200230# define PyObject_GetAttrString py3_PyObject_GetAttrString
Bram Moolenaara9922d62013-05-30 13:01:18 +0200231# define PyObject_HasAttrString py3_PyObject_HasAttrString
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200232# define PyObject_SetAttrString py3_PyObject_SetAttrString
233# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200234# define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT
Bram Moolenaarf4258302013-06-02 18:20:17 +0200235# define PyObject_Call py3_PyObject_Call
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200236# define PyEval_GetLocals py3_PyEval_GetLocals
237# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200238# define PySys_SetObject py3_PySys_SetObject
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200239# define PySys_GetObject py3_PySys_GetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200240# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200241# define PyType_Ready py3_PyType_Ready
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200242# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200243# define PyType_GetFlags py3_PyType_GetFlags
244# endif
Ken Takata119fdd92023-10-04 20:05:05 +0200245# undef Py_BuildValue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200246# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100247# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200248# define Py_Initialize py3_Py_Initialize
249# define Py_Finalize py3_Py_Finalize
250# define Py_IsInitialized py3_Py_IsInitialized
251# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200252# define _Py_FalseStruct (*py3__Py_FalseStruct)
253# define _Py_TrueStruct (*py3__Py_TrueStruct)
Ken Takata119fdd92023-10-04 20:05:05 +0200254# ifndef USE_LIMITED_API
255# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
256# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200257# define PyModule_AddObject py3_PyModule_AddObject
258# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200259# define PyImport_AddModule py3_PyImport_AddModule
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200260# ifdef USE_LIMITED_API
261# if Py_LIMITED_API >= 0x030a0000
262# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
263# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200264# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200265# if PY_VERSION_HEX >= 0x030300f0
266# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
267# else
268# define _PyUnicode_AsString py3__PyUnicode_AsString
269# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200270# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200271# undef PyUnicode_CompareWithASCIIString
272# define PyUnicode_CompareWithASCIIString py3_PyUnicode_CompareWithASCIIString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200273# undef PyUnicode_AsEncodedString
274# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200275# undef PyUnicode_AsUTF8String
276# define PyUnicode_AsUTF8String py3_PyUnicode_AsUTF8String
Bram Moolenaar19e60942011-06-19 00:27:51 +0200277# undef PyBytes_AsString
278# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200279# ifndef PyBytes_AsStringAndSize
280# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
281# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200282# undef PyBytes_FromString
283# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100284# undef PyBytes_FromStringAndSize
285# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200286# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200287# define _Py_Dealloc py3__Py_Dealloc
288# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200289# define PyFloat_FromDouble py3_PyFloat_FromDouble
290# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200291# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200292# define PyType_Type (*py3_PyType_Type)
Ken Takata119fdd92023-10-04 20:05:05 +0200293# ifndef USE_LIMITED_API
294# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
295# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200296# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200297# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200298# define PyNumber_Check (*py3_PyNumber_Check)
299# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200300# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200301# ifdef Py_DEBUG
302# define _Py_NegativeRefcount py3__Py_NegativeRefcount
303# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200304# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
305# else
306# define PyModule_Create2 py3_PyModule_Create2
307# endif
308# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200309# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
310# define _PyObject_DebugFree py3__PyObject_DebugFree
311# else
312# define PyObject_Malloc py3_PyObject_Malloc
313# define PyObject_Free py3_PyObject_Free
314# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200315# define _PyObject_GC_New py3__PyObject_GC_New
316# define PyObject_GC_Del py3_PyObject_GC_Del
317# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200318# define PyType_GenericAlloc py3_PyType_GenericAlloc
319# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200320# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200321# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200322# ifndef PyUnicode_FromFormat
323# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
324# else
325# define Py_UNICODE_USE_UCS_FUNCTIONS
326# ifdef Py_UNICODE_WIDE
327# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
328# else
329# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
330# endif
331# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200332# undef PyUnicode_Decode
333# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200334# define PyType_IsSubtype py3_PyType_IsSubtype
335# define PyCapsule_New py3_PyCapsule_New
336# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200337# ifdef USE_LIMITED_API
338# define PyType_GetSlot py3_PyType_GetSlot
339# define PyType_FromSpec py3_PyType_FromSpec
340# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200341
Bram Moolenaar0014a532013-05-29 21:33:39 +0200342# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200343# undef PyObject_NEW
344# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200345( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200346 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200347# elif PY_VERSION_HEX >= 0x030900b0
348# undef PyObject_NEW
349# define PyObject_NEW(type, typeobj) \
350 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200351# endif
352
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200353/*
354 * Pointers for dynamic link
355 */
356static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100357static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200358static void (*py3_Py_Initialize)(void);
359static PyObject* (*py3_PyList_New)(Py_ssize_t size);
360static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
361static void (*py3_PyGILState_Release)(PyGILState_STATE);
362static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200363static PyObject* (*py3_PySys_GetObject)(char *);
364static int (*py3_PyList_Append)(PyObject *, PyObject *);
365static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200366static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200367static int (*py3_PySequence_Check)(PyObject *);
368static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
369static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200370static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200371static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
372static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
373static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200374static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200375# if PY_VERSION_HEX >= 0x030601f0
376static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
377 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
378static int (*py3_PySlice_Unpack)(PyObject *slice,
379 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
380# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200381static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100382 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
383 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200384static PyObject* (*py3_PyErr_NoMemory)(void);
385static void (*py3_Py_Finalize)(void);
386static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200387static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200388static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200389# ifndef USE_LIMITED_API
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200391static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200392# else
393static PyObject* (*py3_Py_CompileString)(const char *, const char *, int);
394static PyObject* (*py3_PyEval_EvalCode)(PyObject *co, PyObject *globals, PyObject *locals);
395# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200396static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200397static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100398static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200399static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200400static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200401static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200402static PyObject* (*py3_PyEval_GetGlobals)(void);
403static PyObject* (*py3_PyEval_GetLocals)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200404static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
405static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200406static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200407static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408static PyObject* (*py3_PyErr_Occurred)(void);
409static PyObject* (*py3_PyModule_GetDict)(PyObject *);
410static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
411static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200412static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413static PyObject* (*py3_PyLong_FromLong)(long);
414static PyObject* (*py3_PyDict_New)(void);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200415# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
416 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200417static int (*py3_PyIter_Check)(PyObject *o);
418# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200419static PyObject* (*py3_PyIter_Next)(PyObject *);
420static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200421static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200422static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200423static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200424static PyObject* (*py3_Py_BuildValue)(char *, ...);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200425# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200426static int (*py3_PyType_GetFlags)(PyTypeObject *o);
427# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200428static int (*py3_PyType_Ready)(PyTypeObject *type);
429static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
430static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200431# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
432static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
433# else
434# ifdef Py_UNICODE_WIDE
435static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
436# else
437static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
438# endif
439# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200440static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
441 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200442static long (*py3_PyLong_AsLong)(PyObject *);
443static void (*py3_PyErr_SetNone)(PyObject *);
444static void (*py3_PyEval_InitThreads)(void);
445static void(*py3_PyEval_RestoreThread)(PyThreadState *);
446static PyThreadState*(*py3_PyEval_SaveThread)(void);
447static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
448static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Yee Cheng Chind606fcc2023-09-20 19:59:47 +0200449static void (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200450static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200451static int (*py3_Py_IsInitialized)(void);
452static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200453static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100454static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200455static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Ken Takata119fdd92023-10-04 20:05:05 +0200456# ifndef USE_LIMITED_API
Bram Moolenaardb913952012-06-29 12:54:53 +0200457static iternextfunc py3__PyObject_NextNotImplemented;
Ken Takata119fdd92023-10-04 20:05:05 +0200458# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200459static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200460static PyObject* py3__Py_FalseStruct;
461static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200462static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
463static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200464# ifdef USE_LIMITED_API
465# if Py_LIMITED_API >= 0x030a0000
466static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
467# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200468# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200469# if PY_VERSION_HEX >= 0x030300f0
470static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
471# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200472static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200473# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200474# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200475static int (*py3_PyUnicode_CompareWithASCIIString)(PyObject *unicode, const char* string);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200476static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200477static PyObject* (*py3_PyUnicode_AsUTF8String)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200478static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200479static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200480static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100481static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200482# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200483static void (*py3__Py_Dealloc)(PyObject *obj);
484# endif
485# if PY_VERSION_HEX >= 0x030900b0
486static PyObject* (*py3__PyObject_New)(PyTypeObject *);
487# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200488static PyObject* (*py3_PyFloat_FromDouble)(double num);
489static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200490static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200491static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
492static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200493static PyTypeObject* py3_PyType_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200494# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200495static PyTypeObject* py3_PyStdPrinter_Type;
Ken Takata119fdd92023-10-04 20:05:05 +0200496# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200497static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200498static PyTypeObject* py3_PyFloat_Type;
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200499PyTypeObject* py3_PyBool_Type;
500# if PY_VERSION_HEX >= 0x030c00b0
501PyTypeObject* py3_PyLong_Type;
502# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200503static int (*py3_PyNumber_Check)(PyObject *);
504static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200505static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200506static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
507static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200508# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200509static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
510static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200511static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200512# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200513static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
514# endif
515# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
516static void (*py3__PyObject_DebugFree)(void*);
517static void* (*py3__PyObject_DebugMalloc)(size_t);
518# else
519static void (*py3_PyObject_Free)(void*);
520static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200521# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200522static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
523static void(*py3_PyObject_GC_Del)(void *);
524static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200525static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200526# ifdef USE_LIMITED_API
527static void* (*py3_PyType_GetSlot)(PyTypeObject *, int);
528static PyObject* (*py3_PyType_FromSpec)(PyType_Spec *);
529# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200530
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100531// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200532static PyObject *p3imp_PyExc_AttributeError;
533static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200534static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200535static PyObject *p3imp_PyExc_KeyboardInterrupt;
536static PyObject *p3imp_PyExc_TypeError;
537static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200538static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200539static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200540static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200541static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200542
543# define PyExc_AttributeError p3imp_PyExc_AttributeError
544# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200545# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200546# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
547# define PyExc_TypeError p3imp_PyExc_TypeError
548# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200549# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200550# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200551# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200552# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200553
554/*
555 * Table of name to function pointer of python.
556 */
557# define PYTHON_PROC FARPROC
558static struct
559{
560 char *name;
561 PYTHON_PROC *ptr;
562} py3_funcname_table[] =
563{
564 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100565 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200566 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200567 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
568 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200569 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200570 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200571 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
572 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
573 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
574 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200575 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200576 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200577 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200578 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200579 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
580 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
581 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200582 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200583 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
584 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200585# if PY_VERSION_HEX >= 0x030601f0
586 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
587 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
588# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200589 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
590 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
591 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
592 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200593 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200594 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200595# ifndef USE_LIMITED_API
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200596 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200597 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200598# else
599 {"Py_CompileString", (PYTHON_PROC*)&py3_Py_CompileString},
600 {"PyEval_EvalCode", (PYTHON_PROC*)&PyEval_EvalCode},
601# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200602 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200603 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200604 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
605 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200606 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200607 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200608 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
609 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200610 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
611 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200612 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200613 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200614 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
615 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
616 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
617 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200618 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
619 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200620 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200621# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
622 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200623 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
624# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200625 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
626 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200627 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200628 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200629 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200630 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
631 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200632# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200633 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
634# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200635 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
636 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
637 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
638 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
639 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
640 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
641 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200642 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200643 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Ken Takata119fdd92023-10-04 20:05:05 +0200644# ifndef USE_LIMITED_API
Bram Moolenaardb913952012-06-29 12:54:53 +0200645 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Ken Takata119fdd92023-10-04 20:05:05 +0200646# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200647 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200648 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
649 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200650 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200651 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100652 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200653 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
654 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
655 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200656# ifdef USE_LIMITED_API
657# if Py_LIMITED_API >= 0x030a0000
658 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
659# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200660# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200661# if PY_VERSION_HEX >= 0x030300f0
662 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
663# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200664 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200665# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200666# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200667 {"PyUnicode_CompareWithASCIIString", (PYTHON_PROC*)&py3_PyUnicode_CompareWithASCIIString},
668 {"PyUnicode_AsUTF8String", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8String},
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200669# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
670 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
671# else
672# ifdef Py_UNICODE_WIDE
673 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
674# else
675 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
676# endif
677# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200678 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200679 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200680 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100681 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200682# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200683 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
684# endif
685# if PY_VERSION_HEX >= 0x030900b0
686 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
687# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200688 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
689 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200690 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200691 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
692 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200693 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200694# ifndef USE_LIMITED_API
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200695 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Ken Takata119fdd92023-10-04 20:05:05 +0200696# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200697 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200698 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Zdenek Dohnal15a0a022023-08-15 22:52:01 +0200699# if PY_VERSION_HEX < 0x030c00b0
Bram Moolenaar66b79852012-09-21 14:00:35 +0200700 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200701# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200702 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
703 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200704 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200705# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200706 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
707 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200708 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
709# else
710 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
711# endif
712# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200713 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
714 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200715# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200716 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
717 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200718# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200719 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
720 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
721 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200722 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
723 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
724 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200725# ifdef USE_LIMITED_API
726# if PY_VERSION_HEX >= 0x03040000
727 {"PyType_GetSlot", (PYTHON_PROC*)&py3_PyType_GetSlot},
728# endif
729 {"PyType_FromSpec", (PYTHON_PROC*)&py3_PyType_FromSpec},
730# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200731 {"", NULL},
732};
733
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200734# if PY_VERSION_HEX >= 0x030800f0
735 static inline void
736py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
737{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200738 if (--op->ob_refcnt != 0)
739 {
740# ifdef Py_REF_DEBUG
741 if (op->ob_refcnt < 0)
742 {
743 _Py_NegativeRefcount(filename, lineno, op);
744 }
745# endif
746 }
747 else
748 {
749 _Py_Dealloc(op);
750 }
751}
752
753# undef Py_DECREF
754# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
755
756 static inline void
757py3__Py_XDECREF(PyObject *op)
758{
759 if (op != NULL)
760 {
761 Py_DECREF(op);
762 }
763}
764
765# undef Py_XDECREF
766# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
767# endif
768
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200769# if PY_VERSION_HEX >= 0x030900b0
770 static inline int
771py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
772{
773 return ((PyType_GetFlags(type) & feature) != 0);
774}
775# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
776# endif
777
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200778# if PY_VERSION_HEX >= 0x030a00b2
779 static inline int
780py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
781{
782 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
783}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100784# if PY_VERSION_HEX >= 0x030b00b3
785# undef PyObject_TypeCheck
786# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
787# else
788# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
789# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200790# endif
791
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100792# ifdef MSWIN
793/*
794 * Look up the library "libname" using the InstallPath registry key.
795 * Return NULL when failed. Return an allocated string when successful.
796 */
797 static char *
798py3_get_system_libname(const char *libname)
799{
800 const char *cp = libname;
801 char subkey[128];
802 HKEY hKey;
803 char installpath[MAXPATHL];
804 LONG len = sizeof(installpath);
805 LSTATUS rc;
806 size_t sysliblen;
807 char *syslibname;
808
809 while (*cp != '\0')
810 {
811 if (*cp == ':' || *cp == '\\' || *cp == '/')
812 {
813 // Bail out if "libname" contains path separator, assume it is
814 // an absolute path.
815 return NULL;
816 }
817 ++cp;
818 }
819 vim_snprintf(subkey, sizeof(subkey),
820# ifdef _WIN64
821 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
822# else
823 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
824# endif
825 PY_MAJOR_VERSION, PY_MINOR_VERSION);
826 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
827 != ERROR_SUCCESS)
828 return NULL;
829 rc = RegQueryValueA(hKey, NULL, installpath, &len);
830 RegCloseKey(hKey);
831 if (ERROR_SUCCESS != rc)
832 return NULL;
833 cp = installpath + len;
834 // Just in case registry value contains null terminators.
835 while (cp > installpath && *(cp-1) == '\0')
836 --cp;
837 // Remove trailing path separators.
838 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
839 --cp;
840 // Ignore if InstallPath is effectively empty.
841 if (cp <= installpath)
842 return NULL;
843 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
844 syslibname = alloc(sysliblen);
845 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
846 (int)(cp - installpath), installpath, libname);
847 return syslibname;
848}
849# endif
850
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200851/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852 * Load library and get all pointers.
853 * Parameter 'libname' provides name of DLL.
854 * Return OK or FAIL.
855 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200856 static int
857py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200858{
859 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200860 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
861 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
862 PYTHON_PROC *ucs_as_encoded_string =
863 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200864
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100865# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100866 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000867 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100868 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200869 if (python_loaded())
870 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200871 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000872 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200873 return FAIL;
874 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200875# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200876
877 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200878 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200879 hinstPy3 = load_dll(libname);
880
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100881# ifdef MSWIN
882 if (!hinstPy3)
883 {
884 // Attempt to use the path from InstallPath as stored in the registry.
885 char *syslibname = py3_get_system_libname(libname);
886
887 if (syslibname != NULL)
888 {
889 hinstPy3 = load_dll(syslibname);
890 vim_free(syslibname);
891 }
892 }
893# endif
894
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200895 if (!hinstPy3)
896 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200897 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000898 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200899 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200900 }
901
902 for (i = 0; py3_funcname_table[i].ptr; ++i)
903 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200904 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
905 py3_funcname_table[i].name)) == NULL)
906 {
907 close_dll(hinstPy3);
908 hinstPy3 = 0;
909 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000910 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200911 return FAIL;
912 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913 }
914
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100915 // Load unicode functions separately as only the ucs2 or the ucs4 functions
916 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200917# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200918 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
919 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
920 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200921 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200922# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200923 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
924 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200925 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200926 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200927 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200928 if (*ucs_from_string == NULL || *ucs_decode == NULL
929 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200930 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200931 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200932 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200933 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200934 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200935 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200936 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200937 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200938# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200939 if (*ucs_from_string == NULL || *ucs_decode == NULL
940 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200941 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200942 close_dll(hinstPy3);
943 hinstPy3 = 0;
944 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000945 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200946 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200947 }
948
949 return OK;
950}
951
952/*
953 * If python is enabled (there is installed python on Windows system) return
954 * TRUE, else FALSE.
955 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200956 int
957python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200958{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100959 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200960}
961
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100962/*
963 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200964 * DLL, as this can cause errors (importing data symbols is not reliable).
965 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200966 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100967get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200968{
969 PyObject *exmod = PyImport_ImportModule("builtins");
970 PyObject *exdict = PyModule_GetDict(exmod);
971 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
972 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200973 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200974 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
975 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
976 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200977 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200978 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200979 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200980 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981 Py_XINCREF(p3imp_PyExc_AttributeError);
982 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200983 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200984 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
985 Py_XINCREF(p3imp_PyExc_TypeError);
986 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200987 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200988 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200989 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200990 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991 Py_XDECREF(exmod);
992}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100993#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994
Bram Moolenaardb913952012-06-29 12:54:53 +0200995static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200996#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200997static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200998
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200999#ifdef USE_LIMITED_API
1000# define DESTRUCTOR_FINISH(self) \
1001 ((freefunc)PyType_GetSlot(Py_TYPE(self), Py_tp_free))((PyObject*)self)
1002#else
1003# define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
1004#endif
Bram Moolenaardb913952012-06-29 12:54:53 +02001005
Bram Moolenaar971db462013-05-12 18:44:48 +02001006#define WIN_PYTHON_REF(win) win->w_python3_ref
1007#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001008#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +02001009
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001010 static void
1011call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001012{
Bram Moolenaar0014a532013-05-29 21:33:39 +02001013#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001014 _PyObject_DebugFree(p);
1015#else
1016 PyObject_Free(p);
1017#endif
1018}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001019
1020 static PyObject *
1021call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001022{
1023 return PyType_GenericNew(type,args,kwds);
1024}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001025
1026 static PyObject *
1027call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001028{
1029 return PyType_GenericAlloc(type,nitems);
1030}
1031
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001032static PyObject *OutputGetattro(PyObject *, PyObject *);
1033static int OutputSetattro(PyObject *, PyObject *, PyObject *);
1034static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001035static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001036static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001037static PyObject *WindowGetattro(PyObject *, PyObject *);
1038static int WindowSetattro(PyObject *, PyObject *, PyObject *);
1039static PyObject *RangeGetattro(PyObject *, PyObject *);
1040static PyObject *CurrentGetattro(PyObject *, PyObject *);
1041static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
1042static PyObject *DictionaryGetattro(PyObject *, PyObject *);
1043static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
1044static PyObject *ListGetattro(PyObject *, PyObject *);
1045static int ListSetattro(PyObject *, PyObject *, PyObject *);
1046static PyObject *FunctionGetattro(PyObject *, PyObject *);
1047
1048static struct PyModuleDef vimmodule;
1049
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02001050#define PY_CAN_RECURSE
1051
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001052/*
1053 * Include the code shared with if_python.c
1054 */
1055#include "if_py_both.h"
1056
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001057#ifndef USE_LIMITED_API
1058# if PY_VERSION_HEX >= 0x030300f0
1059# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1060# else
1061# define PY_UNICODE_GET_UTF8_CHARS _PyUnicode_AsString
1062# endif
1063#else
1064# if Py_LIMITED_API >= 0x030A0000
1065# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1066# else
1067// Python limited API before 3.10 lack easy ways to query the raw UTF-8 chars.
1068// We need to first convert the string to bytes, and then extract the chars.
1069// This function is only used for attribute string comparisons, which have
1070// known short length. As such, just allocate a short static buffer to hold
1071// the characters instead of having to allocate/deallcoate it.
1072//
1073// An alternative would be to convert all attribute string comparisons to use
1074// PyUnicode_CompareWithASCIIString to skip having to extract the chars.
1075static char py3_unicode_utf8_chars[20];
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02001076static char* PY_UNICODE_GET_UTF8_CHARS(PyObject* str)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001077{
1078 py3_unicode_utf8_chars[0] = '\0';
1079 PyObject* bytes = PyUnicode_AsUTF8String(str);
1080 if (bytes)
1081 {
1082 char *chars;
1083 Py_ssize_t len;
1084 if (PyBytes_AsStringAndSize(bytes, &chars, &len) != -1)
1085 {
1086 if (len < (Py_ssize_t)sizeof(py3_unicode_utf8_chars))
1087 // PyBytes_AsStringAndSize guarantees null-termination
1088 memcpy(py3_unicode_utf8_chars, chars, len + 1);
1089 }
1090 Py_DECREF(bytes);
1091 }
1092 return py3_unicode_utf8_chars;
1093}
1094# endif
1095#endif
1096
Bram Moolenaar828bff12019-03-19 22:11:41 +01001097// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001098#define GET_ATTR_STRING(name, nameobj) \
1099 char *name = ""; \
1100 if (PyUnicode_Check(nameobj)) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001101 name = (char *)PY_UNICODE_GET_UTF8_CHARS(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001102
1103#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
1104
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001105///////////////////////////////////////////////////////
1106// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001107
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001108static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001109
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001110///////////////////////////////////////////////////////
1111// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001112
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001113 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001114python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001115{
1116 static int recurse = 0;
1117
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001118 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001119 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001120 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001121
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001122 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123 ++recurse;
1124
1125#ifdef DYNAMIC_PYTHON3
1126 if (hinstPy3)
1127#endif
1128 if (Py_IsInitialized())
1129 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001130#ifdef USE_LIMITED_API
1131 shutdown_types();
1132#endif
1133
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001134 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001135 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001136
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001137 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001138 }
1139
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001140 --recurse;
1141}
1142
Bram Moolenaar094454f2015-10-07 10:39:55 +02001143#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001144 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001145python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001146{
1147 return (hinstPy3 != 0);
1148}
1149#endif
1150
Bram Moolenaar94073162018-01-31 21:49:05 +01001151static wchar_t *py_home_buf = NULL;
1152
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001153#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001154/*
1155 * Return TRUE if stdin is readable from Python 3.
1156 */
1157 static BOOL
1158is_stdin_readable(void)
1159{
1160 DWORD mode, eventnum;
1161 struct _stat st;
1162 int fd = fileno(stdin);
1163 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1164
1165 // Check if stdin is connected to the console.
1166 if (GetConsoleMode(hstdin, &mode))
1167 // Check if it is opened as input.
1168 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1169
1170 return _fstat(fd, &st) == 0;
1171}
1172
1173// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1174// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001175// Note that the python DLL is linked to its own stdio DLL which can be
1176// differ from Vim's stdio.
1177 static void
1178reset_stdin(void)
1179{
1180 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1181 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Ken Takata119fdd92023-10-04 20:05:05 +02001182 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001183
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001184 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001185 return;
1186
1187 // Get "freopen" and "stdin" which are used in the python DLL.
1188 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1189 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1190 if (py__acrt_iob_func)
1191 {
1192 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001193 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001194 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001195 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001196 }
1197
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001198 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001199 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001200 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001201 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001202 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001203}
1204#else
1205# define reset_stdin()
1206#endif
1207
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001208// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1209// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1210// Install a hook to python dll's exit() and recover from it.
1211#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1212# define HOOK_EXIT
1213# include <setjmp.h>
1214
1215static jmp_buf exit_hook_jump_buf;
1216static void *orig_exit = NULL;
1217
1218/*
1219 * Function that replaces exit() while calling Py_Initialize().
1220 */
1221 static void
1222hooked_exit(int ret)
1223{
1224 // Recover from exit.
1225 longjmp(exit_hook_jump_buf, 1);
1226}
1227
1228/*
1229 * Install a hook to python dll's exit().
1230 */
1231 static void
1232hook_py_exit(void)
1233{
Ken Takata119fdd92023-10-04 20:05:05 +02001234 HINSTANCE hinst = get_forwarded_dll(hinstPy3);
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001235
1236 if (hinst == NULL || orig_exit != NULL)
1237 return;
1238
1239 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1240}
1241
1242/*
1243 * Remove the hook installed by hook_py_exit().
1244 */
1245 static void
1246restore_py_exit(void)
1247{
1248 HINSTANCE hinst = hinstPy3;
1249
1250 if (hinst == NULL)
1251 return;
1252
1253 if (orig_exit != NULL)
1254 hook_dll_import_func(hinst, "exit", orig_exit);
1255 orig_exit = NULL;
1256}
1257#endif
1258
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001259 static int
1260Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001261{
1262 if (!py3initialised)
1263 {
1264#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001265 if (!python3_enabled(TRUE))
1266 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001267 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001268 goto fail;
1269 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001270#endif
1271
1272 init_structs();
1273
Bram Moolenaar94073162018-01-31 21:49:05 +01001274 if (*p_py3home != NUL)
1275 {
1276 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001277
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001278 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001279 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001280 if (py_home_buf != NULL && mbstowcs(
1281 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1282 Py_SetPythonHome(py_home_buf);
1283 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001284#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001285 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001286 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001287#endif
1288
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001289 PyImport_AppendInittab("vim", Py3Init_vim);
1290
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001291#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1292 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1293#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001294 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001295
1296#ifdef HOOK_EXIT
1297 // Catch exit() called in Py_Initialize().
1298 hook_py_exit();
1299 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001300 {
1301 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001302 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001303 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001304 else
1305 {
1306 // exit() was called in Py_Initialize().
1307 restore_py_exit();
1308 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1309 goto fail;
1310 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001311#else
1312 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001313#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001314
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001315#if PY_VERSION_HEX < 0x03090000
1316 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001317 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001318#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001319#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001320 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001321#endif
1322
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001323 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001324 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001325
Bram Moolenaardb913952012-06-29 12:54:53 +02001326 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1327
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001328 // Remove the element from sys.path that was added because of our
1329 // argv[0] value in Py3Init_vim(). Previously we used an empty
1330 // string, but depending on the OS we then get an empty entry or
1331 // the current directory in sys.path.
1332 // Only after vim has been imported, the element does exist in
1333 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001334 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 +02001335
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001336 // Without the call to PyEval_SaveThread, thread specific state (such
1337 // as the system trace hook), will be lost between invocations of
1338 // Python code.
1339 // GIL may have been created and acquired in PyEval_InitThreads() and
1340 // thread state is created in Py_Initialize(); there
1341 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1342 // have threads enabled!), so the following does both: unlock GIL and
1343 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001344 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001345
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001346 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001347 }
1348
1349 return 0;
1350
1351fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001352 // We call PythonIO_Flush() here to print any Python errors.
1353 // This is OK, as it is possible to call this function even
1354 // if PythonIO_Init_io() has not completed successfully (it will
1355 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001356 PythonIO_Flush();
1357 return -1;
1358}
1359
1360/*
1361 * External interface
1362 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001363 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001364DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001365{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001366#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001369 PyObject *cmdstr;
1370 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001371 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001372
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001373 if (python_end_called)
1374 goto theend;
1375
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001376 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001377 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001378
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001379 init_range(arg);
1380
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001381 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001382
1383#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001384 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001385 saved_locale = setlocale(LC_NUMERIC, NULL);
1386 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001387 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001388 else
1389 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001390 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001391 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1392 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001393 }
1394#endif
1395
1396 pygilstate = PyGILState_Ensure();
1397
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001398 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1399 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001400 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001401 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1402 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001403 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001404
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001405 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001406 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001407
1408 PyGILState_Release(pygilstate);
1409
1410#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1411 if (saved_locale != NULL)
1412 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001413 (void)setlocale(LC_NUMERIC, saved_locale);
1414 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001415 }
1416#endif
1417
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001418 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001419 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420
1421theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001422 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001423}
1424
1425/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001426 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001428 void
1429ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001430{
1431 char_u *script;
1432
1433 script = script_get(eap, eap->arg);
1434 if (!eap->skip)
1435 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001436 if (p_pyx == 0)
1437 p_pyx = 3;
1438
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001439 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001440 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001441 (runner) run_cmd,
1442 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443 }
1444 vim_free(script);
1445}
1446
1447#define BUFFER_SIZE 2048
1448
1449/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001450 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001451 */
1452 void
1453ex_py3file(exarg_T *eap)
1454{
1455 static char buffer[BUFFER_SIZE];
1456 const char *file;
1457 char *p;
1458 int i;
1459
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001460 if (p_pyx == 0)
1461 p_pyx = 3;
1462
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001463 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1464 // stdio file pointer, but Vim and the Python DLL are compiled with
1465 // different options under Windows, meaning that stdio pointers aren't
1466 // compatible between the two. Yuk.
1467 //
1468 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1469 //
1470 // Using bytes so that Python can detect the source encoding as it normally
1471 // does. The doc does not say "compile" accept bytes, though.
1472 //
1473 // We need to escape any backslashes or single quotes in the file name, so that
1474 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001475
1476 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001477 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001478
1479 for (i=0; i<2; ++i)
1480 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001481 file = (char *)eap->arg;
1482 while (*file && p < buffer + (BUFFER_SIZE - 3))
1483 {
1484 if (*file == '\\' || *file == '\'')
1485 *p++ = '\\';
1486 *p++ = *file++;
1487 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001488 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001489 if (*file != '\0')
1490 return;
1491 if (i==0)
1492 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001493 strcpy(p,"','rb').read(),'");
1494 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001495 }
1496 else
1497 {
1498 strcpy(p,"','exec'))");
1499 p += 10;
1500 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001501 }
1502
1503
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001504 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001505 DoPyCommand(buffer,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001506 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001507 (runner) run_cmd,
1508 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509}
1510
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001511 void
1512ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001513{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001514 if (p_pyx == 0)
1515 p_pyx = 3;
1516
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001517 DoPyCommand((char *)eap->arg,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001518 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001519 (runner)run_do,
1520 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001521}
1522
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001523///////////////////////////////////////////////////////
1524// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001525
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001526// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001527
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001528 static PyObject *
1529OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001530{
Bram Moolenaar77045652012-09-21 13:46:06 +02001531 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001532
1533 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001534 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001535 else if (strcmp(name, "errors") == 0)
1536 return PyString_FromString("strict");
1537 else if (strcmp(name, "encoding") == 0)
1538 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001539
1540 return PyObject_GenericGetAttr(self, nameobj);
1541}
1542
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001543 static int
1544OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001545{
Bram Moolenaar77045652012-09-21 13:46:06 +02001546 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001547
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001548 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001549}
1550
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001551///////////////////////////////////////////////////////
1552// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001553
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001554// Window type - Implementation functions
1555// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001556
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001557#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1558
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001559// Buffer type - Implementation functions
1560// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001561
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001562#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1563
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001564static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001565static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001566
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001567// Line range type - Implementation functions
1568// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001569
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001570#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1571
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001572static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001573static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1574static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001575
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001576// Current objects type - Implementation functions
1577// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001578
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001579static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001580 (lenfunc) BufferLength, // sq_length, len(x)
1581 (binaryfunc) 0, // sq_concat, x+y
1582 (ssizeargfunc) 0, // sq_repeat, x*n
1583 (ssizeargfunc) BufferItem, // sq_item, x[i]
1584 0, // was_sq_slice, x[i:j]
1585 0, // sq_ass_item, x[i]=v
1586 0, // sq_ass_slice, x[i:j]=v
1587 0, // sq_contains
1588 0, // sq_inplace_concat
1589 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001590};
1591
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001592static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001593 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001594 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001595 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001596};
1597
1598
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001599// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001600
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001601 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001602BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001603{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001604 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001605
Bram Moolenaar77045652012-09-21 13:46:06 +02001606 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001607
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001608 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1609 return r;
1610
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001611 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001612 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001613
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001614 r = BufferAttr((BufferObject *)(self), name);
1615 if (r || PyErr_Occurred())
1616 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001617 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001618 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001619}
1620
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001621 static int
1622BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1623{
1624 GET_ATTR_STRING(name, nameobj);
1625
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001626 return BufferSetattr(self, name, val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001627}
1628
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001629//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001630
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001631 static PyObject *
1632BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001633{
Bram Moolenaardb913952012-06-29 12:54:53 +02001634 if (PyLong_Check(idx))
1635 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001637 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001638 }
1639 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001640 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001641 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001642
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001643 if (CheckBuffer((BufferObject *) self))
1644 return NULL;
1645
Bram Moolenaar922a4662014-03-30 16:11:43 +02001646 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001647 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001648 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001649 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001651 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001652 }
1653 else
1654 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001655 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001657 }
1658}
1659
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001660 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001661BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1662{
Bram Moolenaardb913952012-06-29 12:54:53 +02001663 if (PyLong_Check(idx))
1664 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001665 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001666
1667 if (CheckBuffer((BufferObject *) self))
1668 return -1;
1669
Bram Moolenaar19e60942011-06-19 00:27:51 +02001670 return RBAsItem((BufferObject *)(self), n, val, 1,
1671 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1672 NULL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001673 }
1674 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001675 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001676 Py_ssize_t start, stop, step, slicelen;
1677
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001678 if (CheckBuffer((BufferObject *) self))
1679 return -1;
1680
Bram Moolenaar922a4662014-03-30 16:11:43 +02001681 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001682 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001683 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001684 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001685 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001686 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1687 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1688 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001689 }
1690 else
1691 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001692 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001693 return -1;
1694 }
1695}
1696
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001697static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001698 (lenfunc) RangeLength, // sq_length, len(x)
1699 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1700 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1701 (ssizeargfunc) RangeItem, // sq_item, x[i]
1702 0, // was_sq_slice, x[i:j]
1703 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1704 0, // sq_ass_slice, x[i:j]=v
1705 0, // sq_contains
1706 0, // sq_inplace_concat
1707 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001708};
1709
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001710static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001711 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001712 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001713 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001714};
1715
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001716// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001717
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001718 static PyObject *
1719RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001720{
Bram Moolenaar77045652012-09-21 13:46:06 +02001721 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001722
1723 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001724 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001725 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001726 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001727 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001728 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001729}
1730
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001731////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001732
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001733 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001734RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001735{
1736 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001737 ((RangeObject *)(self))->start,
1738 ((RangeObject *)(self))->end,
1739 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001740}
1741
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001742 static Py_ssize_t
1743RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1744{
1745 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1746 ((RangeObject *)(self))->start,
1747 ((RangeObject *)(self))->end,
1748 &((RangeObject *)(self))->end);
1749}
1750
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001751 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001752RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001753{
Bram Moolenaardb913952012-06-29 12:54:53 +02001754 if (PyLong_Check(idx))
1755 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001756 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001757 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001758 }
1759 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001760 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001761 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001762
Bram Moolenaar922a4662014-03-30 16:11:43 +02001763 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001764 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1765 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001766 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001767 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001768 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001769 }
1770 else
1771 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001772 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001773 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001774 }
1775}
1776
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001777 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001778RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1779{
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 if (PyLong_Check(idx))
1781 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001782 long n = PyLong_AsLong(idx);
1783 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001784 }
1785 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001787 Py_ssize_t start, stop, step, slicelen;
1788
Bram Moolenaar922a4662014-03-30 16:11:43 +02001789 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001790 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1791 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001792 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001793 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001794 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001795 }
1796 else
1797 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001798 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001799 return -1;
1800 }
1801}
1802
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001803// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001804
1805 static PyObject *
1806TabPageGetattro(PyObject *self, PyObject *nameobj)
1807{
1808 PyObject *r;
1809
1810 GET_ATTR_STRING(name, nameobj);
1811
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001812 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1813 return r;
1814
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001815 if (CheckTabPage((TabPageObject *)(self)))
1816 return NULL;
1817
1818 r = TabPageAttr((TabPageObject *)(self), name);
1819 if (r || PyErr_Occurred())
1820 return r;
1821 else
1822 return PyObject_GenericGetAttr(self, nameobj);
1823}
1824
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001825// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001826
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001827 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001828WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001829{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001830 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001831
Bram Moolenaar77045652012-09-21 13:46:06 +02001832 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001833
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001834 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1835 return r;
1836
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001837 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001838 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001839
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001840 r = WindowAttr((WindowObject *)(self), name);
1841 if (r || PyErr_Occurred())
1842 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001843 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001844 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001845}
1846
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001847 static int
1848WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001849{
Bram Moolenaar77045652012-09-21 13:46:06 +02001850 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001851
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001852 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001853}
1854
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001855// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001856
1857static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001858 (lenfunc) TabListLength, // sq_length, len(x)
1859 (binaryfunc) 0, // sq_concat, x+y
1860 (ssizeargfunc) 0, // sq_repeat, x*n
1861 (ssizeargfunc) TabListItem, // sq_item, x[i]
1862 0, // sq_slice, x[i:j]
1863 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1864 0, // sq_ass_slice, x[i:j]=v
1865 0, // sq_contains
1866 0, // sq_inplace_concat
1867 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001868};
1869
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001870// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001871
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001872static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001873 (lenfunc) WinListLength, // sq_length, len(x)
1874 (binaryfunc) 0, // sq_concat, x+y
1875 (ssizeargfunc) 0, // sq_repeat, x*n
1876 (ssizeargfunc) WinListItem, // sq_item, x[i]
1877 0, // sq_slice, x[i:j]
1878 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1879 0, // sq_ass_slice, x[i:j]=v
1880 0, // sq_contains
1881 0, // sq_inplace_concat
1882 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001883};
1884
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001885/*
1886 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001887 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001888 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001889CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001890{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001891 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001892 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001893 if (!(r = CurrentGetattr(self, name)))
1894 return PyObject_GenericGetAttr(self, nameobj);
1895 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001896}
1897
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001898 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001899CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001900{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001901 GET_ATTR_STRING(name, nameobj);
1902 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001903}
1904
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001905// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001906
Bram Moolenaar66b79852012-09-21 14:00:35 +02001907 static PyObject *
1908DictionaryGetattro(PyObject *self, PyObject *nameobj)
1909{
1910 DictionaryObject *this = ((DictionaryObject *) (self));
1911
1912 GET_ATTR_STRING(name, nameobj);
1913
1914 if (strcmp(name, "locked") == 0)
1915 return PyLong_FromLong(this->dict->dv_lock);
1916 else if (strcmp(name, "scope") == 0)
1917 return PyLong_FromLong(this->dict->dv_scope);
1918
1919 return PyObject_GenericGetAttr(self, nameobj);
1920}
1921
1922 static int
1923DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1924{
1925 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001926 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001927}
1928
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001929// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001930
Bram Moolenaar66b79852012-09-21 14:00:35 +02001931 static PyObject *
1932ListGetattro(PyObject *self, PyObject *nameobj)
1933{
1934 GET_ATTR_STRING(name, nameobj);
1935
1936 if (strcmp(name, "locked") == 0)
1937 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1938
1939 return PyObject_GenericGetAttr(self, nameobj);
1940}
1941
1942 static int
1943ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1944{
1945 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001946 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001947}
1948
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001949// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001950
Bram Moolenaardb913952012-06-29 12:54:53 +02001951 static PyObject *
1952FunctionGetattro(PyObject *self, PyObject *nameobj)
1953{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001954 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001955 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001956
1957 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001958
Bram Moolenaar8110a092016-04-14 15:56:09 +02001959 r = FunctionAttr(this, name);
1960 if (r || PyErr_Occurred())
1961 return r;
1962 else
1963 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001964}
1965
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001966// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001967
1968 void
1969python3_buffer_free(buf_T *buf)
1970{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001971 BufferObject *bp = BUF_PYTHON_REF(buf);
1972 if (bp == NULL)
1973 return;
1974 bp->buf = INVALID_BUFFER_VALUE;
1975 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001976}
1977
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001978 void
1979python3_window_free(win_T *win)
1980{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001981 WindowObject *wp = WIN_PYTHON_REF(win);
1982 if (wp == NULL)
1983 return;
1984 wp->win = INVALID_WINDOW_VALUE;
1985 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001986}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001987
1988 void
1989python3_tabpage_free(tabpage_T *tab)
1990{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001991 TabPageObject *tp = TAB_PYTHON_REF(tab);
1992 if (tp == NULL)
1993 return;
1994 tp->tab = INVALID_TABPAGE_VALUE;
1995 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001996}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001997
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001998 static PyObject *
1999Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002000{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002001 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002002 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
2003
Bram Moolenaar1dc28782013-05-21 19:11:01 +02002004 if (init_types())
2005 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002006
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002007 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002008 PySys_SetArgv(1, argv);
2009
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002010 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02002011 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002012
Bram Moolenaardee2e312013-06-23 16:35:47 +02002013 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002014 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002015
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002016 if (init_sys_path())
2017 return NULL;
2018
2019 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002020}
2021
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002022//////////////////////////////////////////////////////////////////////////
2023// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002024
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002025/*
2026 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002027 * All internal newlines are replaced by null characters.
2028 *
2029 * On errors, the Python exception data is set, and NULL is returned.
2030 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002031 static PyObject *
2032LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002033{
2034 PyObject *result;
2035 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01002036 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002037
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002038 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002039 p = tmp;
2040 if (p == NULL)
2041 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002042 PyErr_NoMemory();
2043 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002044 }
2045
2046 while (*str)
2047 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002048 if (*str == '\n')
2049 *p = '\0';
2050 else
2051 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002052
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002053 ++p;
2054 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002055 }
2056 *p = '\0';
2057
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01002058 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002059
2060 vim_free(tmp);
2061 return result;
2062}
2063
Bram Moolenaardb913952012-06-29 12:54:53 +02002064 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002065do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02002066{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002067 DoPyCommand((char *) str,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02002068 init_range_eval,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002069 (runner) run_eval,
2070 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002071 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02002072 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002073 rettv->v_type = VAR_NUMBER;
2074 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02002075 }
2076}
2077
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002078 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002079set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02002080{
Bram Moolenaarb641df42015-02-03 13:16:04 +01002081 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02002082}
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002083
2084 int
Dominique Pellé4927bc72023-09-24 16:12:07 +02002085python3_version(void)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002086{
2087#ifdef USE_LIMITED_API
2088 return Py_LIMITED_API;
2089#else
2090 return PY_VERSION_HEX;
2091#endif
2092}