blob: a04cca31ae50876c86074c592d78ef684ed07129 [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
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200245#undef Py_BuildValue
246# 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)
Bram Moolenaardb913952012-06-29 12:54:53 +0200254# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200255# define PyModule_AddObject py3_PyModule_AddObject
256# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200257# define PyImport_AddModule py3_PyImport_AddModule
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200258# ifdef USE_LIMITED_API
259# if Py_LIMITED_API >= 0x030a0000
260# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
261# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200262# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200263# if PY_VERSION_HEX >= 0x030300f0
264# define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize
265# else
266# define _PyUnicode_AsString py3__PyUnicode_AsString
267# endif
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200268# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200269# undef PyUnicode_CompareWithASCIIString
270# define PyUnicode_CompareWithASCIIString py3_PyUnicode_CompareWithASCIIString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200271# undef PyUnicode_AsEncodedString
272# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200273# undef PyUnicode_AsUTF8String
274# define PyUnicode_AsUTF8String py3_PyUnicode_AsUTF8String
Bram Moolenaar19e60942011-06-19 00:27:51 +0200275# undef PyBytes_AsString
276# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200277# ifndef PyBytes_AsStringAndSize
278# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
279# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200280# undef PyBytes_FromString
281# define PyBytes_FromString py3_PyBytes_FromString
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100282# undef PyBytes_FromStringAndSize
283# define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200284# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200285# define _Py_Dealloc py3__Py_Dealloc
286# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200287# define PyFloat_FromDouble py3_PyFloat_FromDouble
288# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200290# define PyType_Type (*py3_PyType_Type)
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200291# define PyStdPrinter_Type (*py3_PyStdPrinter_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200292# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200293# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200294# define PyNumber_Check (*py3_PyNumber_Check)
295# define PyNumber_Long (*py3_PyNumber_Long)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200296# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200297# ifdef Py_DEBUG
298# define _Py_NegativeRefcount py3__Py_NegativeRefcount
299# define _Py_RefTotal (*py3__Py_RefTotal)
Bram Moolenaar0014a532013-05-29 21:33:39 +0200300# define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs
301# else
302# define PyModule_Create2 py3_PyModule_Create2
303# endif
304# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200305# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
306# define _PyObject_DebugFree py3__PyObject_DebugFree
307# else
308# define PyObject_Malloc py3_PyObject_Malloc
309# define PyObject_Free py3_PyObject_Free
310# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200311# define _PyObject_GC_New py3__PyObject_GC_New
312# define PyObject_GC_Del py3_PyObject_GC_Del
313# define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200314# define PyType_GenericAlloc py3_PyType_GenericAlloc
315# define PyType_GenericNew py3_PyType_GenericNew
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200316# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200317# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200318# ifndef PyUnicode_FromFormat
319# define PyUnicode_FromFormat py3_PyUnicode_FromFormat
320# else
321# define Py_UNICODE_USE_UCS_FUNCTIONS
322# ifdef Py_UNICODE_WIDE
323# define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat
324# else
325# define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat
326# endif
327# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200328# undef PyUnicode_Decode
329# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200330# define PyType_IsSubtype py3_PyType_IsSubtype
331# define PyCapsule_New py3_PyCapsule_New
332# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200333# ifdef USE_LIMITED_API
334# define PyType_GetSlot py3_PyType_GetSlot
335# define PyType_FromSpec py3_PyType_FromSpec
336# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200337
Bram Moolenaar0014a532013-05-29 21:33:39 +0200338# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200339# undef PyObject_NEW
340# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200341( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200342 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200343# elif PY_VERSION_HEX >= 0x030900b0
344# undef PyObject_NEW
345# define PyObject_NEW(type, typeobj) \
346 ((type *)py3__PyObject_New(typeobj))
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200347# endif
348
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200349/*
350 * Pointers for dynamic link
351 */
352static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100353static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200354static void (*py3_Py_Initialize)(void);
355static PyObject* (*py3_PyList_New)(Py_ssize_t size);
356static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
357static void (*py3_PyGILState_Release)(PyGILState_STATE);
358static int (*py3_PySys_SetObject)(char *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200359static PyObject* (*py3_PySys_GetObject)(char *);
360static int (*py3_PyList_Append)(PyObject *, PyObject *);
361static int (*py3_PyList_Insert)(PyObject *, int, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200362static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200363static int (*py3_PySequence_Check)(PyObject *);
364static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
365static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200366static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200367static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
368static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
369static int (*py3_PyMapping_Check)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200370static PyObject* (*py3_PyMapping_Keys)(PyObject *);
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200371# if PY_VERSION_HEX >= 0x030601f0
372static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length,
373 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step);
374static int (*py3_PySlice_Unpack)(PyObject *slice,
375 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
376# endif
Bram Moolenaar922a4662014-03-30 16:11:43 +0200377static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length,
Bram Moolenaar063a46b2014-01-14 16:36:51 +0100378 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
379 Py_ssize_t *slicelen);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200380static PyObject* (*py3_PyErr_NoMemory)(void);
381static void (*py3_Py_Finalize)(void);
382static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200383static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200384static int (*py3_PyErr_ExceptionMatches)(PyObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200385# ifndef USE_LIMITED_API
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200386static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200387static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200388# else
389static PyObject* (*py3_Py_CompileString)(const char *, const char *, int);
390static PyObject* (*py3_PyEval_EvalCode)(PyObject *co, PyObject *globals, PyObject *locals);
391# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200392static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
Bram Moolenaara9922d62013-05-30 13:01:18 +0200393static int (*py3_PyObject_HasAttrString)(PyObject *, const char *);
Bram Moolenaar0b400082013-11-03 00:28:25 +0100394static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200395static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200396static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200397static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200398static PyObject* (*py3_PyEval_GetGlobals)();
399static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200400static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
401static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200402static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200403static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200404static PyObject* (*py3_PyErr_Occurred)(void);
405static PyObject* (*py3_PyModule_GetDict)(PyObject *);
406static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
407static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200408static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200409static PyObject* (*py3_PyLong_FromLong)(long);
410static PyObject* (*py3_PyDict_New)(void);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200411# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
412 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200413static int (*py3_PyIter_Check)(PyObject *o);
414# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200415static PyObject* (*py3_PyIter_Next)(PyObject *);
416static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200417static PyObject* (*py3_PyObject_Repr)(PyObject *);
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200418static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200419static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200420static PyObject* (*py3_Py_BuildValue)(char *, ...);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200421# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200422static int (*py3_PyType_GetFlags)(PyTypeObject *o);
423# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200424static int (*py3_PyType_Ready)(PyTypeObject *type);
425static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
426static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200427# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
428static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...);
429# else
430# ifdef Py_UNICODE_WIDE
431static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...);
432# else
433static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...);
434# endif
435# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200436static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
437 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200438static long (*py3_PyLong_AsLong)(PyObject *);
439static void (*py3_PyErr_SetNone)(PyObject *);
440static void (*py3_PyEval_InitThreads)(void);
441static void(*py3_PyEval_RestoreThread)(PyThreadState *);
442static PyThreadState*(*py3_PyEval_SaveThread)(void);
443static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
444static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Yee Cheng Chind606fcc2023-09-20 19:59:47 +0200445static void (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200446static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200447static int (*py3_Py_IsInitialized)(void);
448static void (*py3_PyErr_Clear)(void);
Bram Moolenaarc476e522013-06-23 13:46:40 +0200449static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100450static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200451static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200452static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200453static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200454static PyObject* py3__Py_FalseStruct;
455static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200456static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
457static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200458# ifdef USE_LIMITED_API
459# if Py_LIMITED_API >= 0x030a0000
460static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
461# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200462# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200463# if PY_VERSION_HEX >= 0x030300f0
464static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size);
465# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200466static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200467# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200468# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200469static int (*py3_PyUnicode_CompareWithASCIIString)(PyObject *unicode, const char* string);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200470static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200471static PyObject* (*py3_PyUnicode_AsUTF8String)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200472static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200473static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200474static PyObject* (*py3_PyBytes_FromString)(char *str);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100475static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200476# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200477static void (*py3__Py_Dealloc)(PyObject *obj);
478# endif
479# if PY_VERSION_HEX >= 0x030900b0
480static PyObject* (*py3__PyObject_New)(PyTypeObject *);
481# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200482static PyObject* (*py3_PyFloat_FromDouble)(double num);
483static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200484static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200485static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
486static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200487static PyTypeObject* py3_PyType_Type;
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200488static PyTypeObject* py3_PyStdPrinter_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200489static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200490static PyTypeObject* py3_PyFloat_Type;
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200491PyTypeObject* py3_PyBool_Type;
492# if PY_VERSION_HEX >= 0x030c00b0
493PyTypeObject* py3_PyLong_Type;
494# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200495static int (*py3_PyNumber_Check)(PyObject *);
496static PyObject* (*py3_PyNumber_Long)(PyObject *);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200497static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200498static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
499static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200500# ifdef Py_DEBUG
Bram Moolenaar0014a532013-05-29 21:33:39 +0200501static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
502static Py_ssize_t* py3__Py_RefTotal;
Bram Moolenaar0014a532013-05-29 21:33:39 +0200503static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200504# else
Bram Moolenaar0014a532013-05-29 21:33:39 +0200505static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
506# endif
507# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
508static void (*py3__PyObject_DebugFree)(void*);
509static void* (*py3__PyObject_DebugMalloc)(size_t);
510# else
511static void (*py3_PyObject_Free)(void*);
512static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200513# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200514static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *);
515static void(*py3_PyObject_GC_Del)(void *);
516static void(*py3_PyObject_GC_UnTrack)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200517static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200518# ifdef USE_LIMITED_API
519static void* (*py3_PyType_GetSlot)(PyTypeObject *, int);
520static PyObject* (*py3_PyType_FromSpec)(PyType_Spec *);
521# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200522
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100523// Imported exception objects
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200524static PyObject *p3imp_PyExc_AttributeError;
525static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200526static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200527static PyObject *p3imp_PyExc_KeyboardInterrupt;
528static PyObject *p3imp_PyExc_TypeError;
529static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar41009372013-07-01 22:03:04 +0200530static PyObject *p3imp_PyExc_SystemExit;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200531static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200532static PyObject *p3imp_PyExc_ImportError;
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200533static PyObject *p3imp_PyExc_OverflowError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200534
535# define PyExc_AttributeError p3imp_PyExc_AttributeError
536# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200537# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200538# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
539# define PyExc_TypeError p3imp_PyExc_TypeError
540# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar41009372013-07-01 22:03:04 +0200541# define PyExc_SystemExit p3imp_PyExc_SystemExit
Bram Moolenaar8661b172013-05-15 15:44:28 +0200542# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200543# define PyExc_ImportError p3imp_PyExc_ImportError
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200544# define PyExc_OverflowError p3imp_PyExc_OverflowError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200545
546/*
547 * Table of name to function pointer of python.
548 */
549# define PYTHON_PROC FARPROC
550static struct
551{
552 char *name;
553 PYTHON_PROC *ptr;
554} py3_funcname_table[] =
555{
556 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100557 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200558 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200559 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
560 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200561 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200562 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200563 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
564 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
565 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
566 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200567 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200568 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200569 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200570 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200571 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
572 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
573 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200574 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast},
Bram Moolenaardb913952012-06-29 12:54:53 +0200575 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
576 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaar3b48b112018-07-04 22:03:25 +0200577# if PY_VERSION_HEX >= 0x030601f0
578 {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices},
579 {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack},
580# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200581 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
582 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
583 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
584 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200585 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200586 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200587# ifndef USE_LIMITED_API
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200588 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200589 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200590# else
591 {"Py_CompileString", (PYTHON_PROC*)&py3_Py_CompileString},
592 {"PyEval_EvalCode", (PYTHON_PROC*)&PyEval_EvalCode},
593# endif
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200594 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
Bram Moolenaara9922d62013-05-30 13:01:18 +0200595 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200596 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
597 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200598 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT},
Bram Moolenaarf4258302013-06-02 18:20:17 +0200599 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200600 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
601 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200602 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
603 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200604 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200605 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200606 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
607 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
608 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
609 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200610 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
611 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200612 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200613# if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \
614 (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000)
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200615 {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check},
616# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200617 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
618 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200619 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr},
Bram Moolenaarbcb40972013-05-30 13:22:13 +0200620 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200621 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200622 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
623 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200624# if PY_VERSION_HEX >= 0x03040000
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200625 {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags},
626# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200627 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
628 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
629 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
630 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
631 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
632 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
633 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
Bram Moolenaar5f87b232013-06-13 20:57:50 +0200634 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200635 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200636 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200637 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200638 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
639 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200640 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaarc476e522013-06-23 13:46:40 +0200641 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100642 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200643 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
644 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
645 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200646# ifdef USE_LIMITED_API
647# if Py_LIMITED_API >= 0x030a0000
648 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
649# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200650# else
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200651# if PY_VERSION_HEX >= 0x030300f0
652 {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize},
653# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200654 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200655# endif
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200656# endif
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200657 {"PyUnicode_CompareWithASCIIString", (PYTHON_PROC*)&py3_PyUnicode_CompareWithASCIIString},
658 {"PyUnicode_AsUTF8String", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8String},
Bram Moolenaar1a3b5692013-05-30 12:40:39 +0200659# ifndef Py_UNICODE_USE_UCS_FUNCTIONS
660 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat},
661# else
662# ifdef Py_UNICODE_WIDE
663 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat},
664# else
665 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat},
666# endif
667# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200668 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200669 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200670 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100671 {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200672# if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0 || defined(USE_LIMITED_API)
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200673 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
674# endif
675# if PY_VERSION_HEX >= 0x030900b0
676 {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New},
677# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200678 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
679 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200680 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200681 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
682 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200683 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaard4a8c982018-05-15 22:31:18 +0200684 {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200685 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200686 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Zdenek Dohnal15a0a022023-08-15 22:52:01 +0200687# if PY_VERSION_HEX < 0x030c00b0
Bram Moolenaar66b79852012-09-21 14:00:35 +0200688 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Zdenek Dohnale5e47092023-08-13 19:37:09 +0200689# endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200690 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check},
691 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200692 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200693# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200694 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
695 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
Bram Moolenaar0014a532013-05-29 21:33:39 +0200696 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs},
697# else
698 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
699# endif
700# if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200701 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
702 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200703# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200704 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
705 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200706# endif
Bram Moolenaar774267b2013-05-21 20:51:59 +0200707 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New},
708 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del},
709 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack},
Bram Moolenaardb913952012-06-29 12:54:53 +0200710 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
711 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
712 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200713# ifdef USE_LIMITED_API
714# if PY_VERSION_HEX >= 0x03040000
715 {"PyType_GetSlot", (PYTHON_PROC*)&py3_PyType_GetSlot},
716# endif
717 {"PyType_FromSpec", (PYTHON_PROC*)&py3_PyType_FromSpec},
718# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200719 {"", NULL},
720};
721
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200722# if PY_VERSION_HEX >= 0x030800f0
723 static inline void
724py3__Py_DECREF(const char *filename UNUSED, int lineno UNUSED, PyObject *op)
725{
Bram Moolenaar13a1f3f2019-10-23 21:37:25 +0200726 if (--op->ob_refcnt != 0)
727 {
728# ifdef Py_REF_DEBUG
729 if (op->ob_refcnt < 0)
730 {
731 _Py_NegativeRefcount(filename, lineno, op);
732 }
733# endif
734 }
735 else
736 {
737 _Py_Dealloc(op);
738 }
739}
740
741# undef Py_DECREF
742# define Py_DECREF(op) py3__Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
743
744 static inline void
745py3__Py_XDECREF(PyObject *op)
746{
747 if (op != NULL)
748 {
749 Py_DECREF(op);
750 }
751}
752
753# undef Py_XDECREF
754# define Py_XDECREF(op) py3__Py_XDECREF(_PyObject_CAST(op))
755# endif
756
Bram Moolenaaree1b9312020-07-16 22:15:53 +0200757# if PY_VERSION_HEX >= 0x030900b0
758 static inline int
759py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature)
760{
761 return ((PyType_GetFlags(type) & feature) != 0);
762}
763# define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f)
764# endif
765
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200766# if PY_VERSION_HEX >= 0x030a00b2
767 static inline int
768py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type)
769{
770 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
771}
Zdenek Dohnalfee511c2022-06-27 13:59:00 +0100772# if PY_VERSION_HEX >= 0x030b00b3
773# undef PyObject_TypeCheck
774# define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
775# else
776# define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t)
777# endif
Zdenek Dohnal90478f32021-06-14 15:08:30 +0200778# endif
779
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100780# ifdef MSWIN
781/*
782 * Look up the library "libname" using the InstallPath registry key.
783 * Return NULL when failed. Return an allocated string when successful.
784 */
785 static char *
786py3_get_system_libname(const char *libname)
787{
788 const char *cp = libname;
789 char subkey[128];
790 HKEY hKey;
791 char installpath[MAXPATHL];
792 LONG len = sizeof(installpath);
793 LSTATUS rc;
794 size_t sysliblen;
795 char *syslibname;
796
797 while (*cp != '\0')
798 {
799 if (*cp == ':' || *cp == '\\' || *cp == '/')
800 {
801 // Bail out if "libname" contains path separator, assume it is
802 // an absolute path.
803 return NULL;
804 }
805 ++cp;
806 }
807 vim_snprintf(subkey, sizeof(subkey),
808# ifdef _WIN64
809 "Software\\Python\\PythonCore\\%d.%d\\InstallPath",
810# else
811 "Software\\Python\\PythonCore\\%d.%d-32\\InstallPath",
812# endif
813 PY_MAJOR_VERSION, PY_MINOR_VERSION);
814 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_QUERY_VALUE, &hKey)
815 != ERROR_SUCCESS)
816 return NULL;
817 rc = RegQueryValueA(hKey, NULL, installpath, &len);
818 RegCloseKey(hKey);
819 if (ERROR_SUCCESS != rc)
820 return NULL;
821 cp = installpath + len;
822 // Just in case registry value contains null terminators.
823 while (cp > installpath && *(cp-1) == '\0')
824 --cp;
825 // Remove trailing path separators.
826 while (cp > installpath && (*(cp-1) == '\\' || *(cp-1) == '/'))
827 --cp;
828 // Ignore if InstallPath is effectively empty.
829 if (cp <= installpath)
830 return NULL;
831 sysliblen = (cp - installpath) + 1 + STRLEN(libname) + 1;
832 syslibname = alloc(sysliblen);
833 vim_snprintf(syslibname, sysliblen, "%.*s\\%s",
834 (int)(cp - installpath), installpath, libname);
835 return syslibname;
836}
837# endif
838
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200839/*
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200840 * Load library and get all pointers.
841 * Parameter 'libname' provides name of DLL.
842 * Return OK or FAIL.
843 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200844 static int
845py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200846{
847 int i;
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200848 PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString;
849 PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode;
850 PYTHON_PROC *ucs_as_encoded_string =
851 (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100853# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100854 // Can't have Python and Python3 loaded at the same time.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000855 // It causes a crash, because RTLD_GLOBAL is needed for
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100856 // standard C extension libraries of one or both python versions.
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200857 if (python_loaded())
858 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200859 if (verbose)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000860 emsg(_(e_this_vim_cannot_execute_py3_after_using_python));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200861 return FAIL;
862 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200863# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200864
865 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200866 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200867 hinstPy3 = load_dll(libname);
868
Bram Moolenaarb2f9e0e2020-12-25 13:52:37 +0100869# ifdef MSWIN
870 if (!hinstPy3)
871 {
872 // Attempt to use the path from InstallPath as stored in the registry.
873 char *syslibname = py3_get_system_libname(libname);
874
875 if (syslibname != NULL)
876 {
877 hinstPy3 = load_dll(syslibname);
878 vim_free(syslibname);
879 }
880 }
881# endif
882
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200883 if (!hinstPy3)
884 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200885 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000886 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200887 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200888 }
889
890 for (i = 0; py3_funcname_table[i].ptr; ++i)
891 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200892 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
893 py3_funcname_table[i].name)) == NULL)
894 {
895 close_dll(hinstPy3);
896 hinstPy3 = 0;
897 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000898 semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200899 return FAIL;
900 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200901 }
902
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100903 // Load unicode functions separately as only the ucs2 or the ucs4 functions
904 // will be present in the library.
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200905# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200906 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
907 *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
908 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200909 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200910# else
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200911 *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
912 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200913 "PyUnicodeUCS2_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200914 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200915 "PyUnicodeUCS2_AsEncodedString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200916 if (*ucs_from_string == NULL || *ucs_decode == NULL
917 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200918 {
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200919 *ucs_from_string = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200920 "PyUnicodeUCS4_FromString");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200921 *ucs_decode = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200922 "PyUnicodeUCS4_Decode");
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200923 *ucs_as_encoded_string = symbol_from_dll(hinstPy3,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200924 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200925 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200926# endif
Bram Moolenaar7b24ce02018-03-29 18:15:26 +0200927 if (*ucs_from_string == NULL || *ucs_decode == NULL
928 || *ucs_as_encoded_string == NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200929 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200930 close_dll(hinstPy3);
931 hinstPy3 = 0;
932 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000933 semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200934 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200935 }
936
937 return OK;
938}
939
940/*
941 * If python is enabled (there is installed python on Windows system) return
942 * TRUE, else FALSE.
943 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200944 int
945python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200946{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100947 return py3_runtime_link_init((char *)p_py3dll, verbose) == OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200948}
949
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100950/*
951 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200952 * DLL, as this can cause errors (importing data symbols is not reliable).
953 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200954 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100955get_py3_exceptions(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200956{
957 PyObject *exmod = PyImport_ImportModule("builtins");
958 PyObject *exdict = PyModule_GetDict(exmod);
959 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
960 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200961 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200962 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
963 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
964 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar41009372013-07-01 22:03:04 +0200965 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200966 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200967 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError");
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200968 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200969 Py_XINCREF(p3imp_PyExc_AttributeError);
970 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200971 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200972 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
973 Py_XINCREF(p3imp_PyExc_TypeError);
974 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar41009372013-07-01 22:03:04 +0200975 Py_XINCREF(p3imp_PyExc_SystemExit);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200976 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200977 Py_XINCREF(p3imp_PyExc_ImportError);
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200978 Py_XINCREF(p3imp_PyExc_OverflowError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979 Py_XDECREF(exmod);
980}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100981#endif // DYNAMIC_PYTHON3
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200982
Bram Moolenaardb913952012-06-29 12:54:53 +0200983static int py3initialised = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +0200984#define PYINITIALISED py3initialised
Bram Moolenaarc4f83382017-07-07 14:50:44 +0200985static int python_end_called = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +0200986
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200987#ifdef USE_LIMITED_API
988# define DESTRUCTOR_FINISH(self) \
989 ((freefunc)PyType_GetSlot(Py_TYPE(self), Py_tp_free))((PyObject*)self)
990#else
991# define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self)
992#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200993
Bram Moolenaar971db462013-05-12 18:44:48 +0200994#define WIN_PYTHON_REF(win) win->w_python3_ref
995#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200996#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200997
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200998 static void
999call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001000{
Bram Moolenaar0014a532013-05-29 21:33:39 +02001001#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002 _PyObject_DebugFree(p);
1003#else
1004 PyObject_Free(p);
1005#endif
1006}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001007
1008 static PyObject *
1009call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010{
1011 return PyType_GenericNew(type,args,kwds);
1012}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001013
1014 static PyObject *
1015call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001016{
1017 return PyType_GenericAlloc(type,nitems);
1018}
1019
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001020static PyObject *OutputGetattro(PyObject *, PyObject *);
1021static int OutputSetattro(PyObject *, PyObject *, PyObject *);
1022static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001023static int BufferSetattro(PyObject *, PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001024static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001025static PyObject *WindowGetattro(PyObject *, PyObject *);
1026static int WindowSetattro(PyObject *, PyObject *, PyObject *);
1027static PyObject *RangeGetattro(PyObject *, PyObject *);
1028static PyObject *CurrentGetattro(PyObject *, PyObject *);
1029static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
1030static PyObject *DictionaryGetattro(PyObject *, PyObject *);
1031static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
1032static PyObject *ListGetattro(PyObject *, PyObject *);
1033static int ListSetattro(PyObject *, PyObject *, PyObject *);
1034static PyObject *FunctionGetattro(PyObject *, PyObject *);
1035
1036static struct PyModuleDef vimmodule;
1037
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02001038#define PY_CAN_RECURSE
1039
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001040/*
1041 * Include the code shared with if_python.c
1042 */
1043#include "if_py_both.h"
1044
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001045#ifndef USE_LIMITED_API
1046# if PY_VERSION_HEX >= 0x030300f0
1047# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1048# else
1049# define PY_UNICODE_GET_UTF8_CHARS _PyUnicode_AsString
1050# endif
1051#else
1052# if Py_LIMITED_API >= 0x030A0000
1053# define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL)
1054# else
1055// Python limited API before 3.10 lack easy ways to query the raw UTF-8 chars.
1056// We need to first convert the string to bytes, and then extract the chars.
1057// This function is only used for attribute string comparisons, which have
1058// known short length. As such, just allocate a short static buffer to hold
1059// the characters instead of having to allocate/deallcoate it.
1060//
1061// An alternative would be to convert all attribute string comparisons to use
1062// PyUnicode_CompareWithASCIIString to skip having to extract the chars.
1063static char py3_unicode_utf8_chars[20];
1064char* PY_UNICODE_GET_UTF8_CHARS(PyObject* str)
1065{
1066 py3_unicode_utf8_chars[0] = '\0';
1067 PyObject* bytes = PyUnicode_AsUTF8String(str);
1068 if (bytes)
1069 {
1070 char *chars;
1071 Py_ssize_t len;
1072 if (PyBytes_AsStringAndSize(bytes, &chars, &len) != -1)
1073 {
1074 if (len < (Py_ssize_t)sizeof(py3_unicode_utf8_chars))
1075 // PyBytes_AsStringAndSize guarantees null-termination
1076 memcpy(py3_unicode_utf8_chars, chars, len + 1);
1077 }
1078 Py_DECREF(bytes);
1079 }
1080 return py3_unicode_utf8_chars;
1081}
1082# endif
1083#endif
1084
Bram Moolenaar828bff12019-03-19 22:11:41 +01001085// NOTE: Must always be used at the start of a block, since it declares "name".
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001086#define GET_ATTR_STRING(name, nameobj) \
1087 char *name = ""; \
1088 if (PyUnicode_Check(nameobj)) \
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001089 name = (char *)PY_UNICODE_GET_UTF8_CHARS(nameobj)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001090
1091#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
1092
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001093///////////////////////////////////////////////////////
1094// Internal function prototypes.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001095
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001096static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001097
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001098///////////////////////////////////////////////////////
1099// 1. Python interpreter main program.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001100
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001101 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001102python3_end(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001103{
1104 static int recurse = 0;
1105
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001106 // If a crash occurs while doing this, don't try again.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001107 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001108 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001109
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001110 python_end_called = TRUE;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001111 ++recurse;
1112
1113#ifdef DYNAMIC_PYTHON3
1114 if (hinstPy3)
1115#endif
1116 if (Py_IsInitialized())
1117 {
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02001118#ifdef USE_LIMITED_API
1119 shutdown_types();
1120#endif
1121
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001122 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +02001123 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001125 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001126 }
1127
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001128 --recurse;
1129}
1130
Bram Moolenaar094454f2015-10-07 10:39:55 +02001131#if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001132 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001133python3_loaded(void)
Bram Moolenaar4c3a3262010-07-24 15:42:14 +02001134{
1135 return (hinstPy3 != 0);
1136}
1137#endif
1138
Bram Moolenaar94073162018-01-31 21:49:05 +01001139static wchar_t *py_home_buf = NULL;
1140
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001141#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001142/*
1143 * Return TRUE if stdin is readable from Python 3.
1144 */
1145 static BOOL
1146is_stdin_readable(void)
1147{
1148 DWORD mode, eventnum;
1149 struct _stat st;
1150 int fd = fileno(stdin);
1151 HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
1152
1153 // Check if stdin is connected to the console.
1154 if (GetConsoleMode(hstdin, &mode))
1155 // Check if it is opened as input.
1156 return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
1157
1158 return _fstat(fd, &st) == 0;
1159}
1160
1161// Python 3.5 or later will abort inside Py_Initialize() when stdin has
1162// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001163// Note that the python DLL is linked to its own stdio DLL which can be
1164// differ from Vim's stdio.
1165 static void
1166reset_stdin(void)
1167{
1168 FILE *(*py__acrt_iob_func)(unsigned) = NULL;
1169 FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001170 HINSTANCE hinst = hinstPy3;
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001171
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001172 if (hinst == NULL || is_stdin_readable())
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001173 return;
1174
1175 // Get "freopen" and "stdin" which are used in the python DLL.
1176 // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later.
1177 py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func");
1178 if (py__acrt_iob_func)
1179 {
1180 HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst,
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001181 "__acrt_iob_func");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001182 if (hpystdiodll)
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001183 pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001184 }
1185
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001186 // Reconnect stdin to CONIN$.
Bram Moolenaar253b16a2020-10-06 19:59:06 +02001187 if (pyfreopen != NULL)
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001188 pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001189 else
Bram Moolenaarc6ed2542020-10-10 23:26:28 +02001190 freopen("CONIN$", "r", stdin);
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001191}
1192#else
1193# define reset_stdin()
1194#endif
1195
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001196// Python 3.2 or later will abort inside Py_Initialize() when mandatory
1197// modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.).
1198// Install a hook to python dll's exit() and recover from it.
1199#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0)
1200# define HOOK_EXIT
1201# include <setjmp.h>
1202
1203static jmp_buf exit_hook_jump_buf;
1204static void *orig_exit = NULL;
1205
1206/*
1207 * Function that replaces exit() while calling Py_Initialize().
1208 */
1209 static void
1210hooked_exit(int ret)
1211{
1212 // Recover from exit.
1213 longjmp(exit_hook_jump_buf, 1);
1214}
1215
1216/*
1217 * Install a hook to python dll's exit().
1218 */
1219 static void
1220hook_py_exit(void)
1221{
1222 HINSTANCE hinst = hinstPy3;
1223
1224 if (hinst == NULL || orig_exit != NULL)
1225 return;
1226
1227 orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit);
1228}
1229
1230/*
1231 * Remove the hook installed by hook_py_exit().
1232 */
1233 static void
1234restore_py_exit(void)
1235{
1236 HINSTANCE hinst = hinstPy3;
1237
1238 if (hinst == NULL)
1239 return;
1240
1241 if (orig_exit != NULL)
1242 hook_dll_import_func(hinst, "exit", orig_exit);
1243 orig_exit = NULL;
1244}
1245#endif
1246
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001247 static int
1248Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001249{
1250 if (!py3initialised)
1251 {
1252#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001253 if (!python3_enabled(TRUE))
1254 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001255 emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001256 goto fail;
1257 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001258#endif
1259
1260 init_structs();
1261
Bram Moolenaar94073162018-01-31 21:49:05 +01001262 if (*p_py3home != NUL)
1263 {
1264 size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001265
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001266 // The string must not change later, make a copy in static memory.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001267 py_home_buf = ALLOC_MULT(wchar_t, len);
Bram Moolenaar94073162018-01-31 21:49:05 +01001268 if (py_home_buf != NULL && mbstowcs(
1269 py_home_buf, (char *)p_py3home, len) != (size_t)-1)
1270 Py_SetPythonHome(py_home_buf);
1271 }
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001272#ifdef PYTHON3_HOME
Bram Moolenaar94073162018-01-31 21:49:05 +01001273 else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
Bram Moolenaar10005652015-12-31 21:03:23 +01001274 Py_SetPythonHome(PYTHON3_HOME);
Bram Moolenaar644d37b2010-11-16 19:26:02 +01001275#endif
1276
Bram Moolenaar7bc4f932012-10-14 03:22:56 +02001277 PyImport_AppendInittab("vim", Py3Init_vim);
1278
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001279#if !defined(DYNAMIC_PYTHON3) && defined(MSWIN)
1280 hinstPy3 = GetModuleHandle(PYTHON3_DLL);
1281#endif
Bram Moolenaar56b8dc32020-08-06 21:47:11 +02001282 reset_stdin();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001283
1284#ifdef HOOK_EXIT
1285 // Catch exit() called in Py_Initialize().
1286 hook_py_exit();
1287 if (setjmp(exit_hook_jump_buf) == 0)
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001288 {
1289 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001290 restore_py_exit();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001291 }
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001292 else
1293 {
1294 // exit() was called in Py_Initialize().
1295 restore_py_exit();
1296 emsg(_(e_critical_error_in_python3_initialization_check_your_installation));
1297 goto fail;
1298 }
K.Takataa7fbaa42022-12-26 14:46:51 +00001299#else
1300 Py_Initialize();
Bram Moolenaar63ff72a2022-02-07 13:54:01 +00001301#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001302
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001303#if PY_VERSION_HEX < 0x03090000
1304 // Initialise threads. This is deprecated since Python 3.9.
Bram Moolenaar456f2bb2011-06-12 21:37:13 +02001305 PyEval_InitThreads();
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001306#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001307#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001308 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001309#endif
1310
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001311 if (PythonIO_Init_io())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001312 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313
Bram Moolenaardb913952012-06-29 12:54:53 +02001314 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
1315
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001316 // Remove the element from sys.path that was added because of our
1317 // argv[0] value in Py3Init_vim(). Previously we used an empty
1318 // string, but depending on the OS we then get an empty entry or
1319 // the current directory in sys.path.
1320 // Only after vim has been imported, the element does exist in
1321 // sys.path.
Bram Moolenaar19e60942011-06-19 00:27:51 +02001322 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 +02001323
Bram Moolenaarefc0d942020-10-11 18:05:02 +02001324 // Without the call to PyEval_SaveThread, thread specific state (such
1325 // as the system trace hook), will be lost between invocations of
1326 // Python code.
1327 // GIL may have been created and acquired in PyEval_InitThreads() and
1328 // thread state is created in Py_Initialize(); there
1329 // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must
1330 // have threads enabled!), so the following does both: unlock GIL and
1331 // save thread state in TLS without deleting thread state
Bram Moolenaar76d711c2013-02-13 14:17:08 +01001332 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001333
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001334 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001335 }
1336
1337 return 0;
1338
1339fail:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001340 // We call PythonIO_Flush() here to print any Python errors.
1341 // This is OK, as it is possible to call this function even
1342 // if PythonIO_Init_io() has not completed successfully (it will
1343 // not do anything in this case).
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001344 PythonIO_Flush();
1345 return -1;
1346}
1347
1348/*
1349 * External interface
1350 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001351 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001352DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001353{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001354#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001355 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001356#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +02001357 PyObject *cmdstr;
1358 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +02001359 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001360
Bram Moolenaarc4f83382017-07-07 14:50:44 +02001361 if (python_end_called)
1362 goto theend;
1363
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001364 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001365 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001366
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001367 init_range(arg);
1368
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001369 Python_Release_Vim(); // leave Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001370
1371#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001372 // Python only works properly when the LC_NUMERIC locale is "C".
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001373 saved_locale = setlocale(LC_NUMERIC, NULL);
1374 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001375 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001376 else
1377 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001378 // Need to make a copy, value may change when setting new locale.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001379 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
1380 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001381 }
1382#endif
1383
1384 pygilstate = PyGILState_Ensure();
1385
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001386 // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
1387 // SyntaxError (unicode error).
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001388 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01001389 (char *)ENC_OPT, ERRORS_DECODE_ARG);
1390 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001391 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +02001392
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001393 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001394 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001395
1396 PyGILState_Release(pygilstate);
1397
1398#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1399 if (saved_locale != NULL)
1400 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001401 (void)setlocale(LC_NUMERIC, saved_locale);
1402 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001403 }
1404#endif
1405
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001406 Python_Lock_Vim(); // enter Vim
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001407 PythonIO_Flush();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001408
1409theend:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001410 return; // keeps lint happy
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001411}
1412
1413/*
Bram Moolenaar368373e2010-07-19 20:46:22 +02001414 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001415 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001416 void
1417ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001418{
1419 char_u *script;
1420
1421 script = script_get(eap, eap->arg);
1422 if (!eap->skip)
1423 {
Bram Moolenaar14816ad2019-02-18 22:04:56 +01001424 if (p_pyx == 0)
1425 p_pyx = 3;
1426
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001427 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001428 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001429 (runner) run_cmd,
1430 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001431 }
1432 vim_free(script);
1433}
1434
1435#define BUFFER_SIZE 2048
1436
1437/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +02001438 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001439 */
1440 void
1441ex_py3file(exarg_T *eap)
1442{
1443 static char buffer[BUFFER_SIZE];
1444 const char *file;
1445 char *p;
1446 int i;
1447
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001448 if (p_pyx == 0)
1449 p_pyx = 3;
1450
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001451 // Have to do it like this. PyRun_SimpleFile requires you to pass a
1452 // stdio file pointer, but Vim and the Python DLL are compiled with
1453 // different options under Windows, meaning that stdio pointers aren't
1454 // compatible between the two. Yuk.
1455 //
1456 // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
1457 //
1458 // Using bytes so that Python can detect the source encoding as it normally
1459 // does. The doc does not say "compile" accept bytes, though.
1460 //
1461 // We need to escape any backslashes or single quotes in the file name, so that
1462 // Python won't mangle the file name.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001463
1464 strcpy(buffer, "exec(compile(open('");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001465 p = buffer + 19; // size of "exec(compile(open('"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001466
1467 for (i=0; i<2; ++i)
1468 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001469 file = (char *)eap->arg;
1470 while (*file && p < buffer + (BUFFER_SIZE - 3))
1471 {
1472 if (*file == '\\' || *file == '\'')
1473 *p++ = '\\';
1474 *p++ = *file++;
1475 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001476 // If we didn't finish the file name, we hit a buffer overflow
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001477 if (*file != '\0')
1478 return;
1479 if (i==0)
1480 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001481 strcpy(p,"','rb').read(),'");
1482 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001483 }
1484 else
1485 {
1486 strcpy(p,"','exec'))");
1487 p += 10;
1488 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001489 }
1490
1491
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492 // Execute the file
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001493 DoPyCommand(buffer,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001494 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001495 (runner) run_cmd,
1496 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001497}
1498
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001499 void
1500ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001501{
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001502 if (p_pyx == 0)
1503 p_pyx = 3;
1504
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001505 DoPyCommand((char *)eap->arg,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02001506 init_range_cmd,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001507 (runner)run_do,
1508 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001509}
1510
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001511///////////////////////////////////////////////////////
1512// 2. Python output stream: writes output via [e]msg().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001513
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001514// Implementation functions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001515
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001516 static PyObject *
1517OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001518{
Bram Moolenaar77045652012-09-21 13:46:06 +02001519 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001520
1521 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001522 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaar6d4431e2016-04-21 20:00:56 +02001523 else if (strcmp(name, "errors") == 0)
1524 return PyString_FromString("strict");
1525 else if (strcmp(name, "encoding") == 0)
1526 return PyString_FromString(ENC_OPT);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001527
1528 return PyObject_GenericGetAttr(self, nameobj);
1529}
1530
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001531 static int
1532OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001533{
Bram Moolenaar77045652012-09-21 13:46:06 +02001534 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001535
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001536 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001537}
1538
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001539///////////////////////////////////////////////////////
1540// 3. Implementation of the Vim module for Python
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001541
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001542// Window type - Implementation functions
1543// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001544
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001545#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1546
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001547// Buffer type - Implementation functions
1548// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001549
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001550#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1551
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001552static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001553static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001554
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001555// Line range type - Implementation functions
1556// --------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001557
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001558#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1559
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001560static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001561static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
1562static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001563
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001564// Current objects type - Implementation functions
1565// -----------------------------------------------
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001566
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001567static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001568 (lenfunc) BufferLength, // sq_length, len(x)
1569 (binaryfunc) 0, // sq_concat, x+y
1570 (ssizeargfunc) 0, // sq_repeat, x*n
1571 (ssizeargfunc) BufferItem, // sq_item, x[i]
1572 0, // was_sq_slice, x[i:j]
1573 0, // sq_ass_item, x[i]=v
1574 0, // sq_ass_slice, x[i:j]=v
1575 0, // sq_contains
1576 0, // sq_inplace_concat
1577 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001578};
1579
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001580static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001581 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001582 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001583 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001584};
1585
1586
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001587// Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001588
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001589 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001590BufferGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001591{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001592 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001593
Bram Moolenaar77045652012-09-21 13:46:06 +02001594 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001595
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001596 if ((r = BufferAttrValid((BufferObject *)(self), name)))
1597 return r;
1598
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001599 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001600 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001601
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001602 r = BufferAttr((BufferObject *)(self), name);
1603 if (r || PyErr_Occurred())
1604 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001605 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001606 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001607}
1608
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001609 static int
1610BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1611{
1612 GET_ATTR_STRING(name, nameobj);
1613
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001614 return BufferSetattr(self, name, val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02001615}
1616
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001617//////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001618
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001619 static PyObject *
1620BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001621{
Bram Moolenaardb913952012-06-29 12:54:53 +02001622 if (PyLong_Check(idx))
1623 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001624 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001625 return BufferItem((BufferObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001626 }
1627 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001628 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001630
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001631 if (CheckBuffer((BufferObject *) self))
1632 return NULL;
1633
Bram Moolenaar922a4662014-03-30 16:11:43 +02001634 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001635 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001637 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001638 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001639 return BufferSlice((BufferObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001640 }
1641 else
1642 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001643 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001644 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001645 }
1646}
1647
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001648 static int
Bram Moolenaar19e60942011-06-19 00:27:51 +02001649BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1650{
Bram Moolenaardb913952012-06-29 12:54:53 +02001651 if (PyLong_Check(idx))
1652 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001653 long n = PyLong_AsLong(idx);
Bram Moolenaarab589462020-07-06 21:03:06 +02001654
1655 if (CheckBuffer((BufferObject *) self))
1656 return -1;
1657
Bram Moolenaar19e60942011-06-19 00:27:51 +02001658 return RBAsItem((BufferObject *)(self), n, val, 1,
1659 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1660 NULL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001661 }
1662 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001663 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001664 Py_ssize_t start, stop, step, slicelen;
1665
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001666 if (CheckBuffer((BufferObject *) self))
1667 return -1;
1668
Bram Moolenaar922a4662014-03-30 16:11:43 +02001669 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001670 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001671 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001672 &step, &slicelen) < 0)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001673 return -1;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001674 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1675 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1676 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001677 }
1678 else
1679 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001680 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001681 return -1;
1682 }
1683}
1684
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001685static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001686 (lenfunc) RangeLength, // sq_length, len(x)
1687 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
1688 (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n
1689 (ssizeargfunc) RangeItem, // sq_item, x[i]
1690 0, // was_sq_slice, x[i:j]
1691 (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v
1692 0, // sq_ass_slice, x[i:j]=v
1693 0, // sq_contains
1694 0, // sq_inplace_concat
1695 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001696};
1697
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001698static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001699 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001700 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001701 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001702};
1703
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001704// Line range object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001705
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001706 static PyObject *
1707RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001708{
Bram Moolenaar77045652012-09-21 13:46:06 +02001709 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001710
1711 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001712 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001713 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001714 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001715 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001716 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001717}
1718
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001719////////////////
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001720
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001721 static int
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001722RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001723{
1724 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001725 ((RangeObject *)(self))->start,
1726 ((RangeObject *)(self))->end,
1727 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001728}
1729
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001730 static Py_ssize_t
1731RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1732{
1733 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1734 ((RangeObject *)(self))->start,
1735 ((RangeObject *)(self))->end,
1736 &((RangeObject *)(self))->end);
1737}
1738
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001739 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001740RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001741{
Bram Moolenaardb913952012-06-29 12:54:53 +02001742 if (PyLong_Check(idx))
1743 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001744 long _idx = PyLong_AsLong(idx);
Bram Moolenaard6e39182013-05-21 18:30:34 +02001745 return RangeItem((RangeObject *)(self), _idx);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001746 }
1747 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001748 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001749 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001750
Bram Moolenaar922a4662014-03-30 16:11:43 +02001751 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001752 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1753 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001754 &step, &slicelen) < 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001755 return NULL;
Bram Moolenaard6e39182013-05-21 18:30:34 +02001756 return RangeSlice((RangeObject *)(self), start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001757 }
1758 else
1759 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001760 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001761 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001762 }
1763}
1764
Bram Moolenaar4ce5fe42020-10-21 21:01:59 +02001765 static int
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001766RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1767{
Bram Moolenaardb913952012-06-29 12:54:53 +02001768 if (PyLong_Check(idx))
1769 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001770 long n = PyLong_AsLong(idx);
1771 return RangeAsItem(self, n, val);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001772 }
1773 else if (PySlice_Check(idx))
Bram Moolenaardb913952012-06-29 12:54:53 +02001774 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001775 Py_ssize_t start, stop, step, slicelen;
1776
Bram Moolenaar922a4662014-03-30 16:11:43 +02001777 if (PySlice_GetIndicesEx((PySliceObject_T *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001778 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1779 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 &step, &slicelen) < 0)
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001781 return -1;
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001782 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001783 }
1784 else
1785 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001786 RAISE_INVALID_INDEX_TYPE(idx);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001787 return -1;
1788 }
1789}
1790
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001791// TabPage object - Implementation
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001792
1793 static PyObject *
1794TabPageGetattro(PyObject *self, PyObject *nameobj)
1795{
1796 PyObject *r;
1797
1798 GET_ATTR_STRING(name, nameobj);
1799
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001800 if ((r = TabPageAttrValid((TabPageObject *)(self), name)))
1801 return r;
1802
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001803 if (CheckTabPage((TabPageObject *)(self)))
1804 return NULL;
1805
1806 r = TabPageAttr((TabPageObject *)(self), name);
1807 if (r || PyErr_Occurred())
1808 return r;
1809 else
1810 return PyObject_GenericGetAttr(self, nameobj);
1811}
1812
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001813// Window object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001814
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001815 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001816WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001817{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001818 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001819
Bram Moolenaar77045652012-09-21 13:46:06 +02001820 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001821
Bram Moolenaar9e822c02013-05-29 22:15:30 +02001822 if ((r = WindowAttrValid((WindowObject *)(self), name)))
1823 return r;
1824
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001825 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001826 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001827
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001828 r = WindowAttr((WindowObject *)(self), name);
1829 if (r || PyErr_Occurred())
1830 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001831 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001832 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001833}
1834
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001835 static int
1836WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001837{
Bram Moolenaar77045652012-09-21 13:46:06 +02001838 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001839
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001840 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001841}
1842
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001843// Tab page list object - Definitions
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001844
1845static PySequenceMethods TabListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001846 (lenfunc) TabListLength, // sq_length, len(x)
1847 (binaryfunc) 0, // sq_concat, x+y
1848 (ssizeargfunc) 0, // sq_repeat, x*n
1849 (ssizeargfunc) TabListItem, // sq_item, x[i]
1850 0, // sq_slice, x[i:j]
1851 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1852 0, // sq_ass_slice, x[i:j]=v
1853 0, // sq_contains
1854 0, // sq_inplace_concat
1855 0, // sq_inplace_repeat
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001856};
1857
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001858// Window list object - Definitions
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001859
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001860static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001861 (lenfunc) WinListLength, // sq_length, len(x)
1862 (binaryfunc) 0, // sq_concat, x+y
1863 (ssizeargfunc) 0, // sq_repeat, x*n
1864 (ssizeargfunc) WinListItem, // sq_item, x[i]
1865 0, // sq_slice, x[i:j]
1866 (ssizeobjargproc)0, // sq_as_item, x[i]=v
1867 0, // sq_ass_slice, x[i:j]=v
1868 0, // sq_contains
1869 0, // sq_inplace_concat
1870 0, // sq_inplace_repeat
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001871};
1872
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001873/*
1874 * Current items object - Implementation
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001875 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001876 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001877CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001878{
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001879 PyObject *r;
Bram Moolenaar77045652012-09-21 13:46:06 +02001880 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001881 if (!(r = CurrentGetattr(self, name)))
1882 return PyObject_GenericGetAttr(self, nameobj);
1883 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001884}
1885
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001886 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001887CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001888{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001889 GET_ATTR_STRING(name, nameobj);
1890 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001891}
1892
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001893// Dictionary object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001894
Bram Moolenaar66b79852012-09-21 14:00:35 +02001895 static PyObject *
1896DictionaryGetattro(PyObject *self, PyObject *nameobj)
1897{
1898 DictionaryObject *this = ((DictionaryObject *) (self));
1899
1900 GET_ATTR_STRING(name, nameobj);
1901
1902 if (strcmp(name, "locked") == 0)
1903 return PyLong_FromLong(this->dict->dv_lock);
1904 else if (strcmp(name, "scope") == 0)
1905 return PyLong_FromLong(this->dict->dv_scope);
1906
1907 return PyObject_GenericGetAttr(self, nameobj);
1908}
1909
1910 static int
1911DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1912{
1913 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001914 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001915}
1916
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001917// List object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001918
Bram Moolenaar66b79852012-09-21 14:00:35 +02001919 static PyObject *
1920ListGetattro(PyObject *self, PyObject *nameobj)
1921{
1922 GET_ATTR_STRING(name, nameobj);
1923
1924 if (strcmp(name, "locked") == 0)
1925 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1926
1927 return PyObject_GenericGetAttr(self, nameobj);
1928}
1929
1930 static int
1931ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1932{
1933 GET_ATTR_STRING(name, nameobj);
Yee Cheng Chin02c51b12023-09-21 16:40:12 +02001934 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001935}
1936
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001937// Function object - Definitions
Bram Moolenaardb913952012-06-29 12:54:53 +02001938
Bram Moolenaardb913952012-06-29 12:54:53 +02001939 static PyObject *
1940FunctionGetattro(PyObject *self, PyObject *nameobj)
1941{
Bram Moolenaar8110a092016-04-14 15:56:09 +02001942 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001943 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001944
1945 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001946
Bram Moolenaar8110a092016-04-14 15:56:09 +02001947 r = FunctionAttr(this, name);
1948 if (r || PyErr_Occurred())
1949 return r;
1950 else
1951 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001952}
1953
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001954// External interface
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001955
1956 void
1957python3_buffer_free(buf_T *buf)
1958{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001959 BufferObject *bp = BUF_PYTHON_REF(buf);
1960 if (bp == NULL)
1961 return;
1962 bp->buf = INVALID_BUFFER_VALUE;
1963 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001964}
1965
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001966 void
1967python3_window_free(win_T *win)
1968{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001969 WindowObject *wp = WIN_PYTHON_REF(win);
1970 if (wp == NULL)
1971 return;
1972 wp->win = INVALID_WINDOW_VALUE;
1973 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001974}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001975
1976 void
1977python3_tabpage_free(tabpage_T *tab)
1978{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001979 TabPageObject *tp = TAB_PYTHON_REF(tab);
1980 if (tp == NULL)
1981 return;
1982 tp->tab = INVALID_TABPAGE_VALUE;
1983 TAB_PYTHON_REF(tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001984}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001985
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001986 static PyObject *
1987Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001988{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001989 // The special value is removed from sys.path in Python3_Init().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001990 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1991
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001992 if (init_types())
1993 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001994
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001995 // Set sys.argv[] to avoid a crash in warn().
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001996 PySys_SetArgv(1, argv);
1997
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001998 if ((vim_module = PyModule_Create(&vimmodule)) == NULL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02001999 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002000
Bram Moolenaardee2e312013-06-23 16:35:47 +02002001 if (populate_module(vim_module))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002002 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002003
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02002004 if (init_sys_path())
2005 return NULL;
2006
2007 return vim_module;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002008}
2009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002010//////////////////////////////////////////////////////////////////////////
2011// 4. Utility functions for handling the interface between Vim and Python.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002012
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002013/*
2014 * Convert a Vim line into a Python string.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002015 * All internal newlines are replaced by null characters.
2016 *
2017 * On errors, the Python exception data is set, and NULL is returned.
2018 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002019 static PyObject *
2020LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002021{
2022 PyObject *result;
2023 Py_ssize_t len = strlen(str);
Bram Moolenaard672dde2020-02-26 13:43:51 +01002024 char *tmp, *p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002025
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002026 tmp = alloc(len + 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002027 p = tmp;
2028 if (p == NULL)
2029 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002030 PyErr_NoMemory();
2031 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002032 }
2033
2034 while (*str)
2035 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002036 if (*str == '\n')
2037 *p = '\0';
2038 else
2039 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002040
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002041 ++p;
2042 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002043 }
2044 *p = '\0';
2045
Bram Moolenaar2e2f52a2020-12-21 16:03:02 +01002046 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002047
2048 vim_free(tmp);
2049 return result;
2050}
2051
Bram Moolenaardb913952012-06-29 12:54:53 +02002052 void
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002053do_py3eval(char_u *str, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02002054{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002055 DoPyCommand((char *) str,
Yee Cheng Chin2ce070c2023-09-19 20:30:22 +02002056 init_range_eval,
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02002057 (runner) run_eval,
2058 (void *) rettv);
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002059 if (rettv->v_type == VAR_UNKNOWN)
Bram Moolenaardb913952012-06-29 12:54:53 +02002060 {
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002061 rettv->v_type = VAR_NUMBER;
2062 rettv->vval.v_number = 0;
Bram Moolenaardb913952012-06-29 12:54:53 +02002063 }
2064}
2065
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002066 int
Bram Moolenaar8e9a24a2019-03-19 22:22:55 +01002067set_ref_in_python3(int copyID)
Bram Moolenaardb913952012-06-29 12:54:53 +02002068{
Bram Moolenaarb641df42015-02-03 13:16:04 +01002069 return set_ref_in_py(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02002070}
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002071
2072 int
Dominique Pellé4927bc72023-09-24 16:12:07 +02002073python3_version(void)
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +02002074{
2075#ifdef USE_LIMITED_API
2076 return Py_LIMITED_API;
2077#else
2078 return PY_VERSION_HEX;
2079#endif
2080}